记录编号 366921 评测结果 AAAAAAAAAA
题目名称 [HZOI 2016][Tyvj 1729] 文艺平衡树 最终得分 100
用户昵称 Gravatar‎MistyEye 是否通过 通过
代码语言 C++ 运行时间 1.502 s
提交时间 2017-01-26 17:30:26 内存使用 0.32 MiB
显示代码纯文本
  1. #include <cstdio>
  2. #include <cstring>
  3. #include <iostream>
  4. #include <algorithm>
  5. using namespace std;
  6. const int maxn = 100005;
  7. struct Node {
  8. int w, sz, sw;
  9. Node *f, *ch[2];
  10. Node(int w=0, Node *f=0) :
  11. w(w), f(f) { ch[0]=ch[1]=0;sw=0;sz=1; }
  12. bool rc() { if(f) return f->ch[1]==this; }
  13. void flip() { swap(ch[0], ch[1]); sw ^= 1; }
  14. void down() {
  15. if(sw) {
  16. if(ch[0]) ch[0]->flip();
  17. if(ch[1]) ch[1]->flip();
  18. sw = 0;
  19. }
  20. }
  21. void up() {
  22. sz = 1;
  23. if(ch[0]) sz += ch[0]->sz;
  24. if(ch[1]) sz += ch[1]->sz;
  25. }
  26. } *s;
  27. void lk(Node *x, Node *y, int c) {
  28. if(x) x->f = y;
  29. if(y) y->ch[c] = x;
  30. }
  31. void rot(Node *x) {
  32. Node *y = x->f; int c = x->rc();
  33. x->down(); y->down();
  34. lk(x, y->f, y->rc());
  35. lk(x->ch[c^1], y, c);
  36. lk(y, x, c^1);
  37. y->up(); x->up();
  38. if(!x->f) s = x;
  39. }
  40. void Splay(Node *x, Node *to=0) {
  41. while(x->f!=to) {
  42. Node *y = x->f;
  43. if(y->f!=to) x->rc()==y->rc() ? rot(y) : rot(x);
  44. rot(x);
  45. }
  46. }
  47. Node *build(int l, int r, Node *f=0) {
  48. if(l>r) return 0;
  49. int mid = (l+r)>>1;
  50. Node *x = new Node(mid, f);
  51. x->ch[0] = build(l, mid-1, x);
  52. x->ch[1] = build(mid+1, r, x);
  53. x->up(); return x;
  54. }
  55. Node *kth(int k, Node *to=0, Node *x=s) {
  56. int size = 1; x->down(); // warn
  57. if(x->ch[0]) size += x->ch[0]->sz;
  58. if(k==size) return Splay(x, to), x;
  59. if(k<size) return kth(k, to, x->ch[0]);
  60. return kth(k-size, to, x->ch[1]);
  61. }
  62. void print(Node *x=s) {
  63. if(!x) return;
  64. x->down();
  65. print(x->ch[0]);
  66. printf("%d ", x->w);
  67. print(x->ch[1]);
  68. }
  69. int main() {
  70. freopen("sph.in", "r", stdin);
  71. freopen("sph.out", "w", stdout);
  72. int N, M;
  73. scanf("%d%d", &N, &M);
  74. s = build(0, N+1);
  75. for(int i=1,l,r; i<=M; ++i) {
  76. scanf("%d%d", &l, &r);
  77. kth(l); kth(r+2, s);
  78. s->ch[1]->ch[0]->flip();
  79. }
  80. kth(1); kth(N+2, s);
  81. print(s->ch[1]->ch[0]); puts("");
  82. getchar(), getchar();
  83. return 0;
  84. }
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.