QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#412669#6737. NeighbourhoodThankto_zy6TL 5601ms67380kbC++145.5kb2024-05-16 17:22:432024-05-16 17:22:43

Judging History

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

  • [2024-05-16 17:22:43]
  • 评测
  • 测评结果:TL
  • 用时:5601ms
  • 内存:67380kb
  • [2024-05-16 17:22:43]
  • 提交

answer

#include<bits/stdc++.h> 
using namespace std;
typedef long long ii;
int he[200005],to[400005],ne[400005],id[400005],cnt;
ii w[200005];
void link(int u,int v,int w){
	cnt++;
	to[cnt]=v;
	ne[cnt]=he[u];
	id[cnt]=w;
	he[u]=cnt;
}
int B;
bool bdr[200005];
int bel[200005],bee[200005],stk[200005],_top[200005],top;
int cl[200005],clutop;
int up[200005],down[200005];
struct nodd{
	int id;
	ii v;
};
bool operator<(nodd x,nodd y){
	return x.v<y.v;
}
vector<nodd>dis[200005][2];int clu;
int ef[200005],fa[200005],wait[200005],low[200005];
ii dud[200005];
int tag[200005];
void puttag(int x,int pr,int k,int tg){
	tag[x]=tg;
	for(int i=he[x];i;i=ne[i]){
		if(to[i]!=pr&&bel[to[i]]==k){
			puttag(to[i],x,k,tg);
		}
	}
}
void getdud(int i){
	int x=down[i];dud[i]=0;
	if(!x)return;
	while(x!=up[i]){
		dud[i]+=w[ef[x]];
		x=fa[x];
	}
}
int get(int x,int i){
	return (x==up[i])?0:1;
}
struct node{
	int v,i;
};
vector<node>eg[200005];
void add_clu(int u,int v){
	clu++;
	bdr[u]=1;up[clu]=u;
	if(!v)v=cl[clutop];
	eg[u].push_back({v,clu});
	down[clu]=v;bdr[v]=1;
	eg[v].push_back({u,clu});
	clutop=0;
}
void dfs(int x){
	wait[x]=1;int tot=0;_top[x]=top;
	for(int i=he[x];i;i=ne[i]){
		if(to[i]==fa[x])continue;
		ef[to[i]]=id[i];fa[to[i]]=x;stk[++top]=to[i];
		dfs(to[i]);
		wait[x]+=wait[to[i]];
		if(low[to[i]])low[x]=low[to[i]],tot++;
	}
	if(tot>1||wait[x]>=B||!fa[x]){
		wait[x]=0;
		low[x]=x;
		for(int i=he[x],cur=0,j=_top[x]+1,wt=0;;i=ne[i]){
			if(to[i]==fa[x]&&fa[x])continue;
			int v=to[i];
			if(wt+wait[v]>B||!v||(cur&&low[v])){
				for(;(j<_top[v]||!v)&&j<=top;j++){
					cl[++clutop]=stk[j];
					if(!bel[stk[j]])bel[stk[j]]=clu+1;
					if(stk[j]!=x){
						if(!bee[ef[stk[j]]])bee[ef[stk[j]]]=clu+1;
					}
				}
				add_clu(x,cur);
				wt=cur=0;
			}
			wt+=wait[v];
            if(low[v])cur=low[v];
			if(!i)break;
		}
	}
}
void sear(int x,int pr,int k,int bz,ii ds){
	if(!bdr[x])dis[k][bz].push_back({x,ds});
	for(int i=he[x];i;i=ne[i]){
		if(to[i]!=pr&&(bel[to[i]]==k||to[i]==up[k]||to[i]==down[k])){
			sear(to[i],x,k,bz,ds+w[id[i]]);
		}
	}
}
void pre(int x,int i){
	if(!x)return;
	int bz=get(x,i);
	vector<nodd>().swap(dis[i][bz]);
	sear(x,0,i,bz,0);
	sort(dis[i][bz].begin(),dis[i][bz].end());
}
int flg[200005];
void sear2(int x,int pr,int k,int tg,int sw){
	flg[x]=sw;
	for(int i=he[x];i;i=ne[i]){
		if(to[i]!=pr&&(bel[to[i]]==k||to[i]==up[k]||to[i]==down[k])){
			if(id[i]==tg){
				sear2(to[i],x,k,tg,1);
			}
			else{
				sear2(to[i],x,k,tg,sw);
			}
		}
	}
}
nodd ttmp1[200005],ttmp2[200005];int top1,top2;
void pre2(int x,int i,int tg,ii delta){
	if(!x)return;top1=top2=0;
	int bz=get(x,i);
	sear2(x,0,i,tg,0);
	for(nodd v:dis[i][bz]){
		if(flg[v.id])ttmp2[++top2]={v.id,v.v+delta};
		else ttmp1[++top1]=v;
	}
	vector<nodd>().swap(dis[i][bz]);
	int j=1,k=1;
	while(j<=top1&&k<=top2){
		if(ttmp1[j].v<ttmp2[k].v){
			dis[i][bz].push_back(ttmp1[j]);
			j++;
		}
		else{
			dis[i][bz].push_back(ttmp2[k]);
			k++;
		}
	}
	while(j<=top1){
		dis[i][bz].push_back(ttmp1[j]);
		j++;
	}
	while(k<=top2){
		dis[i][bz].push_back(ttmp2[k]);
		k++;
	}
}
int qry_clu(int x,int i,ii k){
	if(!x)return 0;
	int bz=get(x,i);
	if(dis[i][bz].empty())return 0;
	if((dis[i][bz].back()).v<=k){
		return dis[i][bz].size();
	}
	int l=1,r=dis[i][bz].size(),mid;
	while(l<r){
		mid=(l+r)>>1;
		if(dis[i][bz][mid-1].v<=k)l=mid+1;
		else r=mid;
	}
//	cerr<<"!"<<x<<" "<<i<<" "<<k<<" "<<l-1<<endl;
	return l-1;
}
int res;
ii d1,d2;
void qry1(int x,int y,int pr,ii ds,ii k){
	if(x==up[y])d1=ds;
	if(x==down[y])d2=ds;
//	if(bdr[x])return;
	if(ds<=k&&!bdr[x])res++;
	for(int i=he[x];i;i=ne[i]){
		if(to[i]!=pr&&(bel[to[i]]==y||to[i]==up[y]||to[i]==down[y])){
			qry1(to[i],y,x,ds+w[id[i]],k);
		}
	}
}
int qry2(int x,int pr,ii k){
	int ret=1;
	if(k<0)return 0;
	for(node v:eg[x]){
		if(v.v!=pr){
			ret+=qry_clu(x,v.i,k);
			if(k>=dud[v.i]&&v.v){
				ret+=qry2(v.v,x,k-dud[v.i]);
			}
		}
	}
	return ret;
}
int main(){
	int n,q;
	scanf("%d%d",&n,&q);B=500;
	for(int i=1;i<n;i++){
		int u,v;
		scanf("%d%d%lld",&u,&v,&w[i]);
		link(u,v,i);
		link(v,u,i);
	}
	dfs(1);//cerr<<"**"<<clu<<endl;
	for(int i=1;i<=clu;i++){
		pre(up[i],i);
		if(down[i])pre(down[i],i);
		int x=down[i];dud[i]=0;
		if(!x)continue;
		int o=0;
		while(x!=up[i]){
			dud[i]+=w[ef[x]];
			for(int j=he[x];j;j=ne[j]){
				if(bel[to[j]]==i&&to[j]!=o&&to[j]!=fa[x]){
					puttag(to[j],x,i,x);
				}
				if(!bdr[x])tag[x]=x;
			}
			o=x;
			x=fa[x];
		}
		for(int j=he[up[x]];j;j=ne[j]){
			if(bel[to[j]]==i&&to[j]!=o){
				puttag(to[j],up[x],i,up[x]);
			}
		}
	}
	while(q--){
		int opt;
		scanf("%d",&opt);
		if(opt==1){
			int i;ii c,d;
			scanf("%d%lld",&i,&c);d=c-w[i];
			w[i]=c;
			int k=bee[i];
			pre2(up[k],k,i,d);
			if(down[k])pre2(down[k],k,i,d);
			getdud(k);
		}
		else{
			int x;ii d;
			scanf("%d%lld",&x,&d);
			res=0;
			if(!bdr[x]){
				if(tag[x]!=up[bel[x]]&&tag[x]!=down[bel[x]]){
					qry1(x,bel[x],0,0,d);
					res+=qry2(up[bel[x]],down[bel[x]],d-d1);
					res+=qry2(down[bel[x]],up[bel[x]],d-d2);
				}
				else{
					if(tag[x]==up[bel[x]]){
						qry1(x,bel[x],0,0,d);
						res+=qry2(up[bel[x]],0,d-d1);
						res-=qry_clu(up[bel[x]],bel[x],d-d1);
					}
					if(tag[x]==down[bel[x]]){
						qry1(x,bel[x],0,0,d);
						res+=qry2(down[bel[x]],0,d-d2);
						res-=qry_clu(down[bel[x]],bel[x],d-d2);
					}
				} 
			}
			else{
				res=qry2(x,-1,d);
			}
			printf("%d\n",res);
		}
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 34424kb

input:

3 7
1 2 3
2 3 1
2 2 1
2 1 3
2 3 4
1 1 1
2 2 1
2 1 0
2 3 1

output:

2
2
3
3
1
2

result:

ok 6 numbers

Test #2:

score: 0
Accepted
time: 2856ms
memory: 66100kb

input:

200000 200000
1 2 146181238
2 3 45037818
3 4 176924060
4 5 969365276
5 6 683948619
6 7 356268194
7 8 871634797
8 9 630254480
9 10 283061123
10 11 204904965
11 12 838381393
12 13 516373812
13 14 253862710
14 15 223572474
15 16 114452997
16 17 145251056
17 18 905638436
18 19 375445402
19 20 549829545
...

output:

219
62303
1358
5532
65345
682
11856
120285
4980
5689
2998
2314
18102
8014
20512
2827
113022
74534
159775
14517
17961
21855
8138
265
3336
3251
7023
35187
4932
151611
14338
101
899
117
64441
888
10380
1833
29381
1014
4806
10770
23734
236
37258
2280
14550
2196
38205
80950
80839
4517
74570
13972
95914
7...

result:

ok 99572 numbers

Test #3:

score: 0
Accepted
time: 1976ms
memory: 66044kb

input:

200000 200000
1 2 656062236
2 3 261234277
3 4 723980474
4 5 755233101
5 6 542749909
6 7 9519713
7 8 878661582
8 9 402365775
9 10 9875710
10 11 894863180
11 12 4247155
12 13 287336772
13 14 76881950
14 15 132946165
15 16 174895351
16 17 93681141
17 18 504958771
18 19 372774409
19 20 406716610
20 21 2...

output:

60189
5717
13802
2030
71980
20394
75205
11241
1388
104056
11760
588
4319
49744
8187
29084
507
39561
2286
3138
42602
77393
15811
5709
15417
48109
9596
846
16875
27181
52
11525
5741
2476
34614
4877
24002
85119
171
246
19408
89
806
42261
25552
865
158
70
3444
25736
60977
4454
11905
126842
35189
3858
15...

result:

ok 180067 numbers

Test #4:

score: 0
Accepted
time: 3574ms
memory: 67380kb

input:

200000 200000
1 2 111377160
2 3 892685763
3 4 889507841
4 5 901236774
5 6 834278466
6 7 298553149
7 8 8150218
8 9 390360541
9 10 621635057
10 11 699702261
11 12 606252166
12 13 657657482
13 14 274827098
14 15 206330963
15 16 546124412
16 17 888769586
17 18 921510546
18 19 117645637
19 20 919234429
2...

output:

171
20066
102752
6642
68416
1708
644
62536
7246
7441
3463
13905
613
38743
54559
8959
4615
103272
40246
26304
149
44249
212
123
34390
82218
521
85028
17557
1014
17048
2759
3173
5518
3043
1266
22438
83793
218
27248
24662
134581
9800
16393
39893
727
13548
3
2669
26080
573
411
9172
3642
2111
19478
563
4...

result:

ok 20072 numbers

Test #5:

score: 0
Accepted
time: 5601ms
memory: 47448kb

input:

200000 200000
1 2 657504560
1 3 539914646
2 4 617534421
2 5 145385645
4 6 11287875
4 7 472341345
1 8 114534025
4 9 940656797
5 10 806152132
3 11 623707008
2 12 841303670
10 13 804880741
1 14 35219013
12 15 924229426
2 16 834808041
5 17 118952529
1 18 561626985
9 19 884416843
10 20 604788348
18 21 11...

output:

605
131
199697
199999
34
3
36514
256
21
2
261
181731
199877
199991
3229
6
122767
4374
200000
53327
40
4
7858
175711
200000
132026
169165
72
112
120267
171500
1
95691
36697
199851
200000
6940
662
200000
1
1168
2
200000
19
16
211
9
11
196444
85
42751
200000
11
200000
115222
28279
177665
691
127
197292...

result:

ok 100112 numbers

Test #6:

score: -100
Time Limit Exceeded

input:

200000 200000
1 2 525193329
1 3 693042313
1 4 194485999
1 5 319160289
1 6 925926764
1 7 592828271
1 8 262594959
1 9 60806042
1 10 704487773
1 11 963570483
1 12 71530643
1 13 66914391
1 14 933028716
1 15 678371635
1 16 714555824
1 17 507124524
1 18 160041085
1 19 769379790
1 20 938594863
1 21 2405638...

output:

85288
1
28130
1
170364
1
5907
1
71883
1
184882
1
1
1
70569
200000
128205
44921
20408
1
82605
17646
1
1
1
1
22715
1
145387
1
161039
175478
1
200000
105070
1
19679
134568
200000
29028
72699
79367
1
4346
111703
200000
200000
200000
1
1
19607
49417
109205
200000
77104
200000
200000
1
1
1
200000
28475
74...

result: