-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoj3481 segtree.cpp
105 lines (88 loc) · 2.16 KB
/
poj3481 segtree.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
#include <vector>
#include <cstdio>
#include <climits>
#include <algorithm>
#define cind(x) scanf("%d", &x)
using namespace std;
const int NONE = 0;
const int MAXN = 100007;
class Node {
public:
int id[2]; // min max
int s[2]; // ls rs
int l, r;
int k;
Node() { }
} t[MAXN<<2];
int n;
vector<int> a;
int q[MAXN*10][3];
inline int get_mid(int l, int r) { return (l+r)>>1; }
void refresh(int x) {
for (int i=0; i<2; ++i)
t[x].id[i] = t[t[x].s[i]].id[i]==NONE ?
t[t[x].s[i^1]].id[i] : t[t[x].s[i]].id[i];
}
void build(int node, int l, int r) {
Node& n = t[node];
n.k = n.id[0] = n.id[1] = NONE;
n.l = l, n.r = r;
if (l!=r) {
n.s[0] = node<<1, n.s[1] = node<<1 | 1;
int mid = get_mid(l, r);
build(n.s[0], l, mid);
build(n.s[1], mid+1, r);
}
}
void insert(int node, int pos, int k) {
Node& n = t[node];
if (n.l==n.r) {
n.id[0] = n.id[1] = pos;
n.k = k;
} else {
insert(n.s[pos>get_mid(n.l, n.r)], pos, k);
refresh(node);
}
}
int query(int node, int flag) {
Node& n = t[node];
if (n.id[0]==NONE) return NONE;
int ret;
if (n.l==n.r) {
ret = n.k;
n.k = n.id[0] = n.id[1] = NONE;
} else {
if (t[n.s[flag]].id[flag] != NONE)
ret = query(n.s[flag], flag);
else
ret = query(n.s[!flag], flag);
refresh(node);
}
return ret;
}
int main() {
int x,y,qn = 0;
while (true) {
cind(q[qn][0]);
if (q[qn][0]==0) break;
if (q[qn][0]==1) {
cind(q[qn][1]), cind(q[qn][2]);
a.push_back(q[qn][2]);
}
++qn;
}
sort(a.begin(), a.end());
a.resize(unique(a.begin(), a.end()) - a.begin());
if (!a.empty()) build(1, 1, a.size());
for (int i=0; i<qn; ++i) {
if (q[i][0]==1) {
int k = q[i][1], prio = q[i][2];
int pos = lower_bound(a.begin(), a.end(), prio) - a.begin() + 1;
insert(1, pos, k);
} else {
int id = query(1, 1 ^ (q[i][0]-2));
printf("%d\n", id==NONE ? 0 : id);
}
}
return 0;
}