跳到正文

题解

题解:P6783 [Ynoi2008] rrusq

RpUtl约 9 分钟
原文
文章信息与版本

文章信息

发布
2026年3月24日
收录
2026年3月24日
更新
2026年10月24日
篇幅
约 9 分钟
题目
P6783

版本

  1. v2当前
  2. v1

思路分析

带权矩形并问题,但是有弱化版带权线段并问题。

先考虑线段怎么做,一种非常经典的做法是 ODT 扫描线。维护 ii 从 11 扫到 nn,每次将线段上 [li,ri][l_i,r_i] 涂上颜色 ii,并实时维护 fif_i 表示当前颜色 ii 覆盖的部分的总权值。

因为只有区间覆盖,所有 ODT 只有 O(n)O(n) 个颜色段,对 fif_i 只有 O(n)O(n) 次修改,对于询问 [L,R][L,R],当 ii 扫到 RR 时,直接查询 ff 在 [L,R][L,R] 的权值和即可。用树状数组维护 fif_i 不难做到 O(nlog⁡n)O(n\log n)。

这个做法很难拓展,可以考虑如何用线段树实现上述过程。

还是维护 fif_i,将线段上 [li,ri][l_i,r_i] 涂上颜色 ii 时,线段树会把这个区间拆成 log⁡n\log n 个区间,我们考虑在这些区间上打上懒标记,表示区间内点均为懒标记的颜色,但是子树内可能还有一些区间已经打上了标记,直接搜索子树暴力清除懒标记即可。

这样做还是 O(nlog⁡n)O(n\log n) 的,为什么?把时间复杂度均摊到每一次覆盖操作上,相当于我们从根出发,把标记打在对应节点上,然后又从根出发,把对应节点上的标记前后逐一回收,类似两次线段树查询操作。这样就做到了对 fif_i 进行 O(nlog⁡n)O(n\log n) 次操作。

有了线段树解法,回到本题,可以发现把这个算法套在 KDT 上是同样适用的,因为二者有类似的查询结构。

这样就做到了对 fif_i 进行 O(mn)O(m\sqrt n) 次操作,如何还是使用树状数组维护,时间复杂度为 O((m+q)nlog⁡n)O((m+q)\sqrt{n}\log n)。但是不难发现查询次数为 qq 次,远远小于修改次数,可以使用单点修改 O(1)O(1),区间查询 O(n)O(\sqrt{n}) 的分块代替树状数组平衡复杂度,最终就做到了 O((m+q)n)O((m+q)\sqrt{n})。

本题的 KDT 写法及其类似于平衡树,但是标记下传仍然有很多差异。

Code

没卡什么常数,不确定能不能过。

