Submission #965224

# Submission time Handle Problem Language Result Execution time Memory
965224 2024-04-18T08:34:51 Z hhnguyen Harbingers (CEOI09_harbingers) C++14
10 / 100
61 ms 17860 KB
#include <bits/stdc++.h>
#define X first
#define Y second

const int N = 100005;
const long long INF = (long long)1e18;

using namespace std;

typedef pair<int, int> Line;

struct operation {
    int pos, top;
    Line overwrite;
    operation(int _p, int _t, Line _o) {
        pos = _p; top = _t; overwrite = _o;
    }
};
vector<operation> undoLst;
Line lines[N];
int n, top;

long long eval(Line line, long long x) {return line.X * x + line.Y;}
bool bad(Line A,Line B,Line C)
{
    Line ab = {B.X - A.X,B.Y - A.Y};
    Line ac = {C.X - A.X,C.Y - A.Y};
    return (ab.X*ac.Y-ab.Y*ac.X >= 0);
}
long long getMin(long long coord) {
    int l = 0, r = top - 1; long long ans = eval(lines[l], coord);
    while (l < r) {
        int mid = l + r >> 1;
        long long x = eval(lines[mid], coord);
        long long y = eval(lines[mid + 1], coord);
        if (x > y) l = mid + 1; else r = mid;
        ans = min(ans, min(x, y));
    }
    return ans;
}

bool insertLine(Line newLine) {
    int l = 1, r = top - 1, k = top;
    while (l <= r) {
        int mid = l + r >> 1;
        if (bad(lines[mid - 1], lines[mid], newLine)) {
            k = mid; r = mid - 1;
        }
        else l = mid + 1;
    }
    undoLst.push_back(operation(k, top, lines[k]));
    top = k + 1;
    lines[k] = newLine;
    return 1;
}

void undo() {
    operation ope = undoLst.back(); undoLst.pop_back();
    top = ope.top; lines[ope.pos] = ope.overwrite;
}

long long f[N], S[N], V[N], d[N];
vector<Line> a[N];

void dfs(int u, int par) {
    if (u > 1)
        f[u] = getMin(V[u]) + S[u] + V[u] * d[u];
    insertLine(make_pair(-d[u], f[u]));
    for (vector<Line>::iterator it = a[u].begin(); it != a[u].end(); ++it) {
        int v = it->X;
        int uv = it->Y;
        if (v == par) continue;
        d[v] = d[u] + uv;
        dfs(v, u);
    }
    undo();
}

int main() {
    ios::sync_with_stdio(0); cin.tie(0);
    cin >> n;
    int u, v, c;
    for (int i = 1; i < n; ++i) {
        cin >> u >> v >> c;
        a[u].push_back(make_pair(v, c));
        a[v].push_back(make_pair(u, c));
    }
    for (int i = 2; i <= n; ++i) cin >> S[i] >> V[i];
    dfs(1, 0);
    for (int i = 2; i <= n; ++i) cout << f[i] << ' ';
    return 0;
}
/*
5
1 2 20
2 3 12
2 4 1
4 5 3
26 9
1 10
500 2
2 30
*/

Compilation message

harbingers.cpp: In function 'long long int getMin(long long int)':
harbingers.cpp:33:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   33 |         int mid = l + r >> 1;
      |                   ~~^~~
harbingers.cpp: In function 'bool insertLine(Line)':
harbingers.cpp:45:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   45 |         int mid = l + r >> 1;
      |                   ~~^~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4696 KB Output is correct
2 Incorrect 2 ms 4956 KB Output isn't correct
3 Incorrect 24 ms 9892 KB Output isn't correct
4 Incorrect 43 ms 12436 KB Output isn't correct
5 Incorrect 47 ms 15308 KB Output isn't correct
6 Incorrect 61 ms 17860 KB Output isn't correct
7 Incorrect 34 ms 10572 KB Output isn't correct
8 Incorrect 53 ms 13648 KB Output isn't correct
9 Incorrect 55 ms 15004 KB Output isn't correct
10 Incorrect 57 ms 14404 KB Output isn't correct