#include <bits/stdc++.h>
#define all(x) (x).begin(),(x).end()
using namespace std;
using ll = long long;
using ld = long double;
//#define int ll
#define sz(x) ((int)(x).size())
using pii = pair<int,int>;
using tii = tuple<int,int,int>;
const int inf = 1e9 + 9;
const int nmax = 1e6 + 1;
template<typename T>
struct AINT {
vector<T> aint;
int n;
void init(int n_) {
n = n_;
aint.resize(n * 2 + 5, T());
}
template<class CB> void walk(CB&& cb) { walk(cb, 1, n); }
template<class CB> void walk(CB&& cb, int l, int r) { walk(cb, l, r, 1, 1, n); }
template<class CB> void walk(CB&& cb, int l, int r, int node, int cl, int cr) {
if(cr < l || r < cl) return;
if(l <= cl && cr <= r && !cb(aint[node], cl, cr)) return;
int mid = (cl + cr) >> 1, L = node + 1, R = node + (mid - cl + 1) * 2;
walk(cb, l, r, L, cl, mid);
walk(cb, l, r, R, mid + 1, cr);
aint[node].pull(aint[L], aint[R]);
}
};
namespace DS {
auto byL = [](pii a, pii b) { return a.first < b.first || (a.first == b.first && a.second > b.second); };
auto byR = [](pii a, pii b) { return a.second < b.second || (a.second == b.second && a.first > b.first); };
multiset<pii, decltype(byL)> sortL(byL);
multiset<pii, decltype(byR)> sortR(byR);
struct segm {
int l, r;
segm(): l(-inf * 2), r(inf * 2) {;}
void pull(const segm& a, const segm& b) { l = max(a.l, b.l); r = min(b.r, a.r); }
};
AINT<segm> aint;
vector<int> ans;
vector<pair<int, segm>> rollback;
vector<pii> efectiv;
void insert(int l, int r) {
efectiv.emplace_back(l, r);
efectiv.emplace_back(-1, -1);
sortL.insert(pii{l, r});
sortR.insert(pii{l, r});
ans.push_back(ans.empty()? inf : ans.back());
aint.walk([&](segm& val, int cl, int cr) {
//cerr << cl << ' ' << cr << '\t' << val.l << ' ' << val.r << '\t' << l << ' ' << r << '\n';
ans.back() = min(ans.back(), r - val.l); return 0;
}, 1, l);
aint.walk([&](segm& val, int cl, int cr) {
ans.back() = min(ans.back(), val.r - l); return 0;
}, r, nmax);
aint.walk([&](segm& val, int cl, int cr) {
rollback.emplace_back(cl, val);
val.r = min(val.r, r);
return 0;
}, l, l);
aint.walk([&](segm& val, int cl, int cr) {
rollback.emplace_back(cl, val);
ans.emplace_back(ans.back());
val.l = max(val.l, l);
return 0;
}, r, r);
assert(ans.size() == rollback.size());
}
int time() { return sz(rollback); }
void pop() {
auto [l, r] = efectiv.back();
if(l != -1 && r != -1) sortL.erase(sortL.find(pii{l, r})), sortR.erase(sortR.find(pii{l, r}));
aint.walk([&](segm& val, int cl, int cr) {
val = rollback.back().second;
rollback.pop_back();
ans.pop_back();
return 0;
}, rollback.back().first, rollback.back().first);
}
int query() {
//for(auto [a, b] : sortL) cerr << a << ' ' << b << '\t';
//cerr << "--\n";
if(ans.empty() || ans.back() >= nmax + 5) {
auto a = *sortL.rbegin(), b = *sortR.begin();
return max(b.second, a.second) - min(b.first, a.first);
}
return ans.back();
}
}
int started = 0;
struct memory {
vector<pii> values;
int enter_time;
void pull(const memory& a, const memory& b) {
if(!started) return;
while(DS::time() > enter_time) DS::pop();
}
};
signed main() {
cin.tie(0) -> sync_with_stdio(0);
int n;
cin >> n;
map<pii, vector<int>> still;
vector<pii> segms;
vector<pii> times;
for(int i = 1; i <= n; i++) {
char ch;
cin >> ch;
if(ch == 'A') {
int l, r;
cin >> l >> r;
segms.emplace_back(l, r);
times.emplace_back(i, n);
still[pii{l, r}].emplace_back(sz(segms) - 1);
}
else {
int l, r;
cin >> l >> r;
auto t = still[pii{l, r}].back();
still[pii{l, r}].pop_back();
segms[t] = pii{-1, -1};
}
int mn = inf, mn2 = inf;
for(auto [a, b] : segms) {
if(a == -1) continue;
for(auto [c, d] : segms) {
if(c == -1) continue;
int diff = max(0, min(b, d) - max(a, c)), supra = max(b, d) - min(a, c);
//cerr << diff << ' ' <<
if(diff < mn) mn2 = inf, mn = diff;
if(diff == mn) mn2 = min(mn2, supra);
}
}
cout << mn2 << '\n';
}
return 0;
}
/**
Istenem! Nu poate fi real.
-- Surse verificate
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
348 KB |
Output is correct |
2 |
Correct |
6 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
47 ms |
348 KB |
Output is correct |
6 |
Correct |
6 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
4 ms |
348 KB |
Output is correct |
9 |
Correct |
71 ms |
516 KB |
Output is correct |
10 |
Correct |
40 ms |
512 KB |
Output is correct |
11 |
Correct |
35 ms |
348 KB |
Output is correct |
12 |
Correct |
37 ms |
348 KB |
Output is correct |
13 |
Correct |
96 ms |
500 KB |
Output is correct |
14 |
Correct |
102 ms |
504 KB |
Output is correct |
15 |
Correct |
86 ms |
348 KB |
Output is correct |
16 |
Correct |
99 ms |
744 KB |
Output is correct |
17 |
Correct |
51 ms |
508 KB |
Output is correct |
18 |
Correct |
6 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
51 ms |
496 KB |
Output is correct |
22 |
Correct |
7 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
79 ms |
864 KB |
Output is correct |
26 |
Correct |
42 ms |
496 KB |
Output is correct |
27 |
Correct |
35 ms |
344 KB |
Output is correct |
28 |
Correct |
34 ms |
348 KB |
Output is correct |
29 |
Correct |
101 ms |
516 KB |
Output is correct |
30 |
Correct |
103 ms |
512 KB |
Output is correct |
31 |
Correct |
95 ms |
348 KB |
Output is correct |
32 |
Correct |
108 ms |
500 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
2 ms |
348 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
408 KB |
Output is correct |
41 |
Correct |
30 ms |
344 KB |
Output is correct |
42 |
Correct |
3 ms |
348 KB |
Output is correct |
43 |
Correct |
1 ms |
504 KB |
Output is correct |
44 |
Correct |
1 ms |
348 KB |
Output is correct |
45 |
Correct |
24 ms |
504 KB |
Output is correct |
46 |
Correct |
2 ms |
348 KB |
Output is correct |
47 |
Correct |
1 ms |
348 KB |
Output is correct |
48 |
Correct |
1 ms |
348 KB |
Output is correct |
49 |
Correct |
35 ms |
496 KB |
Output is correct |
50 |
Correct |
38 ms |
344 KB |
Output is correct |
51 |
Correct |
36 ms |
344 KB |
Output is correct |
52 |
Correct |
36 ms |
488 KB |
Output is correct |
53 |
Correct |
35 ms |
344 KB |
Output is correct |
54 |
Correct |
36 ms |
344 KB |
Output is correct |
55 |
Correct |
35 ms |
348 KB |
Output is correct |
56 |
Correct |
40 ms |
344 KB |
Output is correct |
57 |
Correct |
35 ms |
348 KB |
Output is correct |
58 |
Correct |
37 ms |
348 KB |
Output is correct |
59 |
Correct |
91 ms |
348 KB |
Output is correct |
60 |
Correct |
103 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
348 KB |
Output is correct |
2 |
Correct |
6 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
47 ms |
348 KB |
Output is correct |
6 |
Correct |
6 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
4 ms |
348 KB |
Output is correct |
9 |
Correct |
71 ms |
516 KB |
Output is correct |
10 |
Correct |
40 ms |
512 KB |
Output is correct |
11 |
Correct |
35 ms |
348 KB |
Output is correct |
12 |
Correct |
37 ms |
348 KB |
Output is correct |
13 |
Correct |
96 ms |
500 KB |
Output is correct |
14 |
Correct |
102 ms |
504 KB |
Output is correct |
15 |
Correct |
86 ms |
348 KB |
Output is correct |
16 |
Correct |
99 ms |
744 KB |
Output is correct |
17 |
Correct |
51 ms |
508 KB |
Output is correct |
18 |
Correct |
6 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
51 ms |
496 KB |
Output is correct |
22 |
Correct |
7 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
79 ms |
864 KB |
Output is correct |
26 |
Correct |
42 ms |
496 KB |
Output is correct |
27 |
Correct |
35 ms |
344 KB |
Output is correct |
28 |
Correct |
34 ms |
348 KB |
Output is correct |
29 |
Correct |
101 ms |
516 KB |
Output is correct |
30 |
Correct |
103 ms |
512 KB |
Output is correct |
31 |
Correct |
95 ms |
348 KB |
Output is correct |
32 |
Correct |
108 ms |
500 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
2 ms |
348 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
408 KB |
Output is correct |
41 |
Correct |
30 ms |
344 KB |
Output is correct |
42 |
Correct |
3 ms |
348 KB |
Output is correct |
43 |
Correct |
1 ms |
504 KB |
Output is correct |
44 |
Correct |
1 ms |
348 KB |
Output is correct |
45 |
Correct |
24 ms |
504 KB |
Output is correct |
46 |
Correct |
2 ms |
348 KB |
Output is correct |
47 |
Correct |
1 ms |
348 KB |
Output is correct |
48 |
Correct |
1 ms |
348 KB |
Output is correct |
49 |
Correct |
35 ms |
496 KB |
Output is correct |
50 |
Correct |
38 ms |
344 KB |
Output is correct |
51 |
Correct |
36 ms |
344 KB |
Output is correct |
52 |
Correct |
36 ms |
488 KB |
Output is correct |
53 |
Correct |
35 ms |
344 KB |
Output is correct |
54 |
Correct |
36 ms |
344 KB |
Output is correct |
55 |
Correct |
35 ms |
348 KB |
Output is correct |
56 |
Correct |
40 ms |
344 KB |
Output is correct |
57 |
Correct |
35 ms |
348 KB |
Output is correct |
58 |
Correct |
37 ms |
348 KB |
Output is correct |
59 |
Correct |
91 ms |
348 KB |
Output is correct |
60 |
Correct |
103 ms |
344 KB |
Output is correct |
61 |
Correct |
1 ms |
348 KB |
Output is correct |
62 |
Correct |
0 ms |
348 KB |
Output is correct |
63 |
Correct |
0 ms |
348 KB |
Output is correct |
64 |
Execution timed out |
7052 ms |
968 KB |
Time limit exceeded |
65 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
348 KB |
Output is correct |
2 |
Correct |
6 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
47 ms |
348 KB |
Output is correct |
6 |
Correct |
6 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
4 ms |
348 KB |
Output is correct |
9 |
Correct |
71 ms |
516 KB |
Output is correct |
10 |
Correct |
40 ms |
512 KB |
Output is correct |
11 |
Correct |
35 ms |
348 KB |
Output is correct |
12 |
Correct |
37 ms |
348 KB |
Output is correct |
13 |
Correct |
96 ms |
500 KB |
Output is correct |
14 |
Correct |
102 ms |
504 KB |
Output is correct |
15 |
Correct |
86 ms |
348 KB |
Output is correct |
16 |
Correct |
99 ms |
744 KB |
Output is correct |
17 |
Correct |
51 ms |
508 KB |
Output is correct |
18 |
Correct |
6 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
51 ms |
496 KB |
Output is correct |
22 |
Correct |
7 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
79 ms |
864 KB |
Output is correct |
26 |
Correct |
42 ms |
496 KB |
Output is correct |
27 |
Correct |
35 ms |
344 KB |
Output is correct |
28 |
Correct |
34 ms |
348 KB |
Output is correct |
29 |
Correct |
101 ms |
516 KB |
Output is correct |
30 |
Correct |
103 ms |
512 KB |
Output is correct |
31 |
Correct |
95 ms |
348 KB |
Output is correct |
32 |
Correct |
108 ms |
500 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
2 ms |
348 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
408 KB |
Output is correct |
41 |
Correct |
30 ms |
344 KB |
Output is correct |
42 |
Correct |
3 ms |
348 KB |
Output is correct |
43 |
Correct |
1 ms |
504 KB |
Output is correct |
44 |
Correct |
1 ms |
348 KB |
Output is correct |
45 |
Correct |
24 ms |
504 KB |
Output is correct |
46 |
Correct |
2 ms |
348 KB |
Output is correct |
47 |
Correct |
1 ms |
348 KB |
Output is correct |
48 |
Correct |
1 ms |
348 KB |
Output is correct |
49 |
Correct |
35 ms |
496 KB |
Output is correct |
50 |
Correct |
38 ms |
344 KB |
Output is correct |
51 |
Correct |
36 ms |
344 KB |
Output is correct |
52 |
Correct |
36 ms |
488 KB |
Output is correct |
53 |
Correct |
35 ms |
344 KB |
Output is correct |
54 |
Correct |
36 ms |
344 KB |
Output is correct |
55 |
Correct |
35 ms |
348 KB |
Output is correct |
56 |
Correct |
40 ms |
344 KB |
Output is correct |
57 |
Correct |
35 ms |
348 KB |
Output is correct |
58 |
Correct |
37 ms |
348 KB |
Output is correct |
59 |
Correct |
91 ms |
348 KB |
Output is correct |
60 |
Correct |
103 ms |
344 KB |
Output is correct |
61 |
Correct |
1 ms |
348 KB |
Output is correct |
62 |
Correct |
0 ms |
348 KB |
Output is correct |
63 |
Correct |
0 ms |
348 KB |
Output is correct |
64 |
Execution timed out |
7052 ms |
968 KB |
Time limit exceeded |
65 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
7098 ms |
1108 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
348 KB |
Output is correct |
2 |
Correct |
6 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
47 ms |
348 KB |
Output is correct |
6 |
Correct |
6 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
4 ms |
348 KB |
Output is correct |
9 |
Correct |
71 ms |
516 KB |
Output is correct |
10 |
Correct |
40 ms |
512 KB |
Output is correct |
11 |
Correct |
35 ms |
348 KB |
Output is correct |
12 |
Correct |
37 ms |
348 KB |
Output is correct |
13 |
Correct |
96 ms |
500 KB |
Output is correct |
14 |
Correct |
102 ms |
504 KB |
Output is correct |
15 |
Correct |
86 ms |
348 KB |
Output is correct |
16 |
Correct |
99 ms |
744 KB |
Output is correct |
17 |
Correct |
51 ms |
508 KB |
Output is correct |
18 |
Correct |
6 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
51 ms |
496 KB |
Output is correct |
22 |
Correct |
7 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
79 ms |
864 KB |
Output is correct |
26 |
Correct |
42 ms |
496 KB |
Output is correct |
27 |
Correct |
35 ms |
344 KB |
Output is correct |
28 |
Correct |
34 ms |
348 KB |
Output is correct |
29 |
Correct |
101 ms |
516 KB |
Output is correct |
30 |
Correct |
103 ms |
512 KB |
Output is correct |
31 |
Correct |
95 ms |
348 KB |
Output is correct |
32 |
Correct |
108 ms |
500 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
2 ms |
348 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
408 KB |
Output is correct |
41 |
Correct |
30 ms |
344 KB |
Output is correct |
42 |
Correct |
3 ms |
348 KB |
Output is correct |
43 |
Correct |
1 ms |
504 KB |
Output is correct |
44 |
Correct |
1 ms |
348 KB |
Output is correct |
45 |
Correct |
24 ms |
504 KB |
Output is correct |
46 |
Correct |
2 ms |
348 KB |
Output is correct |
47 |
Correct |
1 ms |
348 KB |
Output is correct |
48 |
Correct |
1 ms |
348 KB |
Output is correct |
49 |
Correct |
35 ms |
496 KB |
Output is correct |
50 |
Correct |
38 ms |
344 KB |
Output is correct |
51 |
Correct |
36 ms |
344 KB |
Output is correct |
52 |
Correct |
36 ms |
488 KB |
Output is correct |
53 |
Correct |
35 ms |
344 KB |
Output is correct |
54 |
Correct |
36 ms |
344 KB |
Output is correct |
55 |
Correct |
35 ms |
348 KB |
Output is correct |
56 |
Correct |
40 ms |
344 KB |
Output is correct |
57 |
Correct |
35 ms |
348 KB |
Output is correct |
58 |
Correct |
37 ms |
348 KB |
Output is correct |
59 |
Correct |
91 ms |
348 KB |
Output is correct |
60 |
Correct |
103 ms |
344 KB |
Output is correct |
61 |
Correct |
1 ms |
348 KB |
Output is correct |
62 |
Correct |
0 ms |
348 KB |
Output is correct |
63 |
Correct |
0 ms |
348 KB |
Output is correct |
64 |
Execution timed out |
7052 ms |
968 KB |
Time limit exceeded |
65 |
Halted |
0 ms |
0 KB |
- |