#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;
};
const int N = 3e5 + 5, INF = 1e9, MAX = 1e8;
int n, q, k, cc, cq;
int ans[N], loc[N], num[N];
vector<int> vx;
multiset<int> occ[N];
map<pair<int, int>, vector<int>> mp;
vector<pair<int, int>> t[N * 4];
struct ST {
vector<int> t[N];
ST() {
fill(t, t + N, vector<int>{-INF});
}
void upd(int x, int y) {
for (; x < N; x |= x + 1)
t[x].push_back(max(t[x].back(), y));
}
int que(int x) {
int ans = -INF;
for (; x >= 0; x = (x & (x + 1)) - 1)
ans = max(ans, t[x].back());
return ans;
}
void del(int x) {
for (; x < N; x |= x + 1)
t[x].pop_back();
}
} fent;
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) {
// cout << "!" << x << " " << y << "\n";
auto p = make_pair(x, y);
upd(mp[{x, y}].back(), cq - 1, p, 1, 0, q - 1);
// cout << "SEG_END " << mp[{x, y}].back() << " " << cq - 1 << " " << x << " " << y << "\n";
mp[{x, y}].pop_back();
}
void updBeg(int x, bool add) {
// cout << "BEG " << x << " " << add << "\n";
if (add)
startSeg(0, x);
else
endSeg(0, x);
}
void updBetween(int x, int y, bool add) {
// cout << "BET " << x << " " << y << " " << add << "\n";
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) {
for (auto &p : t[v]) {
fent.upd(p.first, p.second);
}
if (L == R) {
ans[num[L]] = max(ans[num[L]], fent.que(loc[L]) - vx[loc[L]]);
}
else {
int m = (L + R) >> 1;
calcAns(v * 2, L, m);
calcAns(v * 2 + 1, m + 1, R);
}
for (auto &p : t[v]) {
fent.del(p.first);
}
}
void solve(vector<E> evs) {
vx.clear();
mp.clear();
for (int i = 0; i < N; i++) {
occ[i].clear();
}
for (int i = 0; i < N * 4; i++) {
t[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());
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);
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++;
}
}
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
82 ms |
59000 KB |
Output is correct |
2 |
Correct |
82 ms |
59000 KB |
Output is correct |
3 |
Correct |
82 ms |
59128 KB |
Output is correct |
4 |
Correct |
81 ms |
59000 KB |
Output is correct |
5 |
Correct |
87 ms |
59256 KB |
Output is correct |
6 |
Correct |
89 ms |
59356 KB |
Output is correct |
7 |
Correct |
89 ms |
59420 KB |
Output is correct |
8 |
Correct |
85 ms |
59384 KB |
Output is correct |
9 |
Correct |
90 ms |
59384 KB |
Output is correct |
10 |
Correct |
89 ms |
59384 KB |
Output is correct |
11 |
Correct |
83 ms |
59256 KB |
Output is correct |
12 |
Correct |
90 ms |
59244 KB |
Output is correct |
13 |
Correct |
93 ms |
59260 KB |
Output is correct |
14 |
Correct |
84 ms |
59128 KB |
Output is correct |
15 |
Correct |
89 ms |
59512 KB |
Output is correct |
16 |
Correct |
88 ms |
59436 KB |
Output is correct |
17 |
Correct |
93 ms |
59392 KB |
Output is correct |
18 |
Correct |
95 ms |
59384 KB |
Output is correct |
19 |
Correct |
89 ms |
59448 KB |
Output is correct |
20 |
Correct |
97 ms |
59276 KB |
Output is correct |
21 |
Correct |
87 ms |
59256 KB |
Output is correct |
22 |
Correct |
95 ms |
59484 KB |
Output is correct |
23 |
Correct |
88 ms |
59384 KB |
Output is correct |
24 |
Correct |
89 ms |
59384 KB |
Output is correct |
25 |
Correct |
96 ms |
59384 KB |
Output is correct |
26 |
Correct |
85 ms |
59256 KB |
Output is correct |
27 |
Correct |
85 ms |
59256 KB |
Output is correct |
28 |
Correct |
85 ms |
59384 KB |
Output is correct |
29 |
Correct |
89 ms |
59256 KB |
Output is correct |
30 |
Correct |
86 ms |
59256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
82 ms |
59000 KB |
Output is correct |
2 |
Correct |
82 ms |
59000 KB |
Output is correct |
3 |
Correct |
82 ms |
59128 KB |
Output is correct |
4 |
Correct |
81 ms |
59000 KB |
Output is correct |
5 |
Correct |
87 ms |
59256 KB |
Output is correct |
6 |
Correct |
89 ms |
59356 KB |
Output is correct |
7 |
Correct |
89 ms |
59420 KB |
Output is correct |
8 |
Correct |
85 ms |
59384 KB |
Output is correct |
9 |
Correct |
90 ms |
59384 KB |
Output is correct |
10 |
Correct |
89 ms |
59384 KB |
Output is correct |
11 |
Correct |
83 ms |
59256 KB |
Output is correct |
12 |
Correct |
90 ms |
59244 KB |
Output is correct |
13 |
Correct |
93 ms |
59260 KB |
Output is correct |
14 |
Correct |
84 ms |
59128 KB |
Output is correct |
15 |
Correct |
89 ms |
59512 KB |
Output is correct |
16 |
Correct |
88 ms |
59436 KB |
Output is correct |
17 |
Correct |
93 ms |
59392 KB |
Output is correct |
18 |
Correct |
95 ms |
59384 KB |
Output is correct |
19 |
Correct |
89 ms |
59448 KB |
Output is correct |
20 |
Correct |
97 ms |
59276 KB |
Output is correct |
21 |
Correct |
87 ms |
59256 KB |
Output is correct |
22 |
Correct |
95 ms |
59484 KB |
Output is correct |
23 |
Correct |
88 ms |
59384 KB |
Output is correct |
24 |
Correct |
89 ms |
59384 KB |
Output is correct |
25 |
Correct |
96 ms |
59384 KB |
Output is correct |
26 |
Correct |
85 ms |
59256 KB |
Output is correct |
27 |
Correct |
85 ms |
59256 KB |
Output is correct |
28 |
Correct |
85 ms |
59384 KB |
Output is correct |
29 |
Correct |
89 ms |
59256 KB |
Output is correct |
30 |
Correct |
86 ms |
59256 KB |
Output is correct |
31 |
Correct |
2848 ms |
114244 KB |
Output is correct |
32 |
Correct |
260 ms |
67320 KB |
Output is correct |
33 |
Correct |
2317 ms |
108136 KB |
Output is correct |
34 |
Correct |
2506 ms |
109272 KB |
Output is correct |
35 |
Correct |
2860 ms |
114600 KB |
Output is correct |
36 |
Correct |
2669 ms |
113236 KB |
Output is correct |
37 |
Correct |
1655 ms |
104456 KB |
Output is correct |
38 |
Correct |
1543 ms |
102264 KB |
Output is correct |
39 |
Correct |
1112 ms |
96168 KB |
Output is correct |
40 |
Correct |
1140 ms |
97104 KB |
Output is correct |
41 |
Correct |
1486 ms |
94264 KB |
Output is correct |
42 |
Correct |
1460 ms |
94420 KB |
Output is correct |
43 |
Correct |
185 ms |
69100 KB |
Output is correct |
44 |
Correct |
1464 ms |
93428 KB |
Output is correct |
45 |
Correct |
1364 ms |
90932 KB |
Output is correct |
46 |
Correct |
1027 ms |
86148 KB |
Output is correct |
47 |
Correct |
673 ms |
84036 KB |
Output is correct |
48 |
Correct |
595 ms |
83104 KB |
Output is correct |
49 |
Correct |
821 ms |
87256 KB |
Output is correct |
50 |
Correct |
1058 ms |
92684 KB |
Output is correct |
51 |
Correct |
787 ms |
85860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5052 ms |
188508 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5101 ms |
278896 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
82 ms |
59000 KB |
Output is correct |
2 |
Correct |
82 ms |
59000 KB |
Output is correct |
3 |
Correct |
82 ms |
59128 KB |
Output is correct |
4 |
Correct |
81 ms |
59000 KB |
Output is correct |
5 |
Correct |
87 ms |
59256 KB |
Output is correct |
6 |
Correct |
89 ms |
59356 KB |
Output is correct |
7 |
Correct |
89 ms |
59420 KB |
Output is correct |
8 |
Correct |
85 ms |
59384 KB |
Output is correct |
9 |
Correct |
90 ms |
59384 KB |
Output is correct |
10 |
Correct |
89 ms |
59384 KB |
Output is correct |
11 |
Correct |
83 ms |
59256 KB |
Output is correct |
12 |
Correct |
90 ms |
59244 KB |
Output is correct |
13 |
Correct |
93 ms |
59260 KB |
Output is correct |
14 |
Correct |
84 ms |
59128 KB |
Output is correct |
15 |
Correct |
89 ms |
59512 KB |
Output is correct |
16 |
Correct |
88 ms |
59436 KB |
Output is correct |
17 |
Correct |
93 ms |
59392 KB |
Output is correct |
18 |
Correct |
95 ms |
59384 KB |
Output is correct |
19 |
Correct |
89 ms |
59448 KB |
Output is correct |
20 |
Correct |
97 ms |
59276 KB |
Output is correct |
21 |
Correct |
87 ms |
59256 KB |
Output is correct |
22 |
Correct |
95 ms |
59484 KB |
Output is correct |
23 |
Correct |
88 ms |
59384 KB |
Output is correct |
24 |
Correct |
89 ms |
59384 KB |
Output is correct |
25 |
Correct |
96 ms |
59384 KB |
Output is correct |
26 |
Correct |
85 ms |
59256 KB |
Output is correct |
27 |
Correct |
85 ms |
59256 KB |
Output is correct |
28 |
Correct |
85 ms |
59384 KB |
Output is correct |
29 |
Correct |
89 ms |
59256 KB |
Output is correct |
30 |
Correct |
86 ms |
59256 KB |
Output is correct |
31 |
Correct |
2848 ms |
114244 KB |
Output is correct |
32 |
Correct |
260 ms |
67320 KB |
Output is correct |
33 |
Correct |
2317 ms |
108136 KB |
Output is correct |
34 |
Correct |
2506 ms |
109272 KB |
Output is correct |
35 |
Correct |
2860 ms |
114600 KB |
Output is correct |
36 |
Correct |
2669 ms |
113236 KB |
Output is correct |
37 |
Correct |
1655 ms |
104456 KB |
Output is correct |
38 |
Correct |
1543 ms |
102264 KB |
Output is correct |
39 |
Correct |
1112 ms |
96168 KB |
Output is correct |
40 |
Correct |
1140 ms |
97104 KB |
Output is correct |
41 |
Correct |
1486 ms |
94264 KB |
Output is correct |
42 |
Correct |
1460 ms |
94420 KB |
Output is correct |
43 |
Correct |
185 ms |
69100 KB |
Output is correct |
44 |
Correct |
1464 ms |
93428 KB |
Output is correct |
45 |
Correct |
1364 ms |
90932 KB |
Output is correct |
46 |
Correct |
1027 ms |
86148 KB |
Output is correct |
47 |
Correct |
673 ms |
84036 KB |
Output is correct |
48 |
Correct |
595 ms |
83104 KB |
Output is correct |
49 |
Correct |
821 ms |
87256 KB |
Output is correct |
50 |
Correct |
1058 ms |
92684 KB |
Output is correct |
51 |
Correct |
787 ms |
85860 KB |
Output is correct |
52 |
Correct |
1719 ms |
122076 KB |
Output is correct |
53 |
Correct |
1416 ms |
106804 KB |
Output is correct |
54 |
Correct |
2142 ms |
112356 KB |
Output is correct |
55 |
Correct |
1552 ms |
106808 KB |
Output is correct |
56 |
Correct |
1548 ms |
110328 KB |
Output is correct |
57 |
Correct |
1493 ms |
97548 KB |
Output is correct |
58 |
Correct |
1612 ms |
105304 KB |
Output is correct |
59 |
Correct |
1592 ms |
108796 KB |
Output is correct |
60 |
Correct |
1687 ms |
97564 KB |
Output is correct |
61 |
Correct |
249 ms |
81616 KB |
Output is correct |
62 |
Correct |
1576 ms |
123920 KB |
Output is correct |
63 |
Correct |
1865 ms |
113320 KB |
Output is correct |
64 |
Correct |
1983 ms |
110460 KB |
Output is correct |
65 |
Correct |
1959 ms |
103728 KB |
Output is correct |
66 |
Correct |
1785 ms |
96244 KB |
Output is correct |
67 |
Correct |
567 ms |
77500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
82 ms |
59000 KB |
Output is correct |
2 |
Correct |
82 ms |
59000 KB |
Output is correct |
3 |
Correct |
82 ms |
59128 KB |
Output is correct |
4 |
Correct |
81 ms |
59000 KB |
Output is correct |
5 |
Correct |
87 ms |
59256 KB |
Output is correct |
6 |
Correct |
89 ms |
59356 KB |
Output is correct |
7 |
Correct |
89 ms |
59420 KB |
Output is correct |
8 |
Correct |
85 ms |
59384 KB |
Output is correct |
9 |
Correct |
90 ms |
59384 KB |
Output is correct |
10 |
Correct |
89 ms |
59384 KB |
Output is correct |
11 |
Correct |
83 ms |
59256 KB |
Output is correct |
12 |
Correct |
90 ms |
59244 KB |
Output is correct |
13 |
Correct |
93 ms |
59260 KB |
Output is correct |
14 |
Correct |
84 ms |
59128 KB |
Output is correct |
15 |
Correct |
89 ms |
59512 KB |
Output is correct |
16 |
Correct |
88 ms |
59436 KB |
Output is correct |
17 |
Correct |
93 ms |
59392 KB |
Output is correct |
18 |
Correct |
95 ms |
59384 KB |
Output is correct |
19 |
Correct |
89 ms |
59448 KB |
Output is correct |
20 |
Correct |
97 ms |
59276 KB |
Output is correct |
21 |
Correct |
87 ms |
59256 KB |
Output is correct |
22 |
Correct |
95 ms |
59484 KB |
Output is correct |
23 |
Correct |
88 ms |
59384 KB |
Output is correct |
24 |
Correct |
89 ms |
59384 KB |
Output is correct |
25 |
Correct |
96 ms |
59384 KB |
Output is correct |
26 |
Correct |
85 ms |
59256 KB |
Output is correct |
27 |
Correct |
85 ms |
59256 KB |
Output is correct |
28 |
Correct |
85 ms |
59384 KB |
Output is correct |
29 |
Correct |
89 ms |
59256 KB |
Output is correct |
30 |
Correct |
86 ms |
59256 KB |
Output is correct |
31 |
Correct |
2848 ms |
114244 KB |
Output is correct |
32 |
Correct |
260 ms |
67320 KB |
Output is correct |
33 |
Correct |
2317 ms |
108136 KB |
Output is correct |
34 |
Correct |
2506 ms |
109272 KB |
Output is correct |
35 |
Correct |
2860 ms |
114600 KB |
Output is correct |
36 |
Correct |
2669 ms |
113236 KB |
Output is correct |
37 |
Correct |
1655 ms |
104456 KB |
Output is correct |
38 |
Correct |
1543 ms |
102264 KB |
Output is correct |
39 |
Correct |
1112 ms |
96168 KB |
Output is correct |
40 |
Correct |
1140 ms |
97104 KB |
Output is correct |
41 |
Correct |
1486 ms |
94264 KB |
Output is correct |
42 |
Correct |
1460 ms |
94420 KB |
Output is correct |
43 |
Correct |
185 ms |
69100 KB |
Output is correct |
44 |
Correct |
1464 ms |
93428 KB |
Output is correct |
45 |
Correct |
1364 ms |
90932 KB |
Output is correct |
46 |
Correct |
1027 ms |
86148 KB |
Output is correct |
47 |
Correct |
673 ms |
84036 KB |
Output is correct |
48 |
Correct |
595 ms |
83104 KB |
Output is correct |
49 |
Correct |
821 ms |
87256 KB |
Output is correct |
50 |
Correct |
1058 ms |
92684 KB |
Output is correct |
51 |
Correct |
787 ms |
85860 KB |
Output is correct |
52 |
Execution timed out |
5052 ms |
188508 KB |
Time limit exceeded |
53 |
Halted |
0 ms |
0 KB |
- |