WA+TLE 0 分求调
查看原帖
WA+TLE 0 分求调
1308728
xsmfollower楼主2024/5/18 12:28

心态已经炸裂了。

#include<cstdio>
using namespace std;
const int N=3e5+10,INF=2e9; int rt,tot,fa[N],ch[N][2],cnt[N],val[N],sz[N];
bool get(int x) { return ch[fa[x]][1]==x; }
void pushup(int x) {
  if(!x) return;
  sz[x]=cnt[x];
  if(ch[x][0]) sz[x]+=sz[ch[x][0]];
  if(ch[x][1]) sz[x]+=sz[ch[x][1]]; 
}
void rotate(int x) {
  int y=fa[x],z=fa[y],k=get(x);
  ch[y][k]=ch[x][k^1],fa[ch[x][k^1]]=y;
  ch[x][k^1]=y,fa[y]=x;
  fa[x]=z;
  if(z) ch[z][ch[z][1]==y]=x;
  pushup(y),pushup(x);
}
void splay(int x,int goal=0) {
  while(fa[x]!=goal) {
    int y=fa[x],z=fa[y];
    if(z!=goal) rotate(get(x)==get(y)?y:x);
    rotate(x);
  }
  if(!goal) rt=x;
}
void find(int x) {
  int u=rt;
  if(!u) return;
  while(ch[u][x>val[u]]&&x!=val[u]) u=ch[u][x>val[u]];
  splay(u);
}
void insert(int x) {
  int u=rt,f=0;
  while(u&&val[u]!=x) f=u,u=ch[u][x>val[u]];
  if(u) cnt[u]++,pushup(u),pushup(f);
  else {
    u=++tot,val[u]=x,sz[u]=cnt[u]=1,fa[u]=f;
    if(f) ch[f][x>val[f]]=u,pushup(f);
    else rt=u;
  }
  splay(u);
}
int query_rank(int x) {
  int u=rt,ans=0;
  while(u) {
    if(x<val[u]) u=ch[u][0];
    else {
      ans+=sz[ch[u][0]];
      if(x==val[u]) return splay(u),ans+1;
      ans+=cnt[u],u=ch[u][1];
    }
  }
  return ans+1;
}
int query_kth(int x) {
  int u=rt;
  while(true) {
    if(ch[u][0]&&x<=sz[ch[u][0]]) u=ch[u][0];
    else {
      int tmp=sz[ch[u][0]]+cnt[u];
      if(x<=tmp) return splay(u),val[u];
      x-=tmp,u=ch[u][1];
    }
  }
}
int pre(int x) {
  find(x);
  if(val[rt]<x) return rt;
  int u=ch[rt][0];
  while(ch[u][1]) u=ch[u][1];
  return splay(u),u;
}
int succ(int x) {
  find(x);
  if(val[rt]>x) return rt;
  int u=ch[rt][1];
  while(ch[u][0]) u=ch[u][0];
  return splay(u),u;
}
void del(int x) {
  find(x);
  if(cnt[rt]>1) return cnt[rt]--,pushup(rt),void();
  if(!ch[rt][0]&&!ch[rt][1]) return rt=0,void();
  if(!ch[rt][0]||!ch[rt][1]) return rt=ch[rt][0]?ch[rt][0]:ch[rt][1],fa[rt]=0,void();
  int ort=rt,lb=pre(x);
  splay(lb);
  ch[rt][1]=ch[ort][1],fa[ch[ort][1]]=rt;
  pushup(rt);
} 
int main() {
  freopen("oier.in","r",stdin);
  insert(INF),insert(-INF),rt=1,ch[rt][1]=2,pushup(rt);
  int n,m,c=0,sum=0,cnt=0; scanf("%d %d",&n,&m);
  while(n--) {
    char opt[2]; int k; scanf("%s %d",opt,&k);
    if(opt[0]=='I') if(k-c>=m) insert(k-c),sum++,cnt++;
    if(opt[0]=='A') c+=k,m-=k;
    if(opt[0]=='S') {
      c-=k,m+=k;
      int u=m-1;
 	  while(val[pre(u)]!=-INF) {
 	    printf("%d\n",u);
 	  	int t=u;
		u=val[pre(u)],del(val[pre(t)]),cnt--;
	  }
    }
    if(opt[0]=='F') printf("%d\n",cnt<k?-1:query_kth(cnt-k+2)+c);
    printf("%d %d %d %d\n",sum,cnt,c,m);
  }
  printf("%d",sum-cnt);
  return 0;
}
2024/5/18 12:28
加载中...