QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#577018#5148. Tree DistanceInsert_Username_HereTL 0ms9840kbC++202.6kb2024-09-20 00:15:022024-09-20 00:15:03

Judging History

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

  • [2024-09-20 00:15:03]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:9840kb
  • [2024-09-20 00:15:02]
  • 提交

answer

#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pii;
// const ll mod = 1e9 + 7;
// #include <brawlstars>
// FOR PAIN OR FOR GLORYYY ELLL PRIMOOOOOO

const ll N = 2e5 + 1;
ll root, sz[N], cur, cmin, dis[N], vis[N], num[N], seg[1 << 19 ];
vector<pii> adj[N], bk;
vector<array<ll, 4>> qrys;
ll dcmp(ll i, ll p) {
	sz[i] = 1;
	ll smax = 0;
	for(pii j : adj[i]) {
		if(j.f == p || vis[j.f]) continue;
		sz[i] += dcmp(j.f, i);
		smax = max(smax, sz[j.f]);
	}
	smax = max(smax, cur - sz[i]);
	if(smax < cmin) root = i, cmin = smax;
	return sz[i];
}
void dfs(ll i, ll p) {
	bk.push_back({i, dis[i]});
	for(pii j : adj[i]) {
		if(j.f == p || vis[j.f]) continue;
		dis[j.f] = dis[i] + j.s, dfs(j.f, i);
	}
}
void kms(ll i) {
	bk.clear();
	dis[i] = 0, dfs(i, i);
	sort(bk.begin(), bk.end());
	ll k = 0;
	for(pii j : bk) {
		while(k && dis[j.f] < dis[num[k]]) qrys.push_back({num[k], j.f, 0, dis[j.f] + dis[num[k]]}), k--;
		if(k) qrys.push_back({num[k], j.f, 0, dis[j.f] + dis[num[k]]});
		k++, num[k] = j.f;
	}
	vis[i] = 1;
	ll prv = cur;
	for(pii j : adj[i]) {
		if(vis[j.f]) continue;
		cur = cmin = sz[j.f];
		if(sz[j.f] > sz[i]) cur = cmin = prv - sz[i];
		root = j.f, dcmp(j.f, j.f), kms(j.f);
	}
}
void upd(ll i, ll val, ll i1, ll l1, ll r1) {
	if(l1 > i || r1 < i) return;
	if(l1 == r1) {
		seg[i1] = min(seg[i1], val);
		return;
	}
	ll mid = (l1 + r1) / 2;
	upd(i, val, 2 * i1, l1, mid);
	upd(i, val, 2 * i1 + 1, mid + 1, r1);
	seg[i1] = min(seg[2 * i1], seg[2 * i1 + 1]);
	return;
}
ll qry(ll l, ll r, ll i1, ll l1, ll r1) {
	if(l1 > r || r1 < l) return 1e18;
	if(l <= l1 && r1 <= r) return seg[i1];
	ll mid = (l1 + r1) / 2;
	return min(qry(l, r, 2 * i1, l1, mid), qry(l, r, 2 * i1 + 1, mid + 1, r1));
}
bool comp(const array<ll, 4> &a, const array<ll, 4> &b) {
	if(a[0] == b[0]) {
		if(a[1] == b[1]) return a[2] < b[2];
		return a[1] < b[1];
	}
	return a[0] > b[0];
}

signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	ll n, u, v, w;
	cin >> n;
	for(ll i = 1; i < n; i++) {
		cin >> u >> v >> w, u--, v--;
		adj[u].push_back({v, w});
		adj[v].push_back({u, w});
	}
	root = 0, dfs(0, 0), kms(0);
	ll q, l, r;
	cin >> q;
	for(ll i = 1; i <= q; i++) {
		cin >> l >> r, l--, r--;
		qrys.push_back({l, r, i, 0});
	}
	sort(qrys.begin(), qrys.end(), comp);
	ll ans[q + 1];
	for(ll i = 0; i < (1 << 19); i++) seg[i] = 1e18;
	for(auto i : qrys) {
		if(!i[2]) upd(i[1], i[3], 1, 0, n);
		else ans[i[2]] = qry(0, i[1], 1, 0, n);
	}
	for(ll i = 1; i <= q; i++) cout << (ans[i] == 1e18 ? -1 : ans[i]) << "\n";
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: