#include <bits/stdc++.h>
#define ll long long
#define all(aaa) aaa.begin(), aaa.end()
#pragma GCC optimize("Ofast")
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, cc, cq;
int ans[N], loc[N], num[N];
unordered_map<int, vector<int>> mp[N];
vector<int> vx;
multiset<int> occ[N];
vector<pair<int, int>> t[N * 4], t2[N * 4];
vector<U> updates;
void upd(int l, int r, pair<int, int> p, int v, int L, int R) {
if (l > r)
return;
if (L == l && R == r) {
t[v].push_back(p);
}
else {
int m = (L + R) >> 1;
upd(l, min(m, r), p, v * 2, L, m);
upd(max(m + 1, l), r, p, v * 2 + 1, m + 1, R);
}
}
void startSeg(int x, int y) {
mp[x][y].push_back(cq);
}
void endSeg(int x, int y) {
updates.push_back({mp[x][y].back(), cq - 1, {x, y}});
mp[x][y].pop_back();
}
void updBeg(int x, bool add) {
if (add)
startSeg(0, x);
else
endSeg(0, x);
}
void updBetween(int x, int y, bool add) {
if (x == y)
return;
int m = (x + y) / 2;
if ((x + y) % 2)
m++;
int m1 = lower_bound(all(vx), m) - vx.begin();
if (add)
startSeg(m1, y);
else
endSeg(m1, y);
}
void calcAns(int v, int L, int R) {
if (L == R) {
t2[v] = {{loc[L], num[L]}};
}
else {
int m = (L + R) >> 1;
calcAns(v * 2, L, m);
calcAns(v * 2 + 1, m + 1, R);
merge(all(t2[v * 2]), all(t2[v * 2 + 1]), back_inserter(t2[v]));
}
int i = 0, mx = -INF;
for (auto &p : t2[v]) {
while (i < t[v].size() && t[v][i].first <= p.first) {
mx = max(mx, t[v][i].second);
i++;
}
ans[p.second] = max(ans[p.second], mx - vx[p.first]);
}
}
void solve(vector<E> evs) {
vx.clear();
updates.clear();
for (int i = 0; i < N; i++) {
occ[i].clear();
mp[i].clear();
}
for (int i = 0; i < N * 4; i++) {
t[i].clear();
t2[i].clear();
}
for (E e : evs) {
if (e.t == 2)
vx.push_back(e.x);
}
sort(all(vx));
vx.erase(unique(all(vx)), vx.end());
cq = 0;
for (E e : evs) {
if (e.t == 0) {
auto it = occ[e.y].upper_bound(e.x);
if (!occ[e.y].empty()) {
if (it != occ[e.y].begin() && it != occ[e.y].end()) {
updBetween(*prev(it), *it, false);
}
if (it == occ[e.y].begin()) {
updBeg(*it, false);
}
}
it = occ[e.y].insert(e.x);
if (it == occ[e.y].begin())
updBeg(*it, true);
else
updBetween(*prev(it), *it, true);
if (next(it) != occ[e.y].end())
updBetween(*it, *next(it), 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);
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);
occ[e.y].erase(it);
}
else {
loc[cq] = lower_bound(all(vx), e.x) - vx.begin();
num[cq] = e.y;
cq++;
}
}
sort(all(updates), [](const U &a, const U &b) {
return a.p.first < b.p.first;
});
for (U u : updates) {
upd(u.l, u.r, u.p, 1, 0, q - 1);
}
calcAns(1, 0, q - 1);
}
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});
vx.push_back(x);
}
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;
});
solve(evs);
for (E &e : evs)
e.x = INF - e.x;
solve(evs);
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:85:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while (i < t[v].size() && t[v][i].first <= p.first) {
~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
108 ms |
87288 KB |
Output is correct |
2 |
Correct |
110 ms |
87288 KB |
Output is correct |
3 |
Correct |
109 ms |
87288 KB |
Output is correct |
4 |
Correct |
112 ms |
87304 KB |
Output is correct |
5 |
Correct |
117 ms |
87416 KB |
Output is correct |
6 |
Correct |
119 ms |
87544 KB |
Output is correct |
7 |
Correct |
115 ms |
87544 KB |
Output is correct |
8 |
Correct |
114 ms |
87604 KB |
Output is correct |
9 |
Correct |
112 ms |
87544 KB |
Output is correct |
10 |
Correct |
116 ms |
87552 KB |
Output is correct |
11 |
Correct |
109 ms |
87532 KB |
Output is correct |
12 |
Correct |
110 ms |
87524 KB |
Output is correct |
13 |
Correct |
110 ms |
87416 KB |
Output is correct |
14 |
Correct |
129 ms |
87544 KB |
Output is correct |
15 |
Correct |
111 ms |
87660 KB |
Output is correct |
16 |
Correct |
113 ms |
87544 KB |
Output is correct |
17 |
Correct |
110 ms |
87544 KB |
Output is correct |
18 |
Correct |
111 ms |
87688 KB |
Output is correct |
19 |
Correct |
111 ms |
87672 KB |
Output is correct |
20 |
Correct |
112 ms |
87672 KB |
Output is correct |
21 |
Correct |
112 ms |
87544 KB |
Output is correct |
22 |
Correct |
117 ms |
87624 KB |
Output is correct |
23 |
Correct |
111 ms |
87516 KB |
Output is correct |
24 |
Correct |
111 ms |
87600 KB |
Output is correct |
25 |
Correct |
113 ms |
87556 KB |
Output is correct |
26 |
Correct |
114 ms |
87544 KB |
Output is correct |
27 |
Correct |
113 ms |
87516 KB |
Output is correct |
28 |
Correct |
118 ms |
87556 KB |
Output is correct |
29 |
Correct |
123 ms |
87520 KB |
Output is correct |
30 |
Correct |
112 ms |
87544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
108 ms |
87288 KB |
Output is correct |
2 |
Correct |
110 ms |
87288 KB |
Output is correct |
3 |
Correct |
109 ms |
87288 KB |
Output is correct |
4 |
Correct |
112 ms |
87304 KB |
Output is correct |
5 |
Correct |
117 ms |
87416 KB |
Output is correct |
6 |
Correct |
119 ms |
87544 KB |
Output is correct |
7 |
Correct |
115 ms |
87544 KB |
Output is correct |
8 |
Correct |
114 ms |
87604 KB |
Output is correct |
9 |
Correct |
112 ms |
87544 KB |
Output is correct |
10 |
Correct |
116 ms |
87552 KB |
Output is correct |
11 |
Correct |
109 ms |
87532 KB |
Output is correct |
12 |
Correct |
110 ms |
87524 KB |
Output is correct |
13 |
Correct |
110 ms |
87416 KB |
Output is correct |
14 |
Correct |
129 ms |
87544 KB |
Output is correct |
15 |
Correct |
111 ms |
87660 KB |
Output is correct |
16 |
Correct |
113 ms |
87544 KB |
Output is correct |
17 |
Correct |
110 ms |
87544 KB |
Output is correct |
18 |
Correct |
111 ms |
87688 KB |
Output is correct |
19 |
Correct |
111 ms |
87672 KB |
Output is correct |
20 |
Correct |
112 ms |
87672 KB |
Output is correct |
21 |
Correct |
112 ms |
87544 KB |
Output is correct |
22 |
Correct |
117 ms |
87624 KB |
Output is correct |
23 |
Correct |
111 ms |
87516 KB |
Output is correct |
24 |
Correct |
111 ms |
87600 KB |
Output is correct |
25 |
Correct |
113 ms |
87556 KB |
Output is correct |
26 |
Correct |
114 ms |
87544 KB |
Output is correct |
27 |
Correct |
113 ms |
87516 KB |
Output is correct |
28 |
Correct |
118 ms |
87556 KB |
Output is correct |
29 |
Correct |
123 ms |
87520 KB |
Output is correct |
30 |
Correct |
112 ms |
87544 KB |
Output is correct |
31 |
Correct |
1574 ms |
148968 KB |
Output is correct |
32 |
Correct |
371 ms |
108224 KB |
Output is correct |
33 |
Correct |
1371 ms |
147156 KB |
Output is correct |
34 |
Correct |
1599 ms |
147508 KB |
Output is correct |
35 |
Correct |
1537 ms |
149180 KB |
Output is correct |
36 |
Correct |
1538 ms |
148428 KB |
Output is correct |
37 |
Correct |
1014 ms |
143612 KB |
Output is correct |
38 |
Correct |
942 ms |
141640 KB |
Output is correct |
39 |
Correct |
770 ms |
135544 KB |
Output is correct |
40 |
Correct |
825 ms |
136600 KB |
Output is correct |
41 |
Correct |
930 ms |
134328 KB |
Output is correct |
42 |
Correct |
909 ms |
134704 KB |
Output is correct |
43 |
Correct |
271 ms |
109592 KB |
Output is correct |
44 |
Correct |
927 ms |
133964 KB |
Output is correct |
45 |
Correct |
838 ms |
131236 KB |
Output is correct |
46 |
Correct |
735 ms |
126720 KB |
Output is correct |
47 |
Correct |
582 ms |
124508 KB |
Output is correct |
48 |
Correct |
538 ms |
123612 KB |
Output is correct |
49 |
Correct |
654 ms |
127528 KB |
Output is correct |
50 |
Correct |
756 ms |
132504 KB |
Output is correct |
51 |
Correct |
636 ms |
126428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4789 ms |
288048 KB |
Output is correct |
2 |
Execution timed out |
5086 ms |
290884 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5102 ms |
364700 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
108 ms |
87288 KB |
Output is correct |
2 |
Correct |
110 ms |
87288 KB |
Output is correct |
3 |
Correct |
109 ms |
87288 KB |
Output is correct |
4 |
Correct |
112 ms |
87304 KB |
Output is correct |
5 |
Correct |
117 ms |
87416 KB |
Output is correct |
6 |
Correct |
119 ms |
87544 KB |
Output is correct |
7 |
Correct |
115 ms |
87544 KB |
Output is correct |
8 |
Correct |
114 ms |
87604 KB |
Output is correct |
9 |
Correct |
112 ms |
87544 KB |
Output is correct |
10 |
Correct |
116 ms |
87552 KB |
Output is correct |
11 |
Correct |
109 ms |
87532 KB |
Output is correct |
12 |
Correct |
110 ms |
87524 KB |
Output is correct |
13 |
Correct |
110 ms |
87416 KB |
Output is correct |
14 |
Correct |
129 ms |
87544 KB |
Output is correct |
15 |
Correct |
111 ms |
87660 KB |
Output is correct |
16 |
Correct |
113 ms |
87544 KB |
Output is correct |
17 |
Correct |
110 ms |
87544 KB |
Output is correct |
18 |
Correct |
111 ms |
87688 KB |
Output is correct |
19 |
Correct |
111 ms |
87672 KB |
Output is correct |
20 |
Correct |
112 ms |
87672 KB |
Output is correct |
21 |
Correct |
112 ms |
87544 KB |
Output is correct |
22 |
Correct |
117 ms |
87624 KB |
Output is correct |
23 |
Correct |
111 ms |
87516 KB |
Output is correct |
24 |
Correct |
111 ms |
87600 KB |
Output is correct |
25 |
Correct |
113 ms |
87556 KB |
Output is correct |
26 |
Correct |
114 ms |
87544 KB |
Output is correct |
27 |
Correct |
113 ms |
87516 KB |
Output is correct |
28 |
Correct |
118 ms |
87556 KB |
Output is correct |
29 |
Correct |
123 ms |
87520 KB |
Output is correct |
30 |
Correct |
112 ms |
87544 KB |
Output is correct |
31 |
Correct |
1574 ms |
148968 KB |
Output is correct |
32 |
Correct |
371 ms |
108224 KB |
Output is correct |
33 |
Correct |
1371 ms |
147156 KB |
Output is correct |
34 |
Correct |
1599 ms |
147508 KB |
Output is correct |
35 |
Correct |
1537 ms |
149180 KB |
Output is correct |
36 |
Correct |
1538 ms |
148428 KB |
Output is correct |
37 |
Correct |
1014 ms |
143612 KB |
Output is correct |
38 |
Correct |
942 ms |
141640 KB |
Output is correct |
39 |
Correct |
770 ms |
135544 KB |
Output is correct |
40 |
Correct |
825 ms |
136600 KB |
Output is correct |
41 |
Correct |
930 ms |
134328 KB |
Output is correct |
42 |
Correct |
909 ms |
134704 KB |
Output is correct |
43 |
Correct |
271 ms |
109592 KB |
Output is correct |
44 |
Correct |
927 ms |
133964 KB |
Output is correct |
45 |
Correct |
838 ms |
131236 KB |
Output is correct |
46 |
Correct |
735 ms |
126720 KB |
Output is correct |
47 |
Correct |
582 ms |
124508 KB |
Output is correct |
48 |
Correct |
538 ms |
123612 KB |
Output is correct |
49 |
Correct |
654 ms |
127528 KB |
Output is correct |
50 |
Correct |
756 ms |
132504 KB |
Output is correct |
51 |
Correct |
636 ms |
126428 KB |
Output is correct |
52 |
Correct |
840 ms |
152860 KB |
Output is correct |
53 |
Correct |
743 ms |
138996 KB |
Output is correct |
54 |
Correct |
1111 ms |
146156 KB |
Output is correct |
55 |
Correct |
854 ms |
142944 KB |
Output is correct |
56 |
Correct |
997 ms |
145656 KB |
Output is correct |
57 |
Correct |
899 ms |
136828 KB |
Output is correct |
58 |
Correct |
912 ms |
140636 KB |
Output is correct |
59 |
Correct |
898 ms |
144208 KB |
Output is correct |
60 |
Correct |
912 ms |
136440 KB |
Output is correct |
61 |
Correct |
346 ms |
118760 KB |
Output is correct |
62 |
Correct |
893 ms |
154248 KB |
Output is correct |
63 |
Correct |
997 ms |
147224 KB |
Output is correct |
64 |
Correct |
1022 ms |
145224 KB |
Output is correct |
65 |
Correct |
1043 ms |
142004 KB |
Output is correct |
66 |
Correct |
987 ms |
136240 KB |
Output is correct |
67 |
Correct |
434 ms |
117564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
108 ms |
87288 KB |
Output is correct |
2 |
Correct |
110 ms |
87288 KB |
Output is correct |
3 |
Correct |
109 ms |
87288 KB |
Output is correct |
4 |
Correct |
112 ms |
87304 KB |
Output is correct |
5 |
Correct |
117 ms |
87416 KB |
Output is correct |
6 |
Correct |
119 ms |
87544 KB |
Output is correct |
7 |
Correct |
115 ms |
87544 KB |
Output is correct |
8 |
Correct |
114 ms |
87604 KB |
Output is correct |
9 |
Correct |
112 ms |
87544 KB |
Output is correct |
10 |
Correct |
116 ms |
87552 KB |
Output is correct |
11 |
Correct |
109 ms |
87532 KB |
Output is correct |
12 |
Correct |
110 ms |
87524 KB |
Output is correct |
13 |
Correct |
110 ms |
87416 KB |
Output is correct |
14 |
Correct |
129 ms |
87544 KB |
Output is correct |
15 |
Correct |
111 ms |
87660 KB |
Output is correct |
16 |
Correct |
113 ms |
87544 KB |
Output is correct |
17 |
Correct |
110 ms |
87544 KB |
Output is correct |
18 |
Correct |
111 ms |
87688 KB |
Output is correct |
19 |
Correct |
111 ms |
87672 KB |
Output is correct |
20 |
Correct |
112 ms |
87672 KB |
Output is correct |
21 |
Correct |
112 ms |
87544 KB |
Output is correct |
22 |
Correct |
117 ms |
87624 KB |
Output is correct |
23 |
Correct |
111 ms |
87516 KB |
Output is correct |
24 |
Correct |
111 ms |
87600 KB |
Output is correct |
25 |
Correct |
113 ms |
87556 KB |
Output is correct |
26 |
Correct |
114 ms |
87544 KB |
Output is correct |
27 |
Correct |
113 ms |
87516 KB |
Output is correct |
28 |
Correct |
118 ms |
87556 KB |
Output is correct |
29 |
Correct |
123 ms |
87520 KB |
Output is correct |
30 |
Correct |
112 ms |
87544 KB |
Output is correct |
31 |
Correct |
1574 ms |
148968 KB |
Output is correct |
32 |
Correct |
371 ms |
108224 KB |
Output is correct |
33 |
Correct |
1371 ms |
147156 KB |
Output is correct |
34 |
Correct |
1599 ms |
147508 KB |
Output is correct |
35 |
Correct |
1537 ms |
149180 KB |
Output is correct |
36 |
Correct |
1538 ms |
148428 KB |
Output is correct |
37 |
Correct |
1014 ms |
143612 KB |
Output is correct |
38 |
Correct |
942 ms |
141640 KB |
Output is correct |
39 |
Correct |
770 ms |
135544 KB |
Output is correct |
40 |
Correct |
825 ms |
136600 KB |
Output is correct |
41 |
Correct |
930 ms |
134328 KB |
Output is correct |
42 |
Correct |
909 ms |
134704 KB |
Output is correct |
43 |
Correct |
271 ms |
109592 KB |
Output is correct |
44 |
Correct |
927 ms |
133964 KB |
Output is correct |
45 |
Correct |
838 ms |
131236 KB |
Output is correct |
46 |
Correct |
735 ms |
126720 KB |
Output is correct |
47 |
Correct |
582 ms |
124508 KB |
Output is correct |
48 |
Correct |
538 ms |
123612 KB |
Output is correct |
49 |
Correct |
654 ms |
127528 KB |
Output is correct |
50 |
Correct |
756 ms |
132504 KB |
Output is correct |
51 |
Correct |
636 ms |
126428 KB |
Output is correct |
52 |
Correct |
4789 ms |
288048 KB |
Output is correct |
53 |
Execution timed out |
5086 ms |
290884 KB |
Time limit exceeded |
54 |
Halted |
0 ms |
0 KB |
- |