跳到正文

题解

题解:P14152 千手百眼,天下人间

梦见云月约 7 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 7 分钟
题目
P14152

版本

  1. v3当前
  2. v2
  3. v1

诈骗。

首先撤销是指区间赋值而不是区间异或 11 啊。

题意写的不好再次致歉。

看到时间开到了 101810^{18},同时还有限制,果断离散化。

考虑直接来做是困难的,所以我们考虑倒着做。

对于每个撤销操作,如果它所在时间点未被叉掉,就让它叉掉它所能叉掉的点。

反之跳过即可。

然后再正着跑一边线段树,对于不会被叉掉的正常操作即可。

我们考虑怎么样快速叉点。

显然我们既然已经离散化过了,直接对于离散化过后的数组建线段树,区间标记、单点查询,线段树和树状数组都可以用,还有一些奇怪的东西也可以。

然后就是简单的问题了,或许实际难度第二简单?

时间复杂度 O(m(log⁡m+log⁡n))O(m(\log m+\log n))。

代码:

C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
int n,m;
int a[500005];


int lsh[1500005];
int tot;



struct node{
	int l,r;
	int bj;
}vis[6000005];

struct CZ{
	int opt;
	int id;
	int l,r,t,k;
}q[500005];
map<int,int>mp;
inline void build_vis(int p,int l,int r){
	vis[p].bj=0;
	vis[p].l=l;
	vis[p].r=r;
	if (l==r) return;
	int mid=(l+r)/2;
	build_vis(p<<1,l,mid);
	build_vis(p<<1|1,mid+1,r);
}



inline void change_vis(int p,int L,int R){
	int l=vis[p].l;
	int r=vis[p].r;
	if (L<=l && r<=R){
		vis[p].bj=1;
		return;
	}
	int mid=(l+r)/2;
	if (L<=mid){
		change_vis(p<<1,L,R);
	}
	if (mid<R){
		change_vis(p<<1|1,L,R);
	}
}

inline int ask_vis(int p,int id){
	int l=vis[p].l;
	int r=vis[p].r;
	if (vis[p].bj){
		return 1;
	}
	if (l==r){
		return vis[p].bj;
	}
	int mid=(l+r)/2;
	if (id<=mid){
		return ask_vis(p<<1,id);
	}
	else{
		return ask_vis(p<<1|1,id);
	}
}


inline bool cmp(CZ a,CZ b){
	if (a.t^b.t) return a.t<b.t;
	return a.id<b.id;
}

inline int pos(int x){
	return lower_bound(lsh+1,lsh+tot+1,x)-lsh;
}


struct Node{
	int maxn,lazy;
}tree[6000005];

inline void update(int p){
	tree[p].maxn=max(tree[p<<1].maxn,tree[p<<1|1].maxn);
}

inline void build(int p,int l,int r){
	if (l==r){
		tree[p].maxn=a[l];
		tree[p].lazy=0;
		return;
	}
	int mid=(l+r)/2;
	build(p<<1,l,mid);
	build(p<<1|1,mid+1,r);
	update(p);
}

inline void lazy(int p){
	if (tree[p].lazy){
		tree[p<<1].maxn+=tree[p].lazy;
		tree[p<<1|1].maxn+=tree[p].lazy;
		tree[p<<1].lazy+=tree[p].lazy;
		tree[p<<1|1].lazy+=tree[p].lazy;
		tree[p].lazy=0;
	}
}

inline void change(int p,int l,int r,int L,int R,int val){
	if (L<=l && r<=R){
		tree[p].maxn+=val;
		tree[p].lazy+=val;
		return;
	}
	lazy(p);
	int mid=(l+r)/2;
	if (L<=mid){
		change(p<<1,l,mid,L,R,val);
	}
	if (mid<R){
		change(p<<1|1,mid+1,r,L,R,val);
	}
	update(p);
}

inline int ask(int p,int l,int r,int L,int R){
	if (L<=l && r<=R){
		return tree[p].maxn;
	}
	lazy(p);
	int mid=(l+r)/2;
	int ans=-1e18;
	if (L<=mid){
		ans=ask(p<<1,l,mid,L,R);
	}
	if (mid<R){
		ans=max(ans,ask(p<<1|1,mid+1,r,L,R));
	}
	return ans;
}
int v[1000005];

int ans[1000005];
int res;

signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
	cin>>n>>m;
	for (int i=1;i<=n;i++){
		cin>>a[i];
	}
	for (int i=1;i<=m;i++){
		cin>>q[i].opt;
		q[i].id=i;
		cin>>q[i].t>>q[i].l>>q[i].r;
		if (!mp[q[i].t]){
			lsh[++tot]=q[i].t;
			mp[q[i].t]=1;
		}
		if (q[i].opt==1){
			cin>>q[i].k;
		}
		else if (q[i].opt==2){
			q[i].k=-1;
		}
		else{
			q[i].k=-1;
			if (!mp[q[i].l]){
				lsh[++tot]=q[i].l;
				mp[q[i].l]=1;
			}
			if (!mp[q[i].r]){
				lsh[++tot]=q[i].r;
				mp[q[i].r]=1;
			}

		}
	}

	sort(q+1,q+m+1,cmp);
	sort(lsh+1,lsh+tot+1);
	build_vis(1,1,tot);

	for (int i=m;i>=1;i--){
		v[i]=ask_vis(1,pos(q[i].t));// 为 1 则被撤销。
		if (q[i].opt==3){
			if (!v[i]){
				change_vis(1,pos(q[i].l),pos(q[i].r));
			}
		}
	}
	build(1,1,n);
	for (int i=1;i<=m;i++){
		if (v[i]) continue;
		if (q[i].opt==3){
			continue;
		}
		if (q[i].opt==1){
			change(1,1,n,q[i].l,q[i].r,q[i].k);
		}
		else{
			ans[++res]=ask(1,1,n,q[i].l,q[i].r);
		}
	}
	cout<<res<<endl;
	for (int i=1;i<=res;i++){
		cout<<ans[i]<<endl;
	}
	return 0;
}
原文