# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
787367 |
2023-07-19T06:15:33 Z |
반딧불(#10031) |
도로 건설 사업 (JOI13_construction) |
C++17 |
|
1312 ms |
69200 KB |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
void input();
void makeEdges();
void getMST();
void answerQuery();
int main(){
input();
makeEdges();
getMST();
answerQuery();
}
struct Point{
int x, y, idx;
Point(){}
Point(int x, int y): x(x), y(y){}
bool operator<(const Point &r)const{
if(y != r.y) return y<r.y;
return x<r.x;
}
};
int n, k, q;
Point arr[200002];
int x1[200002], y_1[200002], x2[200002], y2[200002];
void input(){
scanf("%d %d %d", &n, &k, &q);
for(int i=1; i<=n; i++){
scanf("%d %d", &arr[i].x, &arr[i].y);
arr[i].idx = i;
}
for(int i=1; i<=k; i++){
scanf("%d %d %d %d", &x1[i], &y_1[i], &x2[i], &y2[i]);
}
}
struct segTree{
ll tree[1<<21], lazy[1<<21];
void init(int i, int l, int r){
tree[i] = lazy[i] = 0;
if(l==r) return;
int m = (l+r)>>1;
init(i*2, l, m);
init(i*2+1, m+1, r);
}
void propagate(int i, int l, int r){
tree[i] += lazy[i] * (r-l+1);
if(l!=r) lazy[i*2] += lazy[i], lazy[i*2+1] += lazy[i];
lazy[i] = 0;
}
void update(int i, int l, int r, int s, int e, int v){
propagate(i, l, r);
if(r<s || e<l) return;
if(s<=l && r<=e){
lazy[i] += v;
propagate(i, l, r);
return;
}
int m = (l+r)>>1;
update(i*2, l, m, s, e, v);
update(i*2+1, m+1, r, s, e, v);
tree[i] = tree[i*2] + tree[i*2+1];
}
ll query(int i, int l, int r, int s, int e){
propagate(i, l, r);
if(r<s || e<l) return 0;
if(s<=l && r<=e) return tree[i];
int m = (l+r)>>1;
return query(i*2, l, m, s, e) + query(i*2+1, m+1, r, s, e);
}
} tree;
struct Query{
int type;
int l, r, t, pa, pb;
Query(){}
Query(int type, int l, int r, int t): type(type), l(l), r(r), t(t){}
bool operator<(const Query &nxt)const{
if(t != nxt.t) return t < nxt.t;
return type < nxt.type;
}
};
struct Edge{
int x, y; ll w;
Edge(){}
Edge(int x, int y, ll w): x(x), y(y), w(w){}
bool operator<(const Edge &r)const{
return w<r.w;
}
};
vector<Edge> edges;
void makeEdges(){
vector<Query> vec;
vector<Point> points (arr+1, arr+n+1);
vector<int> xCoord;
int L;
int done = 0;
while(1){
sort(points.begin(), points.end());
for(int i=0; i<n-1; i++){
if(points[i].y == points[i+1].y){
vec.push_back(Query(0, points[i].x, points[i+1].x, points[i].y));
vec.back().pa = points[i].idx;
vec.back().pb = points[i+1].idx;
}
}
for(int i=1; i<=k; i++){
vec.push_back(Query(-1, x1[i], x2[i], y_1[i]));
vec.push_back(Query( 1, x1[i], x2[i], y2[i]));
}
for(Query p: vec) xCoord.push_back(p.l), xCoord.push_back(p.r);
sort(xCoord.begin(), xCoord.end());
xCoord.erase(unique(xCoord.begin(), xCoord.end()), xCoord.end());
L = (int)xCoord.size();
for(Query &p: vec){
p.l = lower_bound(xCoord.begin(), xCoord.end(), p.l) - xCoord.begin();
p.r = lower_bound(xCoord.begin(), xCoord.end(), p.r) - xCoord.begin();
}
sort(vec.begin(), vec.end());
for(Query &p: vec){
if(p.type == 0){
ll v = tree.query(1, 0, L-1, p.l, p.r);
if(!v) edges.push_back(Edge(p.pa, p.pb, xCoord[p.r]-xCoord[p.l]));
}
else tree.update(1, 0, L-1, p.l, p.r, -p.type);
}
tree.init(1, 0, L-1);
if(++done == 2) return;
for(Point &p: points) swap(p.x, p.y);
for(int i=1; i<=k; i++) swap(x1[i], y_1[i]), swap(x2[i], y2[i]);
xCoord.clear();
vec.clear();
}
}
struct unionFind{
int par[200002];
void init(int n){
for(int i=1; i<=n; i++) par[i] = i;
}
int find(int x){
if(x==par[x]) return x;
return par[x] = find(par[x]);
}
void merge(int x, int y){
x=find(x), y=find(y);
par[x] = y;
}
} dsu;
int l;
ll weight[200002];
ll sum[200002];
int MIN;
void getMST(){
dsu.init(n);
sort(edges.begin(), edges.end());
for(Edge p: edges){
if(dsu.find(p.x) == dsu.find(p.y)) continue;
dsu.merge(p.x, p.y);
weight[++l] = p.w;
}
MIN = n-l;
for(int i=1; i<=l; i++) sum[i] = sum[i-1] + weight[i];
#ifdef TEST
printf("%d roads: ", l);
for(int i=1; i<=l; i++) printf("%lld ", weight[i]);
puts("");
#endif
}
void answerQuery(){
while(q--){
ll a; int b;
scanf("%lld %d", &a, &b);
if(b < MIN){
puts("-1");
continue;
}
b -= MIN;
int L = max(l-b+1, 1), R = l, ans = l+1;
while(L<=R){
int M=(L+R)/2;
if(weight[M] > a) ans = M, R = M-1;
else L = M+1;
}
printf("%lld\n", sum[ans-1] + ll(n-ans+1)*a);
}
}
Compilation message
construction.cpp: In function 'void input()':
construction.cpp:35:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
35 | scanf("%d %d %d", &n, &k, &q);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
construction.cpp:37:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
37 | scanf("%d %d", &arr[i].x, &arr[i].y);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
construction.cpp:41:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
41 | scanf("%d %d %d %d", &x1[i], &y_1[i], &x2[i], &y2[i]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
construction.cpp: In function 'void answerQuery()':
construction.cpp:194:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
194 | scanf("%lld %d", &a, &b);
| ~~~~~^~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
1488 KB |
Output is correct |
2 |
Correct |
219 ms |
15640 KB |
Output is correct |
3 |
Correct |
267 ms |
15888 KB |
Output is correct |
4 |
Correct |
276 ms |
23600 KB |
Output is correct |
5 |
Correct |
241 ms |
18352 KB |
Output is correct |
6 |
Correct |
236 ms |
15932 KB |
Output is correct |
7 |
Correct |
167 ms |
23600 KB |
Output is correct |
8 |
Correct |
163 ms |
26768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
1488 KB |
Output is correct |
2 |
Correct |
219 ms |
15640 KB |
Output is correct |
3 |
Correct |
267 ms |
15888 KB |
Output is correct |
4 |
Correct |
276 ms |
23600 KB |
Output is correct |
5 |
Correct |
241 ms |
18352 KB |
Output is correct |
6 |
Correct |
236 ms |
15932 KB |
Output is correct |
7 |
Correct |
167 ms |
23600 KB |
Output is correct |
8 |
Correct |
163 ms |
26768 KB |
Output is correct |
9 |
Correct |
1168 ms |
44332 KB |
Output is correct |
10 |
Correct |
1112 ms |
44296 KB |
Output is correct |
11 |
Correct |
1153 ms |
44276 KB |
Output is correct |
12 |
Correct |
1159 ms |
44288 KB |
Output is correct |
13 |
Correct |
746 ms |
35168 KB |
Output is correct |
14 |
Correct |
252 ms |
19640 KB |
Output is correct |
15 |
Correct |
1175 ms |
45604 KB |
Output is correct |
16 |
Correct |
486 ms |
42688 KB |
Output is correct |
17 |
Correct |
478 ms |
42696 KB |
Output is correct |
18 |
Correct |
634 ms |
69200 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
1488 KB |
Output is correct |
2 |
Correct |
219 ms |
15640 KB |
Output is correct |
3 |
Correct |
267 ms |
15888 KB |
Output is correct |
4 |
Correct |
276 ms |
23600 KB |
Output is correct |
5 |
Correct |
241 ms |
18352 KB |
Output is correct |
6 |
Correct |
236 ms |
15932 KB |
Output is correct |
7 |
Correct |
167 ms |
23600 KB |
Output is correct |
8 |
Correct |
163 ms |
26768 KB |
Output is correct |
9 |
Correct |
421 ms |
18864 KB |
Output is correct |
10 |
Correct |
399 ms |
20444 KB |
Output is correct |
11 |
Correct |
361 ms |
15744 KB |
Output is correct |
12 |
Correct |
460 ms |
23816 KB |
Output is correct |
13 |
Correct |
281 ms |
12664 KB |
Output is correct |
14 |
Correct |
388 ms |
22576 KB |
Output is correct |
15 |
Correct |
435 ms |
18692 KB |
Output is correct |
16 |
Correct |
415 ms |
18112 KB |
Output is correct |
17 |
Correct |
339 ms |
23948 KB |
Output is correct |
18 |
Correct |
338 ms |
27100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
1488 KB |
Output is correct |
2 |
Correct |
219 ms |
15640 KB |
Output is correct |
3 |
Correct |
267 ms |
15888 KB |
Output is correct |
4 |
Correct |
276 ms |
23600 KB |
Output is correct |
5 |
Correct |
241 ms |
18352 KB |
Output is correct |
6 |
Correct |
236 ms |
15932 KB |
Output is correct |
7 |
Correct |
167 ms |
23600 KB |
Output is correct |
8 |
Correct |
163 ms |
26768 KB |
Output is correct |
9 |
Correct |
1168 ms |
44332 KB |
Output is correct |
10 |
Correct |
1112 ms |
44296 KB |
Output is correct |
11 |
Correct |
1153 ms |
44276 KB |
Output is correct |
12 |
Correct |
1159 ms |
44288 KB |
Output is correct |
13 |
Correct |
746 ms |
35168 KB |
Output is correct |
14 |
Correct |
252 ms |
19640 KB |
Output is correct |
15 |
Correct |
1175 ms |
45604 KB |
Output is correct |
16 |
Correct |
486 ms |
42688 KB |
Output is correct |
17 |
Correct |
478 ms |
42696 KB |
Output is correct |
18 |
Correct |
634 ms |
69200 KB |
Output is correct |
19 |
Correct |
421 ms |
18864 KB |
Output is correct |
20 |
Correct |
399 ms |
20444 KB |
Output is correct |
21 |
Correct |
361 ms |
15744 KB |
Output is correct |
22 |
Correct |
460 ms |
23816 KB |
Output is correct |
23 |
Correct |
281 ms |
12664 KB |
Output is correct |
24 |
Correct |
388 ms |
22576 KB |
Output is correct |
25 |
Correct |
435 ms |
18692 KB |
Output is correct |
26 |
Correct |
415 ms |
18112 KB |
Output is correct |
27 |
Correct |
339 ms |
23948 KB |
Output is correct |
28 |
Correct |
338 ms |
27100 KB |
Output is correct |
29 |
Correct |
1298 ms |
44420 KB |
Output is correct |
30 |
Correct |
753 ms |
37760 KB |
Output is correct |
31 |
Correct |
1312 ms |
44420 KB |
Output is correct |
32 |
Correct |
364 ms |
14968 KB |
Output is correct |
33 |
Correct |
1213 ms |
44404 KB |
Output is correct |
34 |
Correct |
351 ms |
15000 KB |
Output is correct |
35 |
Correct |
994 ms |
41284 KB |
Output is correct |
36 |
Correct |
1247 ms |
44408 KB |
Output is correct |
37 |
Correct |
643 ms |
42792 KB |
Output is correct |
38 |
Correct |
787 ms |
68008 KB |
Output is correct |
39 |
Correct |
365 ms |
28624 KB |
Output is correct |