QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#243105#4758. Captivating processserene_analysisWA 63ms292256kbC++146.8kb2023-11-07 21:01:512023-11-07 21:01:52

Judging History

你现在查看的是最新测评结果

  • [2023-11-07 21:01:52]
  • 评测
  • 测评结果:WA
  • 用时:63ms
  • 内存:292256kb
  • [2023-11-07 21:01:51]
  • 提交

answer

#include<algorithm>
#include<cstdio>
#include<vector>
#include<map>
#include<set>
const int maxn=4e5+5;
const int logn=2e1+5;
int n,m;
struct tree{
	std::vector<int>son[maxn];
	bool vis[maxn],in[maxn],lop[maxn];
	int to[maxn],stk[maxn],scnt,bel[maxn],pid[maxn],up[maxn][logn];
	std::vector<int>apr[maxn];
	int bcnt;
	void go(int x){
//		printf("go:%d,to[x]=%d,vis[to]=%d,in[to[x]]=%d\n",x,to[x],vis[to[x]],in[to[x]]);
		stk[++scnt]=x,in[x]=true,vis[x]=true;
		if(!vis[to[x]])go(to[x]);
		else if(in[to[x]]){
			bcnt++,stk[scnt+1]=0;
			while(stk[scnt+1]!=to[x])bel[stk[scnt]]=bcnt,in[stk[scnt]]=false,lop[stk[scnt]]=true,scnt--;
			int p=to[x];
			do{
//				printf("pid[%d]=%d\n",p,(int)apr[bcnt].size());
				pid[p]=apr[bcnt].size(),apr[bcnt].push_back(p),p=to[p];
			}while(p!=to[x]);
			while(scnt)in[stk[scnt]]=false,scnt--;
		}
		if(scnt)scnt--,in[x]=false;
		return;
	}
	int d[maxn],size[maxn],dfn[maxn],alt;
	int over(int x,int gd){
		if(gd>d[x])fprintf(stderr,"shik\n");
		for(int i=logn-2;i>=0;i--)if((gd>>i)&1)x=up[x][i];
		return x;
	}
	bool have(int x,int y){return dfn[y]>=dfn[x]&&dfn[y]<=dfn[x]+size[x]-1;}
	void divide(int x){
//		printf("x=%d,d[x]=%d\n",x,d[x]);
		size[x]=1,dfn[x]=++alt;
		for(int j=1;j<=logn-2;j++)up[x][j]=up[up[x][j-1]][j-1];
		for(int v:son[x]){
			if(lop[v])continue;
//			printf("x=%d,v=%d\n",x,v);
			d[v]=d[x]+1,up[v][0]=x,divide(v),size[x]+=size[v];
		}
		return;
	}
	void init(){
		for(int i=1;i<=n;i++)scanf("%d",to+i),son[to[i]].push_back(i);
		for(int i=1;i<=n;i++)if(!vis[i])go(i);
		for(int i=1;i<=n;i++)if(lop[i])divide(i);
		return;
	}
}ft,st;
struct seg{
	int l,r;
	friend bool operator<(seg now,seg oth){return now.l==oth.l?now.r<oth.r:now.l<oth.l;}
};
bool can[maxn][3];
struct ques{int wz,id;};
std::vector<ques>fqu[maxn],squ[maxn];
std::set<seg>sapr[maxn];
bool have(int ip,int x){
//	if(ip<=0||ip>=maxn)fprintf(stderr,"shik\n");
	auto dt=sapr[ip].lower_bound((seg){x,0});
	if(dt==sapr[ip].begin())return false;
//	fprintf(stderr,"have=%d\n",std::prev(dt)->r>=x);
	return std::prev(dt)->r>=x;
}
void fgo(int x){
	int ip=ft.d[x]-st.d[x]+maxn/2;
//	printf("fgo:%d,ip=%d,lin={%d,%d}\n",x,ip,ft.dfn[x],ft.dfn[x]+ft.size[x]-1);
	bool ins=false;
	if(!have(ip,ft.dfn[x]))ins=true,sapr[ip].insert((seg){ft.dfn[x],ft.dfn[x]+ft.size[x]-1});
//	if(x==60){
//		printf("x=%d,ft.dfn[x]=%d,st.up[x][0]=%d,ip=%d,ins=%d\n",x,ft.dfn[x],st.up[x][0],ip,ins);
//	}
	for(ques now:fqu[x]){
//		printf("query,now.wz=%d,now.id=%d\n",now.wz,now.id);
		can[now.id][0]|=have(ft.d[now.wz]-st.d[x]+maxn/2,ft.dfn[now.wz]);
//		if(now.id==4708)printf("can[now.id][0]=%d,ft.dfn[now.wz]=%d\n",can[now.id][0],ft.dfn[now.wz]);
	}
	for(int v:st.son[x])if(!st.lop[v])fgo(v);
	if(ins)sapr[ip].erase((seg){ft.dfn[x],ft.dfn[x]+ft.size[x]-1})/*,printf("ctrl_z:%d\n",x)*/;
	return;
}
std::set<int>papr[maxn];
void sgo(int x){
//	if(x==11)printf("sgo:%d\n",x);
	if(!ft.lop[x]){
//		printf("not on loop\n");
		for(ques now:squ[x])can[now.id][1]|=papr[ft.bel[now.wz]]
			.find((ft.pid[now.wz]+st.d[x])%ft.apr[ft.bel[now.wz]].size())!=papr[ft.bel[now.wz]].end();
		for(int v:st.son[x])if(!st.lop[v])sgo(v);
		return;
	}
	bool ins=false;
	int ip=(ft.pid[x]+st.d[x])%ft.apr[ft.bel[x]].size();
	if(papr[ft.bel[x]].empty()||papr[ft.bel[x]].find(ip)==papr[ft.bel[x]].end())ins=true,papr[ft.bel[x]].insert(ip);
//	if(x==3||x==11){
//		printf("x=%d,ft.bel[x]=%d,papr[ft.bel[x]].size()=%d,st.up[x][0]=%d,ip=%d,ins=%d\n",
//			x,ft.bel[x],(int)papr[ft.bel[x]].size(),st.up[x][0],ip,ins);
//	}
	for(ques now:squ[x]){
//		if(x==40489)printf("now.wz=%d,ft.bel[now.wz]=%d,x=%d,gv=%d\n",now.wz,ft.bel[now.wz],x,
//			(int)((ft.pid[now.wz]+st.d[x])%ft.apr[ft.bel[now.wz]].size()));
		can[now.id][1]|=papr[ft.bel[now.wz]].find
			((ft.pid[now.wz]+st.d[x])%ft.apr[ft.bel[now.wz]].size())!=papr[ft.bel[now.wz]].end();
//		if(now.id==2665)printf("can[now.id][1]=%d\n",can[now.id][1]);
	}
	for(int v:st.son[x])if(!st.lop[v])sgo(v);
	if(ins)papr[ft.bel[x]].erase(ip);
	return;
}
int qx[maxn],qy[maxn];
int gcd(int a,int b){return b?gcd(b,a%b):a;}
struct pii{
	int x,y;
	friend bool operator<(pii now,pii oth){return now.x==oth.x?now.y<oth.y:now.x<oth.x;}
};
std::map<pii,bool>apr[maxn];
signed main(){
//	freopen("wait.in","r",stdin);
//	freopen("wait.out","w",stdout);
	scanf("%d%d",&n,&m);
	ft.init(),st.init();
//	printf("st.lop[11]=%d,st.d[11]=%d,st.up[11][0]=%d\n",st.lop[11],st.d[11],st.up[11][0]);
	for(int i=1;i<=n;i++)if(ft.lop[i]&&st.lop[i]){
		int fp=ft.bel[i],sp=st.bel[i],d=gcd(ft.apr[fp].size(),st.apr[sp].size());
//		if(i==28){
//			printf("i=%d,ft.lop&&st.lop\n",i);
//			printf("fp=%d,sp=%d,d=%d\n",fp,sp,d);
//		}
		apr[((ft.pid[i]-st.pid[i])%d+d)%d][{fp,sp}]=true;
	}
	for(int i=1;i<=m;i++){
		scanf("%d%d",qx+i,qy+i),fqu[qy[i]].push_back((ques){qx[i],i});
		int fd=ft.d[qx[i]],sd=st.d[qy[i]];
//		printf("qx[i]=%d,qy[i]=%d,fd=%d,sd=%d\n",qx[i],qy[i],fd,sd);
//		if(i==4708){
//			fprintf(stderr,"qx[i]=%d,qy[i]=%d\n",qx[i],qy[i]);
//			for(int x=1;x<=n;x++)if(ft.have(x,qx[i])&&st.have(x,qy[i])&&
//				ft.d[x]-st.d[x]==ft.d[qx[i]]-st.d[qy[i]])printf("x=%d\n",x);
//			int x=qx[i],y=qy[i];
//			while(x!=y){
//				printf("x=%d,y=%d\n",x,y);
//				x=ft.to[x],y=st.to[y];
//			}
//			printf("ok,x=%d\n",x);
//			printf("ok,x=%d,ft.pid[x]=%d,ft.pid[qx[i]]=%d,st.d[qy[i]]=%d,st.d[x]=%d,ft.apr[ft.bel[x]].size()=%d,ans=%d\n",
//				x,ft.pid[x],ft.pid[qx[i]],st.d[qy[i]],st.d[x],(int)ft.apr[ft.bel[x]].size(),
//				(int)((ft.pid[x]-ft.pid[qx[i]]-st.d[qy[i]]+st.d[x])%ft.apr[ft.bel[x]].size()));
//		}
		if(fd<=sd){
			int x=ft.over(qx[i],fd),y=st.over(qy[i],fd);
			squ[y].push_back((ques){x,i});
			x=ft.apr[ft.bel[x]][(ft.pid[x]+sd-fd)%ft.apr[ft.bel[x]].size()],y=st.over(y,sd-fd);
			int fp=ft.bel[x],sp=st.bel[y],d=gcd(ft.apr[fp].size(),st.apr[sp].size());
			can[i][2]|=apr[((ft.pid[x]-st.pid[y])%d+d)%d][{fp,sp}];
		}
		else{
			int x=ft.over(qx[i],fd),y=st.over(qy[i],sd);
			y=st.apr[st.bel[y]][(st.pid[y]+fd-sd)%st.apr[st.bel[y]].size()];
			int fp=ft.bel[x],sp=st.bel[y],d=gcd(ft.apr[fp].size(),st.apr[sp].size());
			can[i][2]|=apr[((ft.pid[x]-st.pid[y])%d+d)%d][{fp,sp}];
		}
	}
	for(int i=1;i<=n;i++)if(st.lop[i])fgo(i),sgo(i);
	for(int i=1;i<=n;i++)fqu[i].clear(),squ[i].clear();
//	printf("fir,ok\n");
	std::swap(ft,st);
	for(int i=1;i<=m;i++){
		std::swap(qx[i],qy[i]);
		int fd=ft.d[qx[i]],sd=st.d[qy[i]];
//		printf("qx[i]=%d,qy[i]=%d\n",qx[i],qy[i]);
		if(fd<=sd){
			int x=ft.over(qx[i],fd),y=st.over(qy[i],fd);
//			printf("fd=%d,sd=%d,over,x=%d,y=%d\n",fd,sd,x,y);
			squ[y].push_back((ques){x,i});
		}
	}
	for(int i=1;i<=n;i++)if(st.lop[i])sgo(i);
	for(int i=1;i<=m;i++){
//		printf("i=%d\n",i);
		printf("%s\n",can[i][0]||can[i][1]||can[i][2]?"YES":"NO");
	}
	return 0;
}
/*
10 5
1 4 5 8 3 3 2 7 8 1
1 4 5 8 3 3 2 7 8 1
2 3
4 5
6 7
8 9
1 10
*/
//namespace burningContract

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 43ms
memory: 292184kb

