记录编号 432765 评测结果 AAAAAAAAAA
题目名称 [HZOI 2016][Tyvj 1729] 文艺平衡树 最终得分 100
用户昵称 GravatarTroywar 是否通过 通过
代码语言 C++ 运行时间 0.687 s
提交时间 2017-08-03 19:31:27 内存使用 3.19 MiB
显示代码纯文本
  1. #pragma G++ optimize("O3")
  2. #include<cstdio>
  3. #include<iostream>
  4. namespace FIFO
  5. {
  6. char ch;
  7. char B[1<<20],*S=B,*T=B;
  8. #define getc() (S==T&&(T=(S=B)+fread(B,1,1<<20,stdin),S==T)?0:*S++)
  9. #define isd(c) (c>='0'&&c<='9')
  10. int aa,bb;int F(){
  11. while(ch=getc(),!isd(ch)&&ch!='-');ch=='-'?aa=bb=0:(aa=ch-'0',bb=1);
  12. while(ch=getc(),isd(ch))aa=aa*10+ch-'0';return aa;
  13. }
  14. }
  15. #define gi FIFO::F()
  16. class Troy{
  17. public:
  18. Troy(){
  19. for(top;top<siez;top++)
  20. stk[top]=tree+top;
  21. }
  22. void print(){
  23. dfs(root);
  24. }
  25. void build(int l,int r){
  26. int mid=l+r>>1;
  27. root=newnode(mid,NULL);
  28. root->son[0]=build(l,mid-1,root);
  29. root->son[1]=build(mid+1,r,root);
  30. }
  31. void change(int l,int r){
  32. node *t=find(l);
  33. splay(t,NULL);
  34. t=find(r+2);
  35. splay(t,root);
  36. root->son[1]->son[0]->lazy^=1;
  37. }
  38. private:
  39. struct node{
  40. int v,size,lazy;
  41. node *son[2],*f;
  42. }*root;
  43. const static int siez=100100;
  44. node *stk[siez],tree[siez];int top;
  45. inline node * newnode(int v,node *f){
  46. node *t=stk[--top];
  47. t->size=1;
  48. t->lazy=0;
  49. t->v=v;
  50. t->son[1]=t->son[0]=NULL;
  51. t->f=f;
  52. return t;
  53. }
  54. node *build (int l,int r,node *f){
  55. if(l>r) return NULL;
  56. int mid=l+r>>1;
  57. node *t=newnode(mid,f);
  58. t->son[0]=build(l,mid-1,t);
  59. t->son[1]=build(mid+1,r,t);
  60. return update(t),t;
  61. }
  62. inline int size(node *t){
  63. return t==NULL ?0:t->size;
  64. }
  65. inline void update(node *t){
  66. t->size=1+size(t->son[0])+size(t->son[1]);
  67. }
  68. void updown(node *t){
  69. if(t!=NULL){
  70. std::swap(t->son[0],t->son[1]);
  71. if(t->son[0]!=NULL)
  72. t->son[0]->lazy^=1;
  73. if(t->son[1]!=NULL)
  74. t->son[1]->lazy^=1;
  75. t->lazy=0;
  76. }
  77. }
  78. inline bool son(node* f,node* s){
  79. if(f==NULL) return 0;
  80. return f->son[1]==s;
  81. }
  82. inline void connect(node *f,node *s,bool k){
  83. if(f!=NULL) f->son[k]=s;
  84. else root=s;
  85. if(s!=NULL) s->f=f;
  86. }
  87. inline void rotate(node* t){
  88. node *f=t->f,*g=f->f;
  89. bool a=son(f,t),b=!a;
  90. connect(f,t->son[b],a);
  91. connect(g,t,son(g,f));
  92. connect(t,f,b);
  93. update(f);
  94. update(t);
  95. }
  96. inline void splay(node *t,node *p){
  97. if(t){
  98. while(t->f!=p){
  99. node *f =t->f,*g=f->f;
  100. if(g==p) rotate(t);
  101. else{
  102. if(son(g,f)^son(f,t))
  103. rotate(t),rotate(t);
  104. else
  105. rotate(f),rotate(t);
  106. }
  107. }
  108. }
  109. }
  110. inline node *find(int kth){
  111. for(node *now=root;;){
  112. if(now->lazy){
  113. updown(now);
  114. }
  115. if(kth>size(now->son[0])){
  116. kth-=size(now->son[0]);
  117. if(kth==1) return now;
  118. else now=now->son[1],kth--;
  119. }
  120. else
  121. now=now->son[0];
  122. }
  123. }
  124. inline void dfs(node *t){
  125. if(t->lazy){
  126. updown(t);
  127. }
  128. if(t->son[0]!=NULL)
  129. dfs(t->son[0]);
  130. if(t->v!=0&&t->v!=size(root)-1)
  131. printf("%d ",t->v);
  132. if(t->son[1]!=NULL)
  133. dfs(t->son[1]);
  134. }
  135. }war;
  136. int n,m;
  137. int main(){
  138. freopen("sph.in","r",stdin);
  139. freopen("sph.out","w",stdout);
  140. n=gi,m=gi;
  141. int l,r;
  142. war.build(0,n+1);
  143. for(int i=1;i<=m;i++ ){
  144. l=gi,r=gi;
  145. war.change(l,r);
  146. }
  147. war.print();
  148. }