传送门:
http://www.lydsy.com/JudgeOnline/problem.php?id=1862
http://www.lydsy.com/JudgeOnline/problem.php?id=1056
四大评测系统都说我A了,交到BZOJ上就是RE,f**k,最后交了标程
求查错(常数不是关键):
#include<map> #include<string> #include<cstdio> #include<climits> #include<iostream> #include<algorithm> #define X first #define Y second using namespace std; const int INF=INT_MAX; map<string,int>ntoi; typedef pair<int,int> pii; pii sor[350001]; int tot=0; map<int,string>iton; int KEY=12345679; int rnd(){ KEY=(KEY<<2|1)%10000007; return KEY; } struct Treap{ struct node{ int ind,key,size; node *c[2]; node(int _ind,node *C){ ind=_ind;key=rnd(); size=1;c[0]=c[1]=C; } void rz(){ size=c[0]->size+c[1]->size+1; } }; node *Null,*root; Treap(){ Null=new node(0,0); Null->size=0;Null->key=INF; Null->c[0]=Null->c[1]=Null; root=Null; } void rot(node *&t,bool d){ node *p=t->c[d]; t->c[d]=p->c[!d]; p->c[!d]=t; t->rz();p->rz(); t=p; } void _insert(node *&t,int x){ if(t==Null){ t=new node(x,Null); return; } bool d=sor[x]>sor[t->ind]; _insert(t->c[d],x); if(t->key>t->c[d]->key) rot(t,d); else t->rz(); } void _Delete(node *&t,int x){ if(t==Null)return; if(t->ind==x){ bool d=t->c[1]->key<t->c[0]->key; if(t->c[d]==Null){ delete t; t=Null; return; } rot(t,d); _Delete(t->c[!d],x); }else{ bool d=sor[x]>sor[t->ind]; _Delete(t->c[d],x); } t->rz(); } int _kth(node *&t,int x){ int r=t->c[0]->size; if(r==x)return t->ind; if(x<r)return _kth(t->c[0],x); else return _kth(t->c[1],x-r-1); } int _rank(node *&t,int x){ if(t==Null)return 0; int r=t->c[0]->size; if(x==t->ind)return r; if(sor[x]<sor[t->ind])return _rank(t->c[0],x); return r+1+_rank(t->c[1],x); } void insert(int x){ _insert(root,x); } void del(int x){ _Delete(root,x); } int kth(int x){ return _kth(root,x); } int rank(int x){ return _rank(root,x); } int size(){ return root->size; } void _deb(node *&t){ printf("%d L:%d R:%d\n",t->ind,t->c[0]->ind,t->c[1]->ind); if(t->c[0]!=Null) _deb(t->c[0]); if(t->c[1]!=Null) _deb(t->c[1]); } void deb(){ _deb(root); } }T; int main(){ // freopen("rank.in","r",stdin); // freopen("rank.out","w",stdout); int n; scanf("%d",&n); string opt; int m=n; while(n--){ cin>>opt; if(opt[0]=='+'){ int x,index; scanf("%d",&x); opt.erase(0,1); if(!ntoi.count(opt)){ ntoi[opt]=++tot; iton[tot]=opt; index=tot; sor[index]=pii(x,n); T.insert(tot); } else{ index=ntoi[opt]; T.del(index); sor[index]=pii(x,n); T.insert(index); } continue; }else if(opt[0]=='?'&&opt[1]>='0'&&opt[1]<='9'){ int res=0; for(int i=1;i<opt.length();i++){res*=10;res+=opt[i]-'0';} res=T.size()-res+1; for(int i=res-1;i>max(0,res-10);i--) printf("%s ",iton[T.kth(i)].c_str()); printf("%s\n",iton[T.kth(max(0,res-10))].c_str()); //printf("\n"); //T.deb(); continue; }else{ opt.erase(0,1); printf("%d\n",T.size()-T.rank(ntoi[opt])); continue; } } return 0; }