-
Notifications
You must be signed in to change notification settings - Fork 6
/
295E.cpp
163 lines (132 loc) · 3.5 KB
/
295E.cpp
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// https://codeforces.com/contest/295/problem/E
// Time complexity: O(max(n*log(n), m*log(n))
// Space complexity: O(n)
#include <cstdio> // scanf(...), printf(...)
#include <cstdlib> // srand(...), rand(...)
#include <ctime> // time(...)
#include <vector>
#include <cstdint> // int64_t
using namespace std;
typedef struct Node *pnode;
struct Node {
int key, prior, cnt;
int64_t sum, res;
pnode l, r;
Node(int64_t key) : key(key), prior(rand()), cnt(1), sum(key), res(0), l(nullptr), r(nullptr) {}
};
struct Treap {
private:
pnode root;
void clear(pnode t) {
if (t) {
clear(t->l);
clear(t->r);
delete t;
}
}
int cnt(pnode t) {
return t ? t->cnt : 0;
}
int64_t sum(pnode t) {
return t ? t->sum : 0;
}
int64_t res(pnode t) {
return t ? t->res : 0;
}
void upd(pnode t) {
if (t) {
t->cnt = 1 + cnt(t->l) + cnt(t->r);
t->sum = sum(t->l) + sum(t->r) + t->key;
t->res = res(t->l) + res(t->r) + cnt(t->l) * 1LL * sum(t->r) - cnt(t->r) * 1LL * sum(t->l) +
t->key * 1LL * (cnt(t->l) - cnt(t->r)) + sum(t->r) - sum(t->l);
}
}
void merge(pnode &t, pnode l, pnode r) {
if (!l || !r) {
t = l ? l : r;
} else if (l->prior < r->prior) {
merge(r->l, l, r->l), t = r;
} else {
merge(l->r, l->r, r), t = l;
}
upd(t);
}
// splits into <= key and > key
void split(pnode t, pnode &l, pnode &r, int key) {
if (!t) {
l = r = nullptr;
} else if (t->key <= key) { // search for right ones (leaves left ones, which are <= key)
split(t->r, t->r, r, key), l = t;
} else {
split(t->l, l, t->l, key), r = t;
}
upd(t);
}
void insert(pnode &t, int key) {
pnode l, r;
split(t, l, r, key);
merge(t, l, new Node(key));
merge(t, t, r);
}
void erase(pnode &t, int key) {
pnode l, toErase, r;
split(t, l, r, key);
split(l, l, toErase, key - 1);
delete toErase;
merge(t, l, r);
}
int64_t sumDistances(pnode t, int l, int r) {
pnode left, center, right;
split(t, left, right, l - 1);
split(right, center, right, r);
int64_t toReturn = res(center);
merge(t, left, center);
merge(t, t, right);
return toReturn;
}
public:
Treap() : root(nullptr) {
srand(time(nullptr));
}
~Treap() {
clear(this->root);
}
void insert(int key) {
insert(this->root, key);
}
void erase(int key) {
erase(this->root, key);
}
int64_t sumDistances(int l, int r) {
return sumDistances(this->root, l, r);
}
};
int main() {
int n, m, type;
scanf("%d", &n);
vector<int> coord(n);
Treap T;
for (int i = 0; i < n; ++i) {
scanf("%d", &coord[i]);
T.insert(coord[i]);
}
scanf("%d", &m);
while (m--) {
scanf("%d", &type);
switch (type) {
case 1 :
int p, d;
scanf("%d %d", &p, &d);
--p;
T.erase(coord[p]);
coord[p] += d;
T.insert(coord[p]);
break;
case 2:
int l, r;
scanf("%d %d", &l, &r);
printf("%lld\n", T.sumDistances(l, r));
}
}
return 0;
}