input:

3 2
2 3 1
2 3 1
1 2
1 1

output:

NO
YES

result:

ok 2 token(s): yes count is 1, no count is 1

Test #2:

score: 0
Accepted
time: 63ms
memory: 292200kb

input:

4 2
2 3 4 2
2 4 4 1
1 2
1 4

output:

NO
YES

result:

ok 2 token(s): yes count is 1, no count is 1

Test #3:

score: 0
Accepted
time: 36ms
memory: 292132kb

input:

10 10
7 7 8 10 5 7 9 3 2 3
4 2 7 1 3 3 10 1 9 3
7 8
7 6
7 9
1 5
3 6
5 3
10 6
8 9
9 4
6 7

output:

NO
NO
YES
NO
YES
NO
YES
NO
NO
NO

result:

ok 10 token(s): yes count is 3, no count is 7

Test #4:

score: 0
Accepted
time: 47ms
memory: 292184kb

input:

10 10
5 7 8 2 6 8 7 5 5 10
8 2 5 8 3 9 10 7 2 8
8 7
5 6
4 9
3 6
5 6
2 3
8 4
5 9
6 8
3 4

output:

NO
NO
YES
NO
NO
NO
NO
NO
NO
YES

result:

ok 10 token(s): yes count is 2, no count is 8

