#include <bits/stdc++.h>
#define long long long
#define pii pair<int, int>
#define x first
#define y second
using namespace std;
const int N = 1e5+5;
vector<int> coord;
struct PersistentLiChaoTree {
struct item {
pii val;
item *l, *r;
item(pii val, item *l, item *r) : val(val), l(l), r(r) {}
};
item *ver[N];
stack<item*> S;
long f(long x, pii l) { return 1ll * l.x * x + l.y; }
item* newleaf(pii val) {
item* ret = new item(val, NULL, NULL);
S.emplace(ret);
return ret;
}
item* newpar(pii val, item* l, item *r) {
item* ret = new item(val, l, r);
S.emplace(ret);
return ret;
}
item* update(pii k, item *p, int l = 1, int r = coord.size()) {
int m = (l + r) >> 1;
if(!p) return newleaf(k);
int L = coord[l-1], M = coord[m-1], R = coord[r-1];
if(f(L, k) < f(L, p->val) && f(R, k) < f(R, p->val)) return newpar(k, p->l, p->r);
if(f(L, k) > f(L, p->val) && f(R, k) > f(R, p->val)) return p;
bool lef = f(L, k) < f(L, p->val);
bool mid = f(M, k) < f(M, p->val);
if(l == r) return newleaf(mid ? k : p->val);
if(lef != mid) return newpar(mid ? k : p->val, update(mid ? p->val : k, p->l, l, m), p->r);
else return newpar(mid ? k : p->val, p->l, update(mid ? p->val : k, p->r, m+1, r));
}
long get(int x, item *p, int l = 1, int r = coord.size()) {
int m = (l + r) >> 1;
if(!p) return 1e18;
if(l == r) return f(x, p->val);
if(x <= coord[m-1]) return min(f(x, p->val), get(x, p->l, l, m));
else return min(f(x, p->val), get(x, p->r, m+1, r));
}
void rollback(int x) {
while(S.size() > x) {
item* now = S.top(); S.pop();
delete now;
}
}
} t;
int n;
vector<pii> g[N];
int S[N], V[N], d[N];
long ans[N];
void dfs(int u, int p) {
int tmp = t.S.size();
if(u != 1) ans[u] = t.get(V[u], t.ver[p]) + 1ll * V[u] * d[u] + S[u];
if(u != 1) t.ver[u] = t.update(pii(-d[u], ans[u]), t.ver[p]);
else t.ver[u] = t.newleaf(pii(0, 0));
for(pii v : g[u]) if(v.x != p) {
d[v.x] = d[u] + v.y;
dfs(v.x, u);
}
t.rollback(tmp);
}
int main() {
scanf("%d", &n);
for(int i = 1, a, b, c; i < n; i++) {
scanf("%d %d %d", &a, &b, &c);
g[a].emplace_back(b, c);
g[b].emplace_back(a, c);
}
for(int i = 2; i <= n; i++) {
scanf("%lld %lld", S+i, V+i);
coord.emplace_back(V[i]);
}
sort(coord.begin(), coord.end());
coord.resize(unique(coord.begin(), coord.end()) - coord.begin());
dfs(1, 1);
for(int i = 2; i <= n; i++) printf("%lld ", ans[i]);
printf("\n");
return 0;
}
Compilation message
harbingers.cpp: In member function 'void PersistentLiChaoTree::rollback(int)':
harbingers.cpp:62:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(S.size() > x) {
^
harbingers.cpp: In function 'int main()':
harbingers.cpp:94:36: warning: format '%lld' expects argument of type 'long long int*', but argument 2 has type 'int*' [-Wformat=]
scanf("%lld %lld", S+i, V+i);
~~~ ^
harbingers.cpp:94:36: warning: format '%lld' expects argument of type 'long long int*', but argument 3 has type 'int*' [-Wformat=]
harbingers.cpp:87:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
harbingers.cpp:89:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d", &a, &b, &c);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
harbingers.cpp:94:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld %lld", S+i, V+i);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
8 ms |
3832 KB |
Output is correct |
3 |
Incorrect |
79 ms |
20904 KB |
Output isn't correct |
4 |
Runtime error |
193 ms |
53212 KB |
Memory limit exceeded (if you are sure your verdict is not MLE, please contact us) |
5 |
Runtime error |
218 ms |
40764 KB |
Memory limit exceeded (if you are sure your verdict is not MLE, please contact us) |
6 |
Runtime error |
224 ms |
40692 KB |
Memory limit exceeded (if you are sure your verdict is not MLE, please contact us) |
7 |
Correct |
116 ms |
18672 KB |
Output is correct |
8 |
Runtime error |
225 ms |
33604 KB |
Memory limit exceeded (if you are sure your verdict is not MLE, please contact us) |
9 |
Runtime error |
240 ms |
47288 KB |
Memory limit exceeded (if you are sure your verdict is not MLE, please contact us) |
10 |
Runtime error |
250 ms |
54384 KB |
Memory limit exceeded (if you are sure your verdict is not MLE, please contact us) |