aboutsummaryrefslogtreecommitdiff
path: root/semestr-4/aisd/pracownia4/rozw2.cpp
blob: 17562a977724098beea2d3383a6d3709c29bf961 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <bits/stdc++.h>
using namespace std;

struct treap {
    typedef long long T;
    treap *left = nullptr;
    treap *right = nullptr;
    int rank, items = 1;
    T value;
    T sum;
    bool rev = false;

    treap(T val = T()) : rank(rand()), value(val), sum(val) {}

    inline void update() {
        if (rev) {
            swap(left, right);
            if (left)   left->rev   = !left->rev;
            if (right)  right->rev  = !right->rev;
            rev = false;
        }
    }
};

inline int items(treap *t) { return t ? t->items : 0; }
inline int sum(treap *t)   { return t ? t->sum : 0; }
inline void recalc(treap *t) { 
    t->items = items(t->left) + items(t->right) + 1; 
    t->sum   = sum(t->left) + sum(t->right) + t->value;
}

pair<treap*, treap*> split(treap *t, int k) {
    if (!t) return {nullptr, nullptr};
    t->update();
    if (items(t->left) < k) {
        auto p = split(t->right, k - items(t->left) - 1);
        t->right = p.first;
        recalc(t);
        return {t, p.second};
    }
    else {
        auto p = split(t->left, k);
        t->right = p.first;
        recalc(t);
        return {p.first, t};
    }
}

treap* merge(treap *a, treap *b) {
    if (!a) return b;
    if (!b) return a;
    a->update();
    b->update();
    if (a->rank > b->rank) {
        a->right = merge(a->right, b);
        recalc(a);
        return a;
    }
    else {
        b->left = merge(a, b->left);
        recalc(b);
        return b;
    }
}

treap::T select(treap *t, int k) {
    if (!t) return treap::T();
    t->update();
    int i = items(t->left);
    if (i == k)         return t->value;
    else if (i > k)     return select(t->left, k);
    else                return select(t->right, k - i - 1);
}

treap *insert (treap *t, treap::T val, int k) {
    auto p = split(t, k);
    return merge(merge(p.first, new treap(val)), p.second);
}

treap *erase(treap *t, int k) {
    auto p1 = split(t, k);
    auto p2 = split(p1.second, 1);
    return merge(p1.first, p2.second);
}

treap::T sum(treap *t, int l, int r) {
    auto p1 = split(t, r);
    auto p2 = split(p1.first, l);
    return sum(p2.second);
} 

void write(treap *t) {
    if (!t) return;
    t->update();
    write(t->left);
    cout << t->value << " ";
    write(t->right);
}
void destroy(treap *t) {
    if (!t) return;
    destroy(t->left);
    destroy(t->right);
    delete t;
}

treap *t = 0;

int main() {
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        char c;
        scanf(" %c", &c);
        if (c == 'I') {
            int p, x;
            scanf("%d%d", &p, &x);
            t = insert(t, x, p);
        }
        if (c == 'D') {
            int p;
            scanf("%d", &p);
            t = erase(t, p - 1);
        }
        if (c == 'S') {
            int l, r;
            scanf("%d%d", &l, &r);
            printf("ans: %lld\n", sum(t, l, r));
        }
        cout << "Treap:\n";
        write(t);
        cout << "\n";
    }

    destroy(t);
}