Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- GGGG
- #include <iostream>
- #include <math.h>
- using namespace std;
- struct tree
- {
- int a, n;
- long long l, h;
- struct tree *Left;
- struct tree *Right;
- };
- int h = 0, down = 0, space = 0;
- int count(int a)
- {
- int i = 0;
- a = abs(a);
- if (a == 0) return 1;
- for (; a > 0; i++)
- {
- a /= 10;
- }
- return i;
- }
- void plusl(struct tree *s, int a)
- {
- if (s->Left != NULL) plusl(s->Left, a);
- if (s->Right != NULL) plusl(s->Right, a);
- s->l += count(a);
- }
- int adtr(struct tree *atm, int a) {
- int q;
- if (atm == NULL) return 1;
- if (a > atm->a) {
- h++;
- q = adtr(atm->Right, a);
- h--;
- if (q == 1) {
- atm->Right = new struct tree;
- atm->Right->l = atm->l + atm->n;
- atm = atm->Right;
- atm->a = a;
- atm->Left = NULL;
- atm->Right = NULL;
- atm->h = h + 1;
- atm->n = count(a);
- if (h + 1 > down) down = h + 1;
- }
- }
- else {
- atm->l += count(a);
- if (atm->Right != NULL) plusl(atm->Right, a);
- h++;
- q = adtr(atm->Left, a);
- h--;
- if (q == 1) {
- atm->Left = new struct tree;
- atm->Left->n = count(a);
- atm->Left->l = atm->l - atm->Left->n;
- atm = atm->Left;
- atm->a = a;
- atm->Left = NULL;
- atm->Right = NULL;
- atm->h = h + 1;
- if (h + 1 > down) down = h + 1;
- }
- }
- return 0;
- }
- void print(struct tree *str) {
- if (str->Left != NULL) print(str->Left);
- if (str->Right != NULL) print(str->Right);
- cout << str->l << ' ' << str->a << ' ' << str->n << '\n';
- }
- void printall(struct tree *str, int n)
- {
- int i;
- if (h == n) {
- for (i = 0; i < str->l - space; i++) cout << ' ';
- cout << str->a;
- space += str->n + str->l - space;
- }
- else if (str->Left != NULL)
- {
- h++;
- printall(str->Left, n);
- h--;
- }
- if (str->Right != NULL && h != n)
- {
- h++;
- printall(str->Right, n);
- h--;
- }
- }
- int main() {
- struct tree *tr;
- int i, a;
- tr = new struct tree;
- cin >> tr->a;
- tr->l = 0;
- tr->Left = NULL;
- tr->Right = NULL;
- tr->h = 0;
- tr->n = count(tr->a);
- while (scanf("%d", &a) == 1) adtr(tr, a);
- for (i = 0; i <= down; i++)
- {
- printall(tr, i);
- cout << '\n';
- space = 0;
- }
- //print(begin);
- }
- FFFFFFFFFFFFFFFFFFF
- #include <bits/stdc++.h>
- struct tr
- {
- int cnt, val, pri;
- long long sum;
- tr *left, *right;
- tr() { }
- tr(int val) : pri((rand() << 16u) + unsigned(rand())), val(val), sum(val), left(NULL), right(NULL) { }
- };
- typedef tr* tree;
- tree Tree1, Tree2;
- long long sumall(tree t)
- {
- return t ? t->sum : 0;
- }
- int clc(tree t)
- {
- return t ? t->cnt : 0;
- }
- void upd(tree t)
- {
- if (t)
- {
- t->cnt = 1 + clc(t->left) + clc(t->right);
- t->sum = t->val + sumall(t->left) + sumall(t->right);
- }
- }
- void mer(tree l, tree r, tree &t)
- {
- if (!l || !r) t = l ? l : r;
- else if (l->pri > r->pri) mer(l->right, r, l->right), t = l;
- else mer(l, r->left, r->left), t = r;
- upd(t);
- }
- void ssp(tree t, tree &l, tree &r, int pos)
- {
- if (!t) return void(l = r = 0);
- if (pos <= clc(t->left)) ssp(t->left, l, t->left, pos), r = t;
- else ssp(t->right, t->right, r, pos - 1 - clc(t->left)), l = t;
- upd(t);
- }
- void change(int L, int R) {
- int Left1, Left2, Right1, Right2;
- tree A1, B1, C1, A2, B2, C2;
- Left1 = (L + 1) / 2;
- Right1 = R / 2;
- Left2 = L / 2;
- Right2 = (R + 1) / 2 - 1;
- ssp(Tree1, A1, B1, Left1);
- ssp(B1, B1, C1, Right1 - Left1 + 1);
- ssp(Tree2, A2, B2, Left2);
- ssp(B2, B2, C2, Right2 - Left2 + 1);
- mer(A1, B2, B2);
- mer(B2, C1, Tree1);
- mer(A2, B1, B1);
- mer(B1, C2, Tree2);
- }
- long long Sum(int L, int R) {
- int L1, L2, R1, R2;
- long long res;
- tree A1, B1, C1, A2, B2, C2;
- if (L > R) return 0;
- L1 = (L + 1) / 2;
- R1 = R / 2;
- L2 = L / 2;
- R2 = (R + 1) / 2 - 1;
- ssp(Tree1, A1, B1, L1);
- ssp(B1, B1, C1, R1 - L1 + 1);
- ssp(Tree2, A2, B2, L2);
- ssp(B2, B2, C2, R2 - L2 + 1);
- res = sumall(B1) + sumall(B2);
- mer(A1, B1, B1);
- mer(B1, C1, Tree1);
- mer(A2, B2, B2);
- mer(B2, C2, Tree2);
- return res;
- }
- int main() {
- int n, m, i, x, a, b, t, q = 0;
- while (scanf("%d %d", &n, &m), n + m)
- {
- Tree1 = Tree2 = NULL;
- if (q != 0) printf("\n");
- q++;
- printf("Swapper %d:\n", q);
- for (i = 1; i <= n; i++)
- {
- scanf("%d", &x);
- if (i & 1) mer(Tree1, new tr(x), Tree1);
- else mer(Tree2, new tr(x), Tree2);
- }
- for (i = 0; i < m; i++)
- {
- scanf("%d %d %d", &t, &a, &b);
- a--;
- b--;
- if (t == 1) change(a, b);
- else printf("%lld\n", Sum(a, b));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement