#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
using namespace std;
const int N = 2e5 + 5;
struct BIT {
int n;
vector <long long> bit;
BIT(int _n) {
n = _n;
bit.assign(n + 5, 0);
}
void update(int pos, long long val) {
for (; pos <= n; pos += pos & -pos)
bit[pos] += val;
}
void update(int l, int r, long long val) {
update(l, val);
update(r + 1, -val);
}
long long getval(int pos) {
long long res = 0;
for (; pos > 0; pos -= pos & -pos)
res += bit[pos];
return res;
}
};
struct DisJointSet {
int n;
vector <int> par;
DisJointSet(int _n) {
n = _n;
par.assign(n + 5, -1);
}
int getpar(int u) {
return (par[u] < 0) ? u : (par[u] = getpar(par[u]));
}
bool join(int u, int v) {
u = getpar(u);
v = getpar(v);
if (u == v)
return false;
if (par[u] > par[v])
swap(u, v);
par[u] += par[v];
par[v] = u;
return true;
}
};
struct seg {
int h, l, r;
seg(){};
seg(int _h, int _l, int _r) {
h = _h;
l = _l;
r = _r;
}
bool operator < (const seg &s) const {
return h < s.h;
}
};
vector <int> adj[N];
pair <pair <int, int>, pair <int, int>> rec[N];
pair <int, int> a[N];
pair <int, int> b[N];
vector <pair <int, pair <int, int>>> edge;
long long sum[N];
int val[N];
int n,m,k;
int dis(int i, int j) {
return abs(a[i].fi - a[j].fi) + abs(a[i].se - a[j].se);
}
void compress() {
vector <int> val;
for (int i = 1; i <= n; i++)
val.push_back(a[i].fi);
for (int i = 1; i <= m; i++) {
val.push_back(rec[i].fi.fi);
val.push_back(rec[i].se.fi);
}
sort(val.begin(), val.end());
val.resize(unique(val.begin(), val.end()) - val.begin());
for (int i = 1; i <= n; i++)
b[i].fi = lower_bound(val.begin(), val.end(), a[i].fi) - val.begin() + 1;
for (int i = 1; i <= m; i++) {
rec[i].fi.fi = lower_bound(val.begin(), val.end(), rec[i].fi.fi) - val.begin() + 1;
rec[i].se.fi = lower_bound(val.begin(), val.end(), rec[i].se.fi) - val.begin() + 1;
}
val.clear();
for (int i = 1; i <= n; i++)
val.push_back(a[i].se);
for (int i = 1; i <= m; i++) {
val.push_back(rec[i].fi.se);
val.push_back(rec[i].se.se);
}
sort(val.begin(), val.end());
val.resize(unique(val.begin(), val.end()) - val.begin());
for (int i = 1; i <= n; i++)
b[i].se = lower_bound(val.begin(), val.end(), a[i].se) - val.begin() + 1;
for (int i = 1; i <= m; i++) {
rec[i].fi.se = lower_bound(val.begin(), val.end(), rec[i].fi.se) - val.begin() + 1;
rec[i].se.se = lower_bound(val.begin(), val.end(), rec[i].se.se) - val.begin() + 1;
}
}
void buildgraph() {
BIT bit(n + 2 * m);
vector <seg> s;
for (int i = 1; i <= n; i++)
s.push_back(seg(b[i].se, -i, b[i].fi));
for (int i = 1; i <= m; i++)
s.push_back(seg(rec[i].se.se, rec[i].fi.fi, rec[i].se.fi));
sort(s.begin(), s.end());
for (seg sg : s) {
// cout << sg.h << " " << sg.l << " " << sg.r << endl;
int l = sg.l;
int r = sg.r;
if (l < 0) {
int u = -l;
long long v = bit.getval(r);
if (v > 0)
edge.push_back(mp(dis(u, v), mp(u, v)));
bit.update(r, r, u - v);
} else
bit.update(l, r, -1e9);
}
// cout << endl;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m >> k;
for (int i = 1; i <= n; i++)
cin >> a[i].fi >> a[i].se;
for (int i = 1; i <= m; i++)
cin >> rec[i].fi.fi >> rec[i].fi.se >> rec[i].se.fi >> rec[i].se.se;
compress();
buildgraph();
for (int i = 1; i <= n; i++)
swap(b[i].fi, b[i].se);
for (int i = 1; i <= m; i++) {
swap(rec[i].fi.fi, rec[i].fi.se);
swap(rec[i].se.fi, rec[i].se.se);
}
buildgraph();
int cnt = 0;
DisJointSet dsu(n);
sort(edge.begin(), edge.end());
for (pair <int, pair <int, int>> e : edge) {
int u = e.se.fi;
int v = e.se.se;
if (dsu.join(u, v)) {
val[++cnt] = e.fi;
// cerr << u << " " << v << endl;
}
}
for (int i = 1; i <= cnt; i++)
sum[i] = sum[i - 1] + val[i];
while (k--) {
int cost,num;
cin >> cost >> num;
if (cnt + num < n) {
cout << -1 << "\n";
continue;
}
int lo = 0;
int hi = cnt + 1;
while (hi - lo > 1) {
int mid = (lo + hi) >> 1;
if (val[mid] < cost)
lo = mid;
else
hi = mid;
}
lo = max(lo, n - num);
cout << (long long) (n - lo) * cost + sum[lo] << "\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
5996 KB |
Output is correct |
2 |
Correct |
232 ms |
17656 KB |
Output is correct |
3 |
Correct |
243 ms |
17656 KB |
Output is correct |
4 |
Correct |
242 ms |
21672 KB |
Output is correct |
5 |
Correct |
258 ms |
22012 KB |
Output is correct |
6 |
Correct |
229 ms |
17784 KB |
Output is correct |
7 |
Correct |
213 ms |
22044 KB |
Output is correct |
8 |
Correct |
196 ms |
20472 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
5996 KB |
Output is correct |
2 |
Correct |
232 ms |
17656 KB |
Output is correct |
3 |
Correct |
243 ms |
17656 KB |
Output is correct |
4 |
Correct |
242 ms |
21672 KB |
Output is correct |
5 |
Correct |
258 ms |
22012 KB |
Output is correct |
6 |
Correct |
229 ms |
17784 KB |
Output is correct |
7 |
Correct |
213 ms |
22044 KB |
Output is correct |
8 |
Correct |
196 ms |
20472 KB |
Output is correct |
9 |
Correct |
643 ms |
27444 KB |
Output is correct |
10 |
Correct |
651 ms |
27616 KB |
Output is correct |
11 |
Correct |
622 ms |
27464 KB |
Output is correct |
12 |
Correct |
628 ms |
27528 KB |
Output is correct |
13 |
Correct |
480 ms |
28224 KB |
Output is correct |
14 |
Correct |
253 ms |
21900 KB |
Output is correct |
15 |
Correct |
645 ms |
27616 KB |
Output is correct |
16 |
Correct |
371 ms |
37492 KB |
Output is correct |
17 |
Correct |
388 ms |
37492 KB |
Output is correct |
18 |
Correct |
482 ms |
28712 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
5996 KB |
Output is correct |
2 |
Correct |
232 ms |
17656 KB |
Output is correct |
3 |
Correct |
243 ms |
17656 KB |
Output is correct |
4 |
Correct |
242 ms |
21672 KB |
Output is correct |
5 |
Correct |
258 ms |
22012 KB |
Output is correct |
6 |
Correct |
229 ms |
17784 KB |
Output is correct |
7 |
Correct |
213 ms |
22044 KB |
Output is correct |
8 |
Correct |
196 ms |
20472 KB |
Output is correct |
9 |
Correct |
468 ms |
25464 KB |
Output is correct |
10 |
Correct |
469 ms |
26212 KB |
Output is correct |
11 |
Correct |
444 ms |
22136 KB |
Output is correct |
12 |
Correct |
450 ms |
21756 KB |
Output is correct |
13 |
Correct |
450 ms |
19448 KB |
Output is correct |
14 |
Correct |
537 ms |
28160 KB |
Output is correct |
15 |
Correct |
528 ms |
25240 KB |
Output is correct |
16 |
Correct |
461 ms |
24628 KB |
Output is correct |
17 |
Correct |
406 ms |
22584 KB |
Output is correct |
18 |
Correct |
438 ms |
25552 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
5996 KB |
Output is correct |
2 |
Correct |
232 ms |
17656 KB |
Output is correct |
3 |
Correct |
243 ms |
17656 KB |
Output is correct |
4 |
Correct |
242 ms |
21672 KB |
Output is correct |
5 |
Correct |
258 ms |
22012 KB |
Output is correct |
6 |
Correct |
229 ms |
17784 KB |
Output is correct |
7 |
Correct |
213 ms |
22044 KB |
Output is correct |
8 |
Correct |
196 ms |
20472 KB |
Output is correct |
9 |
Correct |
643 ms |
27444 KB |
Output is correct |
10 |
Correct |
651 ms |
27616 KB |
Output is correct |
11 |
Correct |
622 ms |
27464 KB |
Output is correct |
12 |
Correct |
628 ms |
27528 KB |
Output is correct |
13 |
Correct |
480 ms |
28224 KB |
Output is correct |
14 |
Correct |
253 ms |
21900 KB |
Output is correct |
15 |
Correct |
645 ms |
27616 KB |
Output is correct |
16 |
Correct |
371 ms |
37492 KB |
Output is correct |
17 |
Correct |
388 ms |
37492 KB |
Output is correct |
18 |
Correct |
482 ms |
28712 KB |
Output is correct |
19 |
Correct |
468 ms |
25464 KB |
Output is correct |
20 |
Correct |
469 ms |
26212 KB |
Output is correct |
21 |
Correct |
444 ms |
22136 KB |
Output is correct |
22 |
Correct |
450 ms |
21756 KB |
Output is correct |
23 |
Correct |
450 ms |
19448 KB |
Output is correct |
24 |
Correct |
537 ms |
28160 KB |
Output is correct |
25 |
Correct |
528 ms |
25240 KB |
Output is correct |
26 |
Correct |
461 ms |
24628 KB |
Output is correct |
27 |
Correct |
406 ms |
22584 KB |
Output is correct |
28 |
Correct |
438 ms |
25552 KB |
Output is correct |
29 |
Correct |
815 ms |
26988 KB |
Output is correct |
30 |
Correct |
561 ms |
23108 KB |
Output is correct |
31 |
Correct |
769 ms |
27144 KB |
Output is correct |
32 |
Correct |
378 ms |
18124 KB |
Output is correct |
33 |
Correct |
797 ms |
27144 KB |
Output is correct |
34 |
Correct |
400 ms |
16172 KB |
Output is correct |
35 |
Correct |
785 ms |
26932 KB |
Output is correct |
36 |
Correct |
822 ms |
27012 KB |
Output is correct |
37 |
Correct |
582 ms |
42612 KB |
Output is correct |
38 |
Correct |
659 ms |
31604 KB |
Output is correct |
39 |
Correct |
444 ms |
26168 KB |
Output is correct |