Test #5:

score: 0
Accepted
time: 56ms
memory: 292256kb

input:

10 10
1 9 4 4 2 6 7 8 9 4
1 9 9 5 5 1 3 9 3 10
1 7
3 8
7 8
9 8
6 9
10 7
3 5
1 7
8 5
8 5

output:

NO
NO
NO
YES
NO
NO
NO
NO
NO
NO

result:

ok 10 token(s): yes count is 1, no count is 9

Test #6:

score: 0
Accepted
time: 51ms
memory: 292240kb

input:

10 10
8 2 4 4 7 10 9 9 10 5
5 4 9 3 10 6 10 10 5 4
7 8
2 3
6 2
1 2
2 10
5 4
10 4
10 9
10 7
6 1

output:

YES
NO
YES
YES
NO
YES
YES
YES
YES
YES

result:

ok 10 token(s): yes count is 8, no count is 2

Test #7:

score: 0
Accepted
time: 53ms
memory: 292180kb

input:

10 10
4 4 8 3 7 7 7 2 10 9
4 10 6 6 4 10 5 8 5 9
3 7
6 2
1 2
3 2
4 7
1 9
6 5
5 10
6 1
5 2

output:

YES
NO
YES
YES
YES
YES
NO
NO
NO
NO

result:

ok 10 token(s): yes count is 5, no count is 5

Test #8:

score: 0
Accepted
time: 53ms
memory: 292184kb

input:

10 10
3 10 1 8 8 3 8 4 9 10
8 6 4 2 10 3 6 3 4 10
2 7
8 3
2 8
3 3
1 9
4 3
6 8
5 10
8 8
6 8

output:

NO
YES
NO
YES
NO
NO
YES
NO
YES
YES

result:

ok 10 token(s): yes count is 5, no count is 5

Test #9:

score: 0
Accepted
time: 48ms
memory: 292184kb

input:

10 10
1 8 3 5 4 2 6 7 8 7
6 4 6 3 8 7 4 3 9 4
8 2
4 2
4 2
4 3
3 10
4 10
7 1
7 8
4 2
6 3

output:

NO
NO
NO
NO
YES
NO
YES
NO
NO
NO

result:

ok 10 token(s): yes count is 2, no count is 8

Test #10:

score: 0
Accepted
time: 60ms
memory: 292184kb

input:

10 10
5 4 7 3 3 6 4 8 2 9
1 5 2 2 6 3 2 4 1 5
4 4
10 2
2 7
6 4
9 6
9 10
10 9
7 4
7 7
5 4

output:

YES
YES
YES
YES
YES
YES
NO
YES
YES
YES

result:

ok 10 token(s): yes count is 9, no count is 1

Test #11:

score: 0
Accepted
time: 55ms
memory: 292144kb

input:

100 100
41 55 57 45 38 30 83 53 33 51 90 57 82 35 28 54 35 47 39 68 67 98 57 8 71 99 83 39 83 44 76 88 18 41 15 17 39 46 79 79 54 28 57 22 81 33 63 17 1 21 32 58 47 17 4 46 10 45 97 44 29 71 5 50 79 66 83 88 43 97 50 56 5 100 23 1 66 16 76 34 71 60 59 57 87 47 6 23 68 55 79 32 73 10 98 65 90 30 79 5...

output:

YES
NO
NO
YES
NO
YES
YES
NO
NO
YES
NO
NO
YES
YES
NO
NO
NO
NO
YES
YES
NO
YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
YES
NO
NO
NO
NO
NO
YES
NO
YES
NO
NO
NO
YES
YES
NO
YES
NO
YES
YES
NO
NO
NO
NO
NO
NO
NO
YES
NO
NO
NO
YES
NO
NO
YES
NO
NO
NO
NO
NO
NO
YES
YES
NO
YES
NO
NO
YES
NO
NO
NO
NO
YES
YES
YES
NO
NO
NO
YES
N...

result:

ok 100 token(s): yes count is 35, no count is 65

Test #12:

score: 0
Accepted
time: 48ms
memory: 292104kb

input:

100 100
70 57 94 89 18 74 52 32 87 20 73 47 60 55 74 37 33 81 55 2 23 23 69 23 52 12 92 85 38 25 41 66 49 94 27 50 23 35 23 38 58 81 75 92 11 6 36 33 8 60 13 2 58 93 59 67 46 95 45 12 56 87 31 25 18 5 17 10 95 47 38 72 87 84 17 39 74 36 39 23 91 90 86 68 23 20 38 29 60 60 56 19 66 43 21 7 84 25 41 5...

output:

YES
NO
NO
YES
NO
YES
NO
NO
NO
NO
NO
NO
YES
NO
YES
YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
YES
NO
YES
NO
NO
YES
YES
YES
YES
YES
YES
NO
NO
YES
NO
YES
YES
NO
NO
NO
NO
YES
NO
NO
YES
NO
NO
YES
YES
YES
NO
NO
NO
NO
YES
YES
NO
NO
NO
NO
YES
NO
YES
NO
NO
NO
NO
NO
NO
NO
NO
YES
NO
YES
NO
NO
NO
NO
NO
NO
NO
NO
NO...

result:

ok 100 token(s): yes count is 28, no count is 72

Test #13:

score: -100
Wrong Answer
time: 42ms
memory: 292208kb

input:

100 100
48 86 67 28 67 38 10 31 5 90 67 38 64 38 86 22 29 71 25 27 89 34 4 83 99 82 45 79 50 12 13 38 4 49 77 90 4 81 87 92 4 50 65 37 86 90 45 69 17 34 32 38 13 77 10 68 2 69 39 80 82 32 95 25 1 87 100 74 63 57 61 99 14 87 10 11 28 46 23 71 14 80 10 88 79 70 92 13 79 24 83 5 48 68 65 20 61 27 13 39...

output:

NO
NO
NO
NO
NO
YES
NO
NO
NO
NO
YES
YES
NO
NO
NO
YES
NO
NO
NO
NO
NO
NO
YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
YES
NO
YES
NO
NO
YES
NO
NO
YES
NO
NO
NO
NO
NO
NO
NO
NO
YES
NO
NO
NO
NO
NO
YES
NO
NO
NO
NO
NO
NO
NO
YES
NO
YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
YES
NO
NO
NO
NO
NO
N...

result:

wrong answer expected YES, found NO [75th token]