-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtreap.cpp
More file actions
179 lines (170 loc) · 3.25 KB
/
treap.cpp
File metadata and controls
179 lines (170 loc) · 3.25 KB
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
// In the name of Allah.
// We're nothing and you're everything.
// Ya Ali!
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 17;
struct Node{
int k, p;
Node *l, *r;
};
typedef Node* Ni;
void split(Ni t, int k, Ni& l, Ni& r){
if(!t)
l = r = 0;
else if(k < t -> k)
split(t -> l, k, l, t -> l), r = t;
else
split(t -> r, k, t -> r, r), l = t;
}
void insert(Ni &t, Ni it){
if(!t)
t = it;
else if(it -> p < t -> p)
insert(it -> k < t -> k ? t -> l : t -> r, it);
else
split(t, it -> k, it -> l, it -> r), t = it;
}
int main(){
ios::sync_with_stdio(0), cin.tie(0);
}
// Implicit treap // GSS6
// In the name of Allah.
// We're nothing and you're everything.
// Ya Ali!
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e6 + 17, mod = 998244353;
int nxP(){
static int cur = 1;
cur = (ll) cur * 3 % mod;
return cur;
}
struct Store{
int pre, suf, sum, ans;
Store (int val = -mod){
pre = suf = sum = ans = val;
}
Store(int a, int b, int c, int d): pre(a), suf(b), sum(c), ans(d) {}
};
Store operator +(Store l, Store r){
if(l.sum == -mod)
return r;
if(r.sum == -mod)
return l;
return Store(max(l.pre, l.sum + r.pre), max(r.suf, l.suf + r.sum), l.sum + r.sum, max({l.ans, r.ans, l.suf + r.pre}));
}
struct Node{
int k, p, val;
Store ans;
Node *l, *r;
};
typedef Node* Ni;
int cnt(Ni i){
return i ? i -> k : 0;
}
Store ans(Ni i){
return i ? i -> ans : Store();
}
void upd(Ni t){
if(!t) return;
t -> k = cnt(t -> l) + cnt(t -> r) + 1;
t -> ans = ans(t -> l) + t -> val + ans(t -> r);
}
void split(Ni t, int k, Ni& l, Ni& r){
if(!t)
l = r = 0;
else{
if(k <= cnt(t -> l))
split(t -> l, k, l, t -> l), r = t;
else
split(t -> r, k - 1 - cnt(t -> l), t -> r, r), l = t;
upd(t);
}
}
void merge(Ni &t, Ni l, Ni r){
if(!l || !r)
t = l ? l : r;
else if(l -> p > r -> p)
merge(l -> r, l -> r, r), t = l;
else
merge(r -> l, l, r -> l), t = r;
upd(t);
}
Ni root;
void insert(int k, int v){
Ni r;
split(root, k, root, r);
Ni nw = new Node({0, nxP(), v, v});
merge(root, root, nw);
merge(root, root, r);
}
void erase(int k){
// removes kth element
k++;
Ni tmp, r;
split(root, k, root, r);
split(root, k - 1, root, tmp);
merge(root, root, r);
}
int get(int l, int r){
Ni qans, ri;
split(root, r, root, ri);
split(root, l, root, qans);
int ret = ans(qans).ans;
merge(root, root, qans);
merge(root, root, ri);
return ret;
}
void replace(int k, int v){
erase(k);
insert(k, v);
}
void print(Ni v, int h = 0){
return;
if(!v)
return ;
cerr << string(h * 2, ' ') << v -> k << ' ' << v -> p << ' ' << v -> val << " (" << v -> ans.pre << ' '
<< v -> ans.suf << ' ' << v -> ans.sum << ' ' << v -> ans.ans << ')' << '\n';
print(v -> l, h + 1);
print(v -> r, h + 1);
}
int main(){
ios::sync_with_stdio(0), cin.tie(0);
int n;
cin >> n;
for(int i = 0; i < n; i++){
int x;
cin >> x;
insert(i, x);
}
print(root);
int q;
cin >> q;
while(q--){
char ty;
cin >> ty;
if(ty == 'Q'){
int l, r;
cin >> l >> r;
cout << get(l - 1, r) << '\n';
}
else if(ty == 'I'){
int p, x;
cin >> p >> x;
insert(p - 1, x);
}
else if(ty == 'R'){
int p, x;
cin >> p >> x;
replace(p - 1, x);
}
else{
int p;
cin >> p;
erase(p - 1);
}
print(root);
}
}