QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#430078 | #8002. 字符树 | Williamxzh | 25 | 936ms | 88100kb | C++23 | 4.0kb | 2024-06-03 13:38:22 | 2024-06-03 13:38:24 |
Judging History
answer
#include <bits/stdc++.h>
#define il inline
#define pii pair<int,int>
#define fi first
#define se second
using namespace std;
typedef unsigned long long ull;
const ull mul=131;
typedef long long ll;
il int read(){
int x=0,c=getchar();
while(!isdigit(c)) c=getchar();
while(isdigit(c)) x=x*10+c-48,c=getchar();
return x;
}
il void swap(int &x,int &y){int z=x;x=y,y=z;}
il void cmax(int &x,int y){x=x>y?x:y;}
const int N=5e5+5,M=10001;
int T,n,fa[N][20],c[N],a[N],v[N],d[N],lg[N];ll ans[N];unsigned short lcp[M][M];ull h[N][20],pw[N];
int t[N][2],cnt,de[N],p[N];
int sa[N],rk[N],height[N],st[N][20];
vector<pii> e[N];
il void add(int x,int y,int z){e[x].push_back({y,z});}
void dfs(int x,int u){
int y,z;p[x]=u,d[x]=d[fa[x][0]]+1,h[x][0]=1ull*c[x];
for(int i=1;(1<<i)<=d[x]-1;++i) fa[x][i]=fa[fa[x][i-1]][i-1],h[x][i]=h[x][i-1]*pw[1<<(i-1)]+h[fa[x][i-1]][i-1];
for(auto it:e[x]){
y=it.fi,z=it.se;
if(!t[u][z]) t[u][z]=++cnt,de[t[u][z]]=de[u]+1;
dfs(y,t[u][z]);
}
}
struct node{
int u,v,w;
il node(){u=v=w=0;}
il node(int u,int v,int w) : u(u),v(v),w(w) {}
};
il node lcs(int x,int y){
if(x==1 || y==1) return node(x,y,0);
int ret=0,mi=lg[min(d[x],d[y])-1];
for(int i=mi;i>=0;--i)
if(fa[x][i] && fa[y][i] && h[x][i]==h[y][i]) x=fa[x][i],y=fa[y][i],ret+=(1<<i);
return node(x,y,ret);
}
il int cmp(int x,int y){//x<y?1:0
node cur=lcs(x,y);int len=cur.w,u=cur.u,v=cur.v;
if(u!=1 && v!=1) return c[u]<c[v];
if(len==d[x]-1 && len==d[y]-1) return x<y;
return len==d[x]-1;
}
il int calc(int x,int y){
if(x==y) return d[x]-1;
x=rk[x],y=rk[y];if(x>y) swap(x,y);++x;
int k=lg[y-x+1];
return min(st[x][k],st[y-(1<<k)+1][k]);
}
int dfn[N],tot,siz[N],id[N];
void dfs1(int x){
dfn[x]=++tot,id[tot]=x,siz[x]=1;
int y=t[x][0],z=t[x][1];
if(y) dfs1(y),siz[x]+=siz[y];
if(z) dfs1(z),siz[x]+=siz[z];
if(y && z){
for(int i=dfn[y];i<dfn[y]+siz[y];++i)
for(int j=dfn[z];j<dfn[z]+siz[z];++j)
lcp[id[i]][id[j]]=lcp[id[j]][id[i]]=de[x];
}
if(y){
for(int i=dfn[y];i<dfn[y]+siz[y];++i) lcp[id[i]][x]=lcp[x][id[i]]=de[x];
}
if(z){
for(int i=dfn[z];i<dfn[z]+siz[z];++i) lcp[id[i]][x]=lcp[x][id[i]]=de[x];
}
}
il void init(){
cnt=0,tot=0;
for(int i=1;i<=n;++i){
e[i].clear(),c[i]=a[i]=v[i]=p[i]=d[i]=0,ans[i]=0ll;
for(int j=0;j<20;++j) fa[i][j]=0,h[i][j]=0ull,st[i][j]=0;
sa[i]=rk[i]=height[i]=0;
}
for(int i=0;i<=n;++i) t[i][0]=t[i][1]=de[i]=dfn[i]=siz[i]=id[i]=0;
}
il void ini(){
pw[0]=1ull;lg[0]=-1;
for(int i=1;i<N;++i) pw[i]=pw[i-1]*mul;
for(int i=2;i<N;++i) lg[i]=lg[i>>1]+1;
}
int cur[N],mx[N];
int x,y,z,u,w;
int main(){
//freopen("c1.in","r",stdin);
scanf("%d",&T);ini();
while(T--){
scanf("%d",&n);init();
for(int i=2;i<=n;++i) fa[i][0]=read(),c[i]=read(),add(fa[i][0],i,c[i]);
for(int i=1;i<=n;++i) v[i]=read();
for(int i=1;i<=n;++i) a[i]=read();
de[0]=0,dfs(1,0);
for(int i=1;i<=n;++i) sa[i]=i;
stable_sort(sa+1,sa+1+n,cmp);
for(int i=1;i<=n;++i) rk[sa[i]]=i;
for(int i=2;i<=n;++i) height[i]=lcs(sa[i-1],sa[i]).w;
for(int i=1;i<=n;++i) st[i][0]=height[i];
for(int j=1;(1<<j)<=n;++j)
for(int i=1;i+(1<<j)-1<=n;++i) st[i][j]=min(st[i][j-1],st[i+(1<<(j-1))][j-1]);
dfs1(0);
for(int x=1;x<=n;++x){
for(int i=0;i<d[x];++i) mx[i]=0;
for(int y=1;y<=n;++y){
w=(x!=y?lcp[p[x]][p[y]]:d[x]-1);
if(d[y]<d[x] || a[y]>w) continue;
cur[y]=calc(x,y);if(cur[y]!=d[x]-1) continue;
for(int i=a[y];i<=w;++i) cmax(mx[i],v[y]);
}
ans[x]=0ll;
for(int i=0;i<d[x];++i) ans[x]+=1ll*mx[i];
}
for(int i=1;i<=n;++i) printf("%lld ",ans[i]);puts("");
}
return 0;
}
详细
Test #1:
score: 5
Accepted
time: 3ms
memory: 48936kb
input:
5 100 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 0 10 0 11 0 12 0 13 0 14 0 15 0 16 0 17 0 18 0 19 0 20 0 21 0 22 0 23 0 24 0 25 0 26 0 27 0 28 0 29 0 30 0 31 0 32 0 33 0 34 0 35 0 36 0 37 0 38 0 39 0 40 0 41 0 42 0 43 0 44 0 45 0 46 0 47 0 48 0 49 0 50 0 51 0 52 0 53 0 54 0 55 0 56 0 57 0 58 0 59 0 60 0 61 ...
output:
898192584 1872556072 2796790614 3758907885 4721025156 5540285037 6473830830 7439177386 8387406282 9352752838 10318099394 11283445950 12283445950 13283445950 14283445950 15144832174 15985150482 16950497038 17445491372 18445491372 19445491372 20445491372 21364928322 21962113072 22881550022 23800986972...
result:
ok 500 numbers
Test #2:
score: 5
Accepted
time: 3ms
memory: 48848kb
input:
5 100 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 0 10 0 11 0 12 0 13 0 14 0 15 0 16 0 17 0 18 0 19 0 20 0 21 0 22 0 23 0 24 0 25 0 26 0 27 0 28 0 29 0 30 0 31 0 32 0 33 0 34 0 35 0 36 0 37 0 38 0 39 0 40 0 41 0 42 0 43 0 44 0 45 0 46 0 47 0 48 0 49 0 50 0 51 0 52 0 53 0 54 0 55 0 56 0 57 0 58 0 59 0 60 0 61 ...
output:
981392440 1962784880 2955924472 3949064064 4942203656 5935343248 6908906778 7908906778 8895185962 9888325554 10881465146 11874604738 12867744330 13860883922 14860883922 15860883922 16840302698 17840302698 18840302698 19517274953 20432088180 21432088180 22432088180 23318505816 24290110225 25261714634...
result:
ok 500 numbers
Test #3:
score: 5
Accepted
time: 908ms
memory: 88100kb
input:
5 2000 1 0 1 1 2 0 2 1 3 0 3 1 4 0 4 1 5 0 5 1 6 0 6 1 7 0 7 1 8 0 8 1 9 0 9 1 10 0 16 0 21 0 22 0 23 0 24 0 25 0 26 0 27 0 28 0 29 0 30 0 31 0 32 0 33 0 34 0 35 0 36 0 37 0 38 0 39 0 40 0 41 0 42 0 43 0 44 0 45 0 46 0 47 0 48 0 49 0 50 0 51 0 52 0 53 0 54 0 55 0 56 0 57 0 58 0 59 0 60 0 61 0 62 0 6...
output:
1000000000 1998400828 993283036 2997601242 1982118880 2974350138 1000000000 3995472424 1965887128 3985959244 3000000000 2956494828 2000000000 2959713978 0 4994340530 2940499173 3941189620 2944137507 0 5993208636 6992076742 7990944848 8989812954 9988681060 10988681060 11988681060 12988681060 13984153...
result:
ok 10000 numbers
Test #4:
score: 5
Accepted
time: 920ms
memory: 86056kb
input:
5 2000 1 0 1 1 2 0 2 1 3 0 3 1 4 0 4 1 5 0 5 1 6 0 6 1 7 0 7 1 8 0 8 1 9 0 9 1 10 0 16 0 21 0 22 0 23 0 24 0 25 0 26 0 27 0 28 0 29 0 30 0 31 0 32 0 33 0 34 0 35 0 36 0 37 0 38 0 39 0 40 0 41 0 42 0 43 0 44 0 45 0 46 0 47 0 48 0 49 0 50 0 51 0 52 0 53 0 54 0 55 0 56 0 57 0 58 0 59 0 60 0 61 0 62 0 6...
output:
1000000000 1989496542 1996067849 2985991461 1991714992 2960477667 2983449306 3982486380 3983429984 1969526880 1996565664 2953748358 1980701322 1994377494 3970332948 4978981299 1000000000 2954038725 4991414160 3956583228 5975476218 6971971137 7968466056 8964960975 9961455894 10957950813 11954445732 1...
result:
ok 10000 numbers
Test #5:
score: 5
Accepted
time: 936ms
memory: 86112kb
input:
5 2000 1 0 1 1 2 0 2 1 3 0 3 1 4 0 4 1 5 0 5 1 6 0 6 1 7 0 7 1 8 0 8 1 9 0 9 1 10 0 16 0 21 0 22 0 23 0 24 0 25 0 26 0 27 0 28 0 29 0 30 0 31 0 32 0 33 0 34 0 35 0 36 0 37 0 38 0 39 0 40 0 41 0 42 0 43 0 44 0 45 0 46 0 47 0 48 0 49 0 50 0 51 0 52 0 53 0 54 0 55 0 56 0 57 0 58 0 59 0 60 0 61 0 62 0 6...
output:
1000000000 1997363842 2000000000 2996045763 2954596806 1995148332 3000000000 3996045763 0 2983043281 2977715897 1970005876 1989593130 2999881107 2976355872 4993409605 0 3942019132 3999426588 2935916472 5992091526 6990773447 7990773447 8970557459 9958230246 10954121175 11950012104 12942775649 1393866...
result:
ok 10000 numbers
Test #6:
score: 0
Time Limit Exceeded
input:
5 10000 1 0 1 1 2 0 2 1 3 0 3 1 4 0 4 1 5 0 5 1 6 0 6 1 7 0 7 1 8 0 8 1 9 0 9 1 10 0 10 1 11 0 11 1 12 0 12 1 13 0 13 1 14 0 14 1 15 0 15 1 16 0 16 1 17 0 17 1 18 0 18 1 19 0 19 1 20 0 20 1 21 0 21 1 22 0 22 1 23 0 23 1 24 0 24 1 25 0 25 1 26 0 26 1 27 0 27 1 28 0 28 1 29 0 29 1 30 0 30 1 31 0 31 1 ...
output:
result:
Test #7:
score: 0
Time Limit Exceeded
input:
5 10000 1 0 1 1 2 0 2 1 3 0 3 1 4 0 4 1 5 0 5 1 6 0 6 1 7 0 7 1 8 0 8 1 9 0 9 1 10 0 10 1 11 0 11 1 12 0 12 1 13 0 13 1 14 0 14 1 15 0 15 1 16 0 16 1 17 0 17 1 18 0 18 1 19 0 19 1 20 0 20 1 21 0 21 1 22 0 22 1 23 0 23 1 24 0 24 1 25 0 25 1 26 0 26 1 27 0 27 1 28 0 28 1 29 0 29 1 30 0 30 1 31 0 31 1 ...
output:
result:
Test #8:
score: 0
Time Limit Exceeded
input:
5 10000 1 0 1 1 2 0 2 1 3 0 3 1 4 0 4 1 5 0 5 1 6 0 6 1 7 0 7 1 8 0 8 1 9 0 9 1 10 0 10 1 11 0 11 1 12 0 12 1 13 0 13 1 14 0 14 1 15 0 15 1 16 0 16 1 17 0 17 1 18 0 18 1 19 0 19 1 20 0 20 1 21 0 21 1 22 0 22 1 23 0 23 1 24 0 24 1 25 0 25 1 26 0 26 1 27 0 27 1 28 0 28 1 29 0 29 1 30 0 30 1 31 0 31 1 ...
output:
result:
Test #9:
score: 0
Runtime Error
input:
5 100000 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 0 10 0 11 0 12 0 13 0 14 0 15 0 16 0 17 0 18 0 19 0 20 0 21 0 22 0 23 0 24 0 25 0 26 0 27 0 28 0 29 0 30 0 31 0 32 0 33 0 34 0 35 0 36 0 37 0 38 0 39 0 40 0 41 0 42 0 43 0 44 0 45 0 46 0 47 0 48 0 49 0 50 0 51 0 52 0 53 0 54 0 55 0 56 0 57 0 58 0 59 0 60 0 ...
output:
result:
Test #10:
score: 0
Runtime Error
input:
5 100000 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 0 10 0 11 0 12 0 13 0 14 0 15 0 16 0 17 0 18 0 19 0 20 0 21 0 22 0 23 0 24 0 25 0 26 0 27 0 28 0 29 0 30 0 31 0 32 0 33 0 34 0 35 0 36 0 37 0 38 0 39 0 40 0 41 0 42 0 43 0 44 0 45 0 46 0 47 0 48 0 49 0 50 0 51 0 52 0 53 0 54 0 55 0 56 0 57 0 58 0 59 0 60 0 ...
output:
result:
Test #11:
score: 0
Memory Limit Exceeded
input:
5 100000 1 0 1 1 2 0 2 1 3 0 3 1 4 0 4 1 5 0 5 1 6 0 6 1 7 0 7 1 8 0 8 1 9 0 9 1 10 0 10 1 11 0 11 1 12 0 12 1 13 0 13 1 14 0 14 1 15 0 15 1 16 0 16 1 17 0 17 1 18 0 18 1 19 0 19 1 20 0 20 1 21 0 21 1 22 0 22 1 23 0 23 1 24 0 24 1 25 0 25 1 26 0 26 1 27 0 27 1 28 0 28 1 29 0 29 1 30 0 30 1 31 0 31 1...
output:
result:
Test #12:
score: 0
Memory Limit Exceeded
input:
5 100000 1 0 1 1 2 0 2 1 3 0 3 1 4 0 4 1 5 0 5 1 6 0 6 1 7 0 7 1 8 0 8 1 9 0 9 1 10 0 10 1 11 0 11 1 12 0 12 1 13 0 13 1 14 0 14 1 15 0 15 1 16 0 16 1 17 0 17 1 18 0 18 1 19 0 19 1 20 0 20 1 21 0 21 1 22 0 22 1 23 0 23 1 24 0 24 1 25 0 25 1 26 0 26 1 27 0 27 1 28 0 28 1 29 0 29 1 30 0 30 1 31 0 31 1...
output:
result:
Test #13:
score: 0
Runtime Error
input:
5 100000 1 0 1 1 2 0 2 1 3 0 3 1 4 0 4 1 5 0 5 1 6 0 6 1 7 0 7 1 8 0 8 1 9 0 9 1 10 0 10 1 11 0 11 1 12 0 12 1 13 0 13 1 14 0 14 1 15 0 15 1 16 0 16 1 17 0 17 1 18 0 18 1 19 0 19 1 20 0 20 1 21 0 21 1 22 0 22 1 23 0 23 1 24 0 24 1 25 0 25 1 26 0 26 1 27 0 27 1 28 0 28 1 29 0 29 1 30 0 30 1 31 0 31 1...
output:
result:
Test #14:
score: 0
Runtime Error
input:
5 100000 1 0 1 1 2 0 2 1 3 0 3 1 4 0 4 1 5 0 5 1 6 0 6 1 7 0 7 1 8 0 8 1 9 0 9 1 10 0 10 1 11 0 11 1 12 0 12 1 13 0 13 1 14 0 14 1 15 0 15 1 16 0 16 1 17 0 17 1 18 0 18 1 19 0 19 1 20 0 20 1 21 0 21 1 22 0 22 1 23 0 23 1 24 0 24 1 25 0 25 1 26 0 26 1 27 0 27 1 28 0 28 1 29 0 29 1 30 0 30 1 31 0 31 1...
output:
result:
Test #15:
score: 0
Runtime Error
input:
5 100000 1 0 1 1 2 0 2 1 3 0 3 1 4 0 4 1 5 0 5 1 6 0 6 1 7 0 7 1 8 0 8 1 9 0 9 1 10 0 10 1 11 0 11 1 12 0 12 1 13 0 13 1 14 0 14 1 15 0 15 1 16 0 16 1 17 0 17 1 18 0 18 1 19 0 19 1 20 0 20 1 21 0 21 1 22 0 22 1 23 0 23 1 24 0 24 1 25 0 25 1 26 0 26 1 27 0 27 1 28 0 28 1 29 0 29 1 30 0 30 1 31 0 31 1...
output:
result:
Test #16:
score: 0
Runtime Error
input:
5 100000 1 0 1 1 2 0 2 1 3 0 3 1 4 0 4 1 5 0 5 1 6 0 6 1 7 0 7 1 8 0 8 1 9 0 9 1 10 0 10 1 11 0 11 1 12 0 12 1 13 0 13 1 14 0 14 1 15 0 15 1 16 0 16 1 17 0 17 1 18 0 18 1 19 0 19 1 20 0 20 1 21 0 21 1 22 0 22 1 23 0 23 1 24 0 24 1 25 0 25 1 26 0 26 1 27 0 27 1 28 0 28 1 29 0 29 1 30 0 30 1 31 0 31 1...
output:
result:
Test #17:
score: 0
Memory Limit Exceeded
input:
5 500000 1 0 1 1 2 0 2 1 3 0 3 1 4 0 4 1 5 0 5 1 6 0 6 1 7 0 7 1 8 0 8 1 9 0 9 1 10 0 10 1 11 0 11 1 12 0 12 1 13 0 13 1 14 0 14 1 15 0 15 1 16 0 16 1 17 0 17 1 18 0 18 1 19 0 19 1 20 0 20 1 21 0 21 1 22 0 22 1 23 0 23 1 24 0 24 1 25 0 25 1 26 0 26 1 27 0 27 1 28 0 28 1 29 0 29 1 30 0 30 1 31 0 31 1...
output:
result:
Test #18:
score: 0
Memory Limit Exceeded
input:
5 500000 1 0 1 1 2 0 2 1 3 0 3 1 4 0 4 1 5 0 5 1 6 0 6 1 7 0 7 1 8 0 8 1 9 0 9 1 10 0 10 1 11 0 11 1 12 0 12 1 13 0 13 1 14 0 14 1 15 0 15 1 16 0 16 1 17 0 17 1 18 0 18 1 19 0 19 1 20 0 20 1 21 0 21 1 22 0 22 1 23 0 23 1 24 0 24 1 25 0 25 1 26 0 26 1 27 0 27 1 28 0 28 1 29 0 29 1 30 0 30 1 31 0 31 1...
output:
result:
Test #19:
score: 0
Memory Limit Exceeded
input:
5 500000 1 0 1 1 2 0 2 1 3 0 3 1 4 0 4 1 5 0 5 1 6 0 6 1 7 0 7 1 8 0 8 1 9 0 9 1 10 0 10 1 11 0 11 1 12 0 12 1 13 0 13 1 14 0 14 1 15 0 15 1 16 0 16 1 17 0 17 1 18 0 18 1 19 0 19 1 20 0 20 1 21 0 21 1 22 0 22 1 23 0 23 1 24 0 24 1 25 0 25 1 26 0 26 1 27 0 27 1 28 0 28 1 29 0 29 1 30 0 30 1 31 0 31 1...
output:
result:
Test #20:
score: 0
Memory Limit Exceeded
input:
5 500000 1 0 1 1 2 0 2 1 3 0 3 1 4 0 4 1 5 0 5 1 6 0 6 1 7 0 7 1 8 0 8 1 9 0 9 1 10 0 10 1 11 0 11 1 12 0 12 1 13 0 13 1 14 0 14 1 15 0 15 1 16 0 16 1 17 0 17 1 18 0 18 1 19 0 19 1 20 0 20 1 21 0 21 1 22 0 22 1 23 0 23 1 24 0 24 1 25 0 25 1 26 0 26 1 27 0 27 1 28 0 28 1 29 0 29 1 30 0 30 1 31 0 31 1...