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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
| #include <iostream>
#include <math.h>
#include <cstdlib>
#include <sstream>
#include <vector>
using namespace std;
const long long base = 1000000000;
const long long MIN = -100;
struct Treap{
long long el;
long long sum;
long long y;
int c = 1;
Treap *L;
Treap *R;
};
bool exists(vector<long long>& vec, long long x){
bool res = false;
for (int i = 0; i < vec.size(); i++)
if (vec[i] == x){
res = true;
break;
}
return res;
}
void calc(Treap *&Tree){
if (Tree != nullptr){
if (Tree->L != nullptr && Tree->R != nullptr){
Tree->c = Tree->L->c + Tree->R->c + 1;
Tree->sum = Tree->L->sum + Tree->R->sum + Tree->el;
}
else
if (Tree->L != nullptr){
Tree->c = Tree->L->c + 1;
Tree->sum = Tree->L->sum + Tree->el;
}
else
if (Tree->R != nullptr){
Tree->c = Tree->R->c + 1;
Tree->sum = Tree->R->sum + Tree->el;
}
else{
Tree->c = 1;
Tree->sum = Tree->el;
}
}
}
int sizeT(Treap *&Tree){
if (Tree == nullptr)
return 0;
else
return Tree->c;
}
Treap* mergeT(Treap *&Left, Treap *&Right){
Treap *Res = nullptr;
if (Left == nullptr)
return Right;
if (Right == nullptr)
return Left;
if (Left->y > Right->y){
Treap *newR = mergeT(Left->R, Right);
Res = new Treap;
Res->el = Left->el;
Res->y = Left->y;
Res->L = Left->L;
Res->R = newR;
calc(Res);
}
else {
Treap *newL = mergeT(Left, Right->L);
Res = new Treap;
Res->el = Right->el;
Res->y = Right->y;
Res->L = newL;
Res->R = Right->R;
calc(Res);
}
calc(Res);
return Res;
}
void splitT(int x, Treap *&Tree, Treap *&Left, Treap *&Right){
if (Tree == nullptr)
return;
Treap *newTree = nullptr;
int ind = sizeT(Tree->L) + 1;
if (ind <= x){
if (Tree->R == nullptr)
Right = nullptr;
else
splitT(x - ind, Tree->R, newTree, Right);
Left = new Treap;
Left->el = Tree->el;
Left->y = Tree->y;
Left->L = Tree->L;
Left->R = newTree;
calc(Left);
}
else {
if (Tree->L == nullptr)
Left = nullptr;
else
splitT(x, Tree->L, Left, newTree);
Right = new Treap;
Right->el = Tree->el;
Right->y = Tree->y;
Right->L = newTree;
Right->R = Tree->R;
calc(Right);
}
}
void insertT(long long el, int pos, Treap *&Tree){
if (Tree == nullptr){
Tree = new Treap;
Tree->el = Tree->sum = el;
Tree->y = rand() % base*base;
Tree->L = Tree->R = nullptr;
}
else{
Treap *Left = nullptr;
Treap *Right = nullptr;
splitT(pos, Tree, Left, Right);
Treap *X = new Treap;
X->el = X->sum = el;
X->y = rand() % base*base;
X->L = X->R = nullptr;
Treap *First = mergeT(Left, X);
Treap *Last = mergeT(First, Right);
Tree = Last;
calc(Tree);
}
}
long long getSum(int l, int r, Treap *&Tree){
long long res = 0;
Treap *Left = nullptr;
Treap *Mid = nullptr;
Treap *Right1 = nullptr;
Treap *Right2 = nullptr;
splitT(l, Tree, Left, Right1);
splitT(r - l + 1, Right1, Mid, Right2);
if (Mid != nullptr)
res = Mid->sum;
return res;
}
int main()
{
std::ios::sync_with_stdio(false);
Treap *Tree = nullptr;
insertT(0, 0, Tree);
int n;
cin >> n;
long long last = MIN;
int pos = 1;
vector<long long> used;
for (int i = 0; i < n; i++){
char op;
cin >> op;
if (op == '+'){
long long el;
cin >> el;
if (last > MIN)
el = (el + last) % base;
if (!exists(used, el)){
insertT(el, pos, Tree);
used.push_back(el);
pos++;
}
last = MIN;
}
else{
int l, r;
long long res;
cin >> l >> r;
res = getSum(l, r, Tree);
last = res;
cout << res << endl;
}
}
return 0;
} |