#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define pb push_back
#define ff first
#define ss second
#define arr3 array<int, 3>
const ll inf = numeric_limits<ll> :: max();
struct ST{
vector<pair<ll, int>> t;
vector<ll> a, p;
int n;
ST(int ns){
n = ns;
a.resize(n + 1);
t.resize(4 * n);
p.resize(4 * n);
}
void upd(int p, ll x){
a[p] = x;
}
void build(int v, int tl, int tr){
if (tl == tr){
t[v] = {a[tl], tl};
return;
}
int tm = (tl + tr) / 2, vv = 2 * v;
build(vv, tl, tm);
build(vv + 1, tm + 1, tr);
t[v] = max(t[vv], t[vv + 1]);
}
void build(){
fill(p.begin(), p.end(), 0);
build(1, 1, n);
}
pair<ll, int> get(){
return t[1];
}
void push(int& v){
if (!p[v]) return;
int vv = 2 * v;
t[vv].ff += p[v]; p[vv] += p[v];
t[vv + 1].ff += p[v]; p[vv + 1] += p[v];
p[v] = 0;
}
void add(int v, int tl, int tr, int& l, int& r, int& x){
if (l > tr || r < tl) return;
if (l <= tl && tr <= r){
t[v].ff += x;
p[v] += x;
return;
}
int tm = (tl + tr) / 2, vv = 2 * v;
push(v);
add(vv, tl, tm, l, r, x);
add(vv + 1, tm + 1, tr, l, r, x);
t[v] = max(t[vv], t[vv + 1]);
}
void add(int l, int r, int x){
add(1, 1, n, l, r, x);
}
};
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n; cin>>n;
vector<arr3> g[n + 1];
ll tot = 0;
for (int i = 1; i < n; i++){
int a, b, c, d; cin>>a>>b>>c>>d;
tot += c; tot += d;
g[a].pb({b, c, d});
g[b].pb({a, d, c});
}
vector<int> tin(n + 1), tout(n + 1), inv(n + 1), p(n + 1), dd(n + 1);
vector<ll> d(n + 1);
int timer = 0; ll S = 0;
function<void(int, int)> dfs = [&](int v, int pr){
p[v] = pr;
tin[v] = ++timer;
for (auto [i, w1, w2]: g[v]){
if (i == pr) continue;
S += w2;
dd[i] = w1;
d[i] = d[v] + w1;
dfs(i, v);
}
tout[v] = timer;
};
vector<ll> out(n + 1, inf);
vector<bool> used(n + 1);
ST T(n);
for (int i = 1; i <= n; i++){
S = d[i] = dd[i] = timer = 0;
dfs(i, 0);
out[1] = min(out[1], tot - S);
for (int j = 1; j <= n; j++){
T.upd(tin[j], d[j]);
inv[tin[j]] = j;
}
T.build();
fill(used.begin(), used.end(), 0);
used[i] = 1;
for (int j = 2; j <= n; j++){
auto [x, y] = T.get();
if (!x) continue;
int v = inv[y];
while (!used[v]){
T.add(tin[v], tout[v], -dd[v]);
used[v] = 1;
v = p[v];
}
S += x;
out[j] = min(out[j], tot - S);
}
}
for (int i = 1; i <= n; i++) out[i] = min(out[i], out[i - 1]);
int q; cin>>q;
while (q--){
int x; cin>>x;
cout<<out[x]<<"\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
456 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Execution timed out |
2089 ms |
47160 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Execution timed out |
2083 ms |
47208 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
456 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
896 ms |
908 KB |
Output is correct |
14 |
Correct |
655 ms |
860 KB |
Output is correct |
15 |
Correct |
848 ms |
860 KB |
Output is correct |
16 |
Correct |
888 ms |
924 KB |
Output is correct |
17 |
Correct |
896 ms |
856 KB |
Output is correct |
18 |
Correct |
882 ms |
904 KB |
Output is correct |
19 |
Correct |
914 ms |
896 KB |
Output is correct |
20 |
Correct |
859 ms |
856 KB |
Output is correct |
21 |
Correct |
897 ms |
860 KB |
Output is correct |
22 |
Correct |
907 ms |
856 KB |
Output is correct |
23 |
Correct |
866 ms |
900 KB |
Output is correct |
24 |
Correct |
715 ms |
856 KB |
Output is correct |
25 |
Correct |
862 ms |
860 KB |
Output is correct |
26 |
Correct |
675 ms |
920 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Execution timed out |
2089 ms |
47160 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
456 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Execution timed out |
2089 ms |
47160 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |