Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.38 KB | None | 0 0
  1. GGGG
  2.  
  3. #include <iostream>
  4. #include <math.h>
  5.  
  6. using namespace std;
  7.  
  8. struct tree
  9. {
  10. int a, n;
  11. long long l, h;
  12. struct tree *Left;
  13. struct tree *Right;
  14. };
  15.  
  16. int h = 0, down = 0, space = 0;
  17.  
  18. int count(int a)
  19. {
  20. int i = 0;
  21. a = abs(a);
  22. if (a == 0) return 1;
  23. for (; a > 0; i++)
  24. {
  25. a /= 10;
  26. }
  27. return i;
  28. }
  29.  
  30. void plusl(struct tree *s, int a)
  31. {
  32. if (s->Left != NULL) plusl(s->Left, a);
  33. if (s->Right != NULL) plusl(s->Right, a);
  34. s->l += count(a);
  35. }
  36.  
  37. int adtr(struct tree *atm, int a) {
  38. int q;
  39. if (atm == NULL) return 1;
  40. if (a > atm->a) {
  41. h++;
  42. q = adtr(atm->Right, a);
  43. h--;
  44. if (q == 1) {
  45. atm->Right = new struct tree;
  46. atm->Right->l = atm->l + atm->n;
  47. atm = atm->Right;
  48. atm->a = a;
  49. atm->Left = NULL;
  50. atm->Right = NULL;
  51. atm->h = h + 1;
  52. atm->n = count(a);
  53. if (h + 1 > down) down = h + 1;
  54. }
  55. }
  56. else {
  57. atm->l += count(a);
  58. if (atm->Right != NULL) plusl(atm->Right, a);
  59. h++;
  60. q = adtr(atm->Left, a);
  61. h--;
  62. if (q == 1) {
  63. atm->Left = new struct tree;
  64. atm->Left->n = count(a);
  65. atm->Left->l = atm->l - atm->Left->n;
  66. atm = atm->Left;
  67. atm->a = a;
  68. atm->Left = NULL;
  69. atm->Right = NULL;
  70. atm->h = h + 1;
  71. if (h + 1 > down) down = h + 1;
  72. }
  73. }
  74. return 0;
  75. }
  76.  
  77.  
  78. void print(struct tree *str) {
  79. if (str->Left != NULL) print(str->Left);
  80. if (str->Right != NULL) print(str->Right);
  81. cout << str->l << ' ' << str->a << ' ' << str->n << '\n';
  82. }
  83.  
  84. void printall(struct tree *str, int n)
  85. {
  86. int i;
  87. if (h == n) {
  88. for (i = 0; i < str->l - space; i++) cout << ' ';
  89. cout << str->a;
  90. space += str->n + str->l - space;
  91. }
  92. else if (str->Left != NULL)
  93. {
  94. h++;
  95. printall(str->Left, n);
  96. h--;
  97. }
  98. if (str->Right != NULL && h != n)
  99. {
  100. h++;
  101. printall(str->Right, n);
  102. h--;
  103. }
  104. }
  105.  
  106. int main() {
  107. struct tree *tr;
  108. int i, a;
  109. tr = new struct tree;
  110. cin >> tr->a;
  111. tr->l = 0;
  112. tr->Left = NULL;
  113. tr->Right = NULL;
  114. tr->h = 0;
  115. tr->n = count(tr->a);
  116. while (scanf("%d", &a) == 1) adtr(tr, a);
  117. for (i = 0; i <= down; i++)
  118. {
  119. printall(tr, i);
  120. cout << '\n';
  121. space = 0;
  122. }
  123. //print(begin);
  124. }
  125.  
  126.  
  127.  
  128. FFFFFFFFFFFFFFFFFFF
  129.  
  130.  
  131. #include <bits/stdc++.h>
  132.  
  133. struct tr
  134. {
  135. int cnt, val, pri;
  136. long long sum;
  137. tr *left, *right;
  138. tr() { }
  139. tr(int val) : pri((rand() << 16u) + unsigned(rand())), val(val), sum(val), left(NULL), right(NULL) { }
  140.  
  141. };
  142.  
  143. typedef tr* tree;
  144. tree Tree1, Tree2;
  145.  
  146. long long sumall(tree t)
  147. {
  148. return t ? t->sum : 0;
  149. }
  150.  
  151. int clc(tree t)
  152. {
  153. return t ? t->cnt : 0;
  154. }
  155.  
  156. void upd(tree t)
  157. {
  158. if (t)
  159. {
  160. t->cnt = 1 + clc(t->left) + clc(t->right);
  161. t->sum = t->val + sumall(t->left) + sumall(t->right);
  162. }
  163. }
  164.  
  165. void mer(tree l, tree r, tree &t)
  166. {
  167. if (!l || !r) t = l ? l : r;
  168. else if (l->pri > r->pri) mer(l->right, r, l->right), t = l;
  169. else mer(l, r->left, r->left), t = r;
  170. upd(t);
  171. }
  172.  
  173. void ssp(tree t, tree &l, tree &r, int pos)
  174. {
  175. if (!t) return void(l = r = 0);
  176. if (pos <= clc(t->left)) ssp(t->left, l, t->left, pos), r = t;
  177. else ssp(t->right, t->right, r, pos - 1 - clc(t->left)), l = t;
  178. upd(t);
  179. }
  180.  
  181. void change(int L, int R) {
  182. int Left1, Left2, Right1, Right2;
  183. tree A1, B1, C1, A2, B2, C2;
  184. Left1 = (L + 1) / 2;
  185. Right1 = R / 2;
  186. Left2 = L / 2;
  187. Right2 = (R + 1) / 2 - 1;
  188. ssp(Tree1, A1, B1, Left1);
  189. ssp(B1, B1, C1, Right1 - Left1 + 1);
  190. ssp(Tree2, A2, B2, Left2);
  191. ssp(B2, B2, C2, Right2 - Left2 + 1);
  192. mer(A1, B2, B2);
  193. mer(B2, C1, Tree1);
  194. mer(A2, B1, B1);
  195. mer(B1, C2, Tree2);
  196. }
  197.  
  198. long long Sum(int L, int R) {
  199. int L1, L2, R1, R2;
  200. long long res;
  201. tree A1, B1, C1, A2, B2, C2;
  202. if (L > R) return 0;
  203. L1 = (L + 1) / 2;
  204. R1 = R / 2;
  205. L2 = L / 2;
  206. R2 = (R + 1) / 2 - 1;
  207. ssp(Tree1, A1, B1, L1);
  208. ssp(B1, B1, C1, R1 - L1 + 1);
  209. ssp(Tree2, A2, B2, L2);
  210. ssp(B2, B2, C2, R2 - L2 + 1);
  211. res = sumall(B1) + sumall(B2);
  212. mer(A1, B1, B1);
  213. mer(B1, C1, Tree1);
  214. mer(A2, B2, B2);
  215. mer(B2, C2, Tree2);
  216. return res;
  217. }
  218.  
  219. int main() {
  220. int n, m, i, x, a, b, t, q = 0;
  221. while (scanf("%d %d", &n, &m), n + m)
  222. {
  223. Tree1 = Tree2 = NULL;
  224. if (q != 0) printf("\n");
  225. q++;
  226. printf("Swapper %d:\n", q);
  227. for (i = 1; i <= n; i++)
  228. {
  229. scanf("%d", &x);
  230. if (i & 1) mer(Tree1, new tr(x), Tree1);
  231. else mer(Tree2, new tr(x), Tree2);
  232. }
  233. for (i = 0; i < m; i++)
  234. {
  235. scanf("%d %d %d", &t, &a, &b);
  236. a--;
  237. b--;
  238. if (t == 1) change(a, b);
  239. else printf("%lld\n", Sum(a, b));
  240. }
  241. }
  242. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement