QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#695954#5439. Meet in the MiddleHRcohcTL 0ms0kbC++144.1kb2024-10-31 21:09:192024-10-31 21:09:29

Judging History

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

  • [2024-10-31 21:09:29]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:0kb
  • [2024-10-31 21:09:19]
  • 提交

answer

#include <bits/stdc++.h>
#define N 100005
#define M 500005
#define ll long long
#define pb push_back
#define fst first
#define scd second
using namespace std;
inline int read(){
	char ch = getchar(); int x = 0, f = 1;
	while(!isdigit(ch)){if(ch == '-') f = -1; ch = getchar();}
	while(isdigit(ch)){x = (x << 3) + (x << 1) + (ch ^ 48); ch = getchar();}
	return x * f;
}
int Ti, n, q, pos1, pos2;
int dfn[N], in[N], out[N], id[N], lg2[N << 1], st[20][N << 1];
ll d[N], dis[N], ans[M];
vector<pair<int, int>> a[N], b[N], qry[N];

inline int mn(int x, int y){return (id[x] < id[y])? x : y;}

void dfs(int u, int fa){
	st[0][id[u] = ++pos1] = u;
	for(auto tmp : b[u]){
		int v = tmp.fst; ll w = tmp.scd;
		if(v == fa) continue;
		d[v] = d[u] + w;
		dfs(v, u);
		st[0][++pos1] = u;
	}
}
void dfs2(int u, int fa){
	in[u] = ++pos2, dfn[pos2] = u;
	for(auto tmp : a[u]){
		int v = tmp.fst; ll w = tmp.scd;
		if(v == fa) continue;
		dis[v] = dis[u] + w;
		dfs2(v, u);
	}
	out[u] = pos2;
}

int lca(int u, int v){
	if(u == v) return u;
	if(id[u] > id[v]) swap(u, v);
	int k = lg2[id[v] - id[u] + 1];
	return mn(st[k][id[u]], st[k][id[v] - (1 << k) + 1]);
}
inline ll dist(int u, int v){return d[u] + d[v] - 2ll * d[lca(u, v)];}
inline ll D(int u, int v, ll d1, ll d2){return (u == v)? 0 : dist(u, v) + d1 + d2;}

struct Node{
	int u, v;
	ll d1, d2, tag;
	friend Node operator + (const Node x, const Node y){
		Node res = x;
		if(D(y.u, y.v, y.d1, y.d2) > D(res.u, res.v, res.d1, res.d2)) res = y;
		if(D(x.u, y.u, x.d1, y.d1) > D(res.u, res.v, res.d1, res.d2)) res = {x.u, y.u, x.d1, y.d1};
		if(D(x.u, y.v, x.d1, y.d2) > D(res.u, res.v, res.d1, res.d2)) res = {x.u, y.v, x.d1, y.d2};
		if(D(x.v, y.u, x.d2, y.d1) > D(res.u, res.v, res.d1, res.d2)) res = {x.v, y.u, x.d2, y.d1};
		if(D(x.v, y.v, x.d2, y.d2) > D(res.u, res.v, res.d1, res.d2)) res = {x.v, y.v, x.d2, y.d2};
		return res;
	}
};
struct Segment{
	#define Lc (now << 1)
	#define Rc (now << 1 | 1)
	#define mid ((l + r) >> 1)
	Node t[N << 2];
	void pushup(int now){int k = t[now].tag; t[now] = t[Lc] + t[Rc]; t[now].tag = k;}
	void update(int now, ll k){t[now].d1 += k, t[now].d2 += k, t[now].tag += k;}
	void pushdown(int now){if(t[now].tag) update(Lc, t[now].tag), update(Rc, t[now].tag), t[now].tag = 0;}
	void build(int now = 1, int l = 1, int r = n){
		t[now].tag = 0;
		if(l == r){t[now] = {dfn[l], dfn[l], dis[dfn[l]], dis[dfn[l]]}; return;}
		build(Lc, l, mid), build(Rc, mid + 1, r); pushup(now);
	}
	void modify(int Lt, int Rt, ll k, int now = 1, int l = 1, int r = n){
		if(Lt > r || Rt < l) return;
		if(Lt <= l && r <= Rt){update(now, k); return;}
		pushdown(now); modify(Lt, Rt, k, Lc, l, mid), modify(Lt, Rt, k, Rc, mid + 1, r); pushup(now);
	}
}T;

void dfs3(int u, int fa){
	Node cur = T.t[1];
	for(auto tmp : qry[u]) ans[tmp.scd] = max(D(cur.u, tmp.fst, cur.d1, 0), D(cur.v, tmp.fst, cur.d2, 0));
	for(auto tmp : a[u]){
		int v = tmp.fst; ll w = tmp.scd;
		if(v == fa) continue;
		T.modify(1, n, w), T.modify(in[v], out[v], -2ll * w);
		dfs3(v, u);
		T.modify(1, n, -w), T.modify(in[v], out[v], 2ll * w);
	}
}

void init(){
	pos1 = pos2 = 0;
	for(int i = 1; i <= n; i++){
		dfn[i] = in[i] = out[i] = id[i] = d[i] = dis[i] = 0;
		vector<pair<int, int>>().swap(a[i]), vector<pair<int, int>>().swap(b[i]), vector<pair<int, int>>().swap(qry[i]);
	}
}
void solve(){
	n = read();
	init();
	for(int i = 1; i < n; i++){
		int u = read(), v = read(), w = read();
		a[u].pb({v, w}), a[v].pb({u, w});
	}
	for(int i = 1; i < n; i++){
		int u = read(), v = read(), w = read();
		b[u].pb({v, w}), b[v].pb({u, w});
	}
	dfs(1, 0); dfs2(1, 0);
	for(int i = 1; i < 20; i++){
		for(int j = 1; j + (1 << i) - 1 <= pos1; j++) st[i][j] = mn(st[i - 1][j], st[i - 1][j + (1 << (i - 1))]);
	}
	q = read();
	for(int i = 1; i <= q; i++){
		int x = read(), y = read();
		qry[x].pb({y, i});
	}
	T.build(); dfs3(1, 0);
	for(int i = 1; i <= q; i++) printf("%lld\n", ans[i]);
}

int main(){
//	freopen("move.in", "r", stdin);
//	freopen("move.out", "w", stdout);
	for(int i = 2; i < N << 1; i++) lg2[i] = lg2[i >> 1] + 1;
	Ti = read();
	while(Ti--) solve();
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Time Limit Exceeded

input:

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

output:


result: