#include<iostream>
#include<vector>
#include<cmath>
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#define MAXN 300007
using namespace std;
int bucket_sz=70;
int n,k,bucket[MAXN],kofa[MAXN];
int mins[MAXN][3],maxs[MAXN][3];
int li[MAXN],tim,cnt[MAXN];
vector<int> v[MAXN],r[MAXN],news[2];
void init(int N,int K){
n=N; k=K;
for(int i=1;i<=k;i++){
bucket[i]=i/bucket_sz+1;
kofa[i]=bucket[i]/bucket_sz+1;
}
for(int i=1;i<=k;i++){
mins[i][0]=maxs[i][0]=i;
mins[i][1]=maxs[i][1]=bucket[i];
mins[i][2]=maxs[i][2]=kofa[i];
}
for(int i=k+1;i<=n;i++){
mins[i][0]=n+1; maxs[i][0]=0;
mins[i][1]=n+1; maxs[i][1]=0;
mins[i][2]=n+1; maxs[i][2]=0;
}
}
int dfs(int x,int s1,int s2,int s3){
li[x]=tim;
if(s1<maxs[x][2])return 0;
if(maxs[x][2]==s1 and (mins[x][2]!=maxs[x][2] or maxs[x][1]>s2))return 0;
if(mins[x][2]==maxs[x][2] and s2<maxs[x][1])return 0;
if(mins[x][2]==maxs[x][2] and s2==maxs[x][1] and (mins[x][1]!=maxs[x][1] or maxs[x][0]>=s3))return 0;
if(mins[x][2]==maxs[x][2]){
if(mins[x][1]==maxs[x][2]){
maxs[x][0]=s3;
if(mins[x][0]<=maxs[x][0] or mins[x][0]<s3){
if(mins[x][0]<maxs[x][0] or x>k)return 1;
}
}else{
maxs[x][1]=s2;
if(mins[x][1]<maxs[x][1])return 1;
if(mins[x][1]==maxs[x][1])news[0].push_back(x);
}
}else{
maxs[x][2]=s1;
if(mins[x][2]<maxs[x][2])return 1;
if(mins[x][2]==maxs[x][2])news[1].push_back(x);
}
for(int i=0;i<v[x].size();i++){
if(li[v[x][i]]==tim)continue;
if(dfs(v[x][i],s1,s2,s3)==1)return 1;
}
return 0;
}
int dfs2(int x,int s1,int s2,int s3){
li[x]=tim;
if(s1>mins[x][2])return 0;
if(mins[x][2]==s1 and (mins[x][2]!=maxs[x][2] or mins[x][1]<s2))return 0;
if(mins[x][2]==maxs[x][2] and s2>mins[x][1])return 0;
if(mins[x][2]==maxs[x][2] and s2==mins[x][1] and (mins[x][1]!=maxs[x][1] or mins[x][0]<=s3))return 0;
if(mins[x][2]==maxs[x][2]){
if(mins[x][1]==maxs[x][2]){
mins[x][0]=s3;
if(mins[x][0]<=maxs[x][0] or s3<maxs[x][0]){
if(mins[x][0]<maxs[x][0] or x>k)return 1;
}
}else{
mins[x][1]=s2;
if(mins[x][1]<maxs[x][1])return 1;
if(mins[x][1]==maxs[x][1])news[0].push_back(x);
}
}else{
mins[x][2]=s1;
if(mins[x][2]<maxs[x][2])return 1;
if(mins[x][2]==maxs[x][2])news[1].push_back(x);
}
for(int i=0;i<r[x].size();i++){
if(li[r[x][i]]==tim)continue;
if(dfs2(r[x][i],s1,s2,s3)==1)return 1;
}
return 0;
}
void dfs3(int x,int id){
li[x]=tim;
if(mins[x][id]!=n+1)return;
for(int i=0;i<v[x].size();i++){
if(li[v[x][i]]==tim)continue;
dfs3(v[x][i],id);
mins[x][id]=min(mins[x][id],mins[v[x][i]][id]);
}
}
int dfs4(int x,int id){
li[x]=tim;
if(maxs[x][id]!=0)return 0;
int z=0;
for(int i=0;i<r[x].size();i++){
if(li[r[x][i]]==tim)continue;
z=dfs4(r[x][i],id);
maxs[x][id]=max(maxs[x][id],maxs[r[x][i]][id]);
if(z==1)return 1;
}
if(mins[x][id]<=maxs[x][id])return 1;
return 0;
}
int sum;
int add_teleporter(int a, int b){
a++; b++;
if(a==b){
if(a<=k)return 1;
return 0;
}
if(a<=k and b<=k){
if(a>=b)return 1;
return 0;
}
news[0].clear();
news[1].clear();
int z=0;
v[a].push_back(b);
tim++;
z=dfs(b,maxs[a][2],maxs[a][1],maxs[a][0]);
if(z==1)return 1;
r[b].push_back(a);
tim++;
z=dfs2(a,mins[b][2],mins[b][1],mins[b][0]);
if(z==1)return 1;
for(int t=1;t>=0;t--){
tim++;
for(int i:news[t]){
if(li[i]!=tim)dfs3(i,t);
}
tim++;
for(int i:news[t]){
if(li[i]!=tim){
if(dfs4(i,t)==1)return 1;
}
}
}
tim++;
z=dfs(b,maxs[a][2],maxs[a][1],maxs[a][0]);
if(z==1)return 1;
tim++;
z=dfs2(a,mins[b][2],mins[b][1],mins[b][0]);
if(z==1)return 1;
for(int t=1;t>=0;t--){
tim++;
for(int i:news[t]){
if(li[i]!=tim)dfs3(i,t);
}
tim++;
for(int i:news[t]){
if(li[i]!=tim){
if(dfs4(i,t)==1)return 1;
}
}
}
return 0;
}
/*int main(){
init(6, 3);
cout<<add_teleporter(3, 4);
cout<<add_teleporter(5, 0);
cout<<add_teleporter(4, 5);
cout<<add_teleporter(5, 3);
cout<<add_teleporter(1, 4);
return 0;
}*/
Compilation message
game.cpp: In function 'int dfs(int, int, int, int)':
game.cpp:72:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
72 | for(int i=0;i<v[x].size();i++){
| ~^~~~~~~~~~~~
game.cpp: In function 'int dfs2(int, int, int, int)':
game.cpp:111:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
111 | for(int i=0;i<r[x].size();i++){
| ~^~~~~~~~~~~~
game.cpp: In function 'void dfs3(int, int)':
game.cpp:125:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
125 | for(int i=0;i<v[x].size();i++){
| ~^~~~~~~~~~~~
game.cpp: In function 'int dfs4(int, int)':
game.cpp:139:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
139 | for(int i=0;i<r[x].size();i++){
| ~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14676 KB |
Output is correct |
2 |
Correct |
7 ms |
14424 KB |
Output is correct |
3 |
Correct |
7 ms |
14424 KB |
Output is correct |
4 |
Correct |
6 ms |
14424 KB |
Output is correct |
5 |
Correct |
7 ms |
14424 KB |
Output is correct |
6 |
Correct |
6 ms |
14424 KB |
Output is correct |
7 |
Correct |
6 ms |
14552 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14676 KB |
Output is correct |
2 |
Correct |
7 ms |
14424 KB |
Output is correct |
3 |
Correct |
7 ms |
14424 KB |
Output is correct |
4 |
Correct |
6 ms |
14424 KB |
Output is correct |
5 |
Correct |
7 ms |
14424 KB |
Output is correct |
6 |
Correct |
6 ms |
14424 KB |
Output is correct |
7 |
Correct |
6 ms |
14552 KB |
Output is correct |
8 |
Correct |
7 ms |
14424 KB |
Output is correct |
9 |
Correct |
6 ms |
14424 KB |
Output is correct |
10 |
Correct |
6 ms |
14424 KB |
Output is correct |
11 |
Incorrect |
7 ms |
14424 KB |
Wrong Answer[1] |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14676 KB |
Output is correct |
2 |
Correct |
7 ms |
14424 KB |
Output is correct |
3 |
Correct |
7 ms |
14424 KB |
Output is correct |
4 |
Correct |
6 ms |
14424 KB |
Output is correct |
5 |
Correct |
7 ms |
14424 KB |
Output is correct |
6 |
Correct |
6 ms |
14424 KB |
Output is correct |
7 |
Correct |
6 ms |
14552 KB |
Output is correct |
8 |
Correct |
7 ms |
14424 KB |
Output is correct |
9 |
Correct |
6 ms |
14424 KB |
Output is correct |
10 |
Correct |
6 ms |
14424 KB |
Output is correct |
11 |
Incorrect |
7 ms |
14424 KB |
Wrong Answer[1] |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14676 KB |
Output is correct |
2 |
Correct |
7 ms |
14424 KB |
Output is correct |
3 |
Correct |
7 ms |
14424 KB |
Output is correct |
4 |
Correct |
6 ms |
14424 KB |
Output is correct |
5 |
Correct |
7 ms |
14424 KB |
Output is correct |
6 |
Correct |
6 ms |
14424 KB |
Output is correct |
7 |
Correct |
6 ms |
14552 KB |
Output is correct |
8 |
Correct |
7 ms |
14424 KB |
Output is correct |
9 |
Correct |
6 ms |
14424 KB |
Output is correct |
10 |
Correct |
6 ms |
14424 KB |
Output is correct |
11 |
Incorrect |
7 ms |
14424 KB |
Wrong Answer[1] |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14676 KB |
Output is correct |
2 |
Correct |
7 ms |
14424 KB |
Output is correct |
3 |
Correct |
7 ms |
14424 KB |
Output is correct |
4 |
Correct |
6 ms |
14424 KB |
Output is correct |
5 |
Correct |
7 ms |
14424 KB |
Output is correct |
6 |
Correct |
6 ms |
14424 KB |
Output is correct |
7 |
Correct |
6 ms |
14552 KB |
Output is correct |
8 |
Correct |
7 ms |
14424 KB |
Output is correct |
9 |
Correct |
6 ms |
14424 KB |
Output is correct |
10 |
Correct |
6 ms |
14424 KB |
Output is correct |
11 |
Incorrect |
7 ms |
14424 KB |
Wrong Answer[1] |
12 |
Halted |
0 ms |
0 KB |
- |