QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#100738#5017. 相等树链1kri60 5092ms352952kbC++147.1kb2023-04-27 20:48:372023-04-27 20:48:40

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-04-27 20:48:40]
  • 评测
  • 测评结果:60
  • 用时:5092ms
  • 内存:352952kb
  • [2023-04-27 20:48:37]
  • 提交

answer

#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#define ll long long
using namespace std;
int n;
ll ans;
namespace T1{
	int u[400005],v[400005],first[200005],nxt[400005];
	void init(){
		for (int i=2;i<=n;i++){
			scanf("%d",&u[i]);
			v[i]=i;
			nxt[i]=first[u[i]],first[u[i]]=i;
			u[i+n]=v[i],v[i+n]=u[i];
			nxt[i+n]=first[u[i+n]],first[u[i+n]]=i+n;
		}
		return;
	}
	vector<int> id;
	int _book[200005],book[200005];
	vector<int> e[200005];
	int fa[200005],depth[200005],sz[200005],son[200005];
	int idx,dfn[200005],dfo[200005],top[200005];
	void dfs1(int now,int f,int d){
		book[now]=1;
		fa[now]=f,depth[now]=d;
		sz[now]=1,son[now]=0;
		for (int i=first[now];i;i=nxt[i])
			if (v[i]!=f&&_book[v[i]]==1){
				e[now].push_back(v[i]);
				dfs1(v[i],now,d+1);
				sz[now]+=sz[v[i]];
				if (son[now]==0||sz[v[i]]>sz[son[now]])son[now]=v[i];
			}
		return;
	}
	void dfs2(int now,int t){
		dfn[now]=++idx;
		top[now]=t;
		if (son[now]!=0)dfs2(son[now],t);
		for (int i=0;i<(int)e[now].size();i++)
			if (e[now][i]!=son[now])dfs2(e[now][i],e[now][i]);
		dfo[now]=idx;
		return;
	}
	void build(int rt,vector<int> _id){
		id=_id;
		for (int i=0;i<(int)id.size();i++){
			_book[id[i]]=1;
			e[id[i]].clear();
		}
		dfs1(rt,0,0);
		idx=0;
		dfs2(rt,rt);
		return;
	}
	void clear(){
		for (int i=0;i<(int)id.size();i++)_book[id[i]]=book[id[i]]=0;
		return;
	}
	int lca(int a,int b){
		while(top[a]!=top[b]){
			if (depth[top[a]]<depth[top[b]])swap(a,b);
			a=fa[top[a]];
		}
		if (depth[a]<depth[b])return a;
		return b;
	}
	int dis(int a,int b){
		return depth[a]+depth[b]-2*depth[lca(a,b)];
	}
	bool in(int a,int b){//whether a is in b
		return dfn[a]>dfn[b]&&dfn[a]<=dfo[b];
	}
}
struct node{
	int x,y,l,c;
};
node add(node a,int z){
	if (a.x==-1)return a;
	if (T1::book[z]==0)return (node){-1,-1,-1,-1};
	a.c++;
	int l1=T1::dis(z,a.x);
	int l2=T1::dis(z,a.y);
	if (a.l==l1+l2)return a;
	if (l1==a.l+l2){
		a.y=z;
		a.l=l1;
		if (T1::depth[a.x]>T1::depth[a.y])swap(a.x,a.y);
		return a;
	}
	if (l2==a.l+l1){
		a.x=z;
		a.l=l2;
		if (T1::depth[a.x]>T1::depth[a.y])swap(a.x,a.y);
		return a;
	}
	a.x=a.y=a.l=a.c=-1;
	return a;
}
int x;
namespace QwQ{
	using namespace T1;
	ll ans;
	vector<node> d;
	vector<pair<int,int>> ins[200005];
	int maxn;
	struct DS1{
		int a[1000005];
		void add(int t,int v){
			a[maxn+t]+=v;
			return;
		}
		int ask(int t){
			return a[maxn+t];
		}
	}ds1;
	struct SGT{
		struct tree_node{
			int l,r,val;
			tree_node(){
				l=r=val=0;
				return;
			}
			void mem(){
				l=r=val=0;
				return;
			}
		}tree[20000005];
		int tree_cnt;
		void add(int &now,int nowl,int nowr,int pos,int val){
			if (now==0)now=++tree_cnt;
			tree[now].val+=val;
			if (nowl==nowr)return;
			int mid=(nowl+nowr)/2;
			if (pos<=mid)add(tree[now].l,nowl,mid,pos,val);
			else add(tree[now].r,mid+1,nowr,pos,val);
			return;
		}
		int ask(int now,int nowl,int nowr,int lt,int rt){
			if (now==0)return 0;
			if (nowl>=lt&&nowr<=rt)return tree[now].val;
			int mid=(nowl+nowr)/2,s=0;
			if (mid>=lt)s+=ask(tree[now].l,nowl,mid,lt,rt);
			if (mid+1<=rt)s+=ask(tree[now].r,mid+1,nowr,lt,rt);
			return s;
		}
		void clear(){
			while(tree_cnt>0)tree[tree_cnt].mem(),tree_cnt--;
			return;
		}
	}sgt;
	struct DS2{
		int root[1000005];
		void add(int t,int p,int v){
			if (dfn[p]+1<=maxn)sgt.add(root[t+maxn],1,maxn,dfn[p]+1,v);
			if (dfo[p]+1<=maxn)sgt.add(root[t+maxn],1,maxn,dfo[p]+1,-v);
			return;
		}
		int ask(int t,int p){
			return sgt.ask(root[t+maxn],1,maxn,1,dfn[p]);
		}
	}ds2;
	struct DS3{
		int root[1000005];
		void add(int t,int p,int v){
			sgt.add(root[t+maxn],1,maxn,dfn[p],v);
			return;
		}
		int ask(int t,int p){
			if (dfn[p]==dfo[p])return 0;
			return sgt.ask(root[t+maxn],1,maxn,dfn[p]+1,dfo[p]);
		}
	}ds3;
	void dfs(int now){
		for (int i=0;i<(int)ins[now].size();i++){
			int p=ins[now][i].first,c=ins[now][i].second;
			if (p==x)ans+=ds1.ask(depth[now]-c);
			else{
				int qwq=0;
				ans+=ds2.ask(depth[now]+depth[p]-c,p);
				ans+=ds3.ask(depth[now]-c,p);
			}
		}
		for (int i=0;i<(int)ins[now].size();i++){
			int p=ins[now][i].first,c=ins[now][i].second;
			ds1.add(c-depth[p],2);
			if (p==x)ds2.add(c,p,2);
			else ds2.add(c,p,1);
			ds3.add(c-depth[p],p,1);
		}
		for (int i=0;i<(int)e[now].size();i++)dfs(e[now][i]);
		for (int i=0;i<(int)ins[now].size();i++){
			int p=ins[now][i].first,c=ins[now][i].second;
			ds1.add(c-depth[p],-2);
			if (p==x)ds2.add(c,p,-2);
			else ds2.add(c,p,-1);
			ds3.add(c-depth[p],p,-1);
		}
		return;
	}
	ll calc(vector<node> _d){
		maxn=T1::idx;
		ans=0;
		d=_d;
		int cnt=0;
		for (int i=0;i<(int)d.size();i++){
			if (d[i].x==x&&d[i].c==d[i].l)cnt++;
			if (d[i].x!=x)ins[d[i].x].push_back(make_pair(d[i].y,d[i].c));
			if (d[i].y!=x)ins[d[i].y].push_back(make_pair(d[i].x,d[i].c));
		}
		ans=1ll*cnt*(cnt-1);
		dfs(x);
		for (int i=0;i<=2*maxn;i++)ds2.root[i]=ds3.root[i]=0;
		sgt.clear();
		for (int i=0;i<(int)d.size();i++){
			if (d[i].x==x&&d[i].c==d[i].l)cnt++;
			if (d[i].x!=x)ins[d[i].x].clear();
			if (d[i].y!=x)ins[d[i].y].clear();
		}
		return ans;
	}
}
namespace T2{
	int u[400005],v[400005],first[200005],nxt[400005];
	void init(){
		for (int i=2;i<=n;i++){
			scanf("%d",&u[i]);
			v[i]=i;
			nxt[i]=first[u[i]],first[u[i]]=i;
			u[i+n]=v[i],v[i+n]=u[i];
			nxt[i+n]=first[u[i+n]],first[u[i+n]]=i+n;
		}
		return;
	}
	int book[200005];
	int sz[200005],sum,rt;
	void getrt(int now,int fa){
		sz[now]=1;
		int mx=0;
		for (int i=first[now];i;i=nxt[i])
			if (v[i]!=fa&&book[v[i]]==0){
				getrt(v[i],now);
				sz[now]+=sz[v[i]];
				mx=max(mx,sz[v[i]]);
			}
		mx=max(mx,sum-sz[now]);
		if (2*mx<=sum)rt=now;
		return;
	}
	int tot,c[200005];
	void getid(int now,int fa){
		c[++tot]=now;
		for (int i=first[now];i;i=nxt[i])
			if (v[i]!=fa&&book[v[i]]==0)getid(v[i],now);
		return;
	}
	node d[200005];
	void getd(int now,int fa,node s){
		d[now]=s;
		for (int i=first[now];i;i=nxt[i])
			if (v[i]!=fa&&book[v[i]]==0)getd(v[i],now,add(s,v[i]));
		return;
	}
	void work(int now){
		x=now;
		book[now]=1;
		tot=0;
		getid(now,0);
		vector<int> qwq;
		for (int i=1;i<=tot;i++)qwq.push_back(c[i]);
		T1::build(now,qwq);
		getd(now,0,{now,now,0,0});
		vector<node> awa;
		for (int i=2;i<=tot;i++)
			if (d[c[i]].x!=-1&&d[c[i]].y!=-1&&T1::book[d[c[i]].x]==1&&T1::book[d[c[i]].y]==1)
				awa.push_back(d[c[i]]);
		ans+=2;
		for (int i=0;i<awa.size();i++)
			if (awa[i].l==awa[i].c)ans+=2;
		ans+=QwQ::calc(awa);
		for (int i=first[now];i;i=nxt[i]){
			if (book[v[i]]==1)continue;
			tot=0;
			getid(v[i],0);
			awa.clear();
			for (int j=1;j<=tot;j++)
				if (d[c[j]].x!=-1&&d[c[j]].y!=-1&&T1::book[d[c[j]].x]==1&&T1::book[d[c[j]].y]==1)
					awa.push_back(d[c[j]]);
			ans-=QwQ::calc(awa);
		}
		T1::clear();
		getrt(now,0);
		for (int i=first[now];i;i=nxt[i]){
			if (book[v[i]]==1)continue;
			sum=sz[v[i]],rt=0;
			getrt(v[i],now);
			work(rt);
		}
		return;
	}
}
int main(){
	cin>>n;
	T1::init();
	T2::init();
	T2::work(1);
	cout<<ans/2<<endl;
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 10
Accepted

Test #1:

score: 10
Accepted
time: 58ms
memory: 248616kb

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: 54ms
memory: 248620kb

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: 58ms
memory: 248764kb

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: 58ms
memory: 248604kb

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: 43ms
memory: 248696kb

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: 2502ms
memory: 324556kb

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: 3486ms
memory: 345828kb

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: 4432ms
memory: 352952kb

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: 3798ms
memory: 344772kb

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: 4410ms
memory: 352040kb

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: 5092ms
memory: 349396kb

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: 2262ms
memory: 303420kb

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: 2782ms
memory: 308464kb

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: 3099ms
memory: 307076kb

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: 3234ms
memory: 310632kb

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: 4039ms
memory: 311332kb

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: 0
Time Limit Exceeded

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Dependency #3:

100%
Accepted

Test #17:

score: 40
Accepted
time: 2159ms
memory: 295436kb

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: 2971ms
memory: 297500kb

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: -40
Time Limit Exceeded

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:


result: