QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#183322#5017. 相等树链zyz07100 ✓2758ms190828kbC++174.1kb2023-09-19 13:38:542023-09-19 13:38:54

Judging History

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

  • [2023-09-19 13:38:54]
  • 评测
  • 测评结果:100
  • 用时:2758ms
  • 内存:190828kb
  • [2023-09-19 13:38:54]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define For(Ti,Ta,Tb) for(auto Ti=(Ta);Ti<=(Tb);++Ti)
#define Dec(Ti,Ta,Tb) for(auto Ti=(Ta);Ti>=(Tb);--Ti)
#define debug(...) fprintf(stderr,__VA_ARGS__)
#define range(Tx) begin(Tx),end(Tx)
using ll=long long;
using ull=unsigned long long;
mt19937_64 eng(random_device{}());
const int N=2e5+5,LogN=18;
int n,a[N];
ull val[N];
struct Tree{
	int fa[N];
	vector<int> e[N],son[N];
	ull pre[N];
	void input(){
		For(i,2,n){
			cin>>fa[i];
			e[fa[i]].push_back(i);
			e[i].push_back(fa[i]);
		}
	}
	int dep[N],siz[N],dfn[N],rk[N],dfx,mn[N][LogN];
	void dfs(int u){
		pre[u]=pre[fa[u]]^val[u];
		dep[u]=dep[fa[u]]+1;
		rk[dfn[u]=++dfx]=u;
		siz[u]=1;
		for(int v:e[u]){
			if(v!=fa[u]){
				dfs(v);
				siz[u]+=siz[v];
			}
		}
	}
	int argmin(int u,int v){
		return dfn[u]<dfn[v]?u:v;
	}
	void build(){
		For(i,1,n){
			son[i]=e[i];
			if(i>1){
				son[i].erase(find(range(son[i]),fa[i]));
			}
		}
		dfs(1);
		For(i,1,n) mn[i][0]=fa[rk[i]];
		For(j,1,LogN-1){
			For(i,1,n-(1<<j)+1){
				mn[i][j]=argmin(mn[i][j-1],mn[i+(1<<(j-1))][j-1]);
			}
		}
	}
	bool subtree(int u,int v){
		return dfn[u]<=dfn[v]&&dfn[v]<dfn[u]+siz[u];
	}
	int lca(int u,int v){
		if(u==v) return u;
		if((u=dfn[u])>(v=dfn[v])) swap(u,v);
		int k=__lg(v-(u++));
		return argmin(mn[u][k],mn[v-(1<<k)+1][k]);
	}
	int toward(int u,int v){
		if(!subtree(u,v)) return fa[u];
		if(u==v) return u;
		int l=0,r=int(son[u].size())-1;
		while(l<r){
			int mid=(l+r+1)/2;
			if(dfn[son[u][mid]]<=dfn[v]) l=mid;
			else r=mid-1;
		}
		return son[u][l];
	}
	ull chain_val(int u,int v){
		return pre[u]^pre[v]^val[lca(u,v)];
	}
}t1,t2;
struct Path{
	int u,v;
	bool include(int x){
		int lca=t2.lca(u,v);
		return x==lca||t2.subtree(x,u)!=t2.subtree(x,v);
	}
	bool insert(int x){
		if(include(x)) return 1;
		if(Path{u,x}.include(v)) v=x;
		else if(Path{v,x}.include(u)) u=x;
		else return 0;
		return 1;
	}
}; 
int siz[N],mxsiz[N],vis[N];
void get_root(int u,int fa,int tot,int& rt){
	siz[u]=1;
	mxsiz[u]=0;
	for(int v:t1.e[u]){
		if(v!=fa&&!vis[v]){
			get_root(v,u,tot,rt);
			siz[u]+=siz[v];
			mxsiz[u]=max(mxsiz[u],siz[v]);
		}
	}
	mxsiz[u]=max(mxsiz[u],tot-siz[u]);
	if(!rt||mxsiz[u]<mxsiz[rt]) rt=u;
}
template<typename T>
ll calc(vector<T>& vec){
	sort(range(vec));
	ll ans=0;
	for(int i=0;i<int(vec.size());){
		int l=i;
		while(i<int(vec.size())&&vec[i]==vec[l]) ++i;
		ans+=1LL*(i-l)*(i-l-1)/2;
	}
	return ans;
}
template<typename T>
ll calc(vector<T>& vec1,vector<T>& vec2){
	sort(range(vec1));
	sort(range(vec2));
	ll ans=0;
	for(int i=0;i<int(vec1.size());++i){
		auto [L,R]=equal_range(range(vec2),vec1[i]);
		ans+=R-L;
	}
	return ans;
}
ll ans;
void solve(int u){
	int rt=0;
	get_root(u,0,siz[u],rt);
	vis[u=rt]=1;
	vector<tuple<ull,int,int>> vec;
	vector<pair<ull,int>> nvec1,nvec2;
	auto dfs=[&](auto dfs,int u,int fa,ull cur,Path pth){
		if(!pth.insert(u)) return;
		cur^=val[u];
		ull z=cur^t2.chain_val(pth.u,pth.v);
		if(!z) ++ans;
		if(pth.u!=rt&&pth.v!=rt){
			nvec1.emplace_back(z,t1.toward(rt,u));
		}
		nvec2.emplace_back(cur^val[rt],t1.toward(rt,u));
		for(int x:{pth.u,pth.v}){
			vec.emplace_back(cur^t2.chain_val(x,rt),t1.toward(rt,u),t2.toward(rt,x));
		}
		for(int v:t1.e[u]){
			if(v!=fa&&!vis[v]){
				dfs(dfs,v,u,cur,pth);
			}
		}
	};
	for(int v:t1.e[u]){
		if(!vis[v]){
			dfs(dfs,v,u,val[u],{u,u});
		}
	}
	vector<ull> vec1;
	vector<pair<ull,int>> vec2,vec3;
	for(auto [v1,v2,v3]:vec){
		vec1.push_back(v1);
		vec2.emplace_back(v1,v2);
		vec3.emplace_back(v1,v3);
	}
	ans+=calc(vec1)-calc(vec2)-calc(vec3)+calc(vec);
	vector<ull> nvec1_,nvec2_;
	for(auto [v1,v2]:nvec1){
		nvec1_.push_back(v1);
	}
	for(auto [v1,v2]:nvec2){
		nvec2_.push_back(v1);
	}
	ans+=calc(nvec1_,nvec2_)-calc(nvec1,nvec2);
	for(int v:t1.e[u]){
		if(!vis[v]) solve(v);
	}
}
int main(){
	cin.tie(nullptr)->sync_with_stdio(false);
	cin>>n;
	ans=n;
	For(i,1,n) val[i]=eng();
	t1.input();
	t2.input();
	t1.build();
	t2.build();
	siz[1]=n;
	solve(1);
	cout<<ans<<'\n';
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 10
Accepted

Test #1:

score: 10
Accepted
time: 21ms
memory: 36380kb

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:

76002

result:

ok 1 number(s): "76002"

Test #2:

score: 0
Accepted
time: 13ms
memory: 34812kb

input:

5000
1820 281 610 3735 3580 3994 2060 2424 3338 2859 281 532 1286 1771 825 3738 3793 2260 556 4068 3793 4169 4411 4941 122 4270 4711 524 4037 2508 50 3343 2030 1151 4002 533 2994 1440 1762 3851 3050 4470 555 1979 3178 3933 3793 281 4810 520 3793 3535 2526 4422 2859 1561 1544 649 4544 2882 1236 2749 ...

output:

604316

result:

ok 1 number(s): "604316"

Test #3:

score: 0
Accepted
time: 19ms
memory: 38844kb

input:

5000
4333 51 707 3055 3433 1451 1305 1431 3081 302 1633 88 2024 441 120 4650 3927 4970 2578 3170 4245 4204 2102 4954 3140 1039 360 3173 4203 4927 4437 4337 4502 1712 3598 2968 2 4884 1260 1768 585 1815 4346 2938 4638 4886 4482 1095 1452 298 2702 2257 1375 2819 4482 711 220 396 3907 4792 2798 4445 42...

output:

912032

result:

ok 1 number(s): "912032"

Test #4:

score: 0
Accepted
time: 17ms
memory: 32788kb

input:

5000
362 4710 4997 4405 4728 3964 4258 3568 4997 2924 2931 4997 1094 2174 2220 127 4739 260 2591 4130 4916 1614 1408 324 2924 3272 4997 4020 2924 4216 2924 2931 2924 4783 271 1101 2924 246 4953 2553 4588 2924 1770 3738 4617 2508 2486 2137 1348 4847 2632 596 1011 1442 1287 4665 2924 2203 4411 726 109...

output:

1176721

result:

ok 1 number(s): "1176721"

Test #5:

score: 0
Accepted
time: 17ms
memory: 32908kb

input:

5000
3579 3530 3328 388 4864 4954 4597 3600 2428 1610 4533 1797 427 1296 3595 3861 4703 2914 4194 3195 451 585 3600 1134 1649 470 2049 2843 2845 3473 26 845 484 3301 1929 1342 1937 2003 1543 832 2301 2543 1889 1211 1619 1937 4471 585 4440 3600 1398 4687 2931 3982 2334 589 388 4012 873 66 2406 3861 1...

output:

1226857

result:

ok 1 number(s): "1226857"

Subtask #2:

score: 20
Accepted

Test #6:

score: 20
Accepted
time: 2758ms
memory: 188108kb

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:

5859368

result:

ok 1 number(s): "5859368"

Test #7:

score: 0
Accepted
time: 2662ms
memory: 189400kb

input:

200000
161252 109349 161307 131176 54282 35989 53345 116328 52886 20845 166068 198634 185607 110703 32172 153437 50060 194586 193712 73284 32556 105087 55275 157714 22357 182552 31342 100889 145473 91759 18280 144489 108133 130988 11561 20028 121278 138065 83647 33848 33634 31990 198971 110781 12801...

output:

110388948

result:

ok 1 number(s): "110388948"

Test #8:

score: 0
Accepted
time: 2548ms
memory: 189156kb

input:

200000
36915 117643 88659 78272 142053 101722 71138 149291 152470 118051 45210 31645 187500 22733 178345 55170 28768 44890 26946 76823 76271 9886 197447 130337 74747 175940 118067 191159 19884 113644 73935 160371 97288 153196 50668 47567 113533 73075 90904 115114 191694 127924 127338 41621 134964 59...

output:

469103910

result:

ok 1 number(s): "469103910"

Test #9:

score: 0
Accepted
time: 2513ms
memory: 189608kb

input:

200000
3943 160214 22824 98337 873 3550 102218 67841 56961 130137 87920 154401 45794 144615 52487 75188 13477 151928 41794 147148 88519 25499 59155 187395 70572 37799 7846 166650 165689 178923 110784 68004 124416 7070 37566 126445 23236 78630 190578 145179 81517 809 99830 98383 67869 158370 182186 1...

output:

254891707

result:

ok 1 number(s): "254891707"

Test #10:

score: 0
Accepted
time: 2408ms
memory: 190828kb

input:

200000
78377 9603 105868 5816 97565 17017 11229 64332 152282 115911 5141 119594 138303 67697 62645 28928 113832 166252 170769 60777 39110 85804 122988 117490 80461 169830 15334 189378 9037 191383 143689 123124 18788 113025 35138 63649 116803 33050 135937 99323 119570 44477 174794 28051 74975 174331 ...

output:

779879990

result:

ok 1 number(s): "779879990"

Test #11:

score: 0
Accepted
time: 2283ms
memory: 182416kb

input:

200000
31706 198038 102731 72443 44408 116386 129202 193795 176464 175136 12293 17325 194955 2759 172903 37032 60623 73343 55344 138068 10675 29053 29280 94350 175071 73192 10795 127030 18516 28564 170635 88693 143311 110487 10208 57489 1052 33420 156977 149595 34056 171577 39262 71741 71633 61355 1...

output:

4797642624

result:

ok 1 number(s): "4797642624"

Subtask #3:

score: 30
Accepted

Dependency #2:

100%
Accepted

Test #12:

score: 30
Accepted
time: 1534ms
memory: 135800kb

input:

200000
62936 42114 49454 95737 154735 83651 12241 12518 111465 87130 38023 12482 194231 193238 50051 69033 102675 40262 72917 146819 56538 159148 35426 119935 46694 63476 37721 177034 120832 10487 177187 12093 118464 95232 28721 165669 13308 116990 16648 187886 3227 181605 10993 174426 186874 45794 ...

output:

4835011

result:

ok 1 number(s): "4835011"

Test #13:

score: 0
Accepted
time: 1559ms
memory: 137076kb

input:

200000
117346 14600 165510 116888 52961 149634 150636 105338 68881 124191 98067 96397 80646 37652 113476 162093 192955 14846 41952 117488 43809 149110 8646 20663 111792 168686 198728 43285 55020 158833 71561 1845 113596 107783 3475 22566 18234 96583 81970 118130 124029 49120 157625 95040 173297 2239...

output:

88083001

result:

ok 1 number(s): "88083001"

Test #14:

score: 0
Accepted
time: 1617ms
memory: 137756kb

input:

200000
180353 22838 116976 47650 47301 18804 72503 121146 139006 127116 81928 25945 160151 151263 5009 199315 124867 41777 185815 137712 121731 184624 148268 173665 14304 39586 11624 148018 132282 11376 25035 60830 180942 185284 70705 68571 128397 31188 146378 2371 42780 170798 57943 34836 142029 57...

output:

238990581

result:

ok 1 number(s): "238990581"

Test #15:

score: 0
Accepted
time: 1645ms
memory: 138836kb

input:

200000
80521 175292 174888 75736 118844 49204 87310 60053 184430 80104 156394 198987 120213 187426 42888 146128 8808 71884 12255 4173 160042 116667 30064 28297 9281 27261 153520 122562 101424 118131 155936 189138 139003 131872 134996 143266 130692 39730 105133 182377 83727 94141 122163 62000 93510 4...

output:

264537751

result:

ok 1 number(s): "264537751"

Test #16:

score: 0
Accepted
time: 1748ms
memory: 149916kb

input:

200000
70911 55540 46403 16848 190095 173929 31463 33716 187553 157459 63323 168438 50190 61351 45123 153058 80265 38320 113938 35529 72085 127943 20581 188155 68364 10339 76425 174573 147627 72053 53619 91732 160585 160006 16179 161419 182637 190038 104960 68220 8581 166353 126573 150098 15312 1670...

output:

1271317642

result:

ok 1 number(s): "1271317642"

Subtask #4:

score: 40
Accepted

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Dependency #3:

100%
Accepted

Test #17:

score: 40
Accepted
time: 1154ms
memory: 127984kb

input:

200000
197388 79974 100587 177128 134036 175751 11599 38985 121336 94310 134349 148400 184223 198453 78801 128300 153372 112117 70364 25389 78433 165891 72392 37171 184371 110264 135143 36321 197823 40623 77039 53474 189765 103591 150948 47841 61367 128141 43512 23654 104014 117808 142339 64285 3011...

output:

13726233

result:

ok 1 number(s): "13726233"

Test #18:

score: 0
Accepted
time: 1110ms
memory: 125984kb

input:

200000
185100 48652 107422 185865 171700 111282 145211 37813 156334 11477 93539 117159 134664 144994 82754 125448 141937 181998 142224 177970 143433 17551 127813 178693 16140 87118 72964 172649 166920 55724 19118 129112 165021 88500 31331 23299 68598 139845 54660 64814 104328 184967 64327 105257 846...

output:

114796269

result:

ok 1 number(s): "114796269"

Test #19:

score: 0
Accepted
time: 1087ms
memory: 130560kb

input:

200000
64020 177899 77858 66722 133047 73021 135779 29313 74129 185815 63836 165297 136240 32867 72223 106861 147542 36705 114751 24994 141801 41602 69580 82821 132566 8637 162237 94738 147566 188423 189567 149630 71461 184265 114268 66435 164457 169613 123696 25217 11346 8610 14813 51906 111301 441...

output:

611876066

result:

ok 1 number(s): "611876066"

Test #20:

score: 0
Accepted
time: 1042ms
memory: 131208kb

input:

200000
119470 108567 42974 94492 65351 12097 160571 62653 43943 38574 6278 138517 164388 141386 108790 77703 75010 161303 79497 180736 137213 41096 58983 17669 140015 71466 4785 125416 96156 93054 30189 104429 143602 82244 99503 18718 30709 48848 195838 69247 6278 2856 182086 22902 171900 15076 6646...

output:

877731141

result:

ok 1 number(s): "877731141"

Test #21:

score: 0
Accepted
time: 1103ms
memory: 130244kb

input:

200000
82489 94509 116438 60336 76338 77888 92266 195287 121981 98349 177385 170368 133628 130863 85259 164937 180121 47312 146797 88140 94837 196369 81600 94991 100272 63934 186513 76786 168195 136323 88140 188414 184498 55400 73820 128339 159587 186860 64265 31426 62905 110963 124132 149669 44981 ...

output:

555047876

result:

ok 1 number(s): "555047876"

Test #22:

score: 0
Accepted
time: 1274ms
memory: 150056kb

input:

200000
175214 199806 100400 195518 182358 6244 61249 192543 59419 166213 157786 125070 9011 172441 166879 24514 35395 100400 135269 86954 161600 115207 181028 160837 43148 100400 193137 167022 105082 169635 30483 83793 7838 109414 48465 16495 89349 196355 7838 24413 88244 106525 94395 137131 124791 ...

output:

4622295274

result:

ok 1 number(s): "4622295274"

Test #23:

score: 0
Accepted
time: 532ms
memory: 142292kb

input:

200000
180365 180365 81054 100187 8274 93368 1410 18205 140067 23716 109105 190013 198489 174996 93605 140330 87606 26084 175988 6920 31563 88038 180365 173033 141425 945 180365 180365 51346 198987 129643 42250 141379 156098 107612 180365 54625 177252 147170 107612 8274 363 69478 142462 52572 8395 5...

output:

2796709144

result:

ok 1 number(s): "2796709144"

Test #24:

score: 0
Accepted
time: 539ms
memory: 131980kb

input:

200000
135826 114194 157005 40643 145907 145907 52643 69538 38415 88169 48506 88169 151097 145907 133035 120861 38831 150865 10061 123249 118157 145532 88169 184935 76839 145907 192098 30161 120441 145907 88169 132796 193303 63075 30137 145907 44587 88169 48506 23970 47895 65935 94689 196480 131132 ...

output:

2352487674

result:

ok 1 number(s): "2352487674"

Test #25:

score: 0
Accepted
time: 488ms
memory: 128056kb

input:

200000
142221 101607 142221 186170 151694 108107 34291 96863 154195 22128 151694 47801 179969 76013 40159 142221 21753 61355 151694 151694 122882 15000 185814 67967 36212 46636 142221 168649 199799 178383 119022 151694 151694 151694 119130 151694 4272 171500 110460 97357 151694 183961 193622 8502 15...

output:

8975840076

result:

ok 1 number(s): "8975840076"