QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#55436 | #4758. Captivating process | AFewSuns | WA | 16ms | 34808kb | C++ | 5.3kb | 2022-10-13 19:14:20 | 2022-10-13 19:14:21 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
namespace my_std{
#define ll long long
#define bl bool
ll my_pow(ll a,ll b,ll mod){
ll res=1;
if(!b) return 1;
while(b){
if(b&1) res=(res*a)%mod;
a=(a*a)%mod;
b>>=1;
}
return res;
}
ll qpow(ll a,ll b){
ll res=1;
if(!b) return 1;
while(b){
if(b&1) res*=a;
a*=a;
b>>=1;
}
return res;
}
#define db double
#define pf printf
#define pc putchar
#define fr(i,x,y) for(register ll i=(x);i<=(y);i++)
#define pfr(i,x,y) for(register ll i=(x);i>=(y);i--)
#define go(u) for(ll i=head[u];i;i=e[i].nxt)
#define enter pc('\n')
#define space pc(' ')
#define fir first
#define sec second
#define MP make_pair
#define il inline
#define inf 8e18
#define random(x) rand()*rand()%(x)
#define inv(a,mod) my_pow((a),(mod-2),(mod))
il ll read(){
ll sum=0,f=1;
char ch=0;
while(!isdigit(ch)){
if(ch=='-') f=-1;
ch=getchar();
}
while(isdigit(ch)){
sum=sum*10+(ch^48);
ch=getchar();
}
return sum*f;
}
il void write(ll x){
if(x<0){
x=-x;
pc('-');
}
if(x>9) write(x/10);
pc(x%10+'0');
}
il void writeln(ll x){
write(x);
enter;
}
il void writesp(ll x){
write(x);
space;
}
}
using namespace my_std;
ll n,q,tree[100010],ans[100010];
struct edge{
ll nxt,to;
};
struct node{
ll x,y;
}a[100010];
struct Node{
ll fr,sc,t,id;
}b[200020],c[300030];
il bl operator<(const Node &x,const Node &y){
if(x.fr!=y.fr) return x.fr<y.fr;
if(x.sc!=y.sc) return x.sc<y.sc;
return x.id<y.id;
}
struct tree{
ll head[100010],cnt=0,pos[100010],id[100010],len[100010],tot=0,sta[100010],top=0;
ll st[100010],ed[100010],tott=0,dep[100010],a[100010];
bl vis[100010],ck[100010];
edge e[200020];
void add(ll u,ll v){
e[++cnt].nxt=head[u];
e[cnt].to=v;
head[u]=cnt;
}
void dfs0(ll u){
vis[u]=1;
id[u]=tot;
go(u){
ll v=e[i].to;
if(!vis[v]) dfs0(v);
}
}
void dfs1(ll u){
vis[u]=1;
sta[++top]=u;
ll v=a[u];
if(!vis[v]) dfs1(v);
else if(!ck[u]){
ll tmp=top;
while(1){
ck[sta[tmp]]=1;
pos[sta[tmp]]=++len[id[u]];
if(sta[tmp]==v) break;
tmp--;
}
}
vis[u]=0;
top--;
}
void dfs2(ll fa,ll u){
if(!ck[u]){
st[u]=++tott;
dep[u]=dep[fa]+1;
pos[u]=(pos[fa]-1+len[id[u]])%len[id[u]];
}
go(u){
ll v=e[i].to;
if(ck[v]||v==fa) continue;
dfs2(u,v);
}
if(!ck[u]) ed[u]=tott;
}
void init(){
fr(i,1,n){
a[i]=read();
add(i,a[i]);
add(a[i],i);
}
fr(i,1,n){
if(!id[i]){
tot++;
dfs0(i);
}
}
fr(i,1,n) vis[i]=0;
fr(i,1,n) if(!len[id[i]]) dfs1(i);
fr(i,1,n) if(ck[i]) pos[i]=len[id[i]]-pos[i];
fr(i,1,n) if(ck[i]) dfs2(0,i);
return;
}
}F,G;
il ll lowbit(ll x){
return x&(-x);
}
il void mdf(ll x,ll v){
while(x<=n){
tree[x]+=v;
x+=lowbit(x);
}
}
il ll query(ll x){
ll res=0;
while(x){
res+=tree[x];
x-=lowbit(x);
}
return res;
}
void solve1(){
map<pair<pair<ll,ll>,ll>,bl> mp;
fr(i,1,n){
if(F.ck[i]&&G.ck[i]){
ll len=__gcd(F.len[F.id[i]],G.len[G.id[i]]);
mp[MP(MP(F.id[i],G.id[i]),((F.pos[i]-G.pos[i])%len+len)%len)]=1;
}
}
fr(i,1,q){
ll len=__gcd(F.len[F.id[a[i].x]],G.len[G.id[a[i].y]]);
if(mp.count(MP(MP(F.id[a[i].x],G.id[a[i].y]),((F.pos[a[i].x]-G.pos[a[i].y])%len+len)%len))) ans[i]=1;
}
}
void solve2(){
ll cnt=0;
fr(i,1,n) if(!F.ck[i]&&G.ck[i]) b[++cnt]=(Node){G.id[i],(G.pos[i]+F.dep[i])%G.len[G.id[i]],0,i};
fr(i,1,q) if(!F.ck[a[i].x]) b[++cnt]=(Node){G.id[a[i].y],(G.pos[a[i].y]+F.dep[a[i].x])%G.len[G.id[a[i].y]],0,n+i};
sort(b+1,b+cnt+1);
ll lst=1,tot;
fr(i,1,cnt){
if(i==cnt||b[i].fr!=b[i+1].fr||b[i].sc!=b[i+1].sc){
tot=0;
fr(j,lst,i){
if(b[j].id<=n) c[++tot]=(Node){F.dep[b[j].id],0,0,b[j].id};
else c[++tot]=(Node){F.dep[a[b[j].id-n].x]-G.dep[a[b[j].id-n].y],0,0,b[j].id};
}
sort(c+1,c+tot+1);
fr(j,1,tot){
if(c[j].id<=n){
mdf(F.st[c[j].id],1);
mdf(F.ed[c[j].id]+1,-1);
}
else if(query(F.st[a[c[j].id-n].x])) ans[c[j].id-n]=1;
}
fr(j,1,tot){
if(c[j].id<=n){
mdf(F.st[c[j].id],-1);
mdf(F.ed[c[j].id]+1,1);
}
}
lst=i+1;
}
}
}
void solve3(){
ll cnt=0;
fr(i,1,n) if(!F.ck[i]&&!G.ck[i]) b[++cnt]=(Node){F.dep[i]-G.dep[i],0,0,i};
fr(i,1,q) if(!F.ck[a[i].x]&&!G.ck[a[i].y]) b[++cnt]=(Node){F.dep[a[i].x]-G.dep[a[i].y],0,0,n+i};
sort(b+1,b+cnt+1);
ll lst=1,tot;
fr(i,1,cnt){
if(i==cnt||b[i].fr!=b[i+1].fr){
tot=0;
fr(j,i,lst){
if(b[j].id<=n){
c[++tot]=(Node){F.st[b[j].id],0,1,b[j].id};
c[++tot]=(Node){F.ed[b[j].id]+1,0,-1,b[j].id};
}
else c[++tot]=(Node){F.st[a[b[j].id-n].x],0,0,b[j].id};
}
sort(c+1,c+tot+1);
fr(j,1,tot){
if(c[j].id<=n){
mdf(G.st[c[j].id],c[j].t);
mdf(G.ed[c[j].id]+1,-c[j].t);
}
else if(query(G.st[a[c[j].id-n].y])) ans[c[j].id-n]=1;
}
fr(j,1,tot){
if(c[j].id<=n){
mdf(G.st[c[j].id],-c[j].t);
mdf(G.ed[c[j].id]+1,c[j].t);
}
}
lst=i+1;
}
}
}
int main(){
n=read();
q=read();
F.init();
G.init();
fr(i,1,q){
a[i].x=read();
a[i].y=read();
}
solve1();
solve3();
solve2();
swap(F,G);
solve2();
fr(i,1,q){
if(ans[i]) pf("YES\n");
else pf("NO\n");
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 16ms
memory: 34632kb
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: 4ms
memory: 34788kb
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: 9ms
memory: 34764kb
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: 8ms
memory: 34576kb
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: 4ms
memory: 34708kb
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: 14ms
memory: 34712kb
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: 4ms
memory: 34652kb
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: 8ms
memory: 34728kb
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: 8ms
memory: 34760kb
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: 11ms
memory: 34644kb
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: 4ms
memory: 34808kb
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: 8ms
memory: 34648kb
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: 8ms
memory: 34804kb
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]