BZOJ4631 踩气球

本文介绍了一种利用二维线段树解决特定区间更新和查询问题的方法。通过将熊孩子的左右边界映射到平面上的点,并使用二维线段树进行维护,实现了高效的区间更新和查询操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

每个熊孩子抽象成平面上的一个点,横坐标为左边界,纵坐标为右边界,点权为区间长度,把第x个盒子拿空相当于把以(1,x)为左下角,(x,n)为右上角的矩形内的点减一,答案就是有多少个点等于0,因为每个点最多变成1次0所以当矩形内最小值等于0的时候就找到这个最小值然后把他设为INF并把答案+1即可

上述操作可以用KDT或者二维线段树之类的维护

复杂度O(n sqrt(n))

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iomanip>
#include<ctime>
#include<vector>
#include<stack>
#include<set>
#include<bitset>
#include<map>
#include<queue>
using namespace std;
#define MAXN 100010
#define MAXM 1010
#define MOD 1000000007
#define INF 1000000000
#define eps 1e-8
#define ll long long 
struct pt{
	int x;
	int y;
};
int a[MAXN];
pt p[MAXN];
int n,m,q;
int la;
int LX=INF,LY=INF,RX,RY;
pt v[MAXN];
int val[MAXN],mn[MAXN],ch[MAXN];
pt L[MAXN],R[MAXN];
int tot;
int rt;
int son[MAXN][2];
bool cmpx(pt x,pt y){
	return x.x<y.x;
}
bool cmpy(pt x,pt y){
	return x.y<y.y;
}
inline void toch(int x,int y){
	if(!x){
		return ;
	}
	val[x]+=y;
	ch[x]+=y;
	mn[x]+=y;
}
inline void pd(int x){
	if(ch[x]){
		toch(son[x][0],ch[x]);
		toch(son[x][1],ch[x]);
		ch[x]=0;
	}
}
inline void ud(int x){
	mn[x]=val[x];
	if(son[x][0]){
		mn[x]=min(mn[x],mn[son[x][0]]);
	}
	if(son[x][1]){
		mn[x]=min(mn[x],mn[son[x][1]]);
	}
}

void build(int &x,int l,int r,bool f){
	if(l>r){
		return ;
	}
	x=++tot;
	if(l==r){
		v[x]=L[x]=R[x]=p[l];
		mn[x]=val[x]=p[l].y-p[l].x+1;
		return ;
	}
	int mid=l+r>>1;
	if(f){
		nth_element(p+l,p+mid,p+r+1,cmpx);
		
	}else{
		nth_element(p+l,p+mid,p+r+1,cmpy);
	}
	v[x]=L[x]=R[x]=p[mid];
	val[x]=p[mid].y-p[mid].x+1;
	build(son[x][0],l,mid-1,f^1);
	build(son[x][1],mid+1,r,f^1);
	if(son[x][0]){
		L[x].x=min(L[x].x,L[son[x][0]].x);
		L[x].y=min(L[x].y,L[son[x][0]].y);
		R[x].x=max(R[x].x,R[son[x][0]].x);
		R[x].y=max(R[x].y,R[son[x][0]].y);
	}
	if(son[x][1]){
		L[x].x=min(L[x].x,L[son[x][1]].x);
		L[x].y=min(L[x].y,L[son[x][1]].y);
		R[x].x=max(R[x].x,R[son[x][1]].x);
		R[x].y=max(R[x].y,R[son[x][1]].y);
	}
	ud(x);
}

void change(int x,int lx,int ly,int rx,int ry){
	if(L[x].x>rx||L[x].y>ry||R[x].x<lx||R[x].y<ly||!x){
		return ;
	}
	if(L[x].x>=lx&&L[x].y>=ly&&R[x].x<=rx&&R[x].y<=ry){
		toch(x,-1);
		return ;
	}
	pd(x);
	if(v[x].x>=lx&&v[x].y>=ly&&v[x].x<=rx&&v[x].y<=ry){
		val[x]--;
	}
	change(son[x][0],lx,ly,rx,ry);
	change(son[x][1],lx,ly,rx,ry);
	ud(x);
}
void find(int x){
	if(!x){
		return ;
	}
	if(!val[x]){
		val[x]=INF;
		la++;
	}
	pd(x);
	if(!mn[son[x][0]]){
		find(son[x][0]);
	}
	if(!mn[son[x][1]]){
		find(son[x][1]);
	}
	ud(x);
}
int main(){
	int i,x;
	scanf("%d%d",&n,&m);
	for(i=1;i<=n;i++){
		scanf("%d",&a[i]);
	}
	for(i=1;i<=m;i++){
		scanf("%d%d",&p[i].x,&p[i].y);
	}
	build(rt,1,m,1);
	scanf("%d",&q);
	while(q--){
		scanf("%d",&x);
		x=(x+la-1)%n+1;
		a[x]--;
		if(!a[x]){
			change(rt,1,x,x,n);
		}
		find(rt);
		printf("%d\n",la);
	}
	return 0;
}

/*
5 3
1 2 1 2 1
1 5
1 5
1 5
7
1
2
3
4
5
2
4

*/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值