QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#461469 | #5017. 相等树链 | zwh2008 | 0 | 0ms | 0kb | C++17 | 3.1kb | 2024-07-02 19:18:31 | 2024-07-02 19:18:33 |
answer
#include<bits/stdc++.h>
#include<bits/extc++.h>
using namespace std;
using ll=long long;
using ull=unsigned long long;
using pi=pair<int,int>;
#define fi first
#define se second
const int N=3e5+5;
int n,sz[N];
ll ans;
ull w[N];
bool del[N];
mt19937_64 rd(5384);
__gnu_pbds::cc_hash_table<ull,int>v1,v2,v3;
struct tree{
int sn,fa[N],dep[N],mx[20][N],pos[N];
ull s[N];
vector<int>e[N];
int Min(int x,int y){return dep[x]<dep[y]?x:y;}
int lca(int x,int y) {
if(x==y)return x;
x=pos[x],y=pos[y];
if(x>y)swap(x,y);
int k=__lg(y-x);
return fa[Min(mx[k][x+1],mx[k][y-(1<<k)+1])];
}
int dis(int x,int y){return dep[x]+dep[y]-2*dep[lca(x,y)];}
ull val(int x,int y){int z=lca(x,y);return s[x]+s[y]-s[z]-s[fa[z]];}
void dfs(int x) {
dep[x]=dep[fa[x]]+1,mx[0][pos[x]=++sn]=x,s[x]=s[fa[x]]+w[x];
for(int y:e[x])if(y!=fa[x])dfs(y);
}
void init(int n) {
sn=0;for(int i=1;i<=n;i++)fa[i]=0,e[i].clear();
for(int i=2;i<=n;i++)cin>>fa[i],e[fa[i]].push_back(i),e[i].push_back(fa[i]);
dfs(1);
for(int i=1;1<<i<=n;i++)for(int j=1;j+(1<<i)-1<=n;j++)mx[i][j]=Min(mx[i-1][j],mx[i-1][j+(1<<i-1)]);
}
}t1,t2;
struct info{
int x,y,d;
info(){x=y=d=0;}
bool ins(int z) {
if(!x)return x=y=z,1;
int d1=t2.dis(z,x),d2=t2.dis(z,y);
if(d1+d2==d)return 1;
if(d1+d==d2)return x=z,d=d2,1;
if(d2+d==d1)return y=z,d=d1,1;
return 0;
}
};
void gsz(int x,int p){sz[x]=1;for(int y:t1.e[x])if(y!=p&&!del[y])gsz(y,x),sz[x]+=sz[y];}
void grt(int&rt,int tot,int x,int p) {
int mx=0;for(int y:t1.e[x])if(y!=p&&!del[y])grt(rt,tot,y,x),mx=max(mx,sz[y]);
if(max(mx,tot-sz[x])<=tot/2)rt=x;
}
void calc(int u,int x,int p,info t,ull F) {
if(!t.ins(x))return;F+=w[x];
ll w1=t2.val(t.x,u),w2=t2.val(t.y,u);
if(t.d==t2.dis(u,t.x)+t2.dis(u,t.y))ans+=v2[w1+w2-2*w[u]-F];
ans+=v3[w[u]+F-w1]+(t.x!=t.y)*v3[w[u]+F-w2]+v1[F]+v2[w1-w[u]-F]+(t.x!=t.y)*v2[w2-w[u]-F];
// cerr<<u<<' '<<x<<' '<<ans<<' '<<t.x<<' '<<t.y<<' '<<t.d<<'\n';
for(int y:t1.e[x])if(y!=p&&!del[y])calc(u,y,x,t,F);
}
void upd(int u,int x,int p,info t,ull F) {
if(!t.ins(x))return;F+=w[x];
ll w1=t2.val(t.x,u),w2=t2.val(t.y,u);
if(t.d==t2.dis(u,t.x)+t2.dis(u,t.y))v1[w1+w2-2*w[u]-F]++;
v1[w1-w[u]-F]++,v1[w2-w[u]-F]+=(t.x!=t.y),v3[w1-w[u]-F]++,v3[w2-w[u]-F]+=(t.x!=t.y),v2[F]++;
for(int y:t1.e[x])if(y!=p&&!del[y])upd(u,y,x,t,F);
}
void slv(int x) {
int R;gsz(x,0),grt(R,sz[x],x,0),del[R]=1;
// cerr<<R<<'\n';
v1.clear(),v2.clear(),v3.clear(),v2[0]=1;
for(int y:t1.e[R])if(!del[y])calc(R,y,0,info(),0),upd(R,y,0,info(),0);
for(int y:t1.e[R])if(!del[y])slv(y);
}
void solve() {
cin>>n,ans=n;
for(int i=1;i<=n;i++)del[i]=0,w[i]=rd();
t1.init(n),t2.init(n);
slv(1),cout<<ans<<'\n';
}
int main() {
freopen("ex_tree2.in","r",stdin);
freopen("tree.out","w",stdout);
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
double ST=clock();
solve();
cerr<<"Run time: "<<1000*(clock()-ST)/CLOCKS_PER_SEC<<" ms\n";
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Dangerous Syscalls
Test #1:
score: 0
Dangerous Syscalls
input:
5000 1296 1400 867 4533 1296 2007 2059 115 821 2960 3187 1597 2409 2708 4743 4778 1345 3967 911 3400 4249 3793 339 3145 3490 607 4148 3513 3264 3852 568 775 828 1348 423 3678 305 1938 1096 1373 2662 1048 4328 4208 203 779 3103 3372 4523 192 264 792 4943 2211 2494 3513 3555 4935 3277 3390 4624 128 18...
output:
result:
Subtask #2:
score: 0
Dangerous Syscalls
Test #6:
score: 0
Dangerous Syscalls
input:
200000 13177 40498 104798 83659 186055 32788 86489 72123 13521 134868 158968 60124 166316 163390 120935 103000 83938 57807 97940 40447 137723 154683 191864 59080 102808 3969 21451 165592 128776 49468 4101 26441 139317 59503 18053 118809 187783 149816 21609 98521 165692 52964 60425 23437 29614 106886...
output:
result:
Subtask #3:
score: 0
Skipped
Dependency #2:
0%
Subtask #4:
score: 0
Skipped
Dependency #1:
0%