Submission #666425

# Submission time Handle Problem Language Result Execution time Memory
666425 2022-11-28T15:41:44 Z bojackduy Harbingers (CEOI09_harbingers) C++14
100 / 100
132 ms 18324 KB
#include <iostream>
#include <queue>
#include <stack>
#include <algorithm>
#include <string.h>
#include <functional>
#define task ""
#define size() size() * 1ll 
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define pii pair<int, int>
#define pll pair<long long, long long>
#define fi first
#define se second
#define MASK(x) (1LL << (x))
#define BIT(x,i) (((x) >> (i)) & 1)
#define numbit(x) __builtin_popcountll(x)
 
using namespace std;
 
typedef vector<int> vi;
typedef vector<pii> vii;
typedef vector<pll> vll;
typedef long long ll;
 
template<class t>
bool mini(t &x,t y) {
    if (y < x) {
       x = y;
       return 1;
    }
return 0;
}
template<class t>
bool maxi(t &x,t y) {
    if (x < y) {
       x = y;
       return 1;
    }
return 0;
}
 
const int N = 1e5 + 1;
 
int n, st, ed, sz;
int b[N], c[N];
ll dp[N], d[N];
pll q[N], bck[N];
vii a[N];
void dfs(int u, int p) {
    for (auto v: a[u]) if (v.se ^ p) {
        d[v.se] = d[u] + v.fi;
        dfs(v.se, u);
    }
}
long double intersect(pll a, pll b) {
    return (long double) (b.se - a.se) / (a.fi - b.fi);
}
 
// dp[v] = - d[u] * b[v] + dp[u] + d[v] * b[v] + c[v];
void dfs1(int u, int p) {
    int lo = st, hi = ed - 2;
    while (lo <= hi) {
        int mid = lo + hi >> 1;
        if (intersect(q[mid], q[mid + 1]) <= b[u]) lo = mid + 1; else hi = mid - 1;
    }
    hi++;
    dp[u] = q[hi].fi * b[u] + q[hi].se + d[u] * b[u] + c[u];
    pll tmp = pll(-d[u], dp[u]);
    lo = st, hi = ed - 2;
    while (lo <= hi) {
        int mid = lo + hi >> 1;
        if (intersect(q[mid + 1], q[mid]) >= intersect(q[mid + 1], tmp)) hi = mid - 1; else lo = mid + 1;
    }
    lo++;
    int old_ed = ed;
    ed = lo;
    pll old_line = q[ed];
    q[ed++] = tmp;
    for (auto v: a[u]) if (v.se ^ p) {
        dfs1(v.se, u);
    }
    q[lo] = old_line;
    ed = old_ed;
}
void solve(int test = -1) {
    cin >> n;
    for (int i = 1; i < n; i++) {
        int x, y, z;
        cin >> x >> y >> z;
        a[x].pb(pii(z, y));
        a[y].pb(pii(z, x));
    }
    for (int i = 2; i <= n; i++) {
        cin >> c[i] >> b[i];
    }
    dfs(1, 0);
    q[ed++] = pll(0, 0);
    for (auto v: a[1]) {
        dfs1(v.se, 1);
    }
    for (int i = 2; i <= n; i++) cout << dp[i] << ' ';
}
 
int32_t main()  {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    // freopen(task".inp", "r", stdin);
    // freopen(task".out", "w", stdout);
 
    int T = 1;
    // cin >> T;
    for (int i = 1; i <= T; i++) {
        solve(i);
    }
return 0;
}
/*
 
*/

Compilation message

harbingers.cpp: In function 'void dfs1(int, int)':
harbingers.cpp:64:22: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   64 |         int mid = lo + hi >> 1;
      |                   ~~~^~~~
harbingers.cpp:72:22: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   72 |         int mid = lo + hi >> 1;
      |                   ~~~^~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 3 ms 3028 KB Output is correct
3 Correct 40 ms 9300 KB Output is correct
4 Correct 53 ms 12688 KB Output is correct
5 Correct 69 ms 15376 KB Output is correct
6 Correct 100 ms 18324 KB Output is correct
7 Correct 51 ms 9724 KB Output is correct
8 Correct 132 ms 13980 KB Output is correct
9 Correct 123 ms 16164 KB Output is correct
10 Correct 102 ms 14988 KB Output is correct