跳到正文

题解

[ABC455F] Merge Slimes 2

EasonCyx约 8 分钟
原文
文章信息与版本

文章信息

发布
2026年4月25日
收录
2026年4月25日
更新
2026年11月14日
篇幅
约 8 分钟

版本

  1. v2当前
  2. v1

题解

题目传送门

思路

先看合并操作,把 xx 和 yy 合并,成本 xyxy,合并后 x+yx+y。 合并 x,y,zx,y,z,可以发现成本只能为 xy+yz+zxxy+yz+zx,合并后 x+y+zx+y+z。 合并 x,y,z,wx,y,z,w,可以发现成本只能为 xy+xz+xw+yz+yw+zwxy+xz+xw+yz+yw+zw,合并后 x+y+z+wx+y+z+w。 以此类推,合并 a1,a2,…,ana_1,a_2,\dots,a_n,成本为 (∑i=1nai)2−(∑i=1nai2)2\displaystyle\frac{(\sum_{i=1}^n a_i)^2-(\sum_{i=1}^n a_i^2)}{2}。 所以题目等价于维护区间加法,查询区间和,查询区间平方和。 注意到 n,q≤105n,q \le 10^5,可以使用线段树或分块维护。 时间复杂度 O⁡(nlog⁡n)\operatorname{O}(n \log n) 或 O⁡(nn)\operatorname{O}(n \sqrt n)。 这篇题解里面给的代码是分块的代码,线段树的代码点这里。

AC Code

C++
#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 提交记录

原文