#include "parks.h"
#include <bits/stdc++.h>
#define pb push_back
#define ep insert
#define F first
#define S second
using namespace std;
const int N=2e5+5;
int a[N],b[N],par[N],sz[N],n;
vector<int> c[N],r[N];
vector<int> adj[N];
bool cmp(int x,int y){
return a[x]<a[y];
}
bool cmp2(int x,int y){
return b[x]>b[y];
}
int getpar(int x){
if (x==par[x]) return x;
return par[x]=getpar(par[x]);
}
void link(int x,int y){
x=getpar(x);
y=getpar(y);
if (x==y) return;
if (sz[x]>sz[y]) swap(x,y);
sz[y]+=sz[x];
par[x]=y;
return;
}
int construct_roads(std::vector<int> x, std::vector<int> y) {
if (x.size() == 1) {
build({}, {}, {}, {});
return 1;
}
n=x.size();
for (int i=0;i<n;i++){
par[i]=i;
sz[i]=1;
a[i]=x[i];
b[i]=y[i];
c[y[i]].pb(i);
r[x[i]].pb(i);
}
vector<int> U,V,s;
for (int i=0;i<N;i+=2) sort(c[i].begin(),c[i].end(),cmp);
for (int i=0;i<N;i+=2){
for (int j=0;j<int(c[i].size())-1;j++){
if (a[c[i][j+1]]-a[c[i][j]]==2){
link(c[i][j],c[i][j+1]);
U.pb(c[i][j]);
V.pb(c[i][j+1]);
s.pb(0);
adj[c[i][j]].pb(U.size()-1);
adj[c[i][j+1]].pb(U.size()-1);
}
}
}
for (int i=N-1;i>=0;i--){
for (int j=0;j<int(c[i].size());j++){
int l=0,r=int(c[i+2].size())-1,mid,idx=-1;
while (l<=r){
mid=(l+r)/2;
if (a[c[i+2][mid]]==a[c[i][j]]){
idx=mid;
break;
}
if (a[c[i+2][mid]]>a[c[i][j]]) r=mid-1;
else l=mid+1;
}
if (idx!=-1){
link(c[i][j],c[i+2][idx]);
V.pb(c[i][j]);
U.pb(c[i+2][idx]);
s.pb(2);
adj[c[i][j]].pb(U.size()-1);
adj[c[i+2][idx]].pb(U.size()-1);
}
}
}
if (sz[getpar(0)]!=n) return 0;
//for (int i=0;i<N;i+=2) sort(r[i].begin(),r[i].end(),cmp2);
vector<int> d(U.size()),e(U.size());
set<pair<int,int>> bench;
bool h=0;
int last=-1;
for (int i=0;i<U.size();i++){
if (s[i]==0) continue;
if (a[U[i]]!=last){
last=a[U[i]];
h=0;
}
if (!h){
d[i]=a[U[i]]-1;
e[i]=b[U[i]]-1;
bench.ep({d[i],e[i]});
h=!h;
}
else{
d[i]=a[U[i]]+1;
e[i]=b[U[i]]-1;
bench.ep({d[i],e[i]});
h=!h;
}
}
//for (auto u:bench) cout<<u.F<<' '<<u.S<<endl;
for (int i=0;i<U.size();i++){
if (s[i]==2) continue;
int xx=a[U[i]]+1,yy=b[U[i]]-1;
if (!bench.count({xx,yy})){
d[i]=xx;
e[i]=yy;
bench.ep({xx,yy});
continue;
}
yy+=2;
d[i]=xx;
e[i]=yy;
bench.ep({xx,yy});
}
build(U,V,d,e);
return 1;
}
Compilation message
parks.cpp: In function 'int construct_roads(std::vector<int>, std::vector<int>)':
parks.cpp:87:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
87 | for (int i=0;i<U.size();i++){
| ~^~~~~~~~~
parks.cpp:107:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
107 | for (int i=0;i<U.size();i++){
| ~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
14940 KB |
Output is correct |
2 |
Correct |
3 ms |
16988 KB |
Output is correct |
3 |
Correct |
3 ms |
16988 KB |
Output is correct |
4 |
Correct |
3 ms |
16984 KB |
Output is correct |
5 |
Correct |
3 ms |
17144 KB |
Output is correct |
6 |
Correct |
3 ms |
16988 KB |
Output is correct |
7 |
Correct |
3 ms |
16988 KB |
Output is correct |
8 |
Correct |
3 ms |
16988 KB |
Output is correct |
9 |
Correct |
72 ms |
35332 KB |
Output is correct |
10 |
Correct |
9 ms |
18780 KB |
Output is correct |
11 |
Correct |
33 ms |
26828 KB |
Output is correct |
12 |
Correct |
14 ms |
19804 KB |
Output is correct |
13 |
Correct |
12 ms |
21208 KB |
Output is correct |
14 |
Correct |
3 ms |
16988 KB |
Output is correct |
15 |
Correct |
3 ms |
16988 KB |
Output is correct |
16 |
Correct |
75 ms |
35260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
14940 KB |
Output is correct |
2 |
Correct |
3 ms |
16988 KB |
Output is correct |
3 |
Correct |
3 ms |
16988 KB |
Output is correct |
4 |
Correct |
3 ms |
16984 KB |
Output is correct |
5 |
Correct |
3 ms |
17144 KB |
Output is correct |
6 |
Correct |
3 ms |
16988 KB |
Output is correct |
7 |
Correct |
3 ms |
16988 KB |
Output is correct |
8 |
Correct |
3 ms |
16988 KB |
Output is correct |
9 |
Correct |
72 ms |
35332 KB |
Output is correct |
10 |
Correct |
9 ms |
18780 KB |
Output is correct |
11 |
Correct |
33 ms |
26828 KB |
Output is correct |
12 |
Correct |
14 ms |
19804 KB |
Output is correct |
13 |
Correct |
12 ms |
21208 KB |
Output is correct |
14 |
Correct |
3 ms |
16988 KB |
Output is correct |
15 |
Correct |
3 ms |
16988 KB |
Output is correct |
16 |
Correct |
75 ms |
35260 KB |
Output is correct |
17 |
Correct |
3 ms |
16988 KB |
Output is correct |
18 |
Correct |
3 ms |
16988 KB |
Output is correct |
19 |
Incorrect |
3 ms |
16988 KB |
Tree @(3, 5) appears more than once: for edges on positions 1 and 4 |
20 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
14940 KB |
Output is correct |
2 |
Correct |
3 ms |
16988 KB |
Output is correct |
3 |
Correct |
3 ms |
16988 KB |
Output is correct |
4 |
Correct |
3 ms |
16984 KB |
Output is correct |
5 |
Correct |
3 ms |
17144 KB |
Output is correct |
6 |
Correct |
3 ms |
16988 KB |
Output is correct |
7 |
Correct |
3 ms |
16988 KB |
Output is correct |
8 |
Correct |
3 ms |
16988 KB |
Output is correct |
9 |
Correct |
72 ms |
35332 KB |
Output is correct |
10 |
Correct |
9 ms |
18780 KB |
Output is correct |
11 |
Correct |
33 ms |
26828 KB |
Output is correct |
12 |
Correct |
14 ms |
19804 KB |
Output is correct |
13 |
Correct |
12 ms |
21208 KB |
Output is correct |
14 |
Correct |
3 ms |
16988 KB |
Output is correct |
15 |
Correct |
3 ms |
16988 KB |
Output is correct |
16 |
Correct |
75 ms |
35260 KB |
Output is correct |
17 |
Correct |
3 ms |
16988 KB |
Output is correct |
18 |
Correct |
3 ms |
16988 KB |
Output is correct |
19 |
Incorrect |
3 ms |
16988 KB |
Tree @(3, 5) appears more than once: for edges on positions 1 and 4 |
20 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
14940 KB |
Output is correct |
2 |
Correct |
3 ms |
16988 KB |
Output is correct |
3 |
Correct |
3 ms |
16988 KB |
Output is correct |
4 |
Correct |
3 ms |
16984 KB |
Output is correct |
5 |
Correct |
3 ms |
17144 KB |
Output is correct |
6 |
Correct |
3 ms |
16988 KB |
Output is correct |
7 |
Correct |
3 ms |
16988 KB |
Output is correct |
8 |
Correct |
3 ms |
16988 KB |
Output is correct |
9 |
Correct |
72 ms |
35332 KB |
Output is correct |
10 |
Correct |
9 ms |
18780 KB |
Output is correct |
11 |
Correct |
33 ms |
26828 KB |
Output is correct |
12 |
Correct |
14 ms |
19804 KB |
Output is correct |
13 |
Correct |
12 ms |
21208 KB |
Output is correct |
14 |
Correct |
3 ms |
16988 KB |
Output is correct |
15 |
Correct |
3 ms |
16988 KB |
Output is correct |
16 |
Correct |
75 ms |
35260 KB |
Output is correct |
17 |
Correct |
3 ms |
16988 KB |
Output is correct |
18 |
Correct |
3 ms |
16988 KB |
Output is correct |
19 |
Correct |
3 ms |
16988 KB |
Output is correct |
20 |
Correct |
174 ms |
52860 KB |
Output is correct |
21 |
Correct |
149 ms |
49716 KB |
Output is correct |
22 |
Correct |
136 ms |
49752 KB |
Output is correct |
23 |
Correct |
138 ms |
45112 KB |
Output is correct |
24 |
Correct |
39 ms |
22620 KB |
Output is correct |
25 |
Correct |
69 ms |
31028 KB |
Output is correct |
26 |
Correct |
67 ms |
31024 KB |
Output is correct |
27 |
Correct |
136 ms |
48372 KB |
Output is correct |
28 |
Correct |
128 ms |
48492 KB |
Output is correct |
29 |
Correct |
146 ms |
48436 KB |
Output is correct |
30 |
Correct |
132 ms |
48436 KB |
Output is correct |
31 |
Correct |
3 ms |
16984 KB |
Output is correct |
32 |
Incorrect |
13 ms |
19532 KB |
Tree @(185945, 20999) appears more than once: for edges on positions 7091 and 7116 |
33 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
14940 KB |
Output is correct |
2 |
Correct |
3 ms |
16988 KB |
Output is correct |
3 |
Correct |
3 ms |
16988 KB |
Output is correct |
4 |
Correct |
3 ms |
16984 KB |
Output is correct |
5 |
Correct |
3 ms |
17144 KB |
Output is correct |
6 |
Correct |
3 ms |
16988 KB |
Output is correct |
7 |
Correct |
3 ms |
16988 KB |
Output is correct |
8 |
Correct |
3 ms |
16988 KB |
Output is correct |
9 |
Correct |
72 ms |
35332 KB |
Output is correct |
10 |
Correct |
9 ms |
18780 KB |
Output is correct |
11 |
Correct |
33 ms |
26828 KB |
Output is correct |
12 |
Correct |
14 ms |
19804 KB |
Output is correct |
13 |
Correct |
12 ms |
21208 KB |
Output is correct |
14 |
Correct |
3 ms |
16988 KB |
Output is correct |
15 |
Correct |
3 ms |
16988 KB |
Output is correct |
16 |
Correct |
75 ms |
35260 KB |
Output is correct |
17 |
Correct |
156 ms |
50356 KB |
Output is correct |
18 |
Incorrect |
157 ms |
50484 KB |
Tree @(50001, 50003) appears more than once: for edges on positions 49999 and 149998 |
19 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
14940 KB |
Output is correct |
2 |
Correct |
3 ms |
16988 KB |
Output is correct |
3 |
Correct |
3 ms |
16988 KB |
Output is correct |
4 |
Correct |
3 ms |
16984 KB |
Output is correct |
5 |
Correct |
3 ms |
17144 KB |
Output is correct |
6 |
Correct |
3 ms |
16988 KB |
Output is correct |
7 |
Correct |
3 ms |
16988 KB |
Output is correct |
8 |
Correct |
3 ms |
16988 KB |
Output is correct |
9 |
Correct |
72 ms |
35332 KB |
Output is correct |
10 |
Correct |
9 ms |
18780 KB |
Output is correct |
11 |
Correct |
33 ms |
26828 KB |
Output is correct |
12 |
Correct |
14 ms |
19804 KB |
Output is correct |
13 |
Correct |
12 ms |
21208 KB |
Output is correct |
14 |
Correct |
3 ms |
16988 KB |
Output is correct |
15 |
Correct |
3 ms |
16988 KB |
Output is correct |
16 |
Correct |
75 ms |
35260 KB |
Output is correct |
17 |
Correct |
3 ms |
16988 KB |
Output is correct |
18 |
Correct |
3 ms |
16988 KB |
Output is correct |
19 |
Incorrect |
3 ms |
16988 KB |
Tree @(3, 5) appears more than once: for edges on positions 1 and 4 |
20 |
Halted |
0 ms |
0 KB |
- |