#include <bits/stdc++.h>
#define x first
#define y second
using namespace std;
const int N = 1e6 + 5, oo = 1e9;
multiset < int > ms[2][N], L, R;
int tree[4 * N];
void update(int l, int r, int x, int val, int p) {
if(l > x || r < x) return;
if(l == r) {
tree[p] = val;
} else {
int m = l + r >> 1;
update(l, m, x, val, p << 1);
update(m + 1, r, x, val, p << 1 | 1);
tree[p] = min(tree[p << 1], tree[p << 1 | 1]);
}
}
int query(int l, int r, int L, int R, int p) {
if(L > r || l > R) return oo;
if(L <= l && R >= r) return tree[p];
int m = l + r >> 1;
return min(query(l, m, L, R, p << 1), query(m + 1, r, L, R, p << 1 | 1));
}
int main() {
for(int i = 0; i < 4 * N; ++i) tree[i] = oo;
int q;
cin >> q;
multiset < pair < int, int > > m;
for(int i = 1; i <= q; ++i) {
int l, r;
char t;
cin >> t >> l >> r;
if(t == 'R') {
m.erase(m.find({l, r}));
ms[0][l].erase(ms[0][l].find(r));
ms[1][r].erase(ms[1][r].find(l));
L.erase(L.find(l));
R.erase(R.find(r));
int val = oo;
if(!ms[0][l].empty()) {
val = *ms[0][l].begin();
}
update(1, N - 1, l, val, 1);
} else {
m.insert({l, r});
ms[0][l].insert(r);
ms[1][r].insert(l);
L.insert(l);
R.insert(r);
update(1, N - 1, l, *ms[0][l].begin(), 1);
}
int maxL = *L.rbegin(), minR = *R.begin();
// for(auto [l, r] : m) {
// maxL = max(maxL, l);
// minR = min(minR, r);
// }
if(minR <= maxL) {
int best = 1e9;
for(auto [l, r] : m) {
best = min(best, query(1, N - 1, r, N - 1, 1) - l);
}
cout << best << '\n';
} else {
int ansL = -1, ansR = 1e9;
ansR = *ms[0][maxL].begin();
ansL = *ms[1][minR].rbegin();
// for(auto [l, r] : m) {
// if(l == maxL) {
// ansR = min(ansR, r);
// }
// if(r == minR) {
// ansL = max(ansL, l);
// }
// }
cout << ansR - ansL << '\n';
}
}
}
Compilation message
Main.cpp: In function 'void update(int, int, int, int, int)':
Main.cpp:13:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
13 | int m = l + r >> 1;
| ~~^~~
Main.cpp: In function 'int query(int, int, int, int, int)':
Main.cpp:22:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
22 | int m = l + r >> 1;
| ~~^~~
Main.cpp: In function 'int main()':
Main.cpp:60:22: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
60 | for(auto [l, r] : m) {
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
101 ms |
109944 KB |
Output is correct |
2 |
Correct |
82 ms |
109852 KB |
Output is correct |
3 |
Correct |
77 ms |
109944 KB |
Output is correct |
4 |
Correct |
75 ms |
109944 KB |
Output is correct |
5 |
Correct |
100 ms |
109944 KB |
Output is correct |
6 |
Correct |
82 ms |
109944 KB |
Output is correct |
7 |
Correct |
78 ms |
109944 KB |
Output is correct |
8 |
Correct |
82 ms |
109944 KB |
Output is correct |
9 |
Correct |
112 ms |
109944 KB |
Output is correct |
10 |
Correct |
101 ms |
109948 KB |
Output is correct |
11 |
Correct |
97 ms |
110072 KB |
Output is correct |
12 |
Correct |
100 ms |
109944 KB |
Output is correct |
13 |
Correct |
112 ms |
109944 KB |
Output is correct |
14 |
Correct |
109 ms |
109948 KB |
Output is correct |
15 |
Correct |
110 ms |
110072 KB |
Output is correct |
16 |
Correct |
110 ms |
110072 KB |
Output is correct |
17 |
Correct |
99 ms |
109944 KB |
Output is correct |
18 |
Correct |
82 ms |
109944 KB |
Output is correct |
19 |
Correct |
77 ms |
109944 KB |
Output is correct |
20 |
Correct |
76 ms |
109944 KB |
Output is correct |
21 |
Correct |
100 ms |
109944 KB |
Output is correct |
22 |
Correct |
82 ms |
109944 KB |
Output is correct |
23 |
Correct |
76 ms |
109948 KB |
Output is correct |
24 |
Correct |
80 ms |
109944 KB |
Output is correct |
25 |
Correct |
107 ms |
109944 KB |
Output is correct |
26 |
Correct |
98 ms |
110072 KB |
Output is correct |
27 |
Correct |
94 ms |
109944 KB |
Output is correct |
28 |
Correct |
95 ms |
109992 KB |
Output is correct |
29 |
Correct |
109 ms |
110072 KB |
Output is correct |
30 |
Correct |
108 ms |
110072 KB |
Output is correct |
31 |
Correct |
110 ms |
109992 KB |
Output is correct |
32 |
Correct |
109 ms |
110072 KB |
Output is correct |
33 |
Correct |
75 ms |
109944 KB |
Output is correct |
34 |
Correct |
76 ms |
109944 KB |
Output is correct |
35 |
Correct |
76 ms |
109944 KB |
Output is correct |
36 |
Correct |
83 ms |
109948 KB |
Output is correct |
37 |
Correct |
84 ms |
109948 KB |
Output is correct |
38 |
Correct |
78 ms |
109944 KB |
Output is correct |
39 |
Correct |
75 ms |
109880 KB |
Output is correct |
40 |
Correct |
77 ms |
109944 KB |
Output is correct |
41 |
Correct |
94 ms |
109948 KB |
Output is correct |
42 |
Correct |
78 ms |
109944 KB |
Output is correct |
43 |
Correct |
76 ms |
109944 KB |
Output is correct |
44 |
Correct |
77 ms |
109908 KB |
Output is correct |
45 |
Correct |
95 ms |
109944 KB |
Output is correct |
46 |
Correct |
78 ms |
109944 KB |
Output is correct |
47 |
Correct |
76 ms |
109944 KB |
Output is correct |
48 |
Correct |
75 ms |
109944 KB |
Output is correct |
49 |
Correct |
94 ms |
109944 KB |
Output is correct |
50 |
Correct |
97 ms |
109944 KB |
Output is correct |
51 |
Correct |
95 ms |
109944 KB |
Output is correct |
52 |
Correct |
95 ms |
109984 KB |
Output is correct |
53 |
Correct |
94 ms |
109948 KB |
Output is correct |
54 |
Correct |
95 ms |
109944 KB |
Output is correct |
55 |
Correct |
94 ms |
109944 KB |
Output is correct |
56 |
Correct |
96 ms |
109944 KB |
Output is correct |
57 |
Correct |
94 ms |
109944 KB |
Output is correct |
58 |
Correct |
95 ms |
109944 KB |
Output is correct |
59 |
Correct |
108 ms |
110072 KB |
Output is correct |
60 |
Correct |
109 ms |
109944 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
101 ms |
109944 KB |
Output is correct |
2 |
Correct |
82 ms |
109852 KB |
Output is correct |
3 |
Correct |
77 ms |
109944 KB |
Output is correct |
4 |
Correct |
75 ms |
109944 KB |
Output is correct |
5 |
Correct |
100 ms |
109944 KB |
Output is correct |
6 |
Correct |
82 ms |
109944 KB |
Output is correct |
7 |
Correct |
78 ms |
109944 KB |
Output is correct |
8 |
Correct |
82 ms |
109944 KB |
Output is correct |
9 |
Correct |
112 ms |
109944 KB |
Output is correct |
10 |
Correct |
101 ms |
109948 KB |
Output is correct |
11 |
Correct |
97 ms |
110072 KB |
Output is correct |
12 |
Correct |
100 ms |
109944 KB |
Output is correct |
13 |
Correct |
112 ms |
109944 KB |
Output is correct |
14 |
Correct |
109 ms |
109948 KB |
Output is correct |
15 |
Correct |
110 ms |
110072 KB |
Output is correct |
16 |
Correct |
110 ms |
110072 KB |
Output is correct |
17 |
Correct |
99 ms |
109944 KB |
Output is correct |
18 |
Correct |
82 ms |
109944 KB |
Output is correct |
19 |
Correct |
77 ms |
109944 KB |
Output is correct |
20 |
Correct |
76 ms |
109944 KB |
Output is correct |
21 |
Correct |
100 ms |
109944 KB |
Output is correct |
22 |
Correct |
82 ms |
109944 KB |
Output is correct |
23 |
Correct |
76 ms |
109948 KB |
Output is correct |
24 |
Correct |
80 ms |
109944 KB |
Output is correct |
25 |
Correct |
107 ms |
109944 KB |
Output is correct |
26 |
Correct |
98 ms |
110072 KB |
Output is correct |
27 |
Correct |
94 ms |
109944 KB |
Output is correct |
28 |
Correct |
95 ms |
109992 KB |
Output is correct |
29 |
Correct |
109 ms |
110072 KB |
Output is correct |
30 |
Correct |
108 ms |
110072 KB |
Output is correct |
31 |
Correct |
110 ms |
109992 KB |
Output is correct |
32 |
Correct |
109 ms |
110072 KB |
Output is correct |
33 |
Correct |
75 ms |
109944 KB |
Output is correct |
34 |
Correct |
76 ms |
109944 KB |
Output is correct |
35 |
Correct |
76 ms |
109944 KB |
Output is correct |
36 |
Correct |
83 ms |
109948 KB |
Output is correct |
37 |
Correct |
84 ms |
109948 KB |
Output is correct |
38 |
Correct |
78 ms |
109944 KB |
Output is correct |
39 |
Correct |
75 ms |
109880 KB |
Output is correct |
40 |
Correct |
77 ms |
109944 KB |
Output is correct |
41 |
Correct |
94 ms |
109948 KB |
Output is correct |
42 |
Correct |
78 ms |
109944 KB |
Output is correct |
43 |
Correct |
76 ms |
109944 KB |
Output is correct |
44 |
Correct |
77 ms |
109908 KB |
Output is correct |
45 |
Correct |
95 ms |
109944 KB |
Output is correct |
46 |
Correct |
78 ms |
109944 KB |
Output is correct |
47 |
Correct |
76 ms |
109944 KB |
Output is correct |
48 |
Correct |
75 ms |
109944 KB |
Output is correct |
49 |
Correct |
94 ms |
109944 KB |
Output is correct |
50 |
Correct |
97 ms |
109944 KB |
Output is correct |
51 |
Correct |
95 ms |
109944 KB |
Output is correct |
52 |
Correct |
95 ms |
109984 KB |
Output is correct |
53 |
Correct |
94 ms |
109948 KB |
Output is correct |
54 |
Correct |
95 ms |
109944 KB |
Output is correct |
55 |
Correct |
94 ms |
109944 KB |
Output is correct |
56 |
Correct |
96 ms |
109944 KB |
Output is correct |
57 |
Correct |
94 ms |
109944 KB |
Output is correct |
58 |
Correct |
95 ms |
109944 KB |
Output is correct |
59 |
Correct |
108 ms |
110072 KB |
Output is correct |
60 |
Correct |
109 ms |
109944 KB |
Output is correct |
61 |
Correct |
74 ms |
109844 KB |
Output is correct |
62 |
Correct |
72 ms |
109944 KB |
Output is correct |
63 |
Correct |
74 ms |
109944 KB |
Output is correct |
64 |
Execution timed out |
7048 ms |
111692 KB |
Time limit exceeded |
65 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
101 ms |
109944 KB |
Output is correct |
2 |
Correct |
82 ms |
109852 KB |
Output is correct |
3 |
Correct |
77 ms |
109944 KB |
Output is correct |
4 |
Correct |
75 ms |
109944 KB |
Output is correct |
5 |
Correct |
100 ms |
109944 KB |
Output is correct |
6 |
Correct |
82 ms |
109944 KB |
Output is correct |
7 |
Correct |
78 ms |
109944 KB |
Output is correct |
8 |
Correct |
82 ms |
109944 KB |
Output is correct |
9 |
Correct |
112 ms |
109944 KB |
Output is correct |
10 |
Correct |
101 ms |
109948 KB |
Output is correct |
11 |
Correct |
97 ms |
110072 KB |
Output is correct |
12 |
Correct |
100 ms |
109944 KB |
Output is correct |
13 |
Correct |
112 ms |
109944 KB |
Output is correct |
14 |
Correct |
109 ms |
109948 KB |
Output is correct |
15 |
Correct |
110 ms |
110072 KB |
Output is correct |
16 |
Correct |
110 ms |
110072 KB |
Output is correct |
17 |
Correct |
99 ms |
109944 KB |
Output is correct |
18 |
Correct |
82 ms |
109944 KB |
Output is correct |
19 |
Correct |
77 ms |
109944 KB |
Output is correct |
20 |
Correct |
76 ms |
109944 KB |
Output is correct |
21 |
Correct |
100 ms |
109944 KB |
Output is correct |
22 |
Correct |
82 ms |
109944 KB |
Output is correct |
23 |
Correct |
76 ms |
109948 KB |
Output is correct |
24 |
Correct |
80 ms |
109944 KB |
Output is correct |
25 |
Correct |
107 ms |
109944 KB |
Output is correct |
26 |
Correct |
98 ms |
110072 KB |
Output is correct |
27 |
Correct |
94 ms |
109944 KB |
Output is correct |
28 |
Correct |
95 ms |
109992 KB |
Output is correct |
29 |
Correct |
109 ms |
110072 KB |
Output is correct |
30 |
Correct |
108 ms |
110072 KB |
Output is correct |
31 |
Correct |
110 ms |
109992 KB |
Output is correct |
32 |
Correct |
109 ms |
110072 KB |
Output is correct |
33 |
Correct |
75 ms |
109944 KB |
Output is correct |
34 |
Correct |
76 ms |
109944 KB |
Output is correct |
35 |
Correct |
76 ms |
109944 KB |
Output is correct |
36 |
Correct |
83 ms |
109948 KB |
Output is correct |
37 |
Correct |
84 ms |
109948 KB |
Output is correct |
38 |
Correct |
78 ms |
109944 KB |
Output is correct |
39 |
Correct |
75 ms |
109880 KB |
Output is correct |
40 |
Correct |
77 ms |
109944 KB |
Output is correct |
41 |
Correct |
94 ms |
109948 KB |
Output is correct |
42 |
Correct |
78 ms |
109944 KB |
Output is correct |
43 |
Correct |
76 ms |
109944 KB |
Output is correct |
44 |
Correct |
77 ms |
109908 KB |
Output is correct |
45 |
Correct |
95 ms |
109944 KB |
Output is correct |
46 |
Correct |
78 ms |
109944 KB |
Output is correct |
47 |
Correct |
76 ms |
109944 KB |
Output is correct |
48 |
Correct |
75 ms |
109944 KB |
Output is correct |
49 |
Correct |
94 ms |
109944 KB |
Output is correct |
50 |
Correct |
97 ms |
109944 KB |
Output is correct |
51 |
Correct |
95 ms |
109944 KB |
Output is correct |
52 |
Correct |
95 ms |
109984 KB |
Output is correct |
53 |
Correct |
94 ms |
109948 KB |
Output is correct |
54 |
Correct |
95 ms |
109944 KB |
Output is correct |
55 |
Correct |
94 ms |
109944 KB |
Output is correct |
56 |
Correct |
96 ms |
109944 KB |
Output is correct |
57 |
Correct |
94 ms |
109944 KB |
Output is correct |
58 |
Correct |
95 ms |
109944 KB |
Output is correct |
59 |
Correct |
108 ms |
110072 KB |
Output is correct |
60 |
Correct |
109 ms |
109944 KB |
Output is correct |
61 |
Correct |
74 ms |
109844 KB |
Output is correct |
62 |
Correct |
72 ms |
109944 KB |
Output is correct |
63 |
Correct |
74 ms |
109944 KB |
Output is correct |
64 |
Execution timed out |
7048 ms |
111692 KB |
Time limit exceeded |
65 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
7059 ms |
111968 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
101 ms |
109944 KB |
Output is correct |
2 |
Correct |
82 ms |
109852 KB |
Output is correct |
3 |
Correct |
77 ms |
109944 KB |
Output is correct |
4 |
Correct |
75 ms |
109944 KB |
Output is correct |
5 |
Correct |
100 ms |
109944 KB |
Output is correct |
6 |
Correct |
82 ms |
109944 KB |
Output is correct |
7 |
Correct |
78 ms |
109944 KB |
Output is correct |
8 |
Correct |
82 ms |
109944 KB |
Output is correct |
9 |
Correct |
112 ms |
109944 KB |
Output is correct |
10 |
Correct |
101 ms |
109948 KB |
Output is correct |
11 |
Correct |
97 ms |
110072 KB |
Output is correct |
12 |
Correct |
100 ms |
109944 KB |
Output is correct |
13 |
Correct |
112 ms |
109944 KB |
Output is correct |
14 |
Correct |
109 ms |
109948 KB |
Output is correct |
15 |
Correct |
110 ms |
110072 KB |
Output is correct |
16 |
Correct |
110 ms |
110072 KB |
Output is correct |
17 |
Correct |
99 ms |
109944 KB |
Output is correct |
18 |
Correct |
82 ms |
109944 KB |
Output is correct |
19 |
Correct |
77 ms |
109944 KB |
Output is correct |
20 |
Correct |
76 ms |
109944 KB |
Output is correct |
21 |
Correct |
100 ms |
109944 KB |
Output is correct |
22 |
Correct |
82 ms |
109944 KB |
Output is correct |
23 |
Correct |
76 ms |
109948 KB |
Output is correct |
24 |
Correct |
80 ms |
109944 KB |
Output is correct |
25 |
Correct |
107 ms |
109944 KB |
Output is correct |
26 |
Correct |
98 ms |
110072 KB |
Output is correct |
27 |
Correct |
94 ms |
109944 KB |
Output is correct |
28 |
Correct |
95 ms |
109992 KB |
Output is correct |
29 |
Correct |
109 ms |
110072 KB |
Output is correct |
30 |
Correct |
108 ms |
110072 KB |
Output is correct |
31 |
Correct |
110 ms |
109992 KB |
Output is correct |
32 |
Correct |
109 ms |
110072 KB |
Output is correct |
33 |
Correct |
75 ms |
109944 KB |
Output is correct |
34 |
Correct |
76 ms |
109944 KB |
Output is correct |
35 |
Correct |
76 ms |
109944 KB |
Output is correct |
36 |
Correct |
83 ms |
109948 KB |
Output is correct |
37 |
Correct |
84 ms |
109948 KB |
Output is correct |
38 |
Correct |
78 ms |
109944 KB |
Output is correct |
39 |
Correct |
75 ms |
109880 KB |
Output is correct |
40 |
Correct |
77 ms |
109944 KB |
Output is correct |
41 |
Correct |
94 ms |
109948 KB |
Output is correct |
42 |
Correct |
78 ms |
109944 KB |
Output is correct |
43 |
Correct |
76 ms |
109944 KB |
Output is correct |
44 |
Correct |
77 ms |
109908 KB |
Output is correct |
45 |
Correct |
95 ms |
109944 KB |
Output is correct |
46 |
Correct |
78 ms |
109944 KB |
Output is correct |
47 |
Correct |
76 ms |
109944 KB |
Output is correct |
48 |
Correct |
75 ms |
109944 KB |
Output is correct |
49 |
Correct |
94 ms |
109944 KB |
Output is correct |
50 |
Correct |
97 ms |
109944 KB |
Output is correct |
51 |
Correct |
95 ms |
109944 KB |
Output is correct |
52 |
Correct |
95 ms |
109984 KB |
Output is correct |
53 |
Correct |
94 ms |
109948 KB |
Output is correct |
54 |
Correct |
95 ms |
109944 KB |
Output is correct |
55 |
Correct |
94 ms |
109944 KB |
Output is correct |
56 |
Correct |
96 ms |
109944 KB |
Output is correct |
57 |
Correct |
94 ms |
109944 KB |
Output is correct |
58 |
Correct |
95 ms |
109944 KB |
Output is correct |
59 |
Correct |
108 ms |
110072 KB |
Output is correct |
60 |
Correct |
109 ms |
109944 KB |
Output is correct |
61 |
Correct |
74 ms |
109844 KB |
Output is correct |
62 |
Correct |
72 ms |
109944 KB |
Output is correct |
63 |
Correct |
74 ms |
109944 KB |
Output is correct |
64 |
Execution timed out |
7048 ms |
111692 KB |
Time limit exceeded |
65 |
Halted |
0 ms |
0 KB |
- |