QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#432007#6852. Escape The MazeheaksicnAC ✓3259ms318584kbC++172.5kb2024-06-06 16:03:572024-06-06 16:03:57

Judging History

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

  • [2024-06-06 16:03:57]
  • 评测
  • 测评结果:AC
  • 用时:3259ms
  • 内存:318584kb
  • [2024-06-06 16:03:57]
  • 提交

answer

//Man always remember love because of romance only!
#include<bits/stdc++.h>
using namespace std;
inline int read(){
    int X=0,w=0; char ch=0;
    while(!isdigit(ch)) {w|=ch=='-';ch=getchar();}
    while(isdigit(ch)) X=(X<<3)+(X<<1)+(ch^48),ch=getchar();
    return w?-X:X;
}
inline void write(int x){
	if(x<0) putchar('-'),x=-x;
    if(x>9) write(x/10);
    putchar(x%10+'0');
}
#define ll long long
const int N = 200500;
const int Delta = 1e9;
const int Len = Delta << 1;

vector<array<int, 2>> e[N];
ll dis[N];
int T[N], tot, cnt;
int rt;
int L;

int ls[N << 6], rs[N << 6];
ll n, ans[N];
struct line {
	ll k, b;
} p[N << 6];

inline ll calc(line l, ll x) { return l.k * x + l.b; }

void insert(int &x, int l, int r, line num) {
	if (!x) return x = ++cnt, p[x] = num, void();
	int mid = ((ll) l + r) >> 1;
	if (calc(p[x], mid) > calc(num, mid)) swap(p[x], num);
	if (calc(p[x], l) <= calc(num, l) && calc(p[x], r) <= calc(num, r)) return;
	if (calc(p[x], l) > calc(num, l))
		insert(ls[x], l, mid, num);
	else
		insert(rs[x], mid + 1, r, num);
}

const ll INF = 0x7fffffffffff;
ll query(int now, int l, int r, ll x) {
	if (!now) return INF;
	int mid = ((ll) l + r) >> 1;
	return min(calc(p[now], x), x <= mid ? query(ls[now], l, mid, x) : query(rs[now], mid + 1, r, x));
}

int merge(int x, int y, int l, int r) {
	if (!x || !y) return x | y;
	insert(x, l, r, p[y]);
	int mid = ((ll) l + r) >> 1;
	ls[x] = merge(ls[x], ls[y], l, mid);
	rs[x] = merge(rs[x], rs[y], mid + 1, r);
	return x;
}

void dfs(int x, int fa) {
	int go = 0;
	for (auto [y, w] : e[x]) {
		if (y == fa) continue;
		go++;
		dis[y] = dis[x] + w;
		dfs(y, x);
		T[x] = merge(T[x], T[y], 1, Len);
	}
	ans[x] = dis[x] * dis[x] + query(T[x], 1, Len, dis[x] + Delta);
	if (x != rt && !go) ans[x] = 0;
	insert(T[x], 1, Len, {-2 * (dis[x] - L), ans[x] + (dis[x] - L) * (dis[x] - L) + 2 * (dis[x] - L) * Delta});
}

void solve() {
	cin >> n >> L;
	for (int i = 1; i < n; i++) {
		int x, y, w;
		cin >> x >> y >> w;
		e[x].push_back({y, w});
		e[y].push_back({x, w});
	}
	int q;
	cin >> q;
	while (q--) {
		cin >> rt;
		dfs(rt, 0);
		cout << ans[rt] << "\n";
		memset(dis, 0, sizeof dis);
		memset(T, 0, sizeof(T));
		tot = cnt = 0;
		memset(ls, 0, sizeof(ls));
		memset(rs, 0, sizeof(rs));
		memset(p, 0, sizeof(p));
		memset(ans, 0, sizeof(ans));
	}
}

signed main() {
	cin.tie(0)->sync_with_stdio(0);
	int t;
	cin >> t;
	while (t--) {
		solve();
		for (int i = 1; i <= n; i++) {
			e[i].clear();
		}
	}
	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 3259ms
memory: 318584kb

input:

5
6 -23131
2 1 -65179
3 2 -4529
4 3 869
5 4 -43646
6 3 72319
6
1
2
3
4
5
6
100000 -89702
2 1 90843
3 2 -51373
4 3 -37208
5 4 50738
6 4 -56753
7 2 -22185
8 6 -2522
9 5 6214
10 6 51682
11 2 97227
12 11 -72521
13 3 20844
14 9 -63596
15 1 -811
16 1 -63314
17 14 33366
18 11 -13974
19 5 82109
20 10 -35290...

output:

662650564
584430625
385965316
420865225
2352464929
662650564
0
169
1
36
169
1
205
324
576
1
25
4
2401
441
400
10201
361
1600
36
5184
4
1
4611360649
177795556
0
0
1
834747664
1387786009
0
4356
6561
410
361
1849
100
6889
16
1296
81

result:

ok 46 lines