[ABC455F] Merge Slimes 2
题解
题目传送门
思路
先看合并操作,把
合并
合并
以此类推,合并
所以题目等价于维护区间加法,查询区间和,查询区间平方和。
注意到
时间复杂度
这篇题解里面给的代码是分块的代码,线段树的代码点这里。
AC Code
#include <bits/stdc++.h>
#define int long long
using namespace std;
inline int read()
{
int x = 0,p = 1;
char c = getchar();
while(c > '9' || c < '0') p = c == '-' ? -1 : 1,c = getchar();
while(c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48),c = getchar();
return p * x;
}
template<typename _Tp> inline void _write(_Tp x) { if(x > 9) _write(x / 10); putchar(x % 10 | 48); }
template<typename _Tp> inline void write(_Tp x) { if(x < 0) putchar('-'),x = -x; _write(x); }
template<typename _Tp> inline void writeln(_Tp x) { write(x); puts(""); }
const int N = 1e5 + 10;
const int M = 400;
const int mod = 998244353;
const int inv2 = mod + 1 >> 1;
int n,q,L,a[N],b[N],bs[M],bps[M],tag[M],l[M],r[M],len[M];
inline void pushdown(int p)
{
if(!tag[p]) return;
for(int i = l[p];i <= r[p];i++) (a[i] += tag[p]) %= mod;
tag[p] = 0;
}
inline void update(int l,int r,int x)
{
if(b[l] != b[r])
{
pushdown(b[l]); pushdown(b[r]);
for(int i = l;b[i] == b[l];i++)
{
(bps[b[i]] += (-a[i] * a[i] % mod + mod) % mod) %= mod;
(a[i] += x) %= mod;
(bs[b[i]] += x) %= mod;
(bps[b[i]] += a[i] * a[i] % mod) %= mod;
}
for(int i = r;b[i] == b[r];i--)
{
(bps[b[i]] += (-a[i] * a[i] % mod + mod) % mod) %= mod;
(a[i] += x) %= mod;
(bs[b[i]] += x) %= mod;
(bps[b[i]] += a[i] * a[i] % mod) %= mod;
}
for(int i = b[l] + 1;i <= b[r] - 1;i++)
{
(tag[i] += x) %= mod;
(bps[i] += x * 2 * bs[i] % mod + x * x % mod * len[i] % mod) %= mod;
(bs[i] += x * len[i]) %= mod;
}
}
else
{
pushdown(b[l]);
for(int i = l;i <= r;i++)
{
(bps[b[i]] += (-a[i] * a[i] % mod + mod) % mod) %= mod;
(a[i] += x) %= mod;
(bs[b[i]] += x) %= mod;
(bps[b[i]] += a[i] * a[i] % mod) %= mod;
}
}
}
inline int querysum(int l,int r)
{
int ans = 0;
if(b[l] != b[r])
{
pushdown(b[l]); pushdown(b[r]);
for(int i = l;b[i] == b[l];i++) (ans += a[i]) %= mod;
for(int i = r;b[i] == b[r];i--) (ans += a[i]) %= mod;
for(int i = b[l] + 1;i <= b[r] - 1;i++) (ans += bs[i]) %= mod;
}
else
{
pushdown(b[l]);
for(int i = l;i <= r;i++) (ans += a[i]) %= mod;
}
return ans;
}
inline int querypsum(int l,int r)
{
int ans = 0;
if(b[l] != b[r])
{
pushdown(b[l]); pushdown(b[r]);
for(int i = l;b[i] == b[l];i++) (ans += a[i] * a[i] % mod) %= mod;
for(int i = r;b[i] == b[r];i--) (ans += a[i] * a[i] % mod) %= mod;
for(int i = b[l] + 1;i <= b[r] - 1;i++) (ans += bps[i]) %= mod;
}
else
{
pushdown(b[l]);
for(int i = l;i <= r;i++) (ans += a[i] * a[i] % mod) %= mod;
}
return ans;
}
signed main()
{
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
n = read(); L = sqrt(n); q = read();
for(int i = 1;i <= n;i++) b[i] = (i - 1) / L + 1;
for(int i = 1;i <= n;i++) r[b[i]] = i;
for(int i = n;i >= 1;i--) l[b[i]] = i;
for(int i = 1;i <= b[n];i++) len[i] = r[i] - l[i] + 1;
int l,r,x;
while(q--)
{
l = read(); r = read(); x = read();
update(l,r,x);
int sum1 = querysum(l,r);
int sum2 = querypsum(l,r);
int ans = sum1 * sum1 - sum2;
ans %= mod;
(ans += mod) %= mod;
(ans *= inv2) %= mod;
writeln(ans);
}
return 0;
}
Atcoder 提交记录