-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlus grande valeur sous borne dans tableau dynamique.cpp
75 lines (65 loc) · 1.61 KB
/
Plus grande valeur sous borne dans tableau dynamique.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
#include "bits/stdc++.h"
using namespace std;
int n, r;
vector<int> seg;
void init(int index = 1, int l = 0, int r = n - 1) {
if (l == r) {
seg[index] = 0;
} else {
int m = (l + r) / 2;
seg[index] = -1;
init(2 * index, l, m);
init(2 * index + 1, m + 1, r);
}
}
int update(int key, int value, int index = 1, int l = 0, int r = n - 1) {
if (key < l || key > r)
return l == r ? (seg[index] == 1 ? l : -1) : seg[index];
if (l == r) {
seg[index] = value;
return value == 1 ? key : -1;
} else {
int m = (l + r) / 2;
int left = update(key, value, 2 * index, l, m);
int right = update(key, value, 2 * index + 1, m + 1, r);
return seg[index] = right == -1 ? left : right;
}
}
int get(int index, int v = 1, int l = 0, int r = n - 1) {
if (l > index) return -1;
if (l == r) {
return seg[v] == 1 ? l : -1;
} else if (r <= index) {
return seg[v];
} else {
int m = (l + r) / 2;
int left = get(index, 2 * v, l, m);
int right = get(index, 2 * v + 1, m + 1, r);
return right == -1 ? left : right;
}
}
void print() {
for (int i = 1; i < 4 * n; i++) cout << seg[i] << " ";
cout << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
seg.resize(4 * n);
init();
cin >> r;
for (int i = 0; i < r; i++) {
char type;
cin >> type;
if (type == 'M') {
int key, value;
cin >> key >> value;
update(key, value);
} else {
int index;
cin >> index;
cout << get(index) << "\n";
}
}
}