UVA 11922 splay
来源:互联网 发布:yii2 cms 编辑:程序博客网 时间:2024/06/05 15:47
点击打开链接
题意:给n个数,1到n,然后每个操作a,b是将a到b的所有数翻转后添加到末尾
思路:白书的例题,代码参考这位大神的
#include <stdio.h>#include <string.h>#include <stdlib.h>#include <iostream>#include <algorithm>using namespace std;typedef long long ll;typedef unsigned long long ull;const int inf=0x3f3f3f3f;const ll INF=0x3f3f3f3f3f3f3f3fll;const int maxn=1e5+10;struct Node{ Node *ch[2]; int v,s,flip; Node(int v):v(v){ch[0]=ch[1]=NULL;s=1;flip=0;} int cmp(int x) const{ int d=x-(ch[0]==NULL?0:ch[0]->s); if(d==1) return -1; return d<=0?0:1; } void maintain(){ s=1;if(ch[0]!=NULL) s+=ch[0]->s; if(ch[1]!=NULL) s+=ch[1]->s; } void pushdown(){ if(flip){ flip=0; swap(ch[0],ch[1]); if(ch[0]!=NULL) ch[0]->flip=!ch[0]->flip; if(ch[1]!=NULL) ch[1]->flip=!ch[1]->flip; } }};void Rotate(Node* &o,int d){ Node* k=o->ch[d^1];o->ch[d^1]=k->ch[d];k->ch[d]=o; o->maintain();k->maintain();o=k;}void build(Node* &o,int l,int r){ if(l>r) return ; int mid=(l+r)>>1; o=new Node(mid); build(o->ch[0],l,mid-1); build(o->ch[1],mid+1,r); o->maintain();}void pri(Node* &o){ o->pushdown(); if(o->ch[0]!=NULL) pri(o->ch[0]); if(o->v) printf("%d\n",o->v); if(o->ch[1]!=NULL) pri(o->ch[1]);}void removetree(Node* &x){ if(x->ch[0]!=NULL) removetree(x->ch[0]); if(x->ch[1]!=NULL) removetree(x->ch[1]); delete x;x=NULL;}void splay(Node* &o,int k){ o->pushdown(); int d=o->cmp(k); if(d==1) k-=(o->ch[0]==NULL?0:o->ch[0]->s)+1; if(d!=-1){ Node* p=o->ch[d]; p->pushdown(); int d2=p->cmp(k); int k2=(d2==0?k:k-(p->ch[0]==NULL?0:p->ch[0]->s)-1); if(d2!=-1){ splay(p->ch[d2],k2); if(d==d2) Rotate(o,d^1); else Rotate(o->ch[d],d); } Rotate(o,d^1); }}Node* Merge(Node* L,Node* R){ splay(L,L->s); L->ch[1]=R; L->maintain(); return L;}void split(Node* o,int k,Node* &L,Node* &R){ splay(o,k); L=o;R=o->ch[1]; o->ch[1]=NULL; L->maintain();}void debug(Node* r){ if (r==NULL)return; printf("key:%d lson:%d rson:%d\n",r->v,r->ch[0]->v,r->ch[1]->v); debug(r->ch[0]); debug(r->ch[1]);}int main(){ int n,m,x,y; while(scanf("%d%d",&n,&m)!=-1){ Node *root=NULL; Node *l,*r,*mid,*o; build(root,0,n); while(m--){ scanf("%d%d",&x,&y); split(root,x,l,o); split(o,y-x+1,mid,r); mid->flip^=1; root=Merge(Merge(l,r),mid); } pri(root); removetree(root); } return 0;}
0 0
- UVA 11922 splay
- UVA 11922 Permutation Transformer Splay
- UVA 11922 Permutation Transformer(splay)
- UVa 11922 Permutation Transformer(splay)
- 【splay tree】 UVA 11922 Permutation Transformer
- UVa 11922 - Permutation Transformer (Splay)
- UVA 11922 Splay区间翻转+分裂+合并
- uva 11922 Permutation Transforme/splay tree
- UVA 11922 Permutation Transformer(Splay Tree)
- UVA 11922 Permutation Transformer(splay树)
- UVA - 11996(splay入门)
- UVA 11996 splay
- UVA 11922 Permutation Transformer(伸展树 Splay Tree)
- uva 11922 Permutation Transformer(Splay tree,懒标记传递)
- UVA 11922 Permutation Transformer Splay 区间翻转 + 区间合并
- UVA 11922 Permutation Transformer (Splay 区间翻转 + 复制粘贴)
- Permutation Transformer+uva+splay树
- uva 11922 Permutation Transformer 排列变换 Splay 维护序列 翻转操作
- 关于fragment传值的问题
- SAP BDC批导返利协议(参考模板协议 复制——不同客户)
- Asymptotic Analysis——渐近分析
- error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '2' doesn't match value '0'
- BeanUtils.copyProperties() 用法
- UVA 11922 splay
- phpStorm 2016.1 最新版激活方法
- 2016年8月问题记录与总结
- React Native-创建项目和运行项目
- lua学习(5)______table表和函数
- java数组和字符串相互转换
- docker学习总结之常用命令
- SSH框架与配置文件的简单搭建
- Mybatis动态SQL——if、choose、where、set、trim、foreach标记实例