UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#189402#3323. 动态树(tree)Alan_Zhaoyz1005822ms106464kbC++113.8kb2023-10-04 10:57:102023-10-04 13:01:09

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;
const int N=4.5e5+5;
int n,q,fa[N],n_,poi[N];
ll a[N];
vector<int> e[N];
struct Rect{
	int xl,xr,yl,yr;
	bool include(const Rect &x) const{
		return xl<=x.xl&&x.xr<=xr&&yl<=x.yl&&x.yr<=yr;
	}
	bool out_of(const Rect &x) const{
		return xl>x.xr||xr<x.xl||yl>x.yr||yr<x.yl;
	}
};
struct Point{
	int x,y,i;
	ll v;
}ins[N];
struct KDT{
	struct Node{
		Rect p;
		int cnt;
		ll add,sum;
	}t[N*4];
	void apply(int p,ll x){
		t[p].add+=x;
		t[p].sum+=t[p].cnt*x;
	}
	void push(int p){
		if(t[p].add){
			apply(p*2,t[p].add);
			apply(p*2+1,t[p].add);
			t[p].add=0;
		}
	}
	void pull(int p){
		t[p].sum=t[p*2].sum+t[p*2+1].sum;
	}
	void build(int p,int l,int r,bool axis){
		t[p].add=0;
		if(l==r){
			t[p].p.xl=t[p].p.xr=ins[l].x;
			t[p].p.yl=t[p].p.yr=ins[l].y;
			t[p].cnt=1;
			t[p].sum=ins[l].v;
			poi[ins[l].i]=p;
			return;
		}
		int mid=(l+r)/2;
		nth_element(ins+l,ins+mid,ins+r+1,[axis](const Point& p1,const Point& p2){
			return axis?p1.y<p2.y:p1.x<p2.x;
		});
		build(p*2,l,mid,!axis);
		build(p*2+1,mid+1,r,!axis);
		t[p].p.xl=min(t[p*2].p.xl,t[p*2+1].p.xl);
		t[p].p.xr=max(t[p*2].p.xr,t[p*2+1].p.xr);
		t[p].p.yl=min(t[p*2].p.yl,t[p*2+1].p.yl);
		t[p].p.yr=max(t[p*2].p.yr,t[p*2+1].p.yr);
		t[p].cnt=t[p*2].cnt+t[p*2+1].cnt;
		pull(p);
	}
	void range_add(int p,const Rect& x,int v){
		if(x.out_of(t[p].p)){
			return;
		}
		if(x.include(t[p].p)){
			apply(p,v);
			return;
		}
		push(p);
		range_add(p*2,x,v);
		range_add(p*2+1,x,v);
		pull(p);
	}
	ll query(int p,const Rect& x){
		if(x.out_of(t[p].p)){
			return 0;
		}
		if(x.include(t[p].p)){
			return t[p].sum;
		}
		push(p);
		return query(p*2,x)+query(p*2+1,x);
	}
	void iterate(int p,int l,int r){
		if(l>=r) return;
		push(p);
		int mid=(l+r)/2;
		iterate(p*2,l,mid);
		iterate(p*2+1,mid+1,r);
	}
}kdt;
int bel[N],dep[N],dfn[N],dfx,siz[N];
void dfs(int u){
	dep[u]=dep[fa[u]]+1;
	dfn[u]=++dfx;
	siz[u]=1;
	for(int v:e[u]){
		dfs(v);
		siz[u]+=siz[v];
	}
}
void rebuild(){
	kdt.iterate(1,1,n);
	For(i,1,n) a[i]=kdt.t[poi[i]].sum;
	dfx=0;
	dfs(1);
	n=n_;
	For(i,1,n) ins[i]={dfn[i],dep[i],i,a[i]};
	kdt.build(1,1,n,0);
}
bool subtree(int u,int v){
	return dfn[u]<=dfn[v]&&dfn[v]<dfn[u]+siz[u];
}
void dfs_add(int u,int d,int x){
	if(dep[u]>d) return;
	a[u]+=x;
	for(int v:e[u]){
		dfs_add(v,d,x);
	}
}
ll dfs_query(int u,int d){
	if(dep[u]>d) return 0;
	ll res=a[u];
	for(int v:e[u]){
		res+=dfs_query(v,d);
	}
	return res;
}
int main(){
	cin.tie(nullptr)->sync_with_stdio(false);
	#ifdef zyz
		assert(freopen("D.in","r",stdin));
		assert(freopen("D.out","w",stdout));
	#endif
	cin>>n>>q;
	For(i,1,n) cin>>a[i];
	For(i,2,n){
		cin>>fa[i];
		e[fa[i]].push_back(i);
	}
	n_=n;
	n=0;
	rebuild();
	For(_,1,q){
		int tp;
		cin>>tp;
		if(tp==1){
			int u,d,x;
			cin>>u>>d>>x;
			if(u<=n){
				kdt.range_add(1,{dfn[u],dfn[u]+siz[u]-1,dep[u],dep[u]+d},x);
				For(i,n+1,n_){
					if(dep[i]<=dep[u]+d&&subtree(u,bel[i])){
						a[i]+=x;
					}
				}
			}else{
				dfs_add(u,dep[u]+d,x);
			}
		}else if(tp==2){
			int u,d;
			ll ans;
			cin>>u>>d;
			if(u<=n){
				ans=kdt.query(1,{dfn[u],dfn[u]+siz[u]-1,dep[u],dep[u]+d});
				For(i,n+1,n_){
					if(dep[i]<=dep[u]+d&&subtree(u,bel[i])){
						ans+=a[i];
					}
				}
			}else{
				ans=dfs_query(u,dep[u]+d);
			}
			cout<<ans<<'\n';
		}else{
			++n_;
			cin>>fa[n_]>>a[n_];
			e[fa[n_]].push_back(n_);
			dep[n_]=dep[fa[n_]]+1;
			bel[n_]=(fa[n_]<=n?fa[n_]:bel[fa[n_]]);
			if((n_-n)%5000==0){
				rebuild();
			}
		}
	}
	return 0;
}

