标题
增加 1 行删除 1 行
1超绝无旋 Treap,16.21s 通过 P6131,考场上写出直接宗教胜利1超绝无旋 Treap,13.64s 通过 P6131,考场上写出直接宗教胜利
正文
增加 331 行删除 0 行
274 行未改动全部展开275276277278274 行未改动全部展开}cout<<sum;return 0;}```274 行未改动全部展开275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609274 行未改动全部展开}cout<<sum;return 0;}```这份代码还有优化的空间。我们又注意到,在插入树的过程中,无论最终是否成功分离子树,我们都会查询一次这棵树的最大或最小值,每次查询的期望复杂度都是 $O(logn)$ 的,总复杂度就是 $O((logn)^2)$ 的。于是我们提前在插入与删除操作中维护当前子树的最大值与最小值。这样如果插入的树的所有节点都大于或小于当前节点,就不进行查询,直接把整棵树下传。分析一下,不妨设插入树的根节点的权值小于当前节点。每一次分裂都会失去一个最右端的子树,而树的深度又是期望 $logn$ 的......诶,怎么好像时间复杂度一样呢......不过总归是优化了一点的,成功跑到了 13.64s。```cpp#include<bits/stdc++.h>using namespace std;#define int long long#define inf (2e9+7)struct tree{int x=0,sum=1,hp=0,maxi=0,mini=0;tree *left=NULL,*right=NULL;}*root=NULL;tree* newtree(int x){tree *t=new tree;t->maxi=t->mini=t->x=x;t->hp=rand()*1ll+(rand()*1ll<<15ll)+(rand()*1ll<<31ll)+(rand()*1ll<<47ll);return t;}void insert(tree *&t,tree *p){if(!p)return;if(!t){t=p;return;}if(t->hp<p->hp){swap(t,p);}if(p->x<t->x || (p->x==t->x && rand()%2)){if(p->maxi>t->x){tree *q=p;while(q->right && q->right->x<t->x)q=q->right;tree *c=q->right,*d=p;q->right=NULL;if(c)while(d){d->sum-=c->sum;d->maxi=q->x;d=d->right;}insert(t,c);}t->sum+=p->sum;t->maxi=max(t->maxi,p->maxi);t->mini=min(t->mini,p->mini);insert(t->left,p);}else{if(p->mini<t->x){tree *q=p;while(q->left && q->left->x>t->x)q=q->left;tree *c=q->left,*d=p;q->left=NULL;if(c)while(d){d->sum-=c->sum;d->mini=q->x;d=d->left;}insert(t,c);}t->sum+=p->sum;t->maxi=max(t->maxi,p->maxi);t->mini=min(t->mini,p->mini);insert(t->right,p);}}void erase(tree *&t,int x){if(!t)return;if(x==t->x){tree *l=t->left,*r=t->right,*d=t;t=l;insert(t,r);delete d;return;}if(x<t->x){erase(t->left,x);t->mini=(t->left?t->left->mini:t->x);}else{erase(t->right,x);t->maxi=(t->right?t->right->maxi:t->x);}t->sum--;}int query1(tree *t,int x){if(!t)return 0;if(x<=t->x){return query1(t->left,x);}else{return query1(t->right,x)+t->sum-(t->right?t->right->sum:0);}}int query2(tree *t,int k){if(!t)return 0;if(k==(t->left?t->left->sum:0)+1)return t->x;if(k<(t->left?t->left->sum:0)+1){return query2(t->left,k);}else{return query2(t->right,k-(t->left?t->left->sum:0)-1);}}int query3(tree *t,int x){if(!t)return -inf;if(x<=t->x){return query3(t->left,x);}else{return max(t->x,query3(t->right,x));}}int query4(tree *t,int x){if(!t)return inf;if(x<t->x){return min(t->x,query4(t->left,x));}else{return query4(t->right,x);}}signed main(){ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);srand(time(0));int n,m,last=0,sum=0;cin>>n>>m;for(int i=1;i<=n;i++){int x;cin>>x;insert(root,newtree(x));}while(m--){int op,x;cin>>op>>x;x^=last;if(op==1){insert(root,newtree(x));}else if(op==2){erase(root,x);}else if(op==3){last=query1(root,x)+1;sum^=last;}else if(op==4){last=query2(root,x);sum^=last;}else if(op==5){last=query3(root,x);sum^=last;}else if(op==6){last=query4(root,x);sum^=last;}}cout<<sum;return 0;}```为了方便比较,下面这段 AVL 树的运行时间为 11.00s。```cpp#include<bits/stdc++.h>using namespace std;struct tree{int x,si=1,h=0;tree *left=NULL,*right=NULL;}*top=NULL;void geth(tree *t){t->h=max(t->left?t->left->h:0,t->right?t->right->h:0)+1;t->si=(t->left?t->left->si:0)+(t->right?t->right->si:0)+1;}void LL(tree *&t){tree *tmp=t->left;t->left=tmp->right;tmp->right=t;geth(t);geth(tmp);t=tmp;}void RR(tree *&t){tree *tmp=t->right;t->right=tmp->left;tmp->left=t;geth(t);geth(tmp);t=tmp;}void LR(tree *&t){RR(t->left);LL(t);}void RL(tree *&t){LL(t->right);RR(t);}void check(tree *&t){int l=t->left?t->left->h:0,r=t->right?t->right->h:0;if(abs(l-r)<2){return;}if(l>r){tree *tmp=t->left;int ll=tmp->left?tmp->left->h:0,lr=tmp->right?tmp->right->h:0;if(ll>=lr){LL(t);return;}else{LR(t);return;}}else{tree *tmp=t->right;int ll=tmp->left?tmp->left->h:0,lr=tmp->right?tmp->right->h:0;if(ll<=lr){RR(t);return;}else{RL(t);return;}}}void insert(tree *&t,int x){if(!t){t=new tree;t->x=x;return;}if(x<=t->x){insert(t->left,x);}else{insert(t->right,x);}geth(t);check(t);}void erase(tree *&t,int x){if(t->x==x){if(!t->left){t=t->right;}else if(!t->right){t=t->left;}else{tree *p=t->left;while(p->right){p=p->right;}int xi=p->x;erase(t->left,xi);t->x=xi;}if(t){geth(t);check(t);}return;}if(x<t->x){erase(t->left,x);}else{erase(t->right,x);}geth(t);check(t);}int query_count(tree *t,int x){if(!t)return 0;if(x<=t->x){return query_count(t->left,x);}else{return (t->left?t->left->si+1:1)+query_count(t->right,x);}}int query_sort(tree *t,int k){if(!t)return 0;if(k<=(t->left?t->left->si:0)){return query_sort(t->left,k);}else if(k==(t->left?t->left->si:0)+1){return t->x;}else{return query_sort(t->right,k-(t->left?t->left->si:0)-1);}}int pre(tree *t,int x){int maxi=INT_MAX+1;while(t){if(t->x>=x){t=t->left;}else{maxi=t->x;t=t->right;}}return maxi;}int next(tree *t,int x){int mini=INT_MAX;while(t){if(t->x<=x){t=t->right;}else{mini=t->x;t=t->left;}}return mini;}void dfs(tree *t){if(!t)return;cout<<t->x<<"( ";dfs(t->left);cout<<" | ";dfs(t->right);cout<<" )";}int n,t,sum,last;int main(){ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);cin>>n>>t;while(n--){int x;cin>>x;insert(top,x);}while(t--){int op;cin>>op;if(op==1){int x;cin>>x;insert(top,x^last);}if(op==2){int x;cin>>x;erase(top,x^last);}if(op==3){int x;cin>>x;last=query_count(top,x^last)+1;sum^=last;}if(op==4){int x;cin>>x;last=query_sort(top,x^last);sum^=last;}if(op==5){int x;cin>>x;last=pre(top,x^last);sum^=last;}if(op==6){int x;cin>>x;last=next(top,x^last);sum^=last;}//dfs(top);cout<<"\n";}cout<<sum;return 0;}```~~所以直接写 AVL 吧~~[提交记录1](https://www.luogu.com.cn/record/307328521)\[提交记录2](https://www.luogu.com.cn/record/307679965)\[提交记录3](https://www.luogu.com.cn/record/307703874)\[提交记录4](https://www.luogu.com.cn/record/256848948)