C++
#include <bits/stdc++.h>
using namespace std;
const int MAXSIZE=(1<<20);
char buf[1<<20],*p1,*p2;
#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,MAXSIZE,stdin),p1==p2)?EOF:*p1++)
template <class T> inline void read(T &a){
	int x=0,f=1;char ch=gc();
	while(!isdigit(ch)){if(ch=='-')f=-1;ch=gc();}
	while(isdigit(ch))x=(x<<3)+(x<<1)+ch-48,ch=gc();
	return a=x*f,void();
}
const int N=1e5+10;
const int M=1e6+10;
const int T=505;
typedef long long ll;
int K,rt,B,t;
struct node{
	int lc,rc;
	int v[2],L[2],R[2];
	int col,tag,siz;
	ll sum,val;
	bool operator < (const node &u)const{
		return v[K]<u.v[K];
	}
}tr[N],sq[N];
int n,m,q,l[M],pos[N],L[T],R[T];
ll num[N],sum[T],ans[M];
vector<int>Q[N];
inline void add(int x,ll v){
	num[x]+=v,sum[pos[x]]+=v;
}
ll ask(int l,int r){
	ll ans=0;
	if(pos[l]==pos[r]){
		for(int i=l;i<=r;i++)ans+=num[i];
	}else{
		for(int i=l;i<=R[pos[l]];i++)ans+=num[i];
		for(int i=L[pos[r]];i<=r;i++)ans+=num[i];
		for(int i=pos[l]+1;i<pos[r];i++)ans+=sum[i];
	}
	return ans;
}
#define ls tr[p].lc
#define rs tr[p].rc
void inline chkmax(int &u,int v){u=max(u,v);}
void inline chkmin(int &u,int v){u=min(u,v);}
void pushup(int p){
	for(int i=0;i<2;i++){
		tr[p].L[i]=tr[p].R[i]=tr[p].v[i];
		if(ls){
			chkmin(tr[p].L[i],tr[ls].L[i]);
			chkmax(tr[p].R[i],tr[ls].R[i]);
		}
		if(rs){
			chkmin(tr[p].L[i],tr[rs].L[i]);
			chkmax(tr[p].R[i],tr[rs].R[i]);
		}
	}
	tr[p].sum=tr[p].val;
	if(ls)tr[p].sum+=tr[ls].sum;
	if(rs)tr[p].sum+=tr[rs].sum;
	return;
}
void build(int &p,int l,int r,int d){
	if(l>r)return;p=(r+l)>>1;K=d;
	nth_element(tr+l,tr+p,tr+r+1);
	build(ls,l,p-1,d^1);
	build(rs,p+1,r,d^1);
	pushup(p);return;
}
void push_up(int p){
	tr[p].siz=(tr[ls].siz||tr[rs].siz||tr[p].col||tr[p].tag);
}
void pushdown(int p){
	if(ls){
		tr[ls].tag=tr[p].tag;
		push_up(ls);
	}
	if(rs){
		tr[rs].tag=tr[p].tag;
		push_up(rs);
	}
	tr[p].col=tr[p].tag;
	tr[p].tag=0;return;
}
void clear(int p,int o=-1){
	if(!tr[p].siz)return;
	if(tr[p].tag){
		add(tr[p].tag,-tr[p].sum);
		tr[p].tag=0;
	}else{
		if(tr[p].col){
			add(tr[p].col,-tr[p].val);
			tr[p].col=0;
		}
		clear(ls),clear(rs);
	}
	push_up(p);
}
inline bool checkin(node a,node b){
	if(a.L[0]>=b.L[0]&&a.R[0]<=b.R[0]&&a.L[1]>=b.L[1]&&a.R[1]<=b.R[1])return 1;
	else return 0;
}
inline bool checkout(node a,node b){
	if(a.R[0]<b.L[0]||b.R[0]<a.L[0]||a.R[1]<b.L[1]||b.R[1]<a.L[1])return 1;
	else return 0;
}
inline bool check(node a,node b){
	if(a.v[0]>=b.L[0]&&a.v[0]<=b.R[0]&&a.v[1]>=b.L[1]&&a.v[1]<=b.R[1])return 1;
	else return 0;
}
void upd(int p,int u){
	if(checkout(tr[p],sq[u]))return;
	else if(checkin(tr[p],sq[u])){
		clear(p,u);
		add(u,tr[p].sum);
		tr[p].tag=u;
	}else{
		if(tr[p].tag)pushdown(p);
		if(check(tr[p],sq[u])){
			if(tr[p].col)add(tr[p].col,-tr[p].val);
			tr[p].col=u,add(u,tr[p].val);
		}
		if(ls)upd(ls,u);
		if(rs)upd(rs,u);
	}
	push_up(p);
	return;
}
#undef ls
#undef rs
int main(){
	read(n);
	for(int i=1;i<=n;i++){
		tr[i].v[0]=i;
		read(tr[i].v[1]),read(tr[i].val);
	}
	build(rt,1,n,0);
	read(m);t=B=sqrt(n);
	for(int i=1;i<=t;i++){
		L[i]=R[i-1]+1;
		R[i]=L[i]+B-1;
	}
	if(R[t]<m){
		++t;
		L[t]=R[t-1]+1,R[t]=m;
	}
	for(int i=1;i<=t;i++){
		for(int j=L[i];j<=R[i];j++){
			pos[j]=i;
		}
	}
	for(int i=1;i<=m;i++){
		for(int j=0;j<2;j++){
			read(sq[i].L[j]),read(sq[i].R[j]);
		}
	}
	read(q);
	for(int i=1,r;i<=q;i++){
		read(l[i]),read(r);
		Q[r].push_back(i);
	}
	for(int i=1;i<=m;i++){
		upd(rt,i);
		for(auto x:Q[i])ans[x]=ask(l[x],i);
	}
	for(int i=1;i<=q;i++){
		printf("%lld\n",ans[i]);
	}
	return 0;
}

如有错误请指出。

原文