跳到正文

题解

题解:P13963 [ICPC 2023 Nanjing R] 接雨水

wangyanjing约 13 分钟
原文
文章信息与版本

文章信息

发布
2025年10月12日
收录
2026年7月31日
更新
2026年11月9日
篇幅
约 13 分钟
获赞
5
题目
P13963

版本

  1. v1当前

PS:巨难写,但是稳定 O(n+qlog⁡n)O(n + q\log n)。

Solution

Step1:推式子

容易发现,答案和最大值的位置有关。

记:最大值为 MxMx。
记:最大值第一次出现的位置为 ll,最后一次出现的位置为 rr。

那么:

∑i=1nmin⁡(fi,gi)=(∑i=1l−1fi)+(r−l+1)Mx+(∑i=r+1ngi)\sum_{i = 1}^n \min(f_i,g_i) = (\sum_{i = 1}^{l-1} f_i) + (r-l+1)Mx + (\sum_{i = r+1}^n g_i)

证明:

当 i∈[1,l−1]i \in [1,l-1] 时,fi<Mx,gi=Mxf_i <Mx,g_i = Mx,∴min⁡(fi,gi)=fi\therefore\min(f_i,g_i) = f_i。

当 i∈[l,r]i \in [l,r] 时:fi=Mx,gi=Mxf_i = Mx,g_i = Mx,∴min⁡(fi,gi)=Mx\therefore \min(f_i,g_i) = Mx。

当 i∈[r+1,n]i \in [r+1,n] 时,fi=Mx,gi<Mxf_i = Mx,g_i < Mx,∴min⁡(fi,gi)=gi\therefore \min(f_i,g_i) = g_i。

证毕!

Step2:单点修改对 f,gf,g 的影响

沿用题目:修改的位置为 xx。

下面的 axa_x 已修改。

记:a0=an+1=infa_0 = a_{n+1} = inf。

记:

Lmaxx=max⁡i∈[0,x−1],ai>axiLmax_x = \max_{i\in[0,x-1],a_i > a_x} i Rmaxx=min⁡i∈[x+1,n+1],ai>axiRmax_x = \min_{i \in [x+1,n+1],a_i>a_x} i

若对 ff 有影响,则:Lmaxx=0Lmax_x = 0。
此时:对于所有 ii 满足 x≤i≤Rmaxx−1x\le i\le Rmax_x-1,fi←axf_i\leftarrow a_x。

若对 gg 有影响,则:Rmaxx=n+1Rmax_x = n+1。
此时:对于所有 ii 满足 Lmaxx+1≤i≤xLmax_x + 1\le i\le x,gi←axg_i\leftarrow a_x。

Step3:在有单点修改的情况下求 LmaxxLmax_x 和 RmaxxRmax_x

和这个题差不多。

开一颗线段树,维护的信息是最大值,支持单点修改。

LmaxxLmax_x 的做法

如果我告诉你在一个线段树的子树中,保证有解,那怎么做?

那么:
如果右子树的最大值比 axa_x 大,就去右子树,否则就去左子树,这样递归下去,得到的结果就是 LmaxxLmax_x。

记:depdep 为这颗子树的深度,MxdepMxdep 为这颗线段树的深度。

那么,这样的时间复杂度为 O(dep)O(dep)。

dep≤Mxdep=log⁡ndep \le Mxdep = \log n

所以我们这样的时间复杂度是 O(log⁡n)O(\log n) 的。

那么,我们怎样保证有解。

我们可以按照线段树区间查询的方式,把 [1,x][1,x] 拆成 O(log⁡n)O(\log n) 个区间。

这些区间我们可以做到有序。

然后我们倒序枚举每个区间,查最大值是否大于 axa_x 即可。

这一步,也是 O(log⁡n)O(\log n) 的。

RmaxxRmax_x 的做法

同 LmaxxLmax_x 的做法,不再赘述。

Step4:维护 f,gf_,g

结合 Step1 和 Step2,我们发现我们需要一个数据结构,支持区间覆盖,查区间和。

用线段树即可。

这也是 O(log⁡n)O(\log n) 的。

Step5:计算时间复杂度

我们每一步都是 O(log⁡n)O(\log n) 的,所以我们修改一次的时间复杂度也是 O(log⁡n)O(\log n) 的。

那么修改操作的总的时间复杂度是 O(qlog⁡n)O(q \log n),并且稳定。

我们还要花费 O(n)O(n) 的时间,读入,建树。

总时间复杂度:O(n+qlog⁡n)O(n + q\log n)。

Code

Code
C++
#include<cstdio>
#include<algorithm> 
#define ll long long
const int N = 1e5+5;
int n;
ll a[N],f[N],g[N];
struct Info{
	ll Max,fst,last;
	inline Info operator + (const Info x)const{
		Info ans;
		if(Max == x.Max){
			ans.Max = Max;
			ans.fst = fst,
			ans.last = x.last;
		}else ans = Max>x.Max ? (*this) : x;
		return ans;
	}
};
struct Segment_tree1{
	struct Tree{
		int lson,rson;
		Info S;
	}tree[N<<1];
	int root,total = 0,cnt;
	struct Get_Root{
		int rt,l,r;
	}R[30];
	inline void push_up(int rt){
		tree[rt].S = tree[tree[rt].lson].S + tree[tree[rt].rson].S;
	}
	inline void build(int &rt,int l,int r){
		rt = ++total;
		if(l == r) return void(tree[rt].S = {a[l],l,l});
		int mid = l + r >> 1;
		build(tree[rt].lson,l,mid);
		build(tree[rt].rson,mid+1,r);
		push_up(rt);
	}
	inline void Update(int rt,int l,int r,int pos,ll v){
		if(l == r) return void(tree[rt].S = {v,l,l});
		int mid = l + r >> 1;
		if(pos<=mid) Update(tree[rt].lson,l,mid,pos,v);
		else Update(tree[rt].rson,mid+1,r,pos,v);
		push_up(rt);
	}
	inline void Split(int rt,int treeL,int treeR,int askL,int askR){
		if(askL <= treeL && treeR <= askR) 
			return void(R[++cnt] = {rt,treeL,treeR});
		int mid = treeL + treeR >> 1;
		if(askR<=mid) return Split(tree[rt].lson,treeL,mid,askL,askR);
		if(askL>mid) return Split(tree[rt].rson,mid+1,treeR,askL,askR);
		Split(tree[rt].lson,treeL,mid,askL,askR);
		Split(tree[rt].rson,mid+1,treeR,askL,askR);
	}
	inline int AskL(int rt,int l,int r,ll v){
		if(l == r) return l;
		ll Rmax = tree[tree[rt].rson].S.Max;
		int mid = l + r >> 1;
		if(Rmax>v) return AskL(tree[rt].rson,mid+1,r,v);
		else return AskL(tree[rt].lson,l,mid,v);
	}
	inline int Lmax(int pos){
		cnt = 0;
		Split(root,1,n,1,pos);
		for(int i = cnt;i;--i)
			if(tree[R[i].rt].S.Max > a[pos]) 
				return AskL(R[i].rt,R[i].l,R[i].r,a[pos]);
		return 0;
	} 
	inline int AskR(int rt,int l,int r,ll v){
		if(l == r) return l;
		ll Lmax = tree[tree[rt].lson].S.Max;
		int mid = l + r >> 1;
		if(Lmax>v) return AskR(tree[rt].lson,l,mid,v);
		else return AskR(tree[rt].rson,mid+1,r,v);
	}
	inline int Rmax(int pos){
		cnt = 0;
		Split(root,1,n,pos,n);
		for(int i = 1;i<=cnt;++i)
			if(tree[R[i].rt].S.Max > a[pos])
				return AskR(R[i].rt,R[i].l,R[i].r,a[pos]);
		return n+1;
	} 
	inline void clear(void){
		for(int i = 1;i<=total;++i)
			tree[i] = {0,0,{0,0,0}};
		root = total = 0;
	}
}T;
struct Segment_Tree2{
	struct Tree{
		int lson,rson;
		ll Sum,Same;
		inline void push_Same(ll v,ll len){
			Sum = v * len;
			Same = v;
		}
	}tree[N<<1];
	int root,total = 0;
	inline void push_up(int rt){
		tree[rt].Sum = tree[tree[rt].lson].Sum + tree[tree[rt].rson].Sum;
	} 
	inline void build(int &rt,int l,int r,ll *a){
		rt = ++total;
		tree[rt].Same = -1;
		if(l == r) return void(tree[rt].Sum = a[l]);
		int mid = l + r >> 1;
		build(tree[rt].lson,l,mid,a);
		build(tree[rt].rson,mid+1,r,a);
		push_up(rt);
	}
	inline void push_down(int rt,int ln,int rn){
		if(~tree[rt].Same){
			tree[tree[rt].lson].push_Same(tree[rt].Same,ln);
			tree[tree[rt].rson].push_Same(tree[rt].Same,rn);
			tree[rt].Same = -1;
		}
	} 
	inline void Update(int rt,int treeL,int treeR,int askL,int askR,ll v){
		if(askL <= treeL && treeR <= askR) return tree[rt].push_Same(v,treeR-treeL+1);
		int mid = treeL + treeR >> 1;
		push_down(rt,mid-treeL+1,treeR-mid);
		if(askL<=mid) Update(tree[rt].lson,treeL,mid,askL,askR,v);
		if(askR>mid) Update(tree[rt].rson,mid+1,treeR,askL,askR,v);
		push_up(rt);
	} 
	inline ll Ask(int rt,int treeL,int treeR,int askL,int askR){
		if(askL <= treeL && treeR <= askR) return tree[rt].Sum;
		int mid = treeL + treeR >> 1;
		push_down(rt,mid-treeL+1,treeR-mid);
		if(askR<=mid) return Ask(tree[rt].lson,treeL,mid,askL,askR);
		if(askL>mid) return Ask(tree[rt].rson,mid+1,treeR,askL,askR);
		return Ask(tree[rt].lson,treeL,mid,askL,askR) + Ask(tree[rt].rson,mid+1,treeR,askL,askR);
	}
	inline void clear(void){
		for(int i = 1;i<=total;++i)
			tree[i] = {0,0,0,0};
		root = total = 0;
	}
}Tf,Tg; 
inline void Solve(void){
	std::scanf("%d",&n);
	ll S = 0;
	for(int i = 1;i<=n;++i){
		std::scanf("%lld",a+i);
		S += a[i];
	}
	f[0] = f[n+1] = g[0] = g[n+1] = 0;
	for(int i = 1;i<=n;++i) f[i] = std::max(f[i-1],a[i]);
	for(int i = n;i;--i) g[i] = std::max(g[i+1],a[i]);
	T.build(T.root,1,n);
	Tf.build(Tf.root,1,n,f);
	Tg.build(Tg.root,1,n,g);
	int q; 
	std::scanf("%d",&q);
	while(q--){
		int x,v;
		std::scanf("%d %d",&x,&v);
		S += v,a[x] += v;
		T.Update(T.root,1,n,x,a[x]);
		Info Mx = T.tree[T.root].S;
		int L = T.Lmax(x),R = T.Rmax(x);
		if(!L) Tf.Update(Tf.root,1,n,x,R-1,a[x]);
		if(R == n+1) Tg.Update(Tg.root,1,n,L+1,x,a[x]);
		ll ans = 0;
		if(Mx.fst>1) ans += Tf.Ask(Tf.root,1,n,1,Mx.fst-1);
		ans += (Mx.last - Mx.fst + 1) * Mx.Max;
		if(Mx.last<n) ans += Tg.Ask(Tg.root,1,n,Mx.last+1,n);
		ans -= S;
		printf("%lld\n",ans);
	}
	T.clear();
	Tf.clear();
	Tg.clear();
}
int main(){
	int T;
	std::scanf("%d",&T); 
	while(T--) Solve();
	return 0;
}
原文
存档评论1 条抓取于