#include<bits/stdc++.h>
#define MAXN 300007
using namespace std;
const int bucket_sz=300;
int n,k,bucket[MAXN];
int from[MAXN],to[MAXN];
int mins[MAXN],maxs[MAXN];
int li[MAXN],tim,cnt[MAXN];
vector<int> v[MAXN],r[MAXN],news;
void init(int N,int K){
n=N; k=K;
for(int i=1;i<=k;i++){
bucket[i]=i/bucket_sz+1;
}
for(int i=1;i<=k;i++){
mins[i]=maxs[i]=bucket[i];
from[i]=to[i]=i;
}
for(int i=k+1;i<=n;i++){
from[i]=n+1; to[i]=0;
mins[i]=800; maxs[i]=0;
}
}
int dfs(int x,int val,int val2){
li[x]=tim;
if(maxs[x]>val)return 0;
if(maxs[x]==val and (mins[x]!=maxs[x] or to[x]>=val2))return 0;
if(mins[x]==maxs[x]){
to[x]=val2;
if(from[x]<=to[x] or mins[x]<val){
if(from[x]<to[x] or x>k)return 1;
}
}else{
maxs[x]=val;
if(mins[x]<maxs[x])return 1;
if(mins[x]==maxs[x])news.push_back(x);
}
//cnt[x]++;
//if(cnt[x]>1500)cout<<1/0;
for(int i=0;i<v[x].size();i++){
if(li[v[x][i]]==tim)continue;
if(dfs(v[x][i],val,val2)==1)return 1;
}
return 0;
}
int dfs2(int x,int val,int val2){
li[x]=tim;
if(mins[x]<val)return 0;
if(mins[x]==val and (mins[x]!=maxs[x] or from[x]<=val2))return 0;
if(mins[x]==maxs[x]){
from[x]=val2;
if(from[x]<=to[x] or val<maxs[x]){
if(from[x]<to[x] or x>k)return 1;
}
}else{
mins[x]=val;
if(mins[x]<maxs[x])return 1;
if(mins[x]==maxs[x])news.push_back(x);
}
//cnt[x]++;
//if(cnt[x]>1500)cout<<1/0;
for(int i=0;i<r[x].size();i++){
if(li[r[x][i]]==tim)continue;
if(dfs2(r[x][i],val,val2)==1)return 1;
}
return 0;
}
void dfs3(int x){
li[x]=tim;
if(from[x]!=n+1)return;
//cnt[x]++;
//if(cnt[x]>1500)cout<<1/0;
for(int i=0;i<v[x].size();i++){
if(li[v[x][i]]==tim)continue;
dfs3(v[x][i]);
from[x]=min(from[x],from[v[x][i]]);
}
}
int dfs4(int x){
li[x]=tim;
if(to[x]!=0)return 0;
//cnt[x]++;
//if(cnt[x]>1500)cout<<1/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]);
to[x]=max(to[x],to[r[x][i]]);
if(z==1)return 1;
}
if(from[x]<=to[x])return 1;
return 0;
}
int add_teleporter(int a, int b){
a++; b++;
if(a==b){
if(a<=k)return 1;
return 0;
}
news.clear();
v[a].push_back(b); tim++;
int z=dfs(b,maxs[a],to[a]);
if(z==1)return 1;
r[b].push_back(a); tim++;
z=dfs2(a,mins[b],from[b]);
if(z==1)return 1;
tim++;
for(int i:news){
if(li[i]!=tim)dfs3(i);
}
tim++;
for(int i:news){
if(li[i]!=tim){
if(dfs4(i)==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)':
game.cpp:55:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
55 | for(int i=0;i<v[x].size();i++){
| ~^~~~~~~~~~~~
game.cpp: In function 'int dfs2(int, int, int)':
game.cpp:85:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
85 | for(int i=0;i<r[x].size();i++){
| ~^~~~~~~~~~~~
game.cpp: In function 'void dfs3(int)':
game.cpp:102:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
102 | for(int i=0;i<v[x].size();i++){
| ~^~~~~~~~~~~~
game.cpp: In function 'int dfs4(int)':
game.cpp:119:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
119 | for(int i=0;i<r[x].size();i++){
| ~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
14424 KB |
Output is correct |
2 |
Correct |
7 ms |
14424 KB |
Output is correct |
3 |
Correct |
6 ms |
14424 KB |
Output is correct |
4 |
Correct |
6 ms |
14396 KB |
Output is correct |
5 |
Correct |
6 ms |
14424 KB |
Output is correct |
6 |
Correct |
6 ms |
14592 KB |
Output is correct |
7 |
Correct |
6 ms |
14424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
14424 KB |
Output is correct |
2 |
Correct |
7 ms |
14424 KB |
Output is correct |
3 |
Correct |
6 ms |
14424 KB |
Output is correct |
4 |
Correct |
6 ms |
14396 KB |
Output is correct |
5 |
Correct |
6 ms |
14424 KB |
Output is correct |
6 |
Correct |
6 ms |
14592 KB |
Output is correct |
7 |
Correct |
6 ms |
14424 KB |
Output is correct |
8 |
Correct |
6 ms |
14424 KB |
Output is correct |
9 |
Correct |
6 ms |
14424 KB |
Output is correct |
10 |
Correct |
7 ms |
14424 KB |
Output is correct |
11 |
Correct |
6 ms |
14424 KB |
Output is correct |
12 |
Correct |
6 ms |
14424 KB |
Output is correct |
13 |
Correct |
6 ms |
14424 KB |
Output is correct |
14 |
Correct |
6 ms |
14424 KB |
Output is correct |
15 |
Correct |
8 ms |
14424 KB |
Output is correct |
16 |
Correct |
6 ms |
14424 KB |
Output is correct |
17 |
Correct |
6 ms |
14548 KB |
Output is correct |
18 |
Correct |
8 ms |
14424 KB |
Output is correct |
19 |
Correct |
6 ms |
14424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
14424 KB |
Output is correct |
2 |
Correct |
7 ms |
14424 KB |
Output is correct |
3 |
Correct |
6 ms |
14424 KB |
Output is correct |
4 |
Correct |
6 ms |
14396 KB |
Output is correct |
5 |
Correct |
6 ms |
14424 KB |
Output is correct |
6 |
Correct |
6 ms |
14592 KB |
Output is correct |
7 |
Correct |
6 ms |
14424 KB |
Output is correct |
8 |
Correct |
6 ms |
14424 KB |
Output is correct |
9 |
Correct |
6 ms |
14424 KB |
Output is correct |
10 |
Correct |
7 ms |
14424 KB |
Output is correct |
11 |
Correct |
6 ms |
14424 KB |
Output is correct |
12 |
Correct |
6 ms |
14424 KB |
Output is correct |
13 |
Correct |
6 ms |
14424 KB |
Output is correct |
14 |
Correct |
6 ms |
14424 KB |
Output is correct |
15 |
Correct |
8 ms |
14424 KB |
Output is correct |
16 |
Correct |
6 ms |
14424 KB |
Output is correct |
17 |
Correct |
6 ms |
14548 KB |
Output is correct |
18 |
Correct |
8 ms |
14424 KB |
Output is correct |
19 |
Correct |
6 ms |
14424 KB |
Output is correct |
20 |
Correct |
7 ms |
14424 KB |
Output is correct |
21 |
Correct |
7 ms |
14424 KB |
Output is correct |
22 |
Correct |
7 ms |
14424 KB |
Output is correct |
23 |
Correct |
7 ms |
14424 KB |
Output is correct |
24 |
Correct |
7 ms |
14424 KB |
Output is correct |
25 |
Correct |
8 ms |
14468 KB |
Output is correct |
26 |
Correct |
8 ms |
14680 KB |
Output is correct |
27 |
Correct |
10 ms |
14668 KB |
Output is correct |
28 |
Correct |
9 ms |
14424 KB |
Output is correct |
29 |
Correct |
8 ms |
14424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
14424 KB |
Output is correct |
2 |
Correct |
7 ms |
14424 KB |
Output is correct |
3 |
Correct |
6 ms |
14424 KB |
Output is correct |
4 |
Correct |
6 ms |
14396 KB |
Output is correct |
5 |
Correct |
6 ms |
14424 KB |
Output is correct |
6 |
Correct |
6 ms |
14592 KB |
Output is correct |
7 |
Correct |
6 ms |
14424 KB |
Output is correct |
8 |
Correct |
6 ms |
14424 KB |
Output is correct |
9 |
Correct |
6 ms |
14424 KB |
Output is correct |
10 |
Correct |
7 ms |
14424 KB |
Output is correct |
11 |
Correct |
6 ms |
14424 KB |
Output is correct |
12 |
Correct |
6 ms |
14424 KB |
Output is correct |
13 |
Correct |
6 ms |
14424 KB |
Output is correct |
14 |
Correct |
6 ms |
14424 KB |
Output is correct |
15 |
Correct |
8 ms |
14424 KB |
Output is correct |
16 |
Correct |
6 ms |
14424 KB |
Output is correct |
17 |
Correct |
6 ms |
14548 KB |
Output is correct |
18 |
Correct |
8 ms |
14424 KB |
Output is correct |
19 |
Correct |
6 ms |
14424 KB |
Output is correct |
20 |
Correct |
7 ms |
14424 KB |
Output is correct |
21 |
Correct |
7 ms |
14424 KB |
Output is correct |
22 |
Correct |
7 ms |
14424 KB |
Output is correct |
23 |
Correct |
7 ms |
14424 KB |
Output is correct |
24 |
Correct |
7 ms |
14424 KB |
Output is correct |
25 |
Correct |
8 ms |
14468 KB |
Output is correct |
26 |
Correct |
8 ms |
14680 KB |
Output is correct |
27 |
Correct |
10 ms |
14668 KB |
Output is correct |
28 |
Correct |
9 ms |
14424 KB |
Output is correct |
29 |
Correct |
8 ms |
14424 KB |
Output is correct |
30 |
Correct |
21 ms |
16256 KB |
Output is correct |
31 |
Correct |
10 ms |
15448 KB |
Output is correct |
32 |
Correct |
29 ms |
18000 KB |
Output is correct |
33 |
Correct |
17 ms |
16984 KB |
Output is correct |
34 |
Correct |
275 ms |
18848 KB |
Output is correct |
35 |
Correct |
112 ms |
18264 KB |
Output is correct |
36 |
Correct |
80 ms |
17028 KB |
Output is correct |
37 |
Correct |
30 ms |
16976 KB |
Output is correct |
38 |
Correct |
30 ms |
16644 KB |
Output is correct |
39 |
Correct |
24 ms |
16720 KB |
Output is correct |
40 |
Correct |
247 ms |
18816 KB |
Output is correct |
41 |
Correct |
66 ms |
17616 KB |
Output is correct |
42 |
Correct |
42 ms |
17232 KB |
Output is correct |
43 |
Correct |
42 ms |
18520 KB |
Output is correct |
44 |
Correct |
102 ms |
18760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
14424 KB |
Output is correct |
2 |
Correct |
7 ms |
14424 KB |
Output is correct |
3 |
Correct |
6 ms |
14424 KB |
Output is correct |
4 |
Correct |
6 ms |
14396 KB |
Output is correct |
5 |
Correct |
6 ms |
14424 KB |
Output is correct |
6 |
Correct |
6 ms |
14592 KB |
Output is correct |
7 |
Correct |
6 ms |
14424 KB |
Output is correct |
8 |
Correct |
6 ms |
14424 KB |
Output is correct |
9 |
Correct |
6 ms |
14424 KB |
Output is correct |
10 |
Correct |
7 ms |
14424 KB |
Output is correct |
11 |
Correct |
6 ms |
14424 KB |
Output is correct |
12 |
Correct |
6 ms |
14424 KB |
Output is correct |
13 |
Correct |
6 ms |
14424 KB |
Output is correct |
14 |
Correct |
6 ms |
14424 KB |
Output is correct |
15 |
Correct |
8 ms |
14424 KB |
Output is correct |
16 |
Correct |
6 ms |
14424 KB |
Output is correct |
17 |
Correct |
6 ms |
14548 KB |
Output is correct |
18 |
Correct |
8 ms |
14424 KB |
Output is correct |
19 |
Correct |
6 ms |
14424 KB |
Output is correct |
20 |
Correct |
7 ms |
14424 KB |
Output is correct |
21 |
Correct |
7 ms |
14424 KB |
Output is correct |
22 |
Correct |
7 ms |
14424 KB |
Output is correct |
23 |
Correct |
7 ms |
14424 KB |
Output is correct |
24 |
Correct |
7 ms |
14424 KB |
Output is correct |
25 |
Correct |
8 ms |
14468 KB |
Output is correct |
26 |
Correct |
8 ms |
14680 KB |
Output is correct |
27 |
Correct |
10 ms |
14668 KB |
Output is correct |
28 |
Correct |
9 ms |
14424 KB |
Output is correct |
29 |
Correct |
8 ms |
14424 KB |
Output is correct |
30 |
Correct |
21 ms |
16256 KB |
Output is correct |
31 |
Correct |
10 ms |
15448 KB |
Output is correct |
32 |
Correct |
29 ms |
18000 KB |
Output is correct |
33 |
Correct |
17 ms |
16984 KB |
Output is correct |
34 |
Correct |
275 ms |
18848 KB |
Output is correct |
35 |
Correct |
112 ms |
18264 KB |
Output is correct |
36 |
Correct |
80 ms |
17028 KB |
Output is correct |
37 |
Correct |
30 ms |
16976 KB |
Output is correct |
38 |
Correct |
30 ms |
16644 KB |
Output is correct |
39 |
Correct |
24 ms |
16720 KB |
Output is correct |
40 |
Correct |
247 ms |
18816 KB |
Output is correct |
41 |
Correct |
66 ms |
17616 KB |
Output is correct |
42 |
Correct |
42 ms |
17232 KB |
Output is correct |
43 |
Correct |
42 ms |
18520 KB |
Output is correct |
44 |
Correct |
102 ms |
18760 KB |
Output is correct |
45 |
Correct |
180 ms |
31568 KB |
Output is correct |
46 |
Correct |
12 ms |
20824 KB |
Output is correct |
47 |
Correct |
14 ms |
20564 KB |
Output is correct |
48 |
Correct |
370 ms |
49352 KB |
Output is correct |
49 |
Correct |
216 ms |
39392 KB |
Output is correct |
50 |
Execution timed out |
4057 ms |
42120 KB |
Time limit exceeded |
51 |
Halted |
0 ms |
0 KB |
- |