QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#307614#5017. 相等树链Ustinian260 1137ms57872kbC++143.0kb2024-01-18 21:26:202024-01-18 21:26:20

Judging History

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

  • [2024-01-18 21:26:20]
  • 评测
  • 测评结果:0
  • 用时:1137ms
  • 内存:57872kb
  • [2024-01-18 21:26:20]
  • 提交

answer

/*
start coding at : 20:20
*/
#include<bits/stdc++.h>
#define L(i,s,t) for(int i=(s);i<=(t);++i)
#define R(i,s,t) for(int i=(s);i>=(t);--i)
#define T(i,s,t) for(int i=(s);i< (t);++i)
#define iV inline void
#define iI inline int
#define gc getchar
#define eb emplace_back
#define ll long long
#define ull unsigned ll
using namespace std;
iI read() {
    int x=0;char c=gc();
    while(!isdigit(c)) c=gc();
    while(isdigit(c)) x=x*10+(c^48),c=gc();
    return x;
}
iV chx(int &x,int y) {x=x>y?x:y;}
const int N=2e5+3;
mt19937_64 yml(20070505);
int n;
ll ans;
ull rd[N];
vector<int> e1[N],e2[N];
//点分治
int rt,all,sz[N],mx[N]={N};
bool vis[N],cur[N];
iV RT(int u,int fa) {
    sz[u]=1,mx[u]=0;
    for(int v:e1[u]) if(v^fa&&!vis[v]) 
    RT(v,u),sz[u]+=sz[v],chx(mx[u],sz[v]);
    chx(mx[u],all-sz[u]);
    if(mx[u]<mx[rt]) rt=u;
}
iV SZ(int u,int fa) {
    sz[u]=1;
    for(int v:e1[u]) if(v^fa&&!vis[v]) 
    SZ(v,u),sz[u]+=sz[v];
}
int cl,c[N],lv[N]={-1};
ull v1[N],v2[N];
iV T2(int u,int fa,int col) {
    v2[u]=v2[fa]^rd[u],c[u]=col,lv[u]=lv[fa]+1;
    for(int v:e2[u]) if(v^fa&&!vis[v]) T2(v,u,col);
}
int P,U,lim[N];
struct node{ull v;int c;}p[N<<1];
ull t1[N],t2[N];
int ct1,ct2;
iV T1(int u,int fa,int a,int b) {
    if(a&&c[u]^c[a]&&b&&c[u]^c[b]) return;
    if(!a) a=u;
    else if(c[a]==c[u]&&lv[u]>lv[a]) a=u;
    else if(c[a]!=c[u]&&lv[u]>lv[b]) b=u;
    t1[++ct1]=v1[u]=v1[fa]^rd[u];
    if(a) p[++P]={v1[u]^v2[a],c[a]};
    if(b) p[++P]={v1[u]^v2[b],c[b]};
    if(a&&b) t2[++ct2]=v1[u]^v2[a]^v2[b];
    for(int v:e1[u]) if(v^fa&&!vis[v]) T1(v,u,a,b);
}
iV calc_case1(int l,int r,int x,int y,int k) {
    sort(t1+l,t1+r+1),sort(t2+x,t2+y+1);
    int cnt=0;
    L(i,l,r) {
        if(i>l&&t1[i]==t1[i-1]) {ans+=k*cnt;continue;}
        cnt=0;
        while(x<=y&&t2[x]^t1[i]) ++x;
        if(x>y) return;
        while(x<=y&&t2[x]==t1[i]) ++x,++cnt;
        ans+=k*cnt;
    }
}
unordered_map<int,int> mp[N];
int sum[N];
iV upd(node i) {++mp[i.v][i.c],++sum[i.v];}
iI que(node i) {return sum[i.v]-mp[i.v][i.c];}
iV calc_case2() {
    static ull v[N<<1];int V=0;
    L(i,1,P) v[V++]=p[i].v;sort(v,v+V),V=unique(v,v+V)-v;
    L(i,1,P) p[i].v=lower_bound(v,v+V,p[i].v)-v;
    L(i,1,U) {
        int l=lim[i-1]+1,r=lim[i];
        L(j,l,r) ans+=que(p[j]);L(j,l,r) upd(p[j]);
    }
    T(i,0,V) sum[i]=0,mp[i].clear();
}
iV sol(int s) {
    vis[s]=1,v1[s]=v2[s]=rd[s],cl=lv[s]=c[s]=0;
    for(int v:e2[s]) if(!vis[v]) T2(v,s,++cl);
    p[P=1]={0,c[s]},lim[U=1]=1,t1[ct1=1]=v1[s],ct2=0;
    for(int v:e1[s]) if(!vis[v]) {
        int tc1=ct1,tc2=ct2;
        T1(v,s,s,0),lim[++U]=P;
        calc_case1(tc1+1,ct1,tc2+1,ct2,-1);
    }
    calc_case1(1,ct1,1,ct2,1),calc_case2();
    SZ(s,0);
    for(int v:e1[s]) if(!vis[v]) all=sz[v],RT(v,rt=0),sol(rt);
}
int main() {
    n=ans=read();
    L(i,2,n) {int j=read();e1[i].eb(j),e1[j].eb(i);}    
    L(i,2,n) {int j=read();e2[i].eb(j),e2[j].eb(i);}
    L(i,1,n) rd[i]=yml();
    all=n,RT(1,0),sol(rt);
    cout<<ans<<"\n";
    return 0;
}

详细

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 16ms
memory: 39156kb

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:

34041

result:

wrong answer 1st numbers differ - expected: '76002', found: '34041'

Subtask #2:

score: 0
Wrong Answer

Test #6:

score: 0
Wrong Answer
time: 1137ms
memory: 57872kb

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:

1403321

result:

wrong answer 1st numbers differ - expected: '5859368', found: '1403321'

Subtask #3:

score: 0
Skipped

Dependency #2:

0%

Subtask #4:

score: 0
Skipped

Dependency #1:

0%