每个熊孩子抽象成平面上的一个点,横坐标为左边界,纵坐标为右边界,点权为区间长度,把第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
*/