#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
const int N = 2e5 + 5;
int n, m, c, p;
int lab[N], cands[N];
long long pf[N];
array<int, 3> pt[N];
array<int, 4> rect[N];
vector<array<int, 3>> edges;
int find(int u) {
return lab[u] < 0 ? u : lab[u] = find(lab[u]);
}
bool mrg(int u, int v) {
if ((u = find(u)) == (v = find(v))) {
return 0;
}
if (lab[u] > lab[v]) {
swap(u, v);
}
lab[u] += lab[v];
lab[v] = u;
return 1;
}
void bld() {
vector<array<int, 2>> events;
for (int i = 1; i <= m; ++i) {
events.push_back({rect[i][0], rect[i][3]});
events.push_back({rect[i][2] + 1, -rect[i][3]});
}
multiset<int> st;
auto add = [&](int x) {
if (x > 0) {
st.insert(x);
} else {
st.erase(st.find(-x));
}
};
sort(pt + 1, pt + n + 1);
sort(events.begin(), events.end());
for (int i = 1, j = 0; i <= n; ++i) {
while (j < 2 * m && events[j][0] <= pt[i][0]) {
add(events[j++][1]);
}
int k = i;
while (k + 1 <= n && pt[k + 1][0] == pt[i][0]) {
++k;
auto it = st.lower_bound(pt[k - 1][1]);
if (it != st.end() && *it < pt[k][1]) {
continue;
}
edges.push_back({pt[k][1] - pt[k - 1][1], pt[k][2], pt[k - 1][2]});
}
i = k;
}
}
long long qry(int b, int h) {
if (h + p < n) {
return -1;
}
int cnt = lower_bound(cands + 1, cands + p + 1, b) - cands - 1;
if (cnt + h >= n) {
return pf[cnt] + (long long) (n - cnt) * b;
}
return pf[n - h] + (long long) h * b;
}
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
cin >> n >> m >> c;
for (int i = 1; i <= n; ++i) {
int x, y; cin >> x >> y;
pt[i] = {x, y, i};
}
for (int i = 1; i <= m; ++i) {
for (int j = 0; j < 4; ++j) {
cin >> rect[i][j];
}
}
bld();
for (int i = 1; i <= n; ++i) {
swap(pt[i][0], pt[i][1]);
}
for (int i = 1; i <= m; ++i) {
swap(rect[i][0], rect[i][1]);
swap(rect[i][2], rect[i][3]);
}
bld();
fill(lab + 1, lab + n + 1, -1);
sort(edges.begin(), edges.end());
for (auto [c, u, v] : edges) {
if (mrg(u, v)) {
cands[++p] = c;
pf[p] = pf[p - 1] + c;
}
}
while (c--) {
int b, h; cin >> b >> h;
cout << qry(b, h) << "\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5084 KB |
Output is correct |
2 |
Correct |
64 ms |
11972 KB |
Output is correct |
3 |
Correct |
65 ms |
11976 KB |
Output is correct |
4 |
Correct |
95 ms |
14704 KB |
Output is correct |
5 |
Correct |
88 ms |
13108 KB |
Output is correct |
6 |
Correct |
65 ms |
11976 KB |
Output is correct |
7 |
Correct |
75 ms |
15528 KB |
Output is correct |
8 |
Correct |
67 ms |
13432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5084 KB |
Output is correct |
2 |
Correct |
64 ms |
11972 KB |
Output is correct |
3 |
Correct |
65 ms |
11976 KB |
Output is correct |
4 |
Correct |
95 ms |
14704 KB |
Output is correct |
5 |
Correct |
88 ms |
13108 KB |
Output is correct |
6 |
Correct |
65 ms |
11976 KB |
Output is correct |
7 |
Correct |
75 ms |
15528 KB |
Output is correct |
8 |
Correct |
67 ms |
13432 KB |
Output is correct |
9 |
Correct |
266 ms |
28816 KB |
Output is correct |
10 |
Correct |
270 ms |
28792 KB |
Output is correct |
11 |
Correct |
283 ms |
28444 KB |
Output is correct |
12 |
Correct |
265 ms |
28536 KB |
Output is correct |
13 |
Correct |
215 ms |
22040 KB |
Output is correct |
14 |
Correct |
74 ms |
12996 KB |
Output is correct |
15 |
Correct |
262 ms |
26872 KB |
Output is correct |
16 |
Correct |
228 ms |
31356 KB |
Output is correct |
17 |
Correct |
212 ms |
30852 KB |
Output is correct |
18 |
Correct |
216 ms |
36560 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5084 KB |
Output is correct |
2 |
Correct |
64 ms |
11972 KB |
Output is correct |
3 |
Correct |
65 ms |
11976 KB |
Output is correct |
4 |
Correct |
95 ms |
14704 KB |
Output is correct |
5 |
Correct |
88 ms |
13108 KB |
Output is correct |
6 |
Correct |
65 ms |
11976 KB |
Output is correct |
7 |
Correct |
75 ms |
15528 KB |
Output is correct |
8 |
Correct |
67 ms |
13432 KB |
Output is correct |
9 |
Correct |
176 ms |
27840 KB |
Output is correct |
10 |
Correct |
171 ms |
26304 KB |
Output is correct |
11 |
Correct |
162 ms |
23748 KB |
Output is correct |
12 |
Correct |
170 ms |
23584 KB |
Output is correct |
13 |
Correct |
135 ms |
21972 KB |
Output is correct |
14 |
Correct |
194 ms |
27836 KB |
Output is correct |
15 |
Correct |
224 ms |
27056 KB |
Output is correct |
16 |
Correct |
175 ms |
26056 KB |
Output is correct |
17 |
Correct |
159 ms |
26408 KB |
Output is correct |
18 |
Correct |
199 ms |
24120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5084 KB |
Output is correct |
2 |
Correct |
64 ms |
11972 KB |
Output is correct |
3 |
Correct |
65 ms |
11976 KB |
Output is correct |
4 |
Correct |
95 ms |
14704 KB |
Output is correct |
5 |
Correct |
88 ms |
13108 KB |
Output is correct |
6 |
Correct |
65 ms |
11976 KB |
Output is correct |
7 |
Correct |
75 ms |
15528 KB |
Output is correct |
8 |
Correct |
67 ms |
13432 KB |
Output is correct |
9 |
Correct |
266 ms |
28816 KB |
Output is correct |
10 |
Correct |
270 ms |
28792 KB |
Output is correct |
11 |
Correct |
283 ms |
28444 KB |
Output is correct |
12 |
Correct |
265 ms |
28536 KB |
Output is correct |
13 |
Correct |
215 ms |
22040 KB |
Output is correct |
14 |
Correct |
74 ms |
12996 KB |
Output is correct |
15 |
Correct |
262 ms |
26872 KB |
Output is correct |
16 |
Correct |
228 ms |
31356 KB |
Output is correct |
17 |
Correct |
212 ms |
30852 KB |
Output is correct |
18 |
Correct |
216 ms |
36560 KB |
Output is correct |
19 |
Correct |
176 ms |
27840 KB |
Output is correct |
20 |
Correct |
171 ms |
26304 KB |
Output is correct |
21 |
Correct |
162 ms |
23748 KB |
Output is correct |
22 |
Correct |
170 ms |
23584 KB |
Output is correct |
23 |
Correct |
135 ms |
21972 KB |
Output is correct |
24 |
Correct |
194 ms |
27836 KB |
Output is correct |
25 |
Correct |
224 ms |
27056 KB |
Output is correct |
26 |
Correct |
175 ms |
26056 KB |
Output is correct |
27 |
Correct |
159 ms |
26408 KB |
Output is correct |
28 |
Correct |
199 ms |
24120 KB |
Output is correct |
29 |
Correct |
344 ms |
44740 KB |
Output is correct |
30 |
Correct |
235 ms |
28896 KB |
Output is correct |
31 |
Correct |
333 ms |
37772 KB |
Output is correct |
32 |
Correct |
133 ms |
19960 KB |
Output is correct |
33 |
Correct |
334 ms |
38184 KB |
Output is correct |
34 |
Correct |
142 ms |
21084 KB |
Output is correct |
35 |
Correct |
341 ms |
37476 KB |
Output is correct |
36 |
Correct |
350 ms |
42236 KB |
Output is correct |
37 |
Correct |
290 ms |
41236 KB |
Output is correct |
38 |
Correct |
334 ms |
45088 KB |
Output is correct |
39 |
Correct |
185 ms |
25984 KB |
Output is correct |