QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#564612#5148. Tree DistancefryanWA 1570ms289776kbC++173.5kb2024-09-15 11:22:422024-09-15 11:22:43

Judging History

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

  • [2024-09-15 11:22:43]
  • 评测
  • 测评结果:WA
  • 用时:1570ms
  • 内存:289776kb
  • [2024-09-15 11:22:42]
  • 提交

answer

#include "bits/stdc++.h"
using namespace std;
#define int long long
#define all(x) begin(x), end(x)
#define sz(x) (int) (x).size()

const int mxn = 2e5+5;

int n;
vector<array<int,2>> adj[mxn];

//centroid
int vis[mxn], sub[mxn];
vector<array<int,2>> dist[mxn];

void dfs_dist(int v, int p, int dt, int o) {
	if (vis[v]) return;
	dist[o].push_back({v,dt});
	for (auto [u,d] : adj[v]) {
		if (u==p) continue;
		dfs_dist(u,v,dt+d,o);
	}
}
int dfs_sub(int v, int p) {
	if (vis[v]) return 0;
	sub[v] = 1;
	for (auto [u,d] : adj[v]) {
		if (u==p) continue;
		sub[v] += dfs_sub(u, v);
	}
	return sub[v];
}
int centroid(int v, int p, int sz) {
	for (auto [u,d] : adj[v]) {
		if (vis[u] || u==p) continue;
		if (sub[u]*2 > sz) return centroid(u, v, sz);
	}
	return v;
}
void build_centroid(int v) {
	int c = centroid(v,v,dfs_sub(v,v));
	dfs_dist(c, c, 0, c);
	vis[c] = 1;
	for (auto [u,d] : adj[c]) {
		if (vis[u]) continue;
		build_centroid(u);
	}
}

struct RMQ {
	int INF = 1e16;
	int len = 1;
	vector<int> segmx;
	RMQ() {}
	RMQ(int l) {
		while (len < l) len *= 2;
		segmx = vector<int>(2*len,INF);
	}
	RMQ(vector<int> arr) {
		while (len < sz(arr)) len *= 2;
		segmx = vector<int>(2*len,INF);
		for (int i = 0; i < sz(arr); i++) {segmx[len+i] = arr[i];}
		for (int i = len-1; i > 0; i--) {
			segmx[i] = min(segmx[2*i], segmx[2*i+1]);
		}
	}
	void set(int ind, int val) {
		assert(0 <= ind && ind < len);
		ind += len; segmx[ind] = val;
		while (ind /= 2) {segmx[ind] = min(segmx[2*ind], segmx[2*ind+1]);}
	}
	int rangemin(int l, int r) {
		int res = INF; r++;
		for (l += len, r += len; l < r; l /= 2, r /= 2) {
			if (l&1) res = min(res, segmx[l++]);
		    if (r&1) res = min(res, segmx[--r]);
		}
		return res;
	}
};

vector<array<int,3>> rg;
int q;
array<int,3> qry[5*mxn];
RMQ rr;
int ans[mxn];

signed main() {
	ios::sync_with_stdio(false); cin.tie(nullptr);
		
	cin>>n;
	for (int i=1; i<n; i++) {
		int u,v,w; cin>>u>>v>>w; --u; --v;
		adj[u].push_back({v,w});
		adj[v].push_back({u,w});
	}
	
	build_centroid(0);
	
	for (int X=0; X<n; X++) {
		sort(all(dist[X]));
		stack<array<int,2>> mono;
		for (int i=0; i<sz(dist[X]); i++) {
			while (sz(mono) && mono.top()[1] > dist[X][i][1]) {mono.pop();}
			if (sz(mono)) {
				rg.push_back({mono.top()[0],dist[X][i][0],mono.top()[1]+dist[X][i][1]});
			}
			mono.push(dist[X][i]);
		}
		while (sz(mono)) mono.pop();
		for (int i=sz(dist[X])-1; i>=0; i--) {
			while (sz(mono) && mono.top()[1] > dist[X][i][1]) {mono.pop();}
			if (sz(mono)) {
				rg.push_back({dist[X][i][0],mono.top()[0],mono.top()[1]+dist[X][i][1]});
			}
			mono.push(dist[X][i]);
		}
	}
		
	rr = RMQ(n);
	sort(all(rg)); reverse(all(rg));
	cin>>q;
	for (int i=0; i<q; i++) {
		cin>>qry[i][0]>>qry[i][1]; --qry[i][0], --qry[i][1];
		qry[i][2] = i;
	}
	sort(qry,qry+q);
	reverse(qry,qry+q);
	for (int i=0; i<q; i++) {
		assert(qry[i][0] < 2e5 && qry[i][1] < 2e5);
	}
	int rp = 0, qp = 0;
	for (int i=n-1; i>=0; i--) {
		while (rp < sz(rg) && rg[rp][0]==i) {
			int pp = rg[rp][1];
			int vv = rr.rangemin(pp,pp);
			rr.set(pp,min(vv,rg[rp][2]));
			rp++;
		}
		while (qp < q && qry[qp][0]==i) {
			ans[qry[qp][2]] = rr.rangemin(i,qry[qp][1]);
			qp++;
		}
		if (qp < q && qry[qp][0]>n) {
			cout<<qry[qp][0]<<endl;
		}
	}
	if (qp != q) {
		cout<<qp<<"\n";
		cout<<qry[qp][0]<<" "<<qry[qp][1]<<endl;
		return 0;
	}
	for (int i=0; i<q; i++) {
		cout<<(ans[i]>1e15?-1:ans[i])<<"\n";
	}
	
	return 0;
}

詳細信息

Test #1:

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

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
Wrong Answer
time: 1570ms
memory: 289776kb

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:

20346704860
20346704860
20346704860
20346704860
20346704860
20346704860
20346704860
20346704860
20346704860
20346704860
20346704860
20346704860
20346704860
20346704860
20346704860
20346704860
20346704860
20346704860
20346704860
20346704860
20346704860
20346704860
20346704860
20346704860
20346704860
...

result:

wrong answer 1st numbers differ - expected: '29573323', found: '20346704860'