详细

小提示:点击横条可展开更详细的信息

Test #1:

score: 10
Accepted
time: 12ms
memory: 12504kb

input:

4000 4000
265 180 534 72 966 964 149 311 865 754 331 695 422 294 537 80 850 434 336 314 383 552 789 ...

output:

253
371
2155
1810
348
2797
894
680
15842
82
195
520
861
804
280
2495
22
950
1211
116
861
10982
430
8...

result:

ok 1330 lines

Test #2:

score: 10
Accepted
time: 13ms
memory: 12508kb

input:

4000 4000
680 405 819 440 335 591 660 31 352 511 579 6 347 160 131 79 320 127 857 544 20 103 368 642...

output:

774
36
1842
2164
406
3453
964
943
16224
464
934
486
895
853
454
2381
622
642
787
896
3
18084
932
22
...

result:

ok 1330 lines

Test #3:

score: 10
Accepted
time: 374ms
memory: 106464kb

input:

400000 50000
712 817 831 95 925 858 806 45 921 156 391 206 902 771 505 513 504 682 835 849 846 593 5...

output:

1165507
91306540
18881487
16669037
13820567
99612401
192736940
22094825
26566240
121068722
35848350
...

result:

ok 16406 lines

Test #4:

score: 10
Accepted
time: 378ms
memory: 106464kb

input:

400000 50000
956 437 99 677 580 797 831 680 650 794 611 577 124 412 438 233 906 574 479 677 664 955 ...

output:

1176993
79365326
12343229
16575508
14502709
77925551
144771384
22018437
26533358
74404781
11430006
2...

result:

ok 16406 lines

Test #5:

score: 10
Accepted
time: 158ms
memory: 88760kb

input:

400000 50000
148 549 678 173 555 391 699 468 942 337 293 320 620 265 750 856 923 375 887 406 837 894...

output:

133274
493647
166184
495955
374193
489949
999
470
203093
460915
997
643
404055
309045
57455
223
1460...

result:

ok 24941 lines

Test #6:

score: 10
Accepted
time: 678ms
memory: 79612kb

input:

400000 50000
749 979 106 435 122 246 114 44 1000 607 181 939 983 318 994 135 787 71 926 517 132 183 ...

output:

2325
1110
346
407
589
377
1785
1611
1537
950
519
798
1413
12
871
202
881
316
9735
3210
294
344
15616...

result:

ok 24777 lines

Test #7:

score: 10
Accepted
time: 178ms
memory: 88760kb

input:

400000 50000
409 661 251 138 627 486 189 672 107 349 448 492 174 757 485 873 12 552 925 927 27 173 2...

output:

126659
487058
179526
493291
380140
478203
375
572
209235
467111
769
45
393447
297998
51006
413
13437...

result:

ok 24941 lines

Test #8:

score: 10
Accepted
time: 1663ms
memory: 80736kb

input:

400000 50000
341 491 508 570 579 697 897 218 677 137 551 446 985 993 141 914 633 107 822 398 103 447...

output:

6843
187
554
515
769
732
3700
366
311
815
1919
615
1374
428
3120
735
963
929
339
4038
1796
2429
108
...

result:

ok 16643 lines

Test #9:

score: 10
Accepted
time: 1594ms
memory: 80740kb

input:

400000 50000
450 593 834 379 486 283 376 7 694 494 550 632 851 769 997 661 229 225 442 585 423 409 5...

output:

7228
349
861
1052
903
67
1906
1043
213
818
3429
303
1433
1234
3398
580
177
47
423
3589
714
2333
470
...

result:

ok 16643 lines

Test #10:

score: 10
Accepted
time: 774ms
memory: 89824kb

input:

400000 50000
504 475 704 255 41 754 434 496 762 908 328 231 770 999 597 25 616 635 984 615 637 71 76...

output:

296
464
97387445
147552618
125606691
147493024
379250849
1089
1688
1073
254358193
525647635
75699895...

result:

ok 16667 lines

Extra Test:

score: 0
Extra Test Passed