- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSplay.cpp
More file actions
Latest commit
133 lines (117 loc) · 3.47 KB
/
Copy pathSplay.cpp
File metadata and controls
133 lines (117 loc) · 3.47 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
// Code by Denverjin.
#include<bits/stdc++.h>
usingnamespacestd;
#definelddouble
#definelllonglong
#definepii pair <int, int>
#defineullunsignedlonglong
#defineF first
#defineS second
#definePB push_back
#defineMP make_pair
#definerep(i, n) for (int i = 0; i < (int)(n); ++ i)
#definePIacos(-1)
#defineMOD1000000007
#defineMUL19260817
#defineEPS1e-10
#defineINF1e9
#defineLINF1e18
template <typename T> inlinevoidchkmin(T &x, T y) {if (y < x) x = y;}
template <typename T> inlinevoidchkmax(T &x, T y) {if (y > x) x = y;}
template <typename T> inline T add(T x, T y) {return (x + y) % MOD;}
template <typename T> inline T mul(T x, T y) {return1LL * x * y % MOD;}
template <typename T> inline T qp(T x, T n) {
T ans = 1;
do {if (n & 1) ans = mul(ans, x); x = mul(x, x);} while (n >>= 1);
return ans;
}
#ifndef DEBUG
constintSZ = 1 << 13;
char buff[SZ], *pos = buff + SZ - 1;
#definegetchar() (++ pos == buff + SZ ? fread(pos = buff, 1, SZ, stdin), *pos : *pos)
#endif
inline ll read() {
ll x = 0; int f = 1, c = getchar();
for (;!isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = (x<<3) + (x<<1) + (c^48);
return x * f;
}
constint N = 1 << 20;
queue <int> freenodes;
intnewnode() {int x = freenodes.front(); freenodes.pop(); return x;}
structNode {int fa, ch[2], val, rev, sz; Node() {fa = ch[0] = ch[1] = val = rev = 0; sz = 1;}} node[N];
voidinit() {rep(i, N) if (i) freenodes.push(i);}
voidpushdown(int x) {
if (node[x].rev) {
node[x].rev = 0;
if (node[x].ch[0]) node[node[x].ch[0]].rev ^= 1;
if (node[x].ch[1]) node[node[x].ch[1]].rev ^= 1;
swap(node[x].ch[0], node[x].ch[1]);
}
}
voidpushup(int x) {
node[x].sz = node[node[x].ch[0]].sz + 1 + node[node[x].ch[1]].sz;
}
voidrotate(int x, int &t) {
int y = node[x].fa, z = node[y].fa;
int tp = node[y].ch[1] == x;
if (y == t) t = x;
else node[z].ch[node[z].ch[1] == y] = x;
node[x].fa = z; node[y].fa = x;
node[y].ch[tp] = node[x].ch[tp ^ 1];
node[node[y].ch[tp]].fa = y;
node[x].ch[tp ^ 1] = y;
pushup(x); if (y) pushup(y); if (z) pushup(z);
}
voidsplay(int x, int &t) {
while (x != t) {
int y = node[x].fa, z = node[y].fa;
if (y != t) rotate((node[z].ch[1] == y) ^ (node[y].ch[1] == x) ? x : y, t);
rotate(x, t);
}
}
intbuild(int l, int r) {
if (l >= r) return0;
int x = newnode();
int md = (l + r) >> 1;
node[x].val = md;
int L = build(l, md);
int R = build(md + 1, r);
node[x].ch[0] = L; if (L) node[L].fa = x;
node[x].ch[1] = R; if (R) node[R].fa = x;
pushup(x);
return x;
}
intfind(int x, int k) {
// printf("%d %d\n", x, k);
pushdown(x);
int szz = node[node[x].ch[0]].sz;
if (szz + 1 == k) return x;
elseif (szz + 1 < k) returnfind(node[x].ch[1], k - szz - 1);
elsereturnfind(node[x].ch[0], k);
}
voiddbg(int x) {
if (!x) return;
dbg(node[x].ch[0]);
printf("%d ", node[x].val);
dbg(node[x].ch[1]);
}
intmain() {
node[0].sz = 0;
init();
int n = read(), q = read();
int rt = build(0, n + 2);
// dbg(rt); puts("");
rep(i, q) {
int l = read(), r = read();
int L = find(rt, l), R = find(rt, r + 2);
splay(L, rt);
splay(R, node[rt].ch[1]);
node[node[R].ch[0]].rev ^= 1;
// dbg(rt); puts("");
}
// dbg(rt); puts("");
for (int i = 1; i <= n; ++ i)
printf("%d ", node[find(rt, i + 1)].val);
return0;
}