Advertisement
haufont

Untitled

Oct 7th, 2016
380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.07 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;
  14. long long y;
  15. long long cnt;
  16. long long minn;
  17. long long summ;
  18. long long maxn;
  19. bool rvrs;
  20. Treap* leftp;
  21. Treap* rightp;
  22. };
  23.  
  24. void push(Treap* Treap) {
  25. if (Treap == NULL) {
  26. return;
  27. }
  28.  
  29. if (Treap->rvrs) {
  30. Treap->rvrs = false;
  31.  
  32. if (Treap->leftp != NULL) {
  33. Treap->leftp->rvrs ^= true;
  34. }
  35.  
  36. if (Treap->rightp != NULL) {
  37. Treap->rightp->rvrs ^= true;
  38. }
  39.  
  40. auto a = Treap->leftp;
  41. auto b = Treap->rightp;
  42.  
  43. Treap->leftp = b;
  44. Treap->rightp = a;
  45. }
  46. }
  47.  
  48. long long cnt(Treap* Treap) {
  49. if (Treap == NULL) {
  50. return 0;
  51. }
  52. else {
  53. return Treap->cnt;
  54. }
  55. }
  56.  
  57. long long minn(Treap* Treap) {
  58. if (Treap == NULL) {
  59. return INF;
  60. }
  61. else {
  62. return Treap->minn;
  63. }
  64. }
  65.  
  66. long long maxn(Treap* Treap) {
  67. if (Treap == NULL) {
  68. return 0;
  69. }
  70. else {
  71. return Treap->maxn;
  72. }
  73. }
  74.  
  75. long long summ(Treap* Treap) {
  76. if (Treap == NULL) {
  77. return 0;
  78. }
  79. else {
  80. return Treap->summ;
  81. }
  82. }
  83.  
  84. void upd_cnt(Treap* Treap) {
  85. if (Treap != NULL) Treap->cnt = 1 + cnt(Treap->rightp) + cnt(Treap->leftp);
  86. }
  87.  
  88. void upd_minn(Treap* Treap) {
  89. if (Treap != NULL) Treap->minn = min(min(minn(Treap->rightp), minn(Treap->leftp)), Treap->x);
  90. }
  91.  
  92. void upd_summ(Treap* Treap) {
  93. if (Treap != NULL) Treap->summ = summ(Treap->rightp) + summ(Treap->leftp) + Treap->x;
  94. }
  95.  
  96. void upd_maxn(Treap* Treap) {
  97. if (Treap != NULL) Treap->maxn = max(max(maxn(Treap->rightp),maxn(Treap->leftp)),Treap->x);
  98. }
  99.  
  100.  
  101. void recalc(Treap* Treap) {
  102. upd_cnt(Treap);
  103. upd_minn(Treap);
  104. upd_summ(Treap);
  105. upd_maxn(Treap);
  106. }
  107.  
  108. pair<Treap*, Treap*> split(Treap* root, long long x, long long summ = 0) {
  109. push(root);
  110. if (root == NULL) {
  111. return{ NULL, NULL };
  112. }
  113.  
  114. long long id = summ + cnt(root->leftp);
  115.  
  116. if (id < x) {
  117. auto res = split(root->rightp, x, id + 1);
  118. root->rightp = res.first;
  119. recalc(root);
  120. return{ root, res.second };
  121. }
  122. else {
  123. auto res = split(root->leftp, x, summ);
  124. root->leftp = res.second;
  125. recalc(root);
  126. return{ res.first, root };
  127. }
  128. }
  129.  
  130. Treap* Merge(Treap* l, Treap* r) {
  131. push(l);
  132. push(r);
  133. if (l == NULL) {
  134. return r;
  135. }
  136. if (r == NULL) {
  137. return l;
  138. }
  139. if (l->y > r->y) {
  140. l->rightp = Merge(l->rightp, r);
  141. recalc(l);
  142. return l;
  143. }
  144. else {
  145. r->leftp = Merge(l, r->leftp);
  146. recalc(r);
  147. return r;
  148. }
  149. }
  150.  
  151. Treap* Insert(Treap* root, long long value, long long x) {
  152. auto spl = split(root, x);
  153. Treap* nw = new Treap({ value, rand(), 1, value, false, NULL, NULL });
  154. auto a = Merge(Merge(spl.first, nw), spl.second);
  155. recalc(a);
  156. return a;
  157. }
  158.  
  159. Treap* Insert_mass(Treap* root, Treap* nedw, long long x) {
  160. auto spl = split(root, x);
  161. auto a = Merge(Merge(spl.first, nedw), spl.second);
  162. recalc(a);
  163. return a;
  164. }
  165.  
  166. Treap* del(Treap* root, long long l, long long r) {
  167. auto spl1 = split(root, l);
  168. auto spl2 = split(spl1.second, r + 1);
  169. auto a = Merge(spl1.first, spl2.second);
  170. return a;
  171. }
  172.  
  173. pair<Treap*, Treap*> get_range(Treap* root, long long l, long long r) {
  174. auto spl1 = split(root, l);
  175. auto spl2 = split(spl1.second, r - l);
  176. auto a = Merge(spl1.first, spl2.second);
  177. auto b = spl2.first;
  178. return{ a, b };
  179. }
  180.  
  181. long long get_min(Treap* root, long long l, long long r) {
  182. auto spl1 = split(root, l);
  183. auto spl2 = split(spl1.second, r - l);
  184. long long ans = minn(spl2.first);
  185. root = Merge(spl1.first, Merge(spl2.first, spl2.second));
  186. return ans;
  187. }
  188.  
  189. long long get_sum(Treap* root, long long l, long long r) {
  190. auto spl1 = split(root, l);
  191. auto spl2 = split(spl1.second, r - l);
  192. long long ans = summ(spl2.first);
  193. root = Merge(spl1.first, Merge(spl2.first, spl2.second));
  194. return ans;
  195. }
  196.  
  197. long long get_max(Treap* root, long long l, long long r) {
  198. auto spl1 = split(root, l);
  199. auto spl2 = split(spl1.second, r - l);
  200. long long ans = maxn(spl2.first);
  201. root = Merge(spl1.first, Merge(spl2.first, spl2.second));
  202. return ans;
  203. }
  204.  
  205. void RVRS(Treap* root, long long l, long long r) {
  206. auto spl1 = split(root, l);
  207. auto spl2 = split(spl1.second, r - l);
  208. spl2.first->rvrs ^= true;
  209. root = Merge(spl1.first, Merge(spl2.first, spl2.second));
  210. }
  211.  
  212. long long get(Treap* root, long long x, long long summ) {
  213. if (root == NULL) {
  214. return INF;
  215. }
  216.  
  217. long long id = summ + cnt(root->leftp);
  218.  
  219. if (id == x) {
  220. return root->x;
  221. }
  222. else {
  223. return (id < x) ? get(root->rightp, x, id + 1) : get(root->leftp, x, id + 1);
  224. }
  225. }
  226.  
  227. void print_dek(Treap* root) {
  228. if (root == NULL) {
  229. return;
  230. }
  231.  
  232. print_dek(root->leftp);
  233. cout << root->x << ' ';
  234. print_dek(root->rightp);
  235. }
  236.  
  237. int main() {
  238. srand(2);
  239.  
  240. long long n, m, cur, l, r;
  241. Treap* root = NULL;
  242.  
  243. cin >> n >> m;
  244.  
  245. for (int i = 0; i < n; ++i) {
  246. cin >> cur;
  247. root = Insert(root, cur, i);
  248. }
  249. for (int i = 0; i < m; ++i) {
  250. cin >> cur >> l >> r;
  251. l--;
  252. if (cur == 2) {
  253. RVRS(root, l, r);
  254. }
  255. else {
  256. cout << get_sum(root, l, r) << " " << get_min(root, l, r) << " " << get_max(root, l, r) << endl;
  257. }
  258. }
  259.  
  260. return 0;
  261. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement