-
Notifications
You must be signed in to change notification settings - Fork 10
/
QMAX4.cpp
211 lines (182 loc) Β· 3.79 KB
/
QMAX4.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
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
201
202
203
204
205
206
207
208
209
210
211
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> ii;
typedef unsigned long long ull;
#define X first
#define Y second
#define pb push_back
#define mp make_pair
#define ep emplace_back
#define EL printf("\n")
#define sz(A) (int) A.size()
#define FOR(i,l,r) for (int i=l;i<=r;i++)
#define FOD(i,r,l) for (int i=r;i>=l;i--)
#define fillchar(a,x) memset(a, x, sizeof (a))
#define faster ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
const int oo = 1e9+7;
typedef struct node {
int sz, priority;
int val, Max;
node *L, *R;
};
typedef node *Node;
Node root, nil;
int Rand() {
return (rand()*11111 + rand()*33333) % oo;
}
Node newNode(int Val) {
Node ans = new node();
ans->val = ans->Max = Val;
ans->sz = 1;
ans->priority = Rand();
ans->L = ans->R = nil;
return ans;
}
void Init() {
nil = new node();
nil->sz = nil->priority;
nil->val = nil->Max = -oo;
nil->L = nil->R = nil;
root = nil;
}
void Update(Node x) {
x->sz = x->L->sz + x->R->sz + 1;
x->Max = max(max(x->L->Max, x->R->Max), x->val);
}
Node UpTree(Node x, Node y) {
if (y->L == x)
y->L = x->R, x->R = y;
else
y->R = x->L, x->L = y;
Update(y);
Update(x);
return x;
}
void Insert(Node &t, Node x, int i) {
if (t == nil) {
t = x;
return ;
}
if (i <= t->L->sz+1) {
Insert(t->L, x, i);
if (t->L->priority > t->priority) t = UpTree(t->L, t);
} else {
Insert(t->R, x, i-t->L->sz-1);
if (t->R->priority > t->priority) t = UpTree(t->R, t);
}
Update(t);
}
void Insert(Node &t, int val, int pos) {
Node x = newNode(val);
Insert(t, x, pos);
}
Node Join(Node t1, Node t2) {
if (t1 == nil) return t2;
if (t2 == nil) return t1;
if (t1->priority > t2->priority) {
t1->R = Join(t1->R, t2);
Update(t1);
return t1;
} else {
t2->L = Join(t1, t2->L);
Update(t2);
return t2;
}
}
void Split(Node t, int i, Node &t1, Node &t2) {
if (t == nil) {
t1 = t2 = nil;
return ;
}
if (i == 0) {
t1 = nil;
t2 = t;
return ;
}
if (i < t->L->sz+1) {
Split(t->L, i, t1, t->L);
t2 = t;
} else {
i -= t->L->sz+1;
Split(t->R, i, t->R, t2);
t1 = t;
}
Update(t);
}
void Delete(Node &t, int i) {
Node t1, t2, t3;
Split(t, i-1, t1, t2);
Split(t2, 1, t2, t3);
t = Join(t1, t3);
}
int Max(Node t, int l, int r, int L, int R) {
if (t == nil || r < L || R < l) return -oo;
if (L == l && r == R) return t->Max;
int mid = l + t->L->sz;
if (mid < L) return Max(t->R, mid+1, r, L, R);
if (R < mid) return Max(t->L, l, mid-1, L, R);
return max(t->val, max(Max(t->L, l, mid-1, L, mid-1),
Max(t->R, mid+1, r, mid+1, R)));
}
int Max(Node t, int i, int j) {
return Max(t, 1, t->sz, i, j);
}
int get(Node t, int i) {
if (i == t->L->sz+1) return t->val;
if (i < t->L->sz+1) return get(t->L, i);
return get(t->R, i-t->L->sz-1);
}
void Change(Node &t, int i, int v) {
if (i == t->L->sz+1) t->val = v;
else if (i < t->L->sz+1) Change(t->L, i, v);
else Change(t->R, i-t->L->sz-1, v);
Update(t);
}
void Swap(Node &t, int i, int j) {
int x = get(t,i), y = get(t,j);
Change(t,i,y);
Change(t,j,x);
}
//--------CHECK-NODE----------//
int a[100];
void dfs(Node r) {
if (r == nil) return;
dfs(r->L);
a[++a[0]] = r->val;
dfs(r->R);
}
void check(Node t) {
Node r = t;
a[0] = 0;
dfs(r);
FOR(i,1,a[0]) printf("%d ", a[i]);
EL;
}
//---------------------------//
int main() {
// freopen("INP.TXT", "r", stdin);
// freopen("OUT.TXT", "w", stdout);
int n, x, y;
char c;
Init();
scanf("%d\n", &n);
while (n--) {
scanf("%c", &c);
if (c == 'I') {
scanf("%d%d\n", &x,&y);
if (y > root->sz+1) y = root->sz+1;
Insert(root, x, y);
} else if (c == 'S') {
scanf("%d%d\n", &x,&y);
Swap(root, x, y);
} else if (c == 'D') {
scanf("%d\n", &x);
Delete(root, x);
} else {
scanf("%d%d\n", &x,&y);
printf("%d\n", Max(root, x, y));
}
}
return 0;
}