跳到正文

题解

题解:P15901 [TOPC 2025] Reactor

ZMQ_Ink65536约 14 分钟
原文
文章信息与版本

文章信息

收录
2026年3月28日
更新
2026年3月28日
篇幅
约 14 分钟
题目
P15901

版本

  1. v1当前

题解:P15901 [TOPC 2025] Reactor

重工业线段树。

解题思路

首先我们假设 n=1n = 1 进行分析。

  • 在 p1>1p_1 > 1 时:每进行一次修改,p1p_1 不变或者至少降低一半。
  • 在 p1=1p_1 = 1 时:无论如何修改,p1p_1 不变。

可以发现,降低最多触发 log⁡pi\log p_i 次。

因此,如果触发次数过多时,pi=1p_i = 1,则可以直接将此点答案(后面记为 sis_i)加一而不处理任何其他操作。

接下来我们假设 ∑pi=n\sum p_i = n 进行分析:

因为每次操作 k≥1k \ge 1,所以所有操作中的点都需要 +1+ 1,直接上线段树即可。

接下来我们把前面两种情况合并。

设 wiw_i 表示 ii 位置还有多少次操作才会触发减半,则 wiw_i 初始为 pip_i,后面每次减半更新则重置为当前 pip_i。

考虑维护一个 cc,表示最小的 pi−wip_i - w_i。如果 c<=0c <= 0,则说明区间内存在需要单点改的地方,递归下去修改即可。

看似这个东西复杂度是 O(nqlog⁡n)O(nq \log n),但是实际上没有这么高。

考虑每个 pip_i 的贡献。

每个 pip_i 最多给 log⁡n\log n 个区间做贡献,每个贡献最多会影响 log⁡n\log n 次就会被清除掉(变为 11 不继续贡献)。因此实际上对复杂度的贡献是 log⁡2n\log^2 n。

考虑实现,如果区间 max⁡=1\max = 1 则直接给答案 +1+ 1,涉及一个区间加即可。

否则如果有区间 c<=0c <= 0,则判断左右子树并递归,可以直接写单点改,根据上面的证明,这个单点改最多有 nlog⁡2nn \log^2 n 次。为了判断是否纯 11 写区间 max⁡\max,然后为了维护答案再写一个区间和,就全部维护完了。

设 pip_i 的值域为 VV,复杂度为 O(qlog⁡2nlog⁡V)\mathcal O(q \log^2 n \log V),7s7s 时间足够通过。

或许更好的实现可以做到 O(qlog⁡nlog⁡V)\mathcal O(q \log n \log V)。

参考代码

极差的实现,写了 66k,变量名或许和上面的用的不太相同,但是定义和逻辑完全一样。

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n , q , op , l , r , k , x , y , pe[200005];
struct node
{
	int l , r , p , c , tagc , cnt , mxc , sum , tags , mxp;
};
struct xds
{
	node tree[800005];
	inline void build(int p , int l , int r)
	{
		tree[p].l = l;
		tree[p].r = r;
		tree[p].tags = 0;
		tree[p].tagc = 0;
		if(l == r)
		{
			tree[p].p = pe[l];
			tree[p].c = 0;
			tree[p].sum = 0;
			tree[p].mxp = pe[l];
			if(tree[p].p == 1)
			{
				tree[p].cnt = 1;
				tree[p].mxc = 1000000000000000018ll;
			}
			else
			{
				tree[p].cnt = 0;
				tree[p].mxc = tree[p].p;
			}
			return;
		}
		int mid = (l + r) / 2;
		build(p * 2 , l , mid);
		build(p * 2 + 1 , mid + 1 , r);
		tree[p].cnt = tree[p * 2].cnt + tree[p * 2 + 1].cnt;
		tree[p].mxc = min(tree[p * 2].mxc , tree[p * 2 + 1].mxc);
		tree[p].sum = tree[p * 2].sum + tree[p * 2 + 1].sum;
		tree[p].mxp = max(tree[p * 2].mxp , tree[p * 2 + 1].mxp);
		return;
	}
	inline void upd(int p , int l , int r , int k)
	{
		if(k == 0) return;
		if(l <= tree[p].l && tree[p].r <= r)
		{
			if(k < tree[p].mxc)
			{
				if(tree[p].cnt > 0)
				{
					tree[p].sum += tree[p].cnt;
					tree[p].tags++;
				}
				if(tree[p].cnt < (tree[p].r - tree[p].l + 1))
				{
					tree[p].tagc += k;
					tree[p].mxc -= k;
				}
				return;
			}
		}
		if(tree[p].l == tree[p].r)
		{
			if(tree[p].p > 1)
			{
				tree[p].c += tree[p].tagc;
			}
			tree[p].tagc = 0;
			tree[p].tags = 0;
			if(tree[p].p == 1)
			{
				tree[p].sum++;
			}
			else
			{
				tree[p].c += k;
				if(tree[p].c >= tree[p].p)
				{
					tree[p].sum++;
					tree[p].c = 0;
					tree[p].p = max(1ll , tree[p].p / 2);
					tree[p].mxp = tree[p].p;
					if(tree[p].p == 1)
					{
						tree[p].cnt = 1;
						tree[p].mxc = 1000000000000000018ll;
					}
					else
					{
						tree[p].mxc = tree[p].p - tree[p].c;
					}
				}
				else
				{
					tree[p].mxc = tree[p].p - tree[p].c;
				}
			}
			return;
		}
		if(tree[p].tags)
		{
			tree[p * 2].sum += tree[p].tags * tree[p * 2].cnt;
			tree[p * 2].tags += tree[p].tags;
			tree[p * 2 + 1].sum += tree[p].tags * tree[p * 2 + 1].cnt;
			tree[p * 2 + 1].tags += tree[p].tags;
			tree[p].tags = 0;
		}
		if(tree[p].tagc)
		{
			tree[p * 2].tagc += tree[p].tagc;
			if(tree[p * 2].mxc < 10000000000000016ll)
			{
				tree[p * 2].mxc -= tree[p].tagc;
			}
			tree[p * 2 + 1].tagc += tree[p].tagc;
			if(tree[p * 2 + 1].mxc < 10000000000000016ll)
			{
				tree[p * 2 + 1].mxc -= tree[p].tagc;
			}
			tree[p].tagc = 0;
		}
		if(tree[p * 2].r >= l)
		{
			upd(p * 2 , l , r , k);
		}
		if(tree[p * 2 + 1].l <= r)
		{
			upd(p * 2 + 1 , l , r , k);
		}
		tree[p].cnt = tree[p * 2].cnt + tree[p * 2 + 1].cnt;
		tree[p].mxc = min(tree[p * 2].mxc , tree[p * 2 + 1].mxc);
		tree[p].sum = tree[p * 2].sum + tree[p * 2 + 1].sum;
		tree[p].mxp = max(tree[p * 2].mxp , tree[p * 2 + 1].mxp);
		return;
	}
	inline void updw(int p , int l , int r)
	{
		if(l <= tree[p].l && tree[p].r <= r)
		{
			tree[p].sum += (tree[p].r - tree[p].l + 1);
			tree[p].tags++;
			tree[p].tagc = 0;
			tree[p].mxc = 1000000000000000018ll;
			tree[p].mxp = 1;
			return;
		}
		if(tree[p].tags)
		{
			tree[p * 2].sum += tree[p].tags * tree[p * 2].cnt;
			tree[p * 2].tags += tree[p].tags;
			tree[p * 2 + 1].sum += tree[p].tags * tree[p * 2 + 1].cnt;
			tree[p * 2 + 1].tags += tree[p].tags;
			tree[p].tags = 0;
		}
		if(tree[p].tagc)
		{
			tree[p * 2].tagc += tree[p].tagc;
			if(tree[p * 2].mxc < 10000000000000016ll)
			{
				tree[p * 2].mxc -= tree[p].tagc;
			}
			tree[p * 2 + 1].tagc += tree[p].tagc;
			if(tree[p * 2 + 1].mxc < 10000000000000016ll)
			{
				tree[p * 2 + 1].mxc -= tree[p].tagc;
			}
			tree[p].tagc = 0;
		}
		if(tree[p * 2].r >= l)
		{
			updw(p * 2 , l , r);
		}
		if(tree[p * 2 + 1].l <= r)
		{
			updw(p * 2 + 1 , l , r);
		}
		tree[p].cnt = tree[p * 2].cnt + tree[p * 2 + 1].cnt;
		tree[p].mxc = min(tree[p * 2].mxc , tree[p * 2 + 1].mxc);
		tree[p].sum = tree[p * 2].sum + tree[p * 2 + 1].sum;
		tree[p].mxp = max(tree[p * 2].mxp , tree[p * 2 + 1].mxp);
		return;
	}
	inline int query(int p , int l , int r)
	{
		if(l <= tree[p].l && tree[p].r <= r)
		{
			return tree[p].sum;
		}
		if(tree[p].tags)
		{
			tree[p * 2].sum += tree[p].tags * tree[p * 2].cnt;
			tree[p * 2].tags += tree[p].tags;
			tree[p * 2 + 1].sum += tree[p].tags * tree[p * 2 + 1].cnt;
			tree[p * 2 + 1].tags += tree[p].tags;
			tree[p].tags = 0;
		}
		if(tree[p].tagc)
		{
			tree[p * 2].tagc += tree[p].tagc;
			if(tree[p * 2].mxc < 10000000000000016ll)
			{
				tree[p * 2].mxc -= tree[p].tagc;
			}
			tree[p * 2 + 1].tagc += tree[p].tagc;
			if(tree[p * 2 + 1].mxc < 10000000000000016ll)
			{
				tree[p * 2 + 1].mxc -= tree[p].tagc;
			}
			tree[p].tagc = 0;
		}
		int cnt = 0;
		if(tree[p * 2].r >= l)
		{
			cnt += query(p * 2 , l , r);
		}
		if(tree[p * 2 + 1].l <= r)
		{
			cnt += query(p * 2 + 1 , l , r);
		}
		return cnt;
	}
	inline int qrymx(int p , int l , int r)
	{
		if(l <= tree[p].l && tree[p].r <= r)
		{
			return tree[p].mxp;
		}
		if(tree[p].tags)
		{
			tree[p * 2].sum += tree[p].tags * tree[p * 2].cnt;
			tree[p * 2].tags += tree[p].tags;
			tree[p * 2 + 1].sum += tree[p].tags * tree[p * 2 + 1].cnt;
			tree[p * 2 + 1].tags += tree[p].tags;
			tree[p].tags = 0;
		}
		if(tree[p].tagc)
		{
			tree[p * 2].tagc += tree[p].tagc;
			if(tree[p * 2].mxc < 10000000000000016ll)
			{
				tree[p * 2].mxc -= tree[p].tagc;
			}
			tree[p * 2 + 1].tagc += tree[p].tagc;
			if(tree[p * 2 + 1].mxc < 10000000000000016ll)
			{
				tree[p * 2 + 1].mxc -= tree[p].tagc;
			}
			tree[p].tagc = 0;
		}
		int cnt = 0;
		if(tree[p * 2].r >= l)
		{
			cnt = max(cnt , qrymx(p * 2 , l , r));
		}
		if(tree[p * 2 + 1].l <= r)
		{
			cnt = max(cnt , qrymx(p * 2 + 1 , l , r));
		}
		return cnt;
	}
}xds;
signed main()
{
	ios::sync_with_stdio(0);
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> n >> q;
	for(int i = 1 ; i <= n ; i++)
	{
		cin >> pe[i];
	}
	xds.build(1 , 1 , n);
	while(q--)
	{
		cin >> op;
		if(op == 1)
		{
			cin >> x >> y >> k;
			if(xds.qrymx(1 , x , y) == 1)
			{
				xds.updw(1 , x , y);
			}
			else
			{
				xds.upd(1 , x , y , k);
			}
		}
		else
		{
			cin >> x >> y;
			cout << xds.query(1 , x , y) << '\n';
		}
	}
	return 0;
}
原文