QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#165642 | #6852. Escape The Maze | PPP# | AC ✓ | 2531ms | 27072kb | C++17 | 2.6kb | 2023-09-05 20:25:24 | 2023-09-05 20:25:24 |
Judging History
answer
#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
int n, L;
const int maxN = 1e5 + 10;
bool Q;
struct Line {
mutable ll k, m, p;
bool operator<(const Line& o) const {
return Q ? p < o.p : k < o.k;
}
};
struct LineContainer : multiset<Line> {
// (for doubles, use inf = 1/.0, div(a,b) = a/b)
const ll inf = LLONG_MAX;
ll div(ll a, ll b) { // floored division
return a / b - ((a ^ b) < 0 && a % b); }
bool isect(iterator x, iterator y) {
if (y == end()) { x->p = inf; return false; }
if (x->k == y->k) x->p = x->m > y->m ? inf : -inf;
else x->p = div(y->m - x->m, x->k - y->k);
return x->p >= y->p;
}
void add(ll k, ll m) {
auto z = insert({k, m, 0}), y = z++, x = y;
while (isect(y, z)) z = erase(z);
if (x != begin() && isect(--x, y)) isect(x, y = erase(y));
while ((y = x) != begin() && (--x)->p >= y->p)
isect(x, erase(y));
}
ll query(ll x) {
assert(!empty());
Q = 1; auto l = *lower_bound({0,0,x}); Q = 0;
return l.k * x + l.m;
}
};
vector<pair<int,int>> g[maxN];
ll h[maxN];
ll dp[maxN];
LineContainer lc[maxN];
void dfs(int v, int p) {
bool was = false;
lc[v].clear();
for (auto& it : g[v]) {
if (it.first == p) continue;
h[it.first] = h[v] + it.second;
dfs(it.first, v);
ll C = -lc[it.first].query(h[v] + L) + (h[v] + L) * (h[v] + L);
if (!was) {
was = true;
dp[v] = C;
}
else {
dp[v] = min(dp[v], C);
}
if (lc[v].size() < lc[it.first].size()) {
swap(lc[v], lc[it.first]);
}
for (auto& f : lc[it.first]) {
lc[v].add(f.k, f.m);
}
lc[it.first].clear();
}
if (!was) dp[v] = 0;
lc[v].add(2 * h[v], -dp[v] - h[v] * h[v]);
}
void solve() {
cin >> n >> L;
for (int i = 1; i <= n; i++) {
g[i].clear();
}
for (int i = 1; i < n; i++) {
int u, v, w;
cin >> u >> v >> w;
g[u].emplace_back(v, w);
g[v].emplace_back(u, w);
}
int q;
cin >> q;
while (q--) {
int p;
cin >> p;
dfs(p, -1);
cout << dp[p] << '\n';
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#ifdef DEBUG
freopen("input.txt", "r", stdin);
#endif
int tst;
cin >> tst;
while (tst--) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2531ms
memory: 27072kb
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