QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#532849 | #7477. 梦断 SCOI2017 | Crysfly | 0 | 0ms | 0kb | C++17 | 5.6kb | 2024-08-25 13:18:41 | 2024-08-25 13:18:41 |
answer
#include<bits/stdc++.h>
#define For(i,a,b) for(register int i=(a);i<=(b);++i)
#define Rep(i,a,b) for(register int i=(a);i>=(b);--i)
using namespace std;
inline int read()
{
char c=getchar();int x=0;bool f=0;
for(;!isdigit(c);c=getchar())f^=!(c^45);
for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48);
if(f)x=-x;return x;
}
#define fi first
#define se second
#define pb push_back
#define mkp make_pair
typedef pair<int,int>pii;
typedef vector<int>vi;
#define N 200005
#define inf 0x3f3f3f3f
const int A=(1u<<31)-1;
int n,m,c[N],pa[N],l[N],r[N],dep[N];
int anc[N][18];
vi e[N];
int pre[N],suf[N],mn[N],mx[N],sz[N],tou[N];
int ch[N][2],fa[N],tag[N],col[N],ctag[N],stag[N];
#define ls(p) ch[p][0]
#define rs(p) ch[p][1]
void up(int u){
if(!u)return;
int l=ls(u),r=rs(u);
pre[u]=l?pre[l]:u;
suf[u]=r?suf[r]:u;
sz[u]=sz[l]+sz[r]+1;
mx[u]=mn[u]=dep[u];
if(l) mx[u]=max(mx[u],mx[l]),mn[u]=min(mn[u],mn[l]);
if(r) mx[u]=max(mx[u],mx[r]),mn[u]=min(mn[u],mn[r]);
stag[u]=stag[l]|stag[r]|ctag[u];
}
void pt(int u,int c){if(u)tag[u]=c,col[u]=c;}
void down(int u){if(tag[u])pt(ls(u),tag[u]),pt(rs(u),tag[u]),tag[u]=0;}
bool chk(int x){return ch[fa[x]][1]==x;}
void rot(int x){
int y=fa[x],z=fa[y],k=chk(x),s=ch[x][!k];
down(z),down(y);
if(z)ch[z][chk(y)]=x; fa[x]=z;
ch[y][k]=s; if(s)fa[s]=y;
ch[x][!k]=y; fa[y]=x; up(y),up(x);
}
void splay(int x,int to=0){
if(!x)assert(!to);
int qwq=0;
while(fa[x]!=to){
if(fa[fa[x]]!=to)rot(chk(x)^chk(fa[x])?x:fa[x]);
rot(x);
++qwq;
if(qwq>n)exit(-1);
}//up(x);
}
void cut(int x){
if(!x)return;
ch[fa[x]][chk(x)]=0,up(fa[x]),fa[x]=0;
}
void ins(int x,int y){
if(!x||!y)return;
// cout<<"ins "<<tou[x]<<" "<<tou[y]<<endl;
splay(x),splay(y);
if(!ls(x))return ls(x)=y,fa[y]=x,splay(y);
x=ls(x);
while(down(x),x){
if(!rs(x))return rs(x)=y,fa[y]=x,splay(y);
x=rs(x);
}
}
void add(int x,int y){
if(!x||!y)return;
// cout<<"add "<<tou[x]<<' '<<tou[y]<<endl;
splay(x),splay(y);
int p=suf[x];
if(!rs(p))return rs(p)=y,fa[y]=p,splay(y);
p=rs(p);
while(down(p),p){
if(ls(p))p=ls(p);
else return ls(p)=y,fa[y]=p,splay(y);
}
}
int top(int x){return x?splay(x),pre[x]:0;}
int nd[N],tp;
void dfs(int x,int c){
down(x);
if(ctag[x]>>c&1)nd[++tp]=tou[x],ctag[x]^=(1<<c);
if(ls(x)&&(stag[ls(x)]>>c&1))dfs(ls(x),c);
if(rs(x)&&(stag[rs(x)]>>c&1))dfs(rs(x),c);
up(x);
}
int findrt(int x){
int tp=top(l[x]);
Rep(i,17,0)if(top(l[anc[x][i]])==tp)x=anc[x][i];
return x;
}
void split(int l,int r){
splay(l),splay(r,l);
down(l),down(r);
}
int son[N][32];
void mdf1(int u,int c){
int p=findrt(u);
split(l[u],r[u]);
if(col[l[u]]==c)return;
int L=ls(l[u]),R=rs(r[u]),lstc=col[l[u]];
cut(L),cut(R),add(L,R);
up(r[u]),up(l[u]),splay(l[u]);
int f=pa[u],rt=pa[p];
splay(l[f]);
if(f){
splay(l[rt]);
if(col[l[rt]]!=lstc){
if(L) son[rt][lstc]=L;
else if(R) son[rt][lstc]=R;
else son[rt][lstc]=0,splay(l[rt]),ctag[l[rt]]&=(A^(1<<c)),up(l[rt]);
}
}
int t=ls(r[u]);
if(t) son[u][lstc]=t,ctag[l[u]]|=(1<<lstc),cut(t),up(r[u]),up(l[u]);
col[l[u]]=col[r[u]]=c;
if(son[u][c]) ctag[l[u]]&=(A^(1<<c)),ins(r[u],son[u][c]),son[u][c]=0;
if(f){
splay(l[f]);
if(son[f][c]) add(son[f][c],l[u]);
else son[f][c]=l[u],splay(l[f]),splay(r[f],l[f]),ctag[l[f]]|=(1<<c),up(l[f]);
if(col[l[f]]==c) ctag[l[f]]&=(A^(1<<c)),son[f][c]=0,ins(r[f],l[u]);
}
}
void mdf2(int u,int c){
u=findrt(u); split(l[u],r[u]);
if(col[l[u]]==c)return;
int L=ls(l[u]),R=rs(r[u]),lstc=col[l[u]];
cut(L),cut(R),add(L,R),splay(l[u]);
int f=pa[u];
splay(l[f]);
if(f && col[l[f]]!=lstc){
if(L) son[f][lstc]=L;
else if(R) son[f][lstc]=R;
else son[f][lstc]=0,splay(l[f]),ctag[l[f]]&=(A^(1<<lstc));
}
tp=0;
splay(l[u]),dfs(l[u],c),pt(l[u],c);
For(i,1,tp){
int v=nd[i];
ins(r[v],son[v][c]),son[v][c]=0;
}
if(f){
splay(l[f]);
if(son[f][c]) add(son[f][c],l[u]);
else son[f][c]=l[u],splay(l[f]),splay(r[f],l[f]),ctag[l[f]]|=(1<<c),splay(l[f]);
if(col[l[f]]==c) ctag[l[f]]&=(A^(1<<c)),son[f][c]=0,ins(r[f],l[u]);
}
}
void output(int u){
if(!u)return;
output(ls(u));
cout<<"u: "<<tou[u]<<" "<<dep[u]<<" "<<col[u]<<' '<<mx[u]<<" "<<mn[u]<<endl;
down(u);
output(rs(u));
up(u);
}
void ask(int u){
u=findrt(u); split(l[u],r[u]);
int a=col[l[u]];
int b=sz[ls(r[u])]/2+1;
int c=max(mx[ls(r[u])],dep[l[u]])-min(mn[ls(r[u])],dep[l[u]])+1;
// printf("a,b,c %d %d %d %d\n",u,a,b,c);
printf("%d %d %d\n",a,b,c);
}
bool cmpc(int u,int v){return c[u]<c[v];}
void init(int u,int d){
dep[l[u]]=dep[r[u]]=d;
For(i,1,17)anc[u][i]=anc[anc[u][i-1]][i-1];
for(auto v:e[u])anc[v][0]=u,init(v,d+1);
sz[l[u]]=sz[r[u]]=1,tou[l[u]]=tou[r[u]]=u,col[l[u]]=col[r[u]]=c[u];
rs(l[u])=r[u],fa[r[u]]=l[u],up(r[u]),up(l[u]);
auto vec=e[u];
for(int v:e[u]) vec.pb(v);
sort(vec.begin(),vec.end(),cmpc);
int siz=vec.size();
For(i,0,siz-1)
if(c[vec[i]]!=c[u]&&(i==0||c[vec[i-1]]!=c[vec[i]]))
ctag[l[u]]|=1<<c[vec[i]];
For(i,0,siz-1)
if(!son[u][c[vec[i]]]&&c[vec[i]]!=c[u]) son[u][c[vec[i]]]=l[vec[i]];
For(i,1,siz-1)
if(c[vec[i-1]]==c[vec[i]]) add(r[vec[i-1]],l[vec[i]]);
For(i,0,siz-1)
if(c[vec[i]]==c[u]){ins(r[u],l[vec[i]]);break;}
}
signed main()
{
mx[0]=-inf,mn[0]=inf;
n=read();
For(i,1,n)e[pa[i]=read()].pb(i);
For(i,1,n)c[i]=read(),l[i]=i*2-1,r[i]=i*2;
init(1,1);
// exit(0);
m=read();
For(_,1,m){
int o=read(),x=read();
if(o==1)mdf1(x,read());
if(o==2)mdf2(x,read());
if(o==3)ask(x);
// if(o<=2)For(x,1,n)ask(x);
// output(l[2]);
}
return 0;
}
详细
Subtask #1:
score: 0
Runtime Error
Test #1:
score: 0
Runtime Error
input:
1000 0 1 1 2 1 3 3 7 2 1 1 5 9 1 10 4 9 13 4 13 9 16 5 21 11 22 17 14 24 8 24 19 7 13 1 9 12 22 8 35 39 4 9 34 19 28 40 8 34 4 50 39 6 32 4 1 31 25 31 37 46 44 60 12 54 48 25 43 15 69 1 38 2 23 12 73 59 39 75 7 36 9 27 73 2 43 37 81 53 58 58 83 10 86 17 89 82 52 10 14 40 46 25 4 56 61 16 96 21 70 14...
output:
result:
Subtask #2:
score: 0
Runtime Error
Test #2:
score: 0
Runtime Error
input:
100000 0 1 1 1 4 3 4 6 2 4 5 4 9 3 14 3 10 14 16 9 5 14 3 6 13 12 12 21 19 26 21 13 19 26 32 8 5 2 5 30 3 1 12 21 6 44 1 10 32 31 47 1 44 34 9 38 12 44 47 1 3 30 12 62 15 56 5 1 53 3 69 57 53 12 9 58 8 77 20 76 18 63 59 61 80 42 30 70 33 16 38 5 28 42 91 88 85 7 38 98 7 100 1 93 91 103 25 66 95 24 8...
output:
result:
Subtask #3:
score: 0
Runtime Error
Test #9:
score: 0
Runtime Error
input:
100000 0 1 1 1 2 4 3 6 5 5 9 10 8 12 10 13 15 14 16 16 19 17 21 19 21 21 22 23 24 26 29 29 29 30 31 31 34 35 36 37 37 37 40 40 42 42 45 46 45 48 48 49 50 51 51 52 54 56 57 55 58 58 60 62 60 61 65 63 65 65 66 67 68 71 72 74 72 75 77 78 78 77 80 80 80 83 82 84 85 87 87 90 88 92 92 92 92 93 96 96 96 99...