#include "parks.h"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define F first
#define S second
#define INF 1e18
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define pii pair<int,int>
#define pll pair<ll,ll>
#define OK cout<<"Ok"<<endl;
#define MOD (ll)(1e9+7)
const int mxn=2e5+5;
int n,x[mxn],y[mxn],p[mxn],sz[mxn];
int ax[4]={1,1,-1,-1};
int ay[4]={-1,1,1,-1};
vector<pair<pii,int>>vt;
map<pii,int>mp;
vector<int>adj[mxn];
int fnd(int u){
if(p[u]==u) return u;
return p[u]=fnd(p[u]);
}
void Union(int u,int v){
u=fnd(u);
v=fnd(v);
if(u!=v){
if(sz[u]>sz[v]) swap(u,v);
sz[u]+=sz[v];
p[v]=u;
}
}
bool cmp(pair<pii,int> a,pair<pii,int> b){
return a.F.S<b.F.S;
}
bool cmp2(pair<pii,int> a,pair<pii,int> b){
return a.F.S+a.F.F<b.F.S+b.F.F;
}
int construct_roads(vector<int> xx, vector<int> yy) {
n=xx.size();
if(n==1){
build({},{},{},{});
return 1;
}
for(int i=0;i<n;i++){
vt.pb({{xx[i],yy[i]},i+1});
mp[{xx[i],yy[i]}]=i+1;
x[i+1]=xx[i];
y[i+1]=yy[i];
}
set<pii>st;
for(int i=1;i<=n;i++){
p[i]=i;
sz[i]=1;
}
vector<int>idxl,idxr,posl,posr;
sort(all(vt));
for(int i=1;i<n;i++){
if(abs(vt[i].F.F-vt[i-1].F.F)+abs(vt[i].F.S-vt[i-1].F.S)==2){
Union(vt[i-1].S,vt[i].S);
}
}
sort(all(vt),cmp);
for(int i=1;i<n;i++){
if(abs(vt[i].F.F-vt[i-1].F.F)+abs(vt[i].F.S-vt[i-1].F.S)==2){
Union(vt[i-1].S,vt[i].S);
}
}
sort(all(vt),cmp2);
if(sz[fnd(1)]!=n) return 0;
for(int i=1;i<=n;i++){
int l=vt[i-1].F.F,r=vt[i-1].F.S;
int node=mp[{l,r}];
int a=l+2,b=r;
int c=l,d=r+2;
int color=((l+r)/2)%2;
int idx=mp[{a,b}];
int idx2=mp[{c,d}];
if(idx){
int pos1=(l+a)/2,pos2=(r+b)/2;
if(color){
pos2--;
}
else{
pos2++;
}
if(!st.count({pos1,pos2})){
idxl.pb(node-1);
idxr.pb(idx-1);
posl.pb(pos1);
posr.pb(pos2);
st.insert({pos1,pos2});
}
}
if(idx2){
int pos1=(l+c)/2,pos2=(r+d)/2;
if(color){
pos1++;
}
else{
pos1--;
}
if(!st.count({pos1,pos2})){
idxl.pb(node-1);
idxr.pb(idx2-1);
posl.pb(pos1);
posr.pb(pos2);
st.insert({pos1,pos2});
}
}
}
build(idxl,idxr,posl,posr);
return 1;
}
/*
{ g++ parks.cpp grader.cpp } ; if ($?) { .\a.exe }
9
2 2
2 4
2 6
4 2
4 4
4 6
6 2
6 4
6 6
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
160 ms |
33900 KB |
Output is correct |
10 |
Correct |
13 ms |
7892 KB |
Output is correct |
11 |
Correct |
70 ms |
20536 KB |
Output is correct |
12 |
Correct |
20 ms |
9428 KB |
Output is correct |
13 |
Correct |
22 ms |
10032 KB |
Output is correct |
14 |
Correct |
3 ms |
5076 KB |
Output is correct |
15 |
Correct |
3 ms |
5204 KB |
Output is correct |
16 |
Correct |
164 ms |
33856 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
160 ms |
33900 KB |
Output is correct |
10 |
Correct |
13 ms |
7892 KB |
Output is correct |
11 |
Correct |
70 ms |
20536 KB |
Output is correct |
12 |
Correct |
20 ms |
9428 KB |
Output is correct |
13 |
Correct |
22 ms |
10032 KB |
Output is correct |
14 |
Correct |
3 ms |
5076 KB |
Output is correct |
15 |
Correct |
3 ms |
5204 KB |
Output is correct |
16 |
Correct |
164 ms |
33856 KB |
Output is correct |
17 |
Correct |
2 ms |
4948 KB |
Output is correct |
18 |
Correct |
2 ms |
4948 KB |
Output is correct |
19 |
Correct |
2 ms |
4948 KB |
Output is correct |
20 |
Correct |
2 ms |
4948 KB |
Output is correct |
21 |
Correct |
2 ms |
4948 KB |
Output is correct |
22 |
Correct |
2 ms |
4948 KB |
Output is correct |
23 |
Correct |
383 ms |
55360 KB |
Output is correct |
24 |
Correct |
2 ms |
4948 KB |
Output is correct |
25 |
Correct |
4 ms |
5268 KB |
Output is correct |
26 |
Correct |
4 ms |
5332 KB |
Output is correct |
27 |
Correct |
5 ms |
5332 KB |
Output is correct |
28 |
Correct |
131 ms |
25072 KB |
Output is correct |
29 |
Correct |
207 ms |
35152 KB |
Output is correct |
30 |
Correct |
282 ms |
45504 KB |
Output is correct |
31 |
Correct |
372 ms |
55344 KB |
Output is correct |
32 |
Correct |
3 ms |
4948 KB |
Output is correct |
33 |
Correct |
2 ms |
4916 KB |
Output is correct |
34 |
Correct |
2 ms |
4948 KB |
Output is correct |
35 |
Correct |
2 ms |
4948 KB |
Output is correct |
36 |
Correct |
2 ms |
4948 KB |
Output is correct |
37 |
Correct |
2 ms |
4948 KB |
Output is correct |
38 |
Correct |
2 ms |
4948 KB |
Output is correct |
39 |
Correct |
2 ms |
5008 KB |
Output is correct |
40 |
Correct |
2 ms |
4948 KB |
Output is correct |
41 |
Correct |
2 ms |
4948 KB |
Output is correct |
42 |
Correct |
2 ms |
4948 KB |
Output is correct |
43 |
Correct |
3 ms |
5204 KB |
Output is correct |
44 |
Correct |
4 ms |
5332 KB |
Output is correct |
45 |
Correct |
162 ms |
31684 KB |
Output is correct |
46 |
Correct |
252 ms |
44112 KB |
Output is correct |
47 |
Correct |
268 ms |
44000 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
160 ms |
33900 KB |
Output is correct |
10 |
Correct |
13 ms |
7892 KB |
Output is correct |
11 |
Correct |
70 ms |
20536 KB |
Output is correct |
12 |
Correct |
20 ms |
9428 KB |
Output is correct |
13 |
Correct |
22 ms |
10032 KB |
Output is correct |
14 |
Correct |
3 ms |
5076 KB |
Output is correct |
15 |
Correct |
3 ms |
5204 KB |
Output is correct |
16 |
Correct |
164 ms |
33856 KB |
Output is correct |
17 |
Correct |
2 ms |
4948 KB |
Output is correct |
18 |
Correct |
2 ms |
4948 KB |
Output is correct |
19 |
Correct |
2 ms |
4948 KB |
Output is correct |
20 |
Correct |
2 ms |
4948 KB |
Output is correct |
21 |
Correct |
2 ms |
4948 KB |
Output is correct |
22 |
Correct |
2 ms |
4948 KB |
Output is correct |
23 |
Correct |
383 ms |
55360 KB |
Output is correct |
24 |
Correct |
2 ms |
4948 KB |
Output is correct |
25 |
Correct |
4 ms |
5268 KB |
Output is correct |
26 |
Correct |
4 ms |
5332 KB |
Output is correct |
27 |
Correct |
5 ms |
5332 KB |
Output is correct |
28 |
Correct |
131 ms |
25072 KB |
Output is correct |
29 |
Correct |
207 ms |
35152 KB |
Output is correct |
30 |
Correct |
282 ms |
45504 KB |
Output is correct |
31 |
Correct |
372 ms |
55344 KB |
Output is correct |
32 |
Correct |
3 ms |
4948 KB |
Output is correct |
33 |
Correct |
2 ms |
4916 KB |
Output is correct |
34 |
Correct |
2 ms |
4948 KB |
Output is correct |
35 |
Correct |
2 ms |
4948 KB |
Output is correct |
36 |
Correct |
2 ms |
4948 KB |
Output is correct |
37 |
Correct |
2 ms |
4948 KB |
Output is correct |
38 |
Correct |
2 ms |
4948 KB |
Output is correct |
39 |
Correct |
2 ms |
5008 KB |
Output is correct |
40 |
Correct |
2 ms |
4948 KB |
Output is correct |
41 |
Correct |
2 ms |
4948 KB |
Output is correct |
42 |
Correct |
2 ms |
4948 KB |
Output is correct |
43 |
Correct |
3 ms |
5204 KB |
Output is correct |
44 |
Correct |
4 ms |
5332 KB |
Output is correct |
45 |
Correct |
162 ms |
31684 KB |
Output is correct |
46 |
Correct |
252 ms |
44112 KB |
Output is correct |
47 |
Correct |
268 ms |
44000 KB |
Output is correct |
48 |
Correct |
2 ms |
4948 KB |
Output is correct |
49 |
Correct |
2 ms |
4948 KB |
Output is correct |
50 |
Correct |
2 ms |
4948 KB |
Output is correct |
51 |
Correct |
2 ms |
4948 KB |
Output is correct |
52 |
Correct |
2 ms |
5020 KB |
Output is correct |
53 |
Correct |
2 ms |
4948 KB |
Output is correct |
54 |
Correct |
2 ms |
4948 KB |
Output is correct |
55 |
Correct |
348 ms |
52860 KB |
Output is correct |
56 |
Correct |
2 ms |
4948 KB |
Output is correct |
57 |
Correct |
5 ms |
5460 KB |
Output is correct |
58 |
Correct |
9 ms |
6428 KB |
Output is correct |
59 |
Correct |
8 ms |
6100 KB |
Output is correct |
60 |
Correct |
155 ms |
29556 KB |
Output is correct |
61 |
Correct |
227 ms |
39308 KB |
Output is correct |
62 |
Incorrect |
128 ms |
24508 KB |
Solution announced impossible, but it is possible. |
63 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
160 ms |
33900 KB |
Output is correct |
10 |
Correct |
13 ms |
7892 KB |
Output is correct |
11 |
Correct |
70 ms |
20536 KB |
Output is correct |
12 |
Correct |
20 ms |
9428 KB |
Output is correct |
13 |
Correct |
22 ms |
10032 KB |
Output is correct |
14 |
Correct |
3 ms |
5076 KB |
Output is correct |
15 |
Correct |
3 ms |
5204 KB |
Output is correct |
16 |
Correct |
164 ms |
33856 KB |
Output is correct |
17 |
Correct |
2 ms |
4948 KB |
Output is correct |
18 |
Correct |
2 ms |
4948 KB |
Output is correct |
19 |
Correct |
2 ms |
4948 KB |
Output is correct |
20 |
Correct |
327 ms |
56260 KB |
Output is correct |
21 |
Incorrect |
153 ms |
26132 KB |
Solution announced impossible, but it is possible. |
22 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
160 ms |
33900 KB |
Output is correct |
10 |
Correct |
13 ms |
7892 KB |
Output is correct |
11 |
Correct |
70 ms |
20536 KB |
Output is correct |
12 |
Correct |
20 ms |
9428 KB |
Output is correct |
13 |
Correct |
22 ms |
10032 KB |
Output is correct |
14 |
Correct |
3 ms |
5076 KB |
Output is correct |
15 |
Correct |
3 ms |
5204 KB |
Output is correct |
16 |
Correct |
164 ms |
33856 KB |
Output is correct |
17 |
Incorrect |
170 ms |
26744 KB |
Solution announced impossible, but it is possible. |
18 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
160 ms |
33900 KB |
Output is correct |
10 |
Correct |
13 ms |
7892 KB |
Output is correct |
11 |
Correct |
70 ms |
20536 KB |
Output is correct |
12 |
Correct |
20 ms |
9428 KB |
Output is correct |
13 |
Correct |
22 ms |
10032 KB |
Output is correct |
14 |
Correct |
3 ms |
5076 KB |
Output is correct |
15 |
Correct |
3 ms |
5204 KB |
Output is correct |
16 |
Correct |
164 ms |
33856 KB |
Output is correct |
17 |
Correct |
2 ms |
4948 KB |
Output is correct |
18 |
Correct |
2 ms |
4948 KB |
Output is correct |
19 |
Correct |
2 ms |
4948 KB |
Output is correct |
20 |
Correct |
2 ms |
4948 KB |
Output is correct |
21 |
Correct |
2 ms |
4948 KB |
Output is correct |
22 |
Correct |
2 ms |
4948 KB |
Output is correct |
23 |
Correct |
383 ms |
55360 KB |
Output is correct |
24 |
Correct |
2 ms |
4948 KB |
Output is correct |
25 |
Correct |
4 ms |
5268 KB |
Output is correct |
26 |
Correct |
4 ms |
5332 KB |
Output is correct |
27 |
Correct |
5 ms |
5332 KB |
Output is correct |
28 |
Correct |
131 ms |
25072 KB |
Output is correct |
29 |
Correct |
207 ms |
35152 KB |
Output is correct |
30 |
Correct |
282 ms |
45504 KB |
Output is correct |
31 |
Correct |
372 ms |
55344 KB |
Output is correct |
32 |
Correct |
3 ms |
4948 KB |
Output is correct |
33 |
Correct |
2 ms |
4916 KB |
Output is correct |
34 |
Correct |
2 ms |
4948 KB |
Output is correct |
35 |
Correct |
2 ms |
4948 KB |
Output is correct |
36 |
Correct |
2 ms |
4948 KB |
Output is correct |
37 |
Correct |
2 ms |
4948 KB |
Output is correct |
38 |
Correct |
2 ms |
4948 KB |
Output is correct |
39 |
Correct |
2 ms |
5008 KB |
Output is correct |
40 |
Correct |
2 ms |
4948 KB |
Output is correct |
41 |
Correct |
2 ms |
4948 KB |
Output is correct |
42 |
Correct |
2 ms |
4948 KB |
Output is correct |
43 |
Correct |
3 ms |
5204 KB |
Output is correct |
44 |
Correct |
4 ms |
5332 KB |
Output is correct |
45 |
Correct |
162 ms |
31684 KB |
Output is correct |
46 |
Correct |
252 ms |
44112 KB |
Output is correct |
47 |
Correct |
268 ms |
44000 KB |
Output is correct |
48 |
Correct |
2 ms |
4948 KB |
Output is correct |
49 |
Correct |
2 ms |
4948 KB |
Output is correct |
50 |
Correct |
2 ms |
4948 KB |
Output is correct |
51 |
Correct |
2 ms |
4948 KB |
Output is correct |
52 |
Correct |
2 ms |
5020 KB |
Output is correct |
53 |
Correct |
2 ms |
4948 KB |
Output is correct |
54 |
Correct |
2 ms |
4948 KB |
Output is correct |
55 |
Correct |
348 ms |
52860 KB |
Output is correct |
56 |
Correct |
2 ms |
4948 KB |
Output is correct |
57 |
Correct |
5 ms |
5460 KB |
Output is correct |
58 |
Correct |
9 ms |
6428 KB |
Output is correct |
59 |
Correct |
8 ms |
6100 KB |
Output is correct |
60 |
Correct |
155 ms |
29556 KB |
Output is correct |
61 |
Correct |
227 ms |
39308 KB |
Output is correct |
62 |
Incorrect |
128 ms |
24508 KB |
Solution announced impossible, but it is possible. |
63 |
Halted |
0 ms |
0 KB |
- |