QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#199082#5148. Tree DistanceqzezTL 0ms23588kbC++142.3kb2023-10-03 20:54:322023-10-04 17:11:54

Judging History

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

  • [2023-10-04 17:11:54]
  • 管理员手动重测该提交记录
  • 测评结果:TL
  • 用时:0ms
  • 内存:23588kb
  • [2023-10-03 20:54:32]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
using ll=long long;
template<typename T>
ostream& operator << (ostream &out,const vector<T>&x){
	if(x.empty())return out<<"[]";
	out<<'['<<x[0];
	for(int len=x.size(),i=1;i<len;i++)out<<','<<x[i];
	return out<<']';
}
template<typename T>
vector<T> ary(const T *a,int l,int r){
	return vector<T>{a+l,a+1+r};
}
template<typename T>
void debug(T x){
	cerr<<x<<'\n';
}
template<typename T,typename ...S>
void debug(T x,S ...y){
	cerr<<x<<' ',debug(y...);
}
const int N=2e5+10,M=1e6+10;
const ll INF=1e18;
int n,m;
vector<pair<int,int> >to[N];
#define v e.first
#define w e.second
int cnt,rt,siz[N],mx[N],vis[N];
void dfs1(int u,int fa=0){
	cnt++;
	for(auto e:to[u])if(v^fa&&!vis[v]){
		dfs1(v,u);
	}
}
void dfs2(int u,int fa=0){
	siz[u]=1,mx[u]=0;
	for(auto e:to[u])if(v^fa&&!vis[v]){
		dfs2(v,u);
		siz[u]+=siz[v];
		mx[u]=max(mx[u],siz[v]);
	}
	mx[u]=max(mx[u],m-siz[u]);
	if(mx[u]<mx[rt])rt=u;
}
vector<pair<int,ll> >d;
void dfs3(int u,int fa=0,ll dis=0){
	d.push_back({u,dis});
	for(auto e:to[u])if(v^fa)dfs3(v,u,dis+w);
}
int top,stk[N];
vector<pair<int,ll> >o[N];
vector<pair<int,int> >q[N];
ll ans[M];
void update(int x,int y,ll z){
	if(x>y)swap(x,y);
	o[x].push_back({y,z});
}
void insert(){
	top=0;
	for(int len=d.size(),i=0;i<len;i++){
		for(;top;top--){
			update(d[stk[top]].first,d[i].first,d[stk[top]].second+d[i].second);
			if(d[stk[top]].second<=d[i].second)break;
		}
		stk[++top]=i;
	}
}
void solve(int u){
	cnt=rt=0,dfs1(u),dfs2(u),vis[u=rt]=1;
	d.clear(),dfs3(u);
	sort(d.begin(),d.end());
	insert();
	reverse(d.begin(),d.end());
	insert();
	for(auto e:to[u])if(!vis[v])solve(v);
}
#undef v
#undef w
ll c[N];
void add(int x,ll y){
	for(;x<=n&&c[x]>y;x+=x&-x)c[x]=y;
}
ll get(int x,ll y=INF){
	for(;x;x^=x&-x)y=min(y,c[x]);
	return y;
}
int main(){
	scanf("%d",&n);
	for(int u,v,w,i=1;i<n;i++){
		scanf("%d%d%d",&u,&v,&w);
		to[u].push_back({v,w}),to[v].push_back({u,w});
	}
	mx[0]=n+1,solve(1);
	scanf("%d",&m);
	for(int i=1,l,r;i<=m;i++){
		scanf("%d%d",&l,&r);
		q[l].push_back({r,i});
	}
	fill(c+1,c+1+n,INF);
	for(int i=n;i>=1;i--){
		for(auto ot:o[i])add(ot.first,ot.second);
		for(auto qt:q[i])ans[qt.second]=get(qt.first);
	}
	for(int i=1;i<=m;i++)printf("%lld\n",ans[i]<INF?ans[i]:-1);
	return 0;
}

详细

Test #1:

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

input:

5
1 2 5
1 3 3
1 4 4
3 5 2
5
1 1
1 4
2 4
3 4
2 5

output:

-1
3
7
7
2

result:

ok 5 number(s): "-1 3 7 7 2"

Test #2:

score: -100
Time Limit Exceeded

input:

199999
31581 23211 322548833
176307 196803 690953895
34430 82902 340232856
36716 77480 466375266
7512 88480 197594480
95680 61864 679567992
19572 14126 599247796
188006 110716 817477802
160165 184035 722372640
23173 188594 490365246
54801 56250 304741654
10103 45884 643490340
127469 154479 214399361...

output:


result: