| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
mdzz hdu 1950卡过了 这里就tle#include <cstdio>
#include <cstring>
#define tmp (st<<1)
#define mid ((l+r)>>1)
#define lson l,mid,tmp
#define rson mid+1,r,tmp|1
#define maxn 50002
#define len (r-l+1)
#define push_up(x) sum[x]=sum[tmp]+sum[tmp|1]
using namespace std;
int sum[maxn<<2],sta[maxn],top;
inline void build(int l,int r,int st){
if(l==r){
sum[st]=1;
return ;
}
build(lson);
build(rson);
push_up(st);
}
inline void updata(int t,int add,int l,int r,int st){
if(l==r){
sum[st]=add;
return ;
}
if(t<=mid)updata(t,add,lson);
else updata(t,add,rson);
push_up(st);
}
inline int query(int L,int R,int l,int r,int st){
if(L<=l&&r<=R)return sum[st];
int ret=0;
if(L<=mid)ret+=query(L,R,lson);
if(R>mid)ret+=query(L,R,rson);
return ret;
}
int main(){
int n,q,x;
char str[4];
while(~scanf("%d%d",&n,&q)){
memset(sum,0,sizeof(sum));
build(1,n,1);
top=0;
while(q--){
scanf("%s",str);
if(str[0]=='D'){
scanf("%d",&x);
updata(x,0,1,n,1);
sta[++top]=x;
}
else if(str[0]=='R'&&top){
x=sta[top--];
updata(x,1,1,n,1);
}
else if(str[0]=='Q'){
scanf("%d",&x);
int ans=query(x,x,1,n,1);
if(ans==0){
printf("0\n");
continue;
}
int l=1,r=x-1,t,k=0;
while(l<=r){
t=query(mid,x-1,1,n,1);
if(t==x-mid){
k=x-mid;
r=mid-1;
}else l=mid+1;
}
ans+=k;
l=x+1,r=n,k=0;
while(l<=r){
t=query(x+1,mid,1,n,1);
if(t==mid-x){
k=mid-x;
l=mid+1;
}else r=mid-1;
}
ans+=k;
printf("%d\n",ans);
}
}
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator