QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#201772#5148. Tree Distanceluyiming123WA 2225ms170344kbC++145.3kb2023-10-05 16:42:022023-10-05 16:42:02

Judging History

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

  • [2023-10-05 16:42:02]
  • 评测
  • 测评结果:WA
  • 用时:2225ms
  • 内存:170344kb
  • [2023-10-05 16:42:02]
  • 提交

answer

# include <bits/stdc++.h>
using namespace std;
using i64 = long long;
const int N = 2e5 + 5; 
int n;
struct edge
{
	int v, w; bool del;
};
struct node
{
	int u, v; i64 w; 
	node() {}
	node(int _u, int _v, i64 _w) : u(_u), v(_v), w(_w) {}
}Eg; int sizS = 0, mindelt = 1e9;
struct Query
{
	int l, id;
};
vector <Query> Q[N];
bool compare(const struct node &x, const struct node &y)
{
	return x.v < y.v;
}
vector <node> Pts;
vector <edge> g[N]; 
vector<pair<i64, int> > L, R; 
int siz[N];

void find_rt(int x, int fa)
{
	siz[x] = 1;
	for(auto I : g[x])
	{
		int v = I.v; 
		if(v == fa || I.del) continue;
		find_rt(v, x); 
		siz[x] += siz[v];
		int dt = abs(siz[v] - (sizS - siz[v]));
		if(dt < mindelt) mindelt = dt, Eg = {x, v, I.w};
	}
	return;
}

void dfs(int x, int fa, i64 DIST, vector <pair <i64,int> > &A)
{
	A.push_back({DIST, x});
	for(auto I : g[x])
	{
		int v = I.v; 
		if(v == fa || I.del) continue;
		dfs(v, x, DIST + 1ll * I.w, A);
	}
	return;
}
void solve(node Ed)
{
	// printf("solve : (%d,%d,%lld)\n", Ed.u, Ed.v, Ed.w);
	for(auto &I : g[Ed.u]) if(I.v == Ed.v) {I.del = 1; break;}
	for(auto &I : g[Ed.v]) if(I.v == Ed.u) {I.del = 1; break;}
	L.clear(), R.clear();
	set <pair<int, i64> > St;
	dfs(Ed.u, Ed.v, 0, L);
	dfs(Ed.v, Ed.u, 0, R);
	sort(L.begin(), L.end()), sort(R.begin(), R.end());
	for(int i = 0, j = 0; i < (int)L.size(); i++)
	{
		while(j < (int)R.size() && R[j].first <= L[i].first) St.insert({R[j].second, R[j].first}), ++j;
		auto it = St.upper_bound({L[i].second, 0}); 
		if(it != St.end()) Pts.push_back({L[i].second, (*it).first, (*it).second + L[i].first + Ed.w});
		if(it != St.begin())
		{
			--it;
			Pts.push_back({(*it).first, L[i].second, (*it).second + L[i].first + Ed.w});
		}
	}
	St.clear();
	for(int i = 0, j = 0; i < (int)R.size(); i++)
	{
		while(j < (int)L.size() && L[j].first <= R[i].first) St.insert({L[j].second, L[j].first}), ++j; 
		auto it = St.upper_bound({R[i].second, 0});
		if(it != St.end()) Pts.push_back({R[i].second, (*it).first, (*it).second + R[i].first + Ed.w});
		if(it != St.begin())
		{
			--it;
			Pts.push_back({(*it).first, R[i].second, (*it).second + R[i].first + Ed.w});
		}
	}
	int lstR = (int)R.size(); 
	Eg = {-1, -1, -1}, sizS = (int)L.size(), mindelt = 1e9; 
	if(sizS >= 2) find_rt(Ed.u, Ed.v), solve(Eg); 
	Eg = {-1, -1, -1}, sizS = lstR, mindelt = 1e9; 
	if(sizS >= 2) find_rt(Ed.v, Ed.u), solve(Eg); 
	return;
}

struct SgT
{
	struct node
	{
		i64 Max, tag; 
	}T[N << 2];
	void build(int root, int l, int r)
	{
		T[root].Max = 1e15, T[root].tag = -1;
		if(l == r) return;
		int mid = (l + r) >> 1;
		build(root << 1, l, mid), build(root << 1 | 1, mid + 1, r);
		return;
	}
	void pushup(int root)
	{
		T[root].Max = max(T[root << 1].Max, T[root << 1 | 1].Max);
		return;
	}
	void pushdown(int root)
	{
		if(T[root].tag == -1) return; 
		i64 tag = T[root].tag; T[root].tag = -1;
		T[root << 1].Max = tag;
		T[root << 1].tag = tag;
		T[root << 1 | 1].Max = tag;
		T[root << 1 | 1].tag = tag;
		return;
	}
	void update(int root, int l, int r, int s, int t, i64 d)
	{
		if(l <= s && t <= r)
		{
			T[root].Max = T[root].tag = d; return;
		}
		pushdown(root);
		int mid = (s + t) >> 1;
		if(l <= mid) update(root << 1, l, r, s, mid, d);
		if(r > mid) update(root << 1 | 1, l, r, mid + 1, t, d); 
		pushup(root);
		return;
	}
	int query(int root, int l, int r, i64 d) // the Max x that ax > d
	{
		// printf("root = %d, l = %d, r = %d, d = %lld\n", root, l, r, d);
		if(l == r) 
		{
			return T[root].Max <= d ? -1 : l;
		}
		pushdown(root);
		int mid = (l + r) >> 1;
		// printf("lfMax = %lld\n", T[root << 1].Max);
		return T[root << 1].Max <= d ? query(root << 1 | 1, mid + 1, r, d) : query(root << 1, l, mid, d);
	}
	i64 query2(int root, int l, int r, int x)
	{
		if(l == r) return T[root].Max;
		pushdown(root);
		int mid = (l + r) >> 1;
		return (x <= mid) ? query2(root << 1, l, mid, x) : query2(root << 1 | 1, mid + 1, r, x);
	}
	void modify(int l, i64 d)
	{
		// printf("modify : l = %d, d = %lld\n", l, d);
		// [1, l] Max(d, ai)
		int pos = query(1, 1, n, d); 
		// printf("pos = %d\n", pos);
		if(pos == -1 || pos > l) return;
		update(1, pos, l, 1, n, d);
		// printf("update : %d, %d, %lld\n", pos, l, d);
		return;
	}
}T;
i64 Ans[N];
int main(void)
{
	// freopen("in.in", "r", stdin); freopen("out.out", "w", stdout);
	scanf("%d", &n);
	for(int i = 1; i < n; i++)
	{
		int u, v, w; scanf("%d%d%d", &u, &v, &w);
		g[u].push_back({v, w, 0}), g[v].push_back({u, w, 0}); 
	}
	Eg = {-1, -1, -1}, mindelt = 1e9, sizS = n; find_rt(1, 0); solve(Eg); 
	for(auto &I : Pts) if(I.u > I.v) swap(I.u, I.v);
	sort(Pts.begin(), Pts.end(), compare);
	// printf("DEBUG Pts\n");
	// for(auto I : Pts) 
	// {
	// 	printf("%d %d %lld\n", I.u, I.v, I.w);
	// }
	int Test; scanf("%d", &Test);
	for(int i = 1; i <= Test; i++)
	{
		int l, r; scanf("%d%d", &l, &r); 
		if(l != r) Q[r].push_back({l, i});
		else Ans[i] = -1;
	}
	T.build(1, 1, n);
	for(int r = 1, j = 0; r <= n; r++)
	{
		// printf("r = %d\n", r);
		while(j < (int)Pts.size() && Pts[j].v == r) T.modify(Pts[j].u, Pts[j].w), ++j;
		for(auto I : Q[r]) 
		{
			Ans[I.id] = T.query2(1, 1, n, I.l);
		}
	}
	for(int i = 1; i <= Test; i++) printf("%lld\n", Ans[i]);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 17500kb

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: 2225ms
memory: 170344kb

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:

59781685
1178569098929
1310877
1310877
1310877
92126
4366
92126
328055210
55721881562
4366
983605
92126
92126
1310877
92126
8212187
92126
2681285
901653
1310877
1310877
1310877
10272586
92126
784309
8367049186
10272586
4366
4366
92126
4366
4366
92126
1310877
92126
901653
4366
-1
-1
16211870
21085
43...

result:

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