Submission #630054

# Submission time Handle Problem Language Result Execution time Memory
630054 2022-08-15T15:13:21 Z MohamedFaresNebili Harbingers (CEOI09_harbingers) C++14
0 / 100
111 ms 31516 KB
#include <bits/stdc++.h>

            using namespace std;

            using ll = long long;
            using ld = long double;

            #define ff first
            #define ss second
            #define pb push_back
            #define all(x) (x).begin(), (x).end()
            #define lb lower_bound

            const int LOG = 25;

            struct Line{
                int m, c;
                Line(int m = 0, int c = 0) : m(m), c(c) {}
                int calc(int x) { return m * x + c; }
            };

            ld intX(Line A, Line B) {
                return (ld)(B.c - A.c) / (ld)(A.m - B.m);
            }

            int N, S[100005], V[100005], DP[100005];
            vector<array<int, 2>> adj[100005];

            Line hull[100005]; int st = 0;

            int query(int v) {
                int lo = 0, hi = st - 1, ans = 0;
                while(lo <= hi) {
                    int md = (lo + hi) / 2;
                    if(intX(hull[md], hull[md + 1]) < v)
                        lo = md + 1;
                    else ans = md, hi = md - 1;
                }
                return hull[ans].calc(v);
            }
            int update(Line i) {
                if(st < 2 || intX(i, hull[st - 1]) >= intX(i, hull[st - 2]))
                    return st;
                int lo = 1, hi = st - 1, ans = 1;
                while(lo <= hi) {
                    int md = (lo + hi) / 2;
                    if(intX(i, hull[md]) < intX(hull[md], hull[md - 1]))
                        ans = md, hi = md - 1;
                    else lo = md + 1;
                }
                return ans;
            }

            int solve(int v, int p, int d) {
                int prev_size, prev_pos;
                Line prev_line;
                if(v == 1) {
                    DP[v] = 0;
                    hull[st++] = {0, 0};
                }
                else {
                    DP[v] = query(S[v]) + d * S[v] + V[v];
                    Line i(-d, DP[v]);
                    prev_size = st;
                    prev_pos = st = update(i);

                    prev_line = hull[st];
                    hull[st++] = i;
                }

                for(auto u : adj[v]) {
                    if(u[0] == p) continue;
                    solve(u[0], v, d + u[1]);
                }

                if(v != 1) {
                    st = prev_size;
                    hull[prev_pos] = prev_line;
                }
            }

            int32_t main() {
                ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
                cin >> N; memset(DP, -1, sizeof DP);
                for(int l = 0; l < N - 1; l++) {
                    int U, V, C; cin >> U >> V >> C;
                    adj[U].push_back({V, C});
                    adj[V].push_back({U, C});
                }
                for(int l = 2; l <= N; l++)
                    cin >> V[l] >> S[l];

                solve(1, 1, 0);

                for(int l = 2; l <= N; l++)
                    cout << DP[l] << " ";
                return 0;
            }


Compilation message

harbingers.cpp: In function 'int solve(int, int, int)':
harbingers.cpp:80:13: warning: no return statement in function returning non-void [-Wreturn-type]
   80 |             }
      |             ^
harbingers.cpp:78:36: warning: 'prev_pos' may be used uninitialized in this function [-Wmaybe-uninitialized]
   78 |                     hull[prev_pos] = prev_line;
      |                     ~~~~~~~~~~~~~~~^~~~~~~~~~~
harbingers.cpp:77:24: warning: 'prev_size' may be used uninitialized in this function [-Wmaybe-uninitialized]
   77 |                     st = prev_size;
      |                     ~~~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Runtime error 6 ms 7724 KB Execution killed with signal 11
2 Runtime error 9 ms 8276 KB Execution killed with signal 11
3 Runtime error 37 ms 17260 KB Execution killed with signal 11
4 Runtime error 51 ms 21972 KB Execution killed with signal 11
5 Runtime error 63 ms 26672 KB Execution killed with signal 11
6 Runtime error 111 ms 31516 KB Execution killed with signal 11
7 Runtime error 47 ms 13516 KB Execution killed with signal 11
8 Runtime error 71 ms 22336 KB Execution killed with signal 11
9 Runtime error 56 ms 16324 KB Execution killed with signal 11
10 Runtime error 58 ms 24264 KB Execution killed with signal 11