QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#599452#5148. Tree DistancemaxrgbyTL 1ms3536kbC++203.7kb2024-09-29 04:57:522024-09-29 04:57:52

Judging History

This is the latest submission verdict.

  • [2024-09-29 04:57:52]
  • Judged
  • Verdict: TL
  • Time: 1ms
  • Memory: 3536kb
  • [2024-09-29 04:57:52]
  • Submitted

answer

// Source: https://usaco.guide/general/io

#include <bits/stdc++.h>
using namespace std;

vector<pair<int,long long>> adj[200005];
bool vis[200005];
int sz[200005];
vector<pair<int,long long>> dist[200005];

void dfs(int u, int p, long long d, int rt){
	dist[rt].push_back({u,d});
	for(auto [v,w] : adj[u]){
		if(v != p and !vis[v]){
			dfs(v,u,d+w,rt);
		}
	}
}

int dfs2(int u, int p){
	sz[u] = 1;
	for(auto [v,w] : adj[u]){
		if(v != p and !vis[v]){
			sz[u] += dfs2(v,u);
		}
	}
	return sz[u];
}

int centroid(int u, int p, int rootsz){
	for(auto [v,w] : adj[u]){
		if(v != p and !vis[p]){
			if(sz[v]*2 > rootsz){
				return centroid(v,u,rootsz);
			}
		}
	}
	return u;
}

void util(int u, int p){
	vis[u] = true;
	dfs2(u,u);
	int c = centroid(u,u,sz[u]);
	dfs(c,c,0,c);
	vis[c] = true;
	for(auto [v,w] : adj[c]){
		if(v != p and !vis[v]){
			util(v,c);
		}
	}
}

long long seg[800005];

void upd(int v, int l, int r, int pos, long long val){
	if(pos < l or pos > r){
		return ;
	}
	if(l == r){
		seg[v] = min(seg[v],val);
	}else{
		int mid = (l+r)/2;
		upd(v*2,l,mid,pos,val);
		upd(v*2+1,mid+1,r,pos,val);
		seg[v] = min(seg[v*2],seg[v*2+1]);
	}
}

long long query(int v, int l, int r, int ql, int qr){
	if(l > r or r < ql or l > qr){
		return 1e15;
	}
	if(l == ql and r == qr){
		//cout << "l=" << l << "r=" << r << "ql=" << ql << "qr=" << qr << endl;
		//cout << "RETURNING" << seg[v] << endl;
		return seg[v];
	}
	int mid = (l+r)/2;
	long long tmp1 = query(v*2,l,mid,ql,min(mid,qr));
	long long tmp2 = query(v*2+1,mid+1,r,max(ql,mid+1),qr);
	//cout << "l=" << l << "r=" << r << "ql=" << ql << "qr=" << qr << endl;
	//cout << "tmp1=" << tmp1 << "tmp2=" << tmp2 << endl;
	return min(tmp1,tmp2);
}

int main() {
	int n,q;
	cin >> n;
	for(int i = 1;i <= 4*n;i++){
		seg[i] = 1e15;
	}
	for(int i = 0;i < n-1;i++){
		long long a,b,c;
		cin >> a >> b >> c;
		adj[a].push_back({b,c});
		adj[b].push_back({a,c});
	}
	util(1,1);
	vector<array<long long,3>> bounds;
	for(int i = 1;i <= n;i++){
		sort(dist[i].begin(),dist[i].end());
		stack<pair<int,long long>> stk;
		for(auto [a,b] : dist[i]){
			while(!stk.empty() and stk.top().second > b){
				stk.pop();
			}
			if(!stk.empty()){
				bounds.push_back({stk.top().first,a,stk.top().second+b});
			}
			stk.push({a,b});
		}
		while(!stk.empty()){
			stk.pop();
		}
		reverse(dist[i].begin(),dist[i].end());
		for(auto [a,b] : dist[i]){
			while(!stk.empty() and stk.top().second > b){
				stk.pop();
			}
			if(!stk.empty()){
				bounds.push_back({a,stk.top().first,stk.top().second+b});
			}
			stk.push({a,b});
		}
	}
	sort(bounds.begin(),bounds.end());
	/*for(auto [a,b,c] : bounds){
		cout << a << " " << b << " " << c << endl;
	}*/
	cin >> q;
	array<int,3> qry[q];
	long long ans[q];
	for(int i = 0;i < q;i++){
		cin >> qry[i][0] >> qry[i][1];
		qry[i][2] = i;
	}
	sort(bounds.begin(),bounds.end());
	sort(qry,qry+q);
	int ind1 = q-1;
	int ind2 = bounds.size()-1;
	for(int i = n;i >= 1;i--){
		//cout << "i=" << i << endl;
		while(ind2 >= 0 and bounds[ind2][0] == i){
			//cout << "ind2=" << ind2 << " " << bounds[ind2][0] << " " << bounds[ind2][1] << " " << bounds[ind2][2] << endl;
			int tmp1 = bounds[ind2][1];
			long long tmp2 = query(1,1,n,tmp1,tmp1);
			//cout << "tmp=" << tmp1 << "tmp2=" << tmp2 << endl;
			upd(1,1,n,tmp1,bounds[ind2][2]); 
			ind2--;
		}
		//cout << endl;
		while(ind1 >= 0 and qry[ind1][0] == i){
			ans[qry[ind1][2]] = query(1,1,n,i,qry[ind1][1]);
			ind1--;
		}
		/*for(int j = 1;j <= n;j++){
			cout << query(1,1,n,j,j) << " ";
		}
		cout << endl;*/
	}
	for(int i = 0;i < q;i++){
		if(ans[i] >= 1e15){
			cout << -1 << endl;
			continue;
		}
		cout << ans[i] << endl;
	}
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3536kb

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: