Submission #767935

# Submission time Handle Problem Language Result Execution time Memory
767935 2023-06-27T09:53:15 Z hafo Harbingers (CEOI09_harbingers) C++14
100 / 100
99 ms 25464 KB
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define pb push_back
#define pa pair<int, int>
#define pall pair<ll, int>
#define fi first
#define se second
#define TASK "test"
#define Size(x) (int) x.size()
#define all(x) x.begin(), x.end()
using namespace std;

template<typename T1, typename T2> bool mini (T1 &a, T2 b) {if(a > b) a = b; else return 0; return 1;}
template<typename T1, typename T2> bool maxi (T1 &a, T2 b) {if(a < b) a = b; else return 0; return 1;}

const int MOD = 1e9 + 7;
const int LOG = 20;
const int maxn = 1e5 + 7;
const ll oo = 1e18 + 69;

int n, s[maxn], p[maxn], u, v, w;
vector<pa> g[maxn];
ll dp[maxn], f[maxn];

struct line {
    ll m, b;
};

double inter(line &c, line &d) {
    return (double) (d.b - c.b) / (double) (c.m - d.m);
}

struct cht {
    struct opr {
        line ow;
        int sz, pos;
    };
    stack<opr> undo_list;
    line lines[maxn];
    int sz = 1;

    bool bad(line &a, line &b, line &c) {
        return inter(a, c) <= inter(a, b);
    }

    ll calc(line a, ll x) {
        return a.m * x + a.b;
    }

    ll query(ll x) {
        ll res = calc(lines[0], x);
        int l = 0, r = sz - 1, mid;
        while(l < r) {
            mid = l + r >> 1;
            ll s1 = calc(lines[mid], x);
            ll s2 = calc(lines[mid + 1], x);
            mini(res, min({s1, s2}));
            if(s1 < s2) {
                r = mid;
            } else {
                l = mid + 1;
            }
        }
        return res;
    }

    void add(line new_line) {
        int l = 1, r = sz - 1, mid, res = sz;
        while(l <= r) {
            mid = l + r >> 1;
            if(bad(lines[mid - 1], lines[mid], new_line)) {
                res = mid;
                r = mid - 1;
            }
            else {
                l = mid + 1;
            }
        }
        
        undo_list.push({lines[res], sz, res});
        lines[res] = new_line;
        sz = res + 1;
    }

    void undo() {
        auto top = undo_list.top();
        undo_list.pop();
        lines[top.pos] = top.ow;
        sz = top.sz;
    }

} cht;

void dfs(int u, int par) {
    for(auto e:g[u]) {
        int v = e.fi, w = e.se;
        if(v == par) continue;
        f[v] = f[u] + w;
        dp[v] = f[v] * s[v] + p[v] + cht.query(s[v]);
        cht.add({-f[v], dp[v]});
        dfs(v, u);
        cht.undo();

    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    //freopen(TASK".inp", "r", stdin);
    //freopen(TASK".out", "w", stdout);

    cin>>n;
    for(int i = 1; i < n; i++) {
        cin>>u>>v>>w;
        g[u].pb({v, w});
        g[v].pb({u, w});
    }
    for(int i = 2; i <= n; i++) cin>>p[i]>>s[i];

    cht.lines[0] = {0, 0};
    dfs(1, 0);
    for(int i = 2; i <= n; i++) cout<<dp[i]<<" ";
    return 0;
}

Compilation message

harbingers.cpp: In member function 'long long int cht::query(long long int)':
harbingers.cpp:55:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   55 |             mid = l + r >> 1;
      |                   ~~^~~
harbingers.cpp: In member function 'void cht::add(line)':
harbingers.cpp:71:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   71 |             mid = l + r >> 1;
      |                   ~~^~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 3 ms 3156 KB Output is correct
3 Correct 33 ms 12092 KB Output is correct
4 Correct 43 ms 16908 KB Output is correct
5 Correct 54 ms 20952 KB Output is correct
6 Correct 90 ms 25464 KB Output is correct
7 Correct 44 ms 11644 KB Output is correct
8 Correct 99 ms 16868 KB Output is correct
9 Correct 77 ms 20288 KB Output is correct
10 Correct 69 ms 18628 KB Output is correct