QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#410874#6737. NeighbourhoodThankto_zy6RE 0ms16268kbC++143.4kb2024-05-14 16:19:382024-05-14 16:19:39

Judging History

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

  • [2024-05-14 16:19:39]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:16268kb
  • [2024-05-14 16:19:38]
  • 提交

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[1005],down[1005];
vector<ii>dis[1005][2];int clu;
int ef[1005],fa[1005],wait[1005],low[1005];
ii dud[1005];
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;
	eg[u].push_back({v,clu});
	if(v){
		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||low[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[x]>B||!v||(cur&&low[v])){
				for(;(j<_top[v]||!v)&&j<=top;j++){
					cl[++clutop]=stk[j];
					bel[stk[j]]=clu+1;
					if(stk[j]!=x){
						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){
	dis[k][bz].push_back(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<ii>().swap(dis[i][bz]);
	sear(x,0,i,bz,0);
	sort(dis[i][bz].begin(),dis[i][bz].end());
}
int qry_clu(int x,int i,ii k){
	if(!x)return 0;
	int bz=get(x,i);
	if(dis[i][bz].back()<=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]<=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(ds<=k)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=0;
	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=sqrt(n);
	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);
	for(int i=1;i<=clu;i++){
		pre(up[i],i);
		if(down[i])pre(down[i],i);
		getdud(i);
	}
	while(q--){
		int opt;
		scanf("%d",&opt);
		if(opt==1){
			int i;ii c;
			scanf("%d%lld",&i,&c);
			w[i]=c;
			int k=bee[i];
			pre(up[k],k);
			if(down[k])pre(down[k],k);
			getdud(k);
		}
		else{
			int x;ii d;
			scanf("%d%lld",&x,&d);
			res=0;
			if(!bdr[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{
				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: 16268kb

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: -100
Runtime Error

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:


result: