# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
156615 | karma | Harbingers (CEOI09_harbingers) | C++14 | 246 ms | 45604 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define FileName "test"
#define ll long long
#define ld long double
#define pb emplace_back
#define fi first
#define se second
using namespace std;
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ord_set;
typedef pair<int, int> pii;
typedef pair<ll, int> plli;
const int N = int(2e5) + 2;
const int oo = int(1e9) + 7;
const ll Cmax = 1ll * oo * oo;
int in[N], n, out[N], Time = 0, r[N];
ll d[N], f[N], vantoc[N], chuanbi[N];
vector<pii> a[N];
struct line
{
ll a, b;
line (ll a = 0, ll b = 0): a(a), b(b) {}
ll GetVal(int x) {return a * vantoc[r[x]] + b;}
};
struct TSegment
{
int l, r, mid;
line cur;
TSegment* left;
TSegment* right;
TSegment (int l = 0, int r = 0): l(l), r(r)
{
mid = (l + r) >> 1;
cur = line();
if(l == r)
{
left = right = NULL;
return;
}
left = new TSegment(l, mid);
right = new TSegment(mid + 1, r);
}
void Update(int x, int y, line val)
{
if(l > y || r < x) return;
if(x <= l && r <= y)
{
ll lcur = cur.GetVal(l);
ll rcur = cur.GetVal(r);
ll lval = val.GetVal(l);
ll rval = val.GetVal(r);
if(lcur >= lval && rcur >= rval) {cur = val; return;}
if(lcur <= lval && rcur <= rval) return;
ll mval = val.GetVal(mid);
ll mcur = cur.GetVal(mid);
if(lcur >= lval && mcur >= mval)
{
right -> Update(x, y, cur);
cur = val;
return;
}
if(lcur <= lval && mcur <= mval)
{
right -> Update(x, y, val);
return;
}
if(mcur >= mval && rcur >= rval)
{
left -> Update(x, y, cur);
cur = val;
return;
}
if(mcur <= mval && rcur <= rval)
{
left -> Update(x, y, val);
return;
}
}
left -> Update(x, y, val);
right -> Update(x, y, val);
}
ll Query(int x)
{
if(l > x || r < x) return Cmax;
ll res = cur.GetVal(x);
if(l == r) return res;
res = min(res, left -> Query(x));
res = min(res, right -> Query(x));
return res;
}
} Min;
void DFS(int u, int par) {
r[in[u] = ++Time] = u;
for(pii p: a[u]) {
if(par == p.fi) continue;
d[p.fi] = d[u] + p.se;
DFS(p.fi, u);
}
out[u] = Time;
}
void GetAns(int u, int par) {
///f[v] = -d[u] * vantoc[v] + f[u] + chuanbi[v] + d[v] * vantoc[v];
if(u != 1) f[u] = Min.Query(in[u]) + d[u] * vantoc[u] + chuanbi[u];
else f[u] = 0;
Min.Update(in[u], out[u], line(-d[u], f[u]));
for(pii p: a[u]) {
if(par == p.fi) continue;
GetAns(p.fi, u);
}
}
void Enter() {
cin >> n;
for(int i = 1; i < n; ++i) {
int u, v, l;
cin >> u >> v >> l;
a[u].pb(v, l), a[v].pb(u, l);
}
for(int i = 2; i <= n; ++i) cin >> chuanbi[i] >> vantoc[i];
DFS(1, 1);
Min = TSegment(1, n);
GetAns(1, 1);
for(int i = 2; i <= n; ++i) cout << f[i] << ' ';
}
void Solve() {
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
if(fopen(FileName".inp", "r")) {
freopen(FileName".inp", "r", stdin);
freopen(FileName".out", "w", stdout);
}
Enter(), Solve();
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |