题解:P15901 [TOPC 2025] Reactor
重工业线段树。
解题思路
首先我们假设 进行分析。
- 在 时:每进行一次修改, 不变或者至少降低一半。
- 在 时:无论如何修改, 不变。
可以发现,降低最多触发 次。
因此,如果触发次数过多时,,则可以直接将此点答案(后面记为 )加一而不处理任何其他操作。
接下来我们假设 进行分析:
因为每次操作 ,所以所有操作中的点都需要 ,直接上线段树即可。
接下来我们把前面两种情况合并。
设 表示 位置还有多少次操作才会触发减半,则 初始为 ,后面每次减半更新则重置为当前 。
考虑维护一个 ,表示最小的 。如果 ,则说明区间内存在需要单点改的地方,递归下去修改即可。
看似这个东西复杂度是 ,但是实际上没有这么高。
考虑每个 的贡献。
每个 最多给 个区间做贡献,每个贡献最多会影响 次就会被清除掉(变为 不继续贡献)。因此实际上对复杂度的贡献是 。
考虑实现,如果区间 则直接给答案 ,涉及一个区间加即可。
否则如果有区间 ,则判断左右子树并递归,可以直接写单点改,根据上面的证明,这个单点改最多有 次。为了判断是否纯 写区间 ,然后为了维护答案再写一个区间和,就全部维护完了。
设 的值域为 ,复杂度为 , 时间足够通过。
或许更好的实现可以做到 。
参考代码
极差的实现,写了 k,变量名或许和上面的用的不太相同,但是定义和逻辑完全一样。
#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;
}