Advertisement
promitheus_sal

Untitled

Jan 12th, 2024
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <vector>
  3. #include <assert.h>
  4. using namespace std;
  5. #define MAXHW 1000000
  6.  
  7. #define PM_(b) (b?+1:-1)
  8. int H,W;
  9.  
  10. //subtask 5, H=1
  11.  
  12. int NUM[MAXHW];
  13. char D[MAXHW];
  14.  
  15. struct Node {
  16. public:
  17. int lowest=-1;
  18. int lowest_count=-1;
  19. int total_effect=0;
  20. Node* left;
  21. Node* right;
  22. Node* L() {return left?left:left=new Node();}
  23. Node* R() {return right?right:right=new Node();}
  24.  
  25. int count_zeroes() {
  26. return lowest?0:lowest_count;
  27. }
  28.  
  29. void build(int l, int r) {
  30. if(l==r) {
  31. lowest = total_effect = D[l];
  32. lowest_count = 1;
  33. return;
  34. }
  35. int m = (l+r)>>1;
  36. L()->build(l,m);
  37. R()->build(m+1,r);
  38. int ll=left->lowest, lr=right->lowest;
  39. if(ll<lr)
  40. lowest = ll, lowest_count = left->lowest_count;
  41. else if(ll>lr)
  42. lowest = lr, lowest_count = right->lowest_count;
  43. else
  44. lowest = ll, lowest_count = left->lowest_count + right->lowest_count;
  45. total_effect = left->total_effect+right->total_effect;
  46. }
  47.  
  48. void update_self() {
  49. int ll=left->lowest, lr=right->lowest;
  50. if(ll<lr)
  51. lowest = ll, lowest_count = left->lowest_count;
  52. else if(ll>lr)
  53. lowest = lr, lowest_count = right->lowest_count;
  54. else
  55. lowest = ll, lowest_count = left->lowest_count + right->lowest_count;
  56. total_effect = left->total_effect+right->total_effect;
  57. }
  58.  
  59. void update(int l, int r, int t, int nv) {
  60. if(l==r) {
  61. assert(l==t);
  62. lowest = nv;
  63. total_effect = nv;
  64. return;
  65. }
  66. int m = (l+r)>>1;
  67. if(t>m) {
  68. right->update(m+1,r,t,nv);
  69. }
  70. else {
  71. int oldval = left->total_effect;
  72. left->update(l,m,t,nv);
  73. right->lowest += left->total_effect-old_val;
  74. }
  75. update_self();
  76. }
  77.  
  78. static Node* make_tree(int l, int r) {
  79. Node* n = new Node();
  80. n->build(l,r);
  81. return n;
  82. }
  83. };
  84.  
  85. Node* root = nullptr;
  86.  
  87. vector<int> R;
  88. vector<int> C;
  89.  
  90. void give_initial_chart(int h, int w, vector<int> r, vector<int> c) { //R,C is the row and col of seat #i
  91. assert(h==1); //R[i]==1,foreach i
  92. R=r;
  93. C=c;
  94. H=h,W=w;
  95. for(int i=0;i<w;i++)
  96. NUM[C[i]] = i;
  97. if(w==1) {
  98. D[0]=0; return;
  99. }
  100. D[0] = NUM[1]>NUM[0]?1:0;
  101. for(int i=1;i<w-1;i++)
  102. D[i] = PM_(NUM[i-1]>NUM[i])+PM_(NUM[i+1]>NUM[i]);
  103. D[w-1]=NUM[w-2]>NUM[w-1]?1:0;
  104. root = Node::make_tree(0,w-1);
  105. }
  106.  
  107. void swap_seats(int a, int b) {
  108. int x=C[a],y=C[b];
  109. std::swap(NUM[x],NUM[y]);
  110. vector<pair<int,int>> u; //{nv, pos}
  111.  
  112. }
  113.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement