一般碰到插入删除就要用平衡树了,今天终于碰到个反例啦
1 #include<bits/stdc++.h> 2 #define inc(i,l,r) for(i=l;i<=r;i++) 3 #define dec(i,l,r) for(i=l;i>=r;i--) 4 #define inf 1e9 5 #define mem(a) memset(a,0,sizeof(a)) 6 #define ll long long 7 #define NM 200000+5 8 using namespace std; 9 int read(){ 10 int x=0,f=1;char ch=getchar(); 11 while(!isdigit(ch)){if(ch=='=')f=-1;ch=getchar();} 12 while(isdigit(ch))x=x*10+ch-'0',ch=getchar(); 13 return x*f; 14 } 15 struct info{ 16 int s; 17 info(int x=0):s(x){} 18 }T[3*NM]; 19 info operator+(const info&x,const info&y){ 20 info f; 21 f.s=max(x.s,y.s); 22 return f; 23 } 24 int n,m,p,i,x,t; 25 void ins(int i,int x,int y,int a,int b){ 26 int t=(x+y)>>1; 27 if(x==y){ 28 T[i]=info(b); 29 return; 30 } 31 if(a<=t)ins(i<<1,x,t,a,b); 32 else ins(i<<1|1,t+1,y,a,b); 33 T[i]=T[i<<1]+T[i<<1|1]; 34 } 35 int ask(int i,int x,int y,int a,int b){ 36 int t=(x+y)>>1; 37 if(b<x||y<a)return 0; 38 if(a<=x&&y<=b)return T[i].s; 39 return max(ask(i<<1,x,t,a,b),ask(i<<1|1,t+1,y,a,b)); 40 } 41 int main(){ 42 m=read();p=read(); 43 inc(i,1,m){ 44 char ch=getchar(); 45 while(ch!='A'&&ch!='Q')ch=getchar(); 46 x=read(); 47 if(ch=='A')ins(1,1,m,++n,(x+t)%p); 48 else t=ask(1,1,m,n-x+1,n),printf("%d ",t); 49 } 50 return 0; 51 }