#include <bits/stdc++.h>
#define ll long long
#define all(aaa) aaa.begin(), aaa.end()
using namespace std;
struct E {
int p, t, x, y;
};
struct U {
int l, r;
pair<int, int> p;
};
const int N = 3e5 + 5, INF = 1e9, MAX = 1e8;
int n, q, k, cq, cc;
int ans[N];
pair<int, int> lt[N], tmp[N];
unordered_map<int, vector<int>> mp[2][N];
vector<int> vx;
multiset<int> occ[N];
vector<pair<int, int>> t[2][N * 4];
vector<U> updates[2];
void upd(int type, int l, int r, pair<int, int> p, int v, int L, int R) {
if (l > r)
return;
if (L == l && R == r) {
t[type][v].push_back(p);
}
else {
int m = (L + R) >> 1;
upd(type, l, min(m, r), p, v * 2, L, m);
upd(type, max(m + 1, l), r, p, v * 2 + 1, m + 1, R);
}
}
void startSeg(int type, int x, int y) {
mp[type][x][y].push_back(cq);
}
void endSeg(int type, int x, int y) {
updates[type].push_back({mp[type][x][y].back(), cq - 1, {x, y}});
mp[type][x][y].pop_back();
}
void updBeg(int x, bool add) {
if (add)
startSeg(0, 0, x);
else
endSeg(0, 0, x);
}
void updBetween(int x, int y, bool add) {
if (x == y)
return;
int m = (x + y) / 2;
if ((x + y) % 2)
m++;
// cout << "BET " << x << " " << y << " " << add << "\n";
int m1 = lower_bound(all(vx), m) - vx.begin();
if (add) {
startSeg(0, m1, y);
startSeg(1, cc - m1, INF - x);
}
else {
endSeg(0, m1, y);
endSeg(1, cc - m1, INF - x);
}
}
void updEnd(int x, bool add) {
if (add)
startSeg(1, 0, INF - x);
else
endSeg(1, 0, INF - x);
}
void calcAns(int v, int L, int R) {
if (L == R) {
tmp[0] = lt[L];
}
else {
int m = (L + R) >> 1;
calcAns(v * 2, L, m);
calcAns(v * 2 + 1, m + 1, R);
int x = L, y = m + 1, z = 0;
while (x <= m || y <= R) {
if (x <= m && (y == R + 1 || lt[x].first < lt[y].first))
tmp[z] = lt[x++];
else
tmp[z] = lt[y++];
z++;
}
for (int i = L; i <= R; i++)
lt[i] = tmp[i - L];
}
int i = 0, mx = -INF;
for (int j = 0; j <= R - L; j++) {
while (i < t[0][v].size() && t[0][v][i].first <= tmp[j].first) {
mx = max(mx, t[0][v][i].second);
i++;
}
ans[tmp[j].second] = max(ans[tmp[j].second], mx - vx[tmp[j].first]);
}
reverse(tmp, tmp + R - L + 1);
for (int j = 0; j <= R - L; j++)
tmp[j].first = cc - 1 - tmp[j].first;
// for (int j = 0; j <= R - L; j++) {
// cout << tmp[j].first << " " << tmp[j].second << "\n";
// }
// for (auto p : t[1][v]) {
// cout << p.first << " " << p.second << "\n";
// }
// cout << ans[0] << "\n";
i = 0, mx = -INF;
for (int j = 0; j <= R - L; j++) {
while (i < t[1][v].size() && t[1][v][i].first <= tmp[j].first) {
mx = max(mx, t[1][v][i].second);
i++;
}
ans[tmp[j].second] = max(ans[tmp[j].second], mx - INF + vx[cc - 1 - tmp[j].first]);
}
}
void calcUpdates(vector<E> &evs) {
for (E &e : evs) {
if (e.t == 2)
vx.push_back(e.x);
}
sort(all(vx));
vx.erase(unique(all(vx)), vx.end());
cc = vx.size();
cq = 0;
for (E &e : evs) {
// cout << e.p << " " << e.t << " " << e.x << " " << e.y << "\n";
if (e.t == 0) {
auto it = occ[e.y].upper_bound(e.x);
int pr = (it == occ[e.y].begin() ? -1 : *prev(it)),
cur = (it == occ[e.y].end() ? -1 : *it);
if (!occ[e.y].empty()) {
if (pr != -1 && cur != -1) {
updBetween(pr, cur, false);
}
if (pr == -1) {
updBeg(cur, false);
}
if (cur == -1) {
updEnd(pr, false);
}
}
occ[e.y].insert(e.x);
if (pr == -1)
updBeg(e.x, true);
else
updBetween(pr, e.x, true);
if (cur != -1)
updBetween(e.x, cur, true);
else
updEnd(e.x, true);
}
else if (e.t == 1) {
auto it = occ[e.y].find(e.x);
if (it == occ[e.y].begin())
updBeg(*it, false);
else
updBetween(*prev(it), *it, false);
if (next(it) != occ[e.y].end())
updBetween(*it, *next(it), false);
else
updEnd(e.x, false);
if (it != occ[e.y].begin() && next(it) != occ[e.y].end())
updBetween(*prev(it), *next(it), true);
if (it == occ[e.y].begin() && occ[e.y].size() > 1)
updBeg(*next(it), true);
if (next(it) == occ[e.y].end() &&
occ[e.y].size() > 1)
updEnd(*prev(it), true);
occ[e.y].erase(it);
}
else {
lt[cq++] = {lower_bound(all(vx), e.x) - vx.begin(), e.y};
}
}
for (int i = 0; i < 2; i++) {
sort(all(updates[i]), [](const U &a, const U &b) {
return a.p.first < b.p.first;
});
}
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cin >> n >> k >> q;
vector<E> evs;
for (int i = 0; i < n; i++) {
int x, t, a, b;
cin >> x >> t >> a >> b;
t--;
evs.push_back({a, 0, x, t});
evs.push_back({b + 1, 1, x, t});
}
for (int i = 0; i < k; i++) {
evs.push_back({1, 0, INF, i});
evs.push_back({MAX, 1, INF, i});
}
for (int i = 0; i < q; i++) {
int x, y;
cin >> x >> y;
evs.push_back({y, 2, x, i});
}
sort(all(evs), [](const E &a, const E &b) {
return a.p == b.p ? a.t < b.t : a.p < b.p;
});
calcUpdates(evs);
for (int i = 0; i < 2; i++) {
for (U &u : updates[i]) {
upd(i, u.l, u.r, u.p, 1, 0, q - 1);
}
}
calcAns(1, 0, q - 1);
for (int i = 0; i < q; i++) {
if (ans[i] < INF / 2) {
cout << ans[i] << "\n";
}
else {
cout << "-1\n";
}
}
return 0;
}
Compilation message
new_home.cpp: In function 'void calcAns(int, int, int)':
new_home.cpp:107:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while (i < t[0][v].size() && t[0][v][i].first <= tmp[j].first) {
~~^~~~~~~~~~~~~~~~
new_home.cpp:131:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while (i < t[1][v].size() && t[1][v][i].first <= tmp[j].first) {
~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
99 ms |
103672 KB |
Output is correct |
2 |
Correct |
125 ms |
103672 KB |
Output is correct |
3 |
Correct |
103 ms |
103672 KB |
Output is correct |
4 |
Correct |
105 ms |
103800 KB |
Output is correct |
5 |
Correct |
116 ms |
103800 KB |
Output is correct |
6 |
Correct |
110 ms |
104060 KB |
Output is correct |
7 |
Correct |
100 ms |
104056 KB |
Output is correct |
8 |
Correct |
105 ms |
104024 KB |
Output is correct |
9 |
Correct |
106 ms |
104084 KB |
Output is correct |
10 |
Correct |
109 ms |
104172 KB |
Output is correct |
11 |
Correct |
102 ms |
103928 KB |
Output is correct |
12 |
Correct |
102 ms |
104056 KB |
Output is correct |
13 |
Correct |
103 ms |
103928 KB |
Output is correct |
14 |
Correct |
105 ms |
104184 KB |
Output is correct |
15 |
Correct |
102 ms |
104056 KB |
Output is correct |
16 |
Correct |
107 ms |
104236 KB |
Output is correct |
17 |
Correct |
111 ms |
104028 KB |
Output is correct |
18 |
Correct |
111 ms |
104056 KB |
Output is correct |
19 |
Correct |
103 ms |
104132 KB |
Output is correct |
20 |
Correct |
102 ms |
104056 KB |
Output is correct |
21 |
Correct |
113 ms |
103892 KB |
Output is correct |
22 |
Correct |
104 ms |
104056 KB |
Output is correct |
23 |
Correct |
106 ms |
104016 KB |
Output is correct |
24 |
Correct |
102 ms |
104056 KB |
Output is correct |
25 |
Correct |
103 ms |
104124 KB |
Output is correct |
26 |
Correct |
103 ms |
104056 KB |
Output is correct |
27 |
Correct |
104 ms |
104056 KB |
Output is correct |
28 |
Correct |
101 ms |
104056 KB |
Output is correct |
29 |
Correct |
104 ms |
104056 KB |
Output is correct |
30 |
Correct |
107 ms |
103944 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
99 ms |
103672 KB |
Output is correct |
2 |
Correct |
125 ms |
103672 KB |
Output is correct |
3 |
Correct |
103 ms |
103672 KB |
Output is correct |
4 |
Correct |
105 ms |
103800 KB |
Output is correct |
5 |
Correct |
116 ms |
103800 KB |
Output is correct |
6 |
Correct |
110 ms |
104060 KB |
Output is correct |
7 |
Correct |
100 ms |
104056 KB |
Output is correct |
8 |
Correct |
105 ms |
104024 KB |
Output is correct |
9 |
Correct |
106 ms |
104084 KB |
Output is correct |
10 |
Correct |
109 ms |
104172 KB |
Output is correct |
11 |
Correct |
102 ms |
103928 KB |
Output is correct |
12 |
Correct |
102 ms |
104056 KB |
Output is correct |
13 |
Correct |
103 ms |
103928 KB |
Output is correct |
14 |
Correct |
105 ms |
104184 KB |
Output is correct |
15 |
Correct |
102 ms |
104056 KB |
Output is correct |
16 |
Correct |
107 ms |
104236 KB |
Output is correct |
17 |
Correct |
111 ms |
104028 KB |
Output is correct |
18 |
Correct |
111 ms |
104056 KB |
Output is correct |
19 |
Correct |
103 ms |
104132 KB |
Output is correct |
20 |
Correct |
102 ms |
104056 KB |
Output is correct |
21 |
Correct |
113 ms |
103892 KB |
Output is correct |
22 |
Correct |
104 ms |
104056 KB |
Output is correct |
23 |
Correct |
106 ms |
104016 KB |
Output is correct |
24 |
Correct |
102 ms |
104056 KB |
Output is correct |
25 |
Correct |
103 ms |
104124 KB |
Output is correct |
26 |
Correct |
103 ms |
104056 KB |
Output is correct |
27 |
Correct |
104 ms |
104056 KB |
Output is correct |
28 |
Correct |
101 ms |
104056 KB |
Output is correct |
29 |
Correct |
104 ms |
104056 KB |
Output is correct |
30 |
Correct |
107 ms |
103944 KB |
Output is correct |
31 |
Correct |
1518 ms |
194532 KB |
Output is correct |
32 |
Correct |
273 ms |
117032 KB |
Output is correct |
33 |
Correct |
1316 ms |
191968 KB |
Output is correct |
34 |
Correct |
1297 ms |
191680 KB |
Output is correct |
35 |
Correct |
1496 ms |
195308 KB |
Output is correct |
36 |
Correct |
1322 ms |
194016 KB |
Output is correct |
37 |
Correct |
949 ms |
184312 KB |
Output is correct |
38 |
Correct |
918 ms |
181392 KB |
Output is correct |
39 |
Correct |
786 ms |
168672 KB |
Output is correct |
40 |
Correct |
756 ms |
170848 KB |
Output is correct |
41 |
Correct |
921 ms |
163944 KB |
Output is correct |
42 |
Correct |
923 ms |
164992 KB |
Output is correct |
43 |
Correct |
216 ms |
117472 KB |
Output is correct |
44 |
Correct |
970 ms |
163124 KB |
Output is correct |
45 |
Correct |
863 ms |
158404 KB |
Output is correct |
46 |
Correct |
756 ms |
149852 KB |
Output is correct |
47 |
Correct |
556 ms |
145168 KB |
Output is correct |
48 |
Correct |
536 ms |
144164 KB |
Output is correct |
49 |
Correct |
623 ms |
151420 KB |
Output is correct |
50 |
Correct |
713 ms |
160012 KB |
Output is correct |
51 |
Correct |
625 ms |
149452 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3732 ms |
289680 KB |
Output is correct |
2 |
Correct |
4565 ms |
310612 KB |
Output is correct |
3 |
Correct |
2421 ms |
293240 KB |
Output is correct |
4 |
Correct |
3477 ms |
284108 KB |
Output is correct |
5 |
Correct |
4265 ms |
284216 KB |
Output is correct |
6 |
Correct |
4114 ms |
309220 KB |
Output is correct |
7 |
Correct |
2248 ms |
289216 KB |
Output is correct |
8 |
Correct |
3008 ms |
280012 KB |
Output is correct |
9 |
Correct |
3471 ms |
290332 KB |
Output is correct |
10 |
Correct |
3691 ms |
323564 KB |
Output is correct |
11 |
Correct |
2565 ms |
314036 KB |
Output is correct |
12 |
Correct |
3115 ms |
318868 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
5107 ms |
424216 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
99 ms |
103672 KB |
Output is correct |
2 |
Correct |
125 ms |
103672 KB |
Output is correct |
3 |
Correct |
103 ms |
103672 KB |
Output is correct |
4 |
Correct |
105 ms |
103800 KB |
Output is correct |
5 |
Correct |
116 ms |
103800 KB |
Output is correct |
6 |
Correct |
110 ms |
104060 KB |
Output is correct |
7 |
Correct |
100 ms |
104056 KB |
Output is correct |
8 |
Correct |
105 ms |
104024 KB |
Output is correct |
9 |
Correct |
106 ms |
104084 KB |
Output is correct |
10 |
Correct |
109 ms |
104172 KB |
Output is correct |
11 |
Correct |
102 ms |
103928 KB |
Output is correct |
12 |
Correct |
102 ms |
104056 KB |
Output is correct |
13 |
Correct |
103 ms |
103928 KB |
Output is correct |
14 |
Correct |
105 ms |
104184 KB |
Output is correct |
15 |
Correct |
102 ms |
104056 KB |
Output is correct |
16 |
Correct |
107 ms |
104236 KB |
Output is correct |
17 |
Correct |
111 ms |
104028 KB |
Output is correct |
18 |
Correct |
111 ms |
104056 KB |
Output is correct |
19 |
Correct |
103 ms |
104132 KB |
Output is correct |
20 |
Correct |
102 ms |
104056 KB |
Output is correct |
21 |
Correct |
113 ms |
103892 KB |
Output is correct |
22 |
Correct |
104 ms |
104056 KB |
Output is correct |
23 |
Correct |
106 ms |
104016 KB |
Output is correct |
24 |
Correct |
102 ms |
104056 KB |
Output is correct |
25 |
Correct |
103 ms |
104124 KB |
Output is correct |
26 |
Correct |
103 ms |
104056 KB |
Output is correct |
27 |
Correct |
104 ms |
104056 KB |
Output is correct |
28 |
Correct |
101 ms |
104056 KB |
Output is correct |
29 |
Correct |
104 ms |
104056 KB |
Output is correct |
30 |
Correct |
107 ms |
103944 KB |
Output is correct |
31 |
Correct |
1518 ms |
194532 KB |
Output is correct |
32 |
Correct |
273 ms |
117032 KB |
Output is correct |
33 |
Correct |
1316 ms |
191968 KB |
Output is correct |
34 |
Correct |
1297 ms |
191680 KB |
Output is correct |
35 |
Correct |
1496 ms |
195308 KB |
Output is correct |
36 |
Correct |
1322 ms |
194016 KB |
Output is correct |
37 |
Correct |
949 ms |
184312 KB |
Output is correct |
38 |
Correct |
918 ms |
181392 KB |
Output is correct |
39 |
Correct |
786 ms |
168672 KB |
Output is correct |
40 |
Correct |
756 ms |
170848 KB |
Output is correct |
41 |
Correct |
921 ms |
163944 KB |
Output is correct |
42 |
Correct |
923 ms |
164992 KB |
Output is correct |
43 |
Correct |
216 ms |
117472 KB |
Output is correct |
44 |
Correct |
970 ms |
163124 KB |
Output is correct |
45 |
Correct |
863 ms |
158404 KB |
Output is correct |
46 |
Correct |
756 ms |
149852 KB |
Output is correct |
47 |
Correct |
556 ms |
145168 KB |
Output is correct |
48 |
Correct |
536 ms |
144164 KB |
Output is correct |
49 |
Correct |
623 ms |
151420 KB |
Output is correct |
50 |
Correct |
713 ms |
160012 KB |
Output is correct |
51 |
Correct |
625 ms |
149452 KB |
Output is correct |
52 |
Correct |
763 ms |
168288 KB |
Output is correct |
53 |
Correct |
685 ms |
156212 KB |
Output is correct |
54 |
Correct |
1139 ms |
175272 KB |
Output is correct |
55 |
Correct |
905 ms |
169388 KB |
Output is correct |
56 |
Correct |
823 ms |
169412 KB |
Output is correct |
57 |
Correct |
967 ms |
164932 KB |
Output is correct |
58 |
Correct |
1004 ms |
167932 KB |
Output is correct |
59 |
Correct |
894 ms |
168480 KB |
Output is correct |
60 |
Correct |
1072 ms |
165340 KB |
Output is correct |
61 |
Correct |
276 ms |
128212 KB |
Output is correct |
62 |
Correct |
752 ms |
171152 KB |
Output is correct |
63 |
Correct |
939 ms |
172216 KB |
Output is correct |
64 |
Correct |
1006 ms |
174124 KB |
Output is correct |
65 |
Correct |
1044 ms |
174972 KB |
Output is correct |
66 |
Correct |
1012 ms |
167300 KB |
Output is correct |
67 |
Correct |
328 ms |
130312 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
99 ms |
103672 KB |
Output is correct |
2 |
Correct |
125 ms |
103672 KB |
Output is correct |
3 |
Correct |
103 ms |
103672 KB |
Output is correct |
4 |
Correct |
105 ms |
103800 KB |
Output is correct |
5 |
Correct |
116 ms |
103800 KB |
Output is correct |
6 |
Correct |
110 ms |
104060 KB |
Output is correct |
7 |
Correct |
100 ms |
104056 KB |
Output is correct |
8 |
Correct |
105 ms |
104024 KB |
Output is correct |
9 |
Correct |
106 ms |
104084 KB |
Output is correct |
10 |
Correct |
109 ms |
104172 KB |
Output is correct |
11 |
Correct |
102 ms |
103928 KB |
Output is correct |
12 |
Correct |
102 ms |
104056 KB |
Output is correct |
13 |
Correct |
103 ms |
103928 KB |
Output is correct |
14 |
Correct |
105 ms |
104184 KB |
Output is correct |
15 |
Correct |
102 ms |
104056 KB |
Output is correct |
16 |
Correct |
107 ms |
104236 KB |
Output is correct |
17 |
Correct |
111 ms |
104028 KB |
Output is correct |
18 |
Correct |
111 ms |
104056 KB |
Output is correct |
19 |
Correct |
103 ms |
104132 KB |
Output is correct |
20 |
Correct |
102 ms |
104056 KB |
Output is correct |
21 |
Correct |
113 ms |
103892 KB |
Output is correct |
22 |
Correct |
104 ms |
104056 KB |
Output is correct |
23 |
Correct |
106 ms |
104016 KB |
Output is correct |
24 |
Correct |
102 ms |
104056 KB |
Output is correct |
25 |
Correct |
103 ms |
104124 KB |
Output is correct |
26 |
Correct |
103 ms |
104056 KB |
Output is correct |
27 |
Correct |
104 ms |
104056 KB |
Output is correct |
28 |
Correct |
101 ms |
104056 KB |
Output is correct |
29 |
Correct |
104 ms |
104056 KB |
Output is correct |
30 |
Correct |
107 ms |
103944 KB |
Output is correct |
31 |
Correct |
1518 ms |
194532 KB |
Output is correct |
32 |
Correct |
273 ms |
117032 KB |
Output is correct |
33 |
Correct |
1316 ms |
191968 KB |
Output is correct |
34 |
Correct |
1297 ms |
191680 KB |
Output is correct |
35 |
Correct |
1496 ms |
195308 KB |
Output is correct |
36 |
Correct |
1322 ms |
194016 KB |
Output is correct |
37 |
Correct |
949 ms |
184312 KB |
Output is correct |
38 |
Correct |
918 ms |
181392 KB |
Output is correct |
39 |
Correct |
786 ms |
168672 KB |
Output is correct |
40 |
Correct |
756 ms |
170848 KB |
Output is correct |
41 |
Correct |
921 ms |
163944 KB |
Output is correct |
42 |
Correct |
923 ms |
164992 KB |
Output is correct |
43 |
Correct |
216 ms |
117472 KB |
Output is correct |
44 |
Correct |
970 ms |
163124 KB |
Output is correct |
45 |
Correct |
863 ms |
158404 KB |
Output is correct |
46 |
Correct |
756 ms |
149852 KB |
Output is correct |
47 |
Correct |
556 ms |
145168 KB |
Output is correct |
48 |
Correct |
536 ms |
144164 KB |
Output is correct |
49 |
Correct |
623 ms |
151420 KB |
Output is correct |
50 |
Correct |
713 ms |
160012 KB |
Output is correct |
51 |
Correct |
625 ms |
149452 KB |
Output is correct |
52 |
Correct |
3732 ms |
289680 KB |
Output is correct |
53 |
Correct |
4565 ms |
310612 KB |
Output is correct |
54 |
Correct |
2421 ms |
293240 KB |
Output is correct |
55 |
Correct |
3477 ms |
284108 KB |
Output is correct |
56 |
Correct |
4265 ms |
284216 KB |
Output is correct |
57 |
Correct |
4114 ms |
309220 KB |
Output is correct |
58 |
Correct |
2248 ms |
289216 KB |
Output is correct |
59 |
Correct |
3008 ms |
280012 KB |
Output is correct |
60 |
Correct |
3471 ms |
290332 KB |
Output is correct |
61 |
Correct |
3691 ms |
323564 KB |
Output is correct |
62 |
Correct |
2565 ms |
314036 KB |
Output is correct |
63 |
Correct |
3115 ms |
318868 KB |
Output is correct |
64 |
Execution timed out |
5107 ms |
424216 KB |
Time limit exceeded |
65 |
Halted |
0 ms |
0 KB |
- |