-
Notifications
You must be signed in to change notification settings - Fork 249
/
Copy pathCartesianTreeImplicitKeys.cpp
158 lines (137 loc) · 3.27 KB
/
CartesianTreeImplicitKeys.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
/*************************************************************************************
Cartesian tree using implicit keys. Implementation below contains
array segment reverse and finding range minimum. O(logN) on operation.
Based on problem 111240 from informatics.mccme.ru:
http://informatics.mccme.ru/mod/statements/view3.php?chapterid=111240
*************************************************************************************/
#include <iostream>
#include <fstream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <utility>
#include <cstring>
#include <iomanip>
using namespace std;
const int INF = 2 * 1000 * 1000 * 1000;
struct node {
int y, val;
int sz, mn;
bool rev;
node *l, *r;
node (int new_val, int new_y) {
y = new_y; val = new_val;
sz = 1; mn = val;
rev = false;
l = NULL; r = NULL;
}
};
typedef node * pnode;
int getsize(pnode t) {
if (t == NULL)
return 0;
return t->sz;
}
int getmin(pnode t) {
if (t == NULL)
return INF;
return t->mn;
}
void update(pnode t) {
if (t == NULL)
return;
t->sz = getsize(t->l) + 1 + getsize(t->r);
t->mn = min(t->val, min(getmin(t->r), getmin(t->l)));
}
void push(pnode t) {
if (t && t->rev) {
swap(t->l, t->r);
if (t->l)
t->l->rev ^= true;
if (t->r)
t->r->rev ^= true;
t->rev = false;
}
}
void merge(pnode &t, pnode l, pnode r) {
push(l); push(r);
if (l == NULL)
t = r;
else if (r == NULL)
t = l;
else if (l->y > r->y) {
merge(l->r, l->r, r);
t = l;
}
else {
merge(r->l, l, r->l);
t = r;
}
update(t);
}
void split(pnode t, pnode &l, pnode &r, int x, int add = 0) {
push(t);
if (t == NULL) {
l = r = NULL;
return;
}
int key = getsize(t->l) + 1 + add;
if (x <= key) {
split(t->l, l, t->l, x, add);
r = t;
}
else {
split(t->r, t->r, r, x, add + getsize(t->l) + 1);
l = t;
}
update(t);
}
void reverse(pnode t, int l, int r) {
pnode a, b;
split(t, t, a, l, 0);
split(a, a, b, r - l + 2, 0);
a->rev ^= true;
merge(t, t, a);
merge(t, t, b);
}
int getmin(pnode t, int l, int r) {
int ans;
pnode a, b;
split(t, t, a, l, 0);
split(a, a, b, r - l + 2, 0);
ans = getmin(a);
merge(t, t, a);
merge(t, t, b);
return ans;
}
int n, m;
int qt, l, r;
pnode root = NULL, add;
int main() {
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
scanf("%d %d\n", &n, &m);
for (int i = 1; i <= n; i++) {
int x;
scanf("%d", &x);
add = new node(x, rand());
merge(root, root, add);
}
for (int i = 1; i <= m; i++) {
scanf("%d %d %d",&qt, &l, &r);
if (qt == 1) {
reverse(root, l, r);
}
else {
printf("%d\n", getmin(root, l, r));
}
}
return 0;
}