Advertisement
Guest User

Untitled

a guest
May 24th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #define endl "\n"
  3. using namespace std;
  4. struct Treap
  5. {
  6. struct Node
  7. {
  8. int key;
  9. int pr;
  10. Node *l,*r;
  11. int lft;
  12. int rgt;
  13. int sz;
  14. int ans;
  15. Node(int x)
  16. {
  17. key=lft=rgt=x;
  18. pr=rand();
  19. ans=1;
  20. l=r=NULL;
  21. }
  22. void con()
  23. {
  24. sz=1;
  25. ans=1;
  26. lft=rgt=key;
  27. if(l)
  28. {
  29. sz+=l->sz;
  30. lft=l->lft;
  31. ans+=l->ans;
  32. if(key==l->rgt)ans--;
  33. }
  34. if(r)
  35. {
  36. sz+=r->sz;
  37. rgt=r->rgt;
  38. ans+=r->ans;
  39. if(key==r->lft)ans--;
  40. }
  41.  
  42. }
  43. void output()
  44. {
  45. if(l)l->output();
  46. cout<<key<<endl;
  47. if(r)r->output();
  48. }
  49. };
  50. Node *node;
  51. Treap()
  52. {
  53. node=NULL;
  54. }
  55. void Merge(Node *&T,Node *L,Node *R)
  56. {
  57. if(!L)
  58. {
  59. T=R;
  60. return ;
  61. }
  62. if(!R)
  63. {
  64. T=L;
  65. return ;
  66. }
  67. if(L->pr > R->pr)
  68. {
  69. T=L;
  70. Merge(T->r,L->r,R);
  71. }
  72. else
  73. {
  74. T=R;
  75. Merge(T->l,L,R->l);
  76. }
  77. T->con();
  78. }
  79. void Split_sz(Node *T,Node *&L,Node *&R,int x)
  80. {
  81. if(!T)
  82. {
  83. L=R=NULL;
  84. return ;
  85. }
  86. int SZ=1;
  87. if(T->l)SZ+=T->l->sz;
  88. if(SZ<=x)
  89. {
  90. L=T;
  91. Split_sz(T->r,L->r,R,x-SZ);
  92. }
  93. else
  94. {
  95. R=T;
  96. Split_sz(T->l,L,R->l,x);
  97. }
  98. T->con();
  99. }
  100. void Delete(int x)
  101. {
  102. Node *L,*R,*MID;
  103. Split_sz(node,L,R,x);
  104. Split_sz(L,L,MID,x-1);
  105. Merge(node,L,R);
  106. }
  107. void Insert(int x,int col)
  108. {
  109. Node *L,*R,*MID;
  110. Split_sz(node,L,R,x-1);
  111.  
  112. MID = new Node(col);
  113.  
  114. Merge(L,L,MID);
  115. Merge(node,L,R);
  116. }
  117. void Change_colour(int x,int col)
  118. {
  119. Delete(x);
  120. Insert(x,col);
  121. }
  122. int query(int t,int a,int b)
  123. {
  124. if(t==1)
  125. {
  126. Delete(a);
  127. //cout<< (node->ans) <<endl;
  128. }
  129. else if(t==2)
  130. {
  131. Insert(a,b);
  132. //cout<< (node->ans) <<endl;
  133. //cout<<"line128\n";
  134. }
  135. else if(t==3)
  136. {
  137. Change_colour(a,b);
  138. //cout<< (node->ans) <<endl;
  139. }
  140. else if(t==4)
  141. {
  142. cout<< (node->ans) <<endl;
  143. }
  144. //node->output();
  145. }
  146. };
  147. Treap trp;
  148. int main()
  149. {
  150. int n,i,j,t,a,b,c;
  151. cin>>n;
  152. for(i=0;i<n;i++)
  153. {
  154. cin>>a;
  155. //cout<<i<<endl;
  156. trp.query(2,i+1,a);
  157. }
  158. cin>>t;
  159. while(t--)
  160. {
  161. cin>>a;
  162. if(a==4)
  163. {
  164. trp.query(4,0,0);
  165. }
  166. if(a==1)
  167. {
  168. cin>>b;
  169. trp.query(1,b,0);
  170. }
  171. if(a==3)
  172. {
  173. cin>>b>>c;
  174. trp.query(3,b,c);
  175. }
  176. if(a==2)
  177. {
  178. cin>>b>>c;
  179. trp.query(2,b,c);
  180. }
  181. }
  182. return 0;
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement