Advertisement
haufont

Untitled

Oct 7th, 2016
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.58 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <time.h>
  4. #include <stdlib.h>
  5. #include <string>
  6. #include <algorithm>
  7.  
  8. using namespace std;
  9.  
  10. const long long INF = 1e10;
  11.  
  12. struct Treap {
  13. long long x, y, cnt, minn, add_sum, summ;
  14. Treap* leftp;
  15. Treap* rightp;
  16. };
  17.  
  18. long long cnt(Treap* Treap) {
  19. if (Treap == NULL)
  20. return 0;
  21. else
  22. return Treap->cnt;
  23. }
  24.  
  25. long long minn(Treap* Treap) {
  26. if (Treap == NULL)
  27. return INF;
  28. else
  29. return Treap->minn;
  30. }
  31.  
  32. long long summ(Treap* Treap) {
  33. if (Treap == NULL)
  34. return 0;
  35. else
  36. return Treap->summ;
  37. }
  38.  
  39. void push_summ(Treap* Treap){
  40. if (Treap != NULL)
  41. {
  42. Treap->x += Treap->add_sum;
  43. if (Treap->leftp != NULL)
  44. Treap->leftp->add_sum += Treap->add_sum;
  45. if (Treap->rightp != NULL)
  46. Treap->rightp->add_sum += Treap->add_sum;
  47. Treap->add_sum = 0;
  48. }
  49. }
  50.  
  51. void upd_cnt(Treap* Treap)
  52. {
  53. if (Treap != NULL) Treap->cnt = 1 + cnt(Treap->rightp) + cnt(Treap->leftp);
  54. }
  55.  
  56. void upd_minn(Treap* Treap)
  57. {
  58. if (Treap != NULL) Treap->minn = min(min(minn(Treap->rightp), minn(Treap->leftp)), Treap->x);
  59. }
  60.  
  61. void upd_summ(Treap* Treap)
  62. {
  63. if (Treap != NULL) Treap->summ = summ(Treap->rightp) + summ(Treap->leftp) + Treap->x;
  64. }
  65.  
  66. void recalc(Treap* Treap) {
  67. upd_cnt(Treap);
  68. upd_minn(Treap);
  69. push_summ(Treap);
  70. upd_summ(Treap);
  71. }
  72.  
  73. pair<Treap*, Treap*> split(Treap *root, long long x, long long summ = 0)
  74. {
  75. if (root == NULL) {
  76. return{ NULL, NULL };
  77. }
  78. recalc(root);
  79. int id = summ + cnt(root->leftp);
  80. if (id < x) {
  81. auto res = split(root->rightp, x, id + 1);
  82. root->rightp = res.first;
  83. recalc(root);
  84. return{ root, res.second };
  85. }
  86. else {
  87. auto res = split(root->leftp, x, summ);
  88. root->leftp = res.second;
  89. recalc(root);
  90. return{ res.first, root };
  91. }
  92. }
  93.  
  94. Treap* Merge(Treap* l, Treap* r) {
  95. recalc(l);
  96. recalc(r);
  97. if (l == NULL) {
  98. return r;
  99. }
  100. if (r == NULL) {
  101. return l;
  102. }
  103. if (l->y > r->y) {
  104. l->rightp = Merge(l->rightp, r);
  105. recalc(l);
  106. return l;
  107. }
  108. else {
  109. r->leftp = Merge(l, r->leftp);
  110. recalc(r);
  111. return r;
  112. }
  113. }
  114.  
  115. Treap* Insert(Treap* root, long long value, long long x) {
  116. auto spl = split(root, x);
  117. Treap* nw = new Treap({ value, rand(), 1, value, NULL, NULL });
  118. auto a = Merge(Merge(spl.first, nw), spl.second);
  119. recalc(a);
  120. return a;
  121. }
  122.  
  123. Treap* Insert_mass(Treap* root, Treap* nedw, long long x) {
  124. auto spl = split(root, x);
  125. auto a = Merge(Merge(spl.first, nedw), spl.second);
  126. recalc(a);
  127. return a;
  128. }
  129.  
  130. Treap* del(Treap* root, long long l, long long r) {
  131. auto spl1 = split(root, l);
  132. auto spl2 = split(spl1.second, r - l);
  133. auto a = Merge(spl1.first, spl2.second);
  134. recalc(a);
  135. return a;
  136. }
  137.  
  138. Treap* get_sumd(Treap* root, long long l, long long r, long long x) {
  139. auto spl1 = split(root, l);
  140. auto spl2 = split(spl1.second, r - l);
  141. spl2.first->add_sum += x;
  142. auto a = Merge(spl1.first, Merge(spl2.first, spl2.second));
  143. recalc(a);
  144. return a;
  145. }
  146.  
  147. long long get_sum(Treap* root, long long l, long long r) {
  148. auto spl1 = split(root, l);
  149. auto spl2 = split(spl1.second, r - l);
  150. auto ans = summ(spl2.first);
  151. root = Merge(spl1.first, Merge(spl2.first, spl2.second));
  152. return ans;
  153. }
  154.  
  155. long long get_min(Treap* root, long long l, long long r) {
  156. auto spl1 = split(root, l);
  157. auto spl2 = split(spl1.second, r - l);
  158. auto ans = minn(spl2.first);
  159. root = Merge(spl1.first, Merge(spl2.first, spl2.second));
  160. return ans;
  161. }
  162.  
  163. Treap* revelr(Treap* root, long long l1, long long r1, long long l2, long long r2)
  164. {
  165. auto spl1 = split(root, l1);
  166. auto spl2 = split(spl1.second, r1 - l1);
  167. auto spl3 = split(spl2.second, l2 - r1);
  168. auto spl4 = split(spl3.second, r2 - l2);
  169. auto a1 = Merge(Merge(Merge(Merge(spl1.first, spl4.first), spl3.first), spl2.first), spl4.second);
  170. recalc(a1);
  171. return a1;
  172. }
  173.  
  174. void output(Treap* t) {
  175. if (!t) return;
  176. push_summ(t);
  177. output(t->leftp);
  178. cout << t->x << " ";
  179. output(t->rightp);
  180. }
  181.  
  182. int main()
  183. {
  184. int n, m, rt;
  185. cin >> n >> m;
  186. Treap* root = NULL;
  187. for (int i = 0; i < n; i++)
  188. {
  189. cin >> rt;
  190. root = Insert(root, rt, i);
  191. }
  192. int l1, r1, l2, r2;
  193. for (int i = 0; i < m; i++)
  194. {
  195. cin >> rt;
  196. if (rt == 1)
  197. {
  198. cin >> l1 >> r1 >> l2;
  199. l1--;
  200. root = get_sumd(root, l1, r1, l2);
  201. //output(root);
  202. //cout << endl;
  203. }
  204. if (rt == 2)
  205. {
  206. cin >> l1 >> r1;
  207. l1--;
  208. cout << get_sum(root, l1, r1) << " " << get_min(root, l1, r1) << endl;
  209. //output(root);
  210. //cout << endl;
  211. }
  212. if (rt == 3)
  213. {
  214. cin >> l1 >> r1 >> l2 >> r2;
  215. l1--; l2--;
  216. root = revelr(root, min(l1, l2), min(r1, r2), max(l1, l2), max(r1, r2));
  217. //output(root);
  218. //cout << endl;
  219. }
  220. }
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement