답안 #674959

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
674959 2022-12-26T17:19:28 Z skittles1412 Worst Reporter 4 (JOI21_worst_reporter4) C++17
79 / 100
809 ms 131532 KB
#include "bits/extc++.h"

using namespace std;

template <typename T>
void dbgh(const T& t) {
    cerr << t << endl;
}

template <typename T, typename... U>
void dbgh(const T& t, const U&... u) {
    cerr << t << " | ";
    dbgh(u...);
}

#ifdef DEBUG
#define dbg(...)                                           \
    cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]" \
         << ": ";                                          \
    dbgh(__VA_ARGS__)
#else
#define cerr   \
    if (false) \
    cerr
#define dbg(...)
#endif

#define endl "\n"
#define long int64_t
#define sz(x) int(std::size(x))

constexpr int maxn = 2e5 + 5;

struct DS {
    struct Node {
        long v, lazy;
        int lc, rc;
    };

    inline static int heap_ptr = 1;
    inline static Node heap[1 << 25] {};

    static int alloc() {
        return heap_ptr++;
    }
    static void alloc(int& o) {
        if (!o) {
            o = alloc();
        }
    }

    static void maintain(int o) {
        auto& [v, lazy, lc, rc] = heap[o];
        v = min(heap[lc].v, heap[rc].v) + lazy;
    }

    static void merge(int& a, int& b, int l, int r) {
        if (!b) {
            return;
        } else if (!a) {
            a = b;
            return;
        }
        auto& [v, lazy, lc, rc] = heap[a];
        int mid = (l + r) / 2;
        lazy += heap[b].lazy;
        merge(lc, heap[b].lc, l, mid);
        merge(rc, heap[b].rc, mid + 1, r);
        maintain(a);
    }

    friend void merge(DS& a, DS& b) {
        merge(a.root, b.root, 0, maxn);
    }

    static long get(int o, int l, int r, int ind) {
        if (!o) {
            return 0;
        }
        auto& [v, lazy, lc, rc] = heap[o];
        if (l == r) {
            return v;
        }
        int mid = (l + r) / 2;
        if (ind <= mid) {
            return lazy + get(lc, l, mid, ind);
        } else {
            return lazy + get(rc, mid + 1, r, ind);
        }
    }

    static void upd_add(int& o, long x) {
        alloc(o);
        heap[o].lazy += x;
        maintain(o);
    }

    static long query_min(int o, int l, int r, int ql, int qr) {
        if (!o) {
            return 0;
        }
        auto& [v, lazy, lc, rc] = heap[o];
        if (ql <= l && r <= qr) {
            return v;
        }
        int mid = (l + r) / 2;
        long ans = 1e18;
        if (ql <= mid) {
            ans = min(ans, query_min(lc, l, mid, ql, qr));
        }
        if (mid < qr) {
            ans = min(ans, query_min(rc, mid + 1, r, ql, qr));
        }
        return ans + lazy;
    }

    static void upd_set(int& o, int l, int r, int ql, int qr, long x) {
        alloc(o);
        auto& [v, lazy, lc, rc] = heap[o];
        if (ql <= l && r <= qr) {
            lc = rc = 0;
            v = lazy = x;
            return;
        }
        int mid = (l + r) / 2;
        x -= lazy;
        if (ql <= mid) {
            upd_set(lc, l, mid, ql, qr, x);
        }
        if (mid < qr) {
            upd_set(rc, mid + 1, r, ql, qr, x);
        }
        maintain(o);
    }

    int root = 0;

    long get(int ind) const {
        return get(root, 0, maxn, ind);
    }

    void upd_add(long x) {
        upd_add(root, x);
    }

    long query_min(int l, int r) const {
        return query_min(root, 0, maxn, l, r);
    }

    void upd_set(int l, int r, long x) {
        upd_set(root, 0, maxn, l, r, x);
    }

    void upd_min(int qr, long x) {
        int l = 0, r = qr + 1;
        while (l < r) {
            int mid = (l + r) / 2;
            if (query_min(mid, qr) >= x) {
                r = mid;
            } else {
                l = mid + 1;
            }
        }
        if (l <= qr) {
            upd_set(l, qr, x);
        }
    }
};

bool vis[maxn], in_cyc[maxn];
int arr[maxn], cost[maxn];
vector<int> graph[maxn];

DS dp(int u, int p = -1) {
    vis[u] = true;
    DS cur;
    for (auto& v : graph[u]) {
        if (v == p || in_cyc[v]) {
            continue;
        }
        auto&& ndp = dp(v, u);
        merge(cur, ndp);
    }
    long cans = cur.get(arr[u]);
    cur.upd_add(cost[u]);
    if (p != -1) {
        cur.upd_min(arr[u], cans);
    }
    return cur;
}

void solve() {
    int n;
    cin >> n;
    int par[n];
    for (int i = 0; i < n; i++) {
        cin >> par[i] >> arr[i] >> cost[i];
        par[i]--;
    }
    for (int i = 0; i < n; i++) {
        graph[par[i]].push_back(i);
    }
    vector<int> comp(arr, arr + n);
    sort(begin(comp), end(comp));
    comp.erase(unique(begin(comp), end(comp)), comp.end());
    for (int i = 0; i < n; i++) {
        arr[i] =
            int(lower_bound(begin(comp), end(comp), arr[i]) - comp.begin());
    }
    long ans = 0;
    for (int i = 0; i < n; i++) {
        if (vis[i]) {
            continue;
        }
        int u = i;
        vector<int> st;
        do {
            vis[u] = true;
            st.push_back(u);
            u = par[u];
        } while (!vis[u]);
        vector<int> cyc(find(begin(st), end(st), u), st.end());
        DS cds;
        map<int, long> ccost;
        for (auto& a : cyc) {
            dbg(a);
            ccost[arr[a]] += cost[a];
            in_cyc[a] = true;
            auto&& xans = dp(a);
            merge(cds, xans);
        }
        long cans = cds.get(0);
        for (auto& [k, v] : ccost) {
            cans = min(cans, cds.get(k) - v);
        }
        dbg(i, cans);
        ans += cans;
    }
    cout << ans << endl;
}

int main() {
    cin.tie(nullptr);
    cin.exceptions(ios::failbit);
    ios_base::sync_with_stdio(false);
    solve();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4972 KB Output is correct
3 Correct 3 ms 5028 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 11 ms 7440 KB Output is correct
6 Correct 9 ms 6740 KB Output is correct
7 Correct 8 ms 6464 KB Output is correct
8 Correct 12 ms 7380 KB Output is correct
9 Correct 9 ms 6796 KB Output is correct
10 Correct 8 ms 6484 KB Output is correct
11 Correct 7 ms 6436 KB Output is correct
12 Correct 15 ms 6860 KB Output is correct
13 Correct 7 ms 6100 KB Output is correct
14 Correct 15 ms 6672 KB Output is correct
15 Correct 10 ms 5844 KB Output is correct
16 Correct 12 ms 7344 KB Output is correct
17 Correct 9 ms 6740 KB Output is correct
18 Correct 7 ms 6356 KB Output is correct
19 Correct 13 ms 7092 KB Output is correct
20 Correct 9 ms 6884 KB Output is correct
21 Correct 7 ms 6696 KB Output is correct
22 Correct 9 ms 7852 KB Output is correct
23 Correct 9 ms 7508 KB Output is correct
24 Correct 12 ms 7124 KB Output is correct
25 Correct 9 ms 6868 KB Output is correct
26 Correct 11 ms 6316 KB Output is correct
27 Correct 12 ms 7380 KB Output is correct
28 Correct 14 ms 7020 KB Output is correct
29 Correct 14 ms 6728 KB Output is correct
30 Correct 14 ms 5972 KB Output is correct
31 Correct 14 ms 5932 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4972 KB Output is correct
3 Correct 3 ms 5028 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 11 ms 7440 KB Output is correct
6 Correct 9 ms 6740 KB Output is correct
7 Correct 8 ms 6464 KB Output is correct
8 Correct 12 ms 7380 KB Output is correct
9 Correct 9 ms 6796 KB Output is correct
10 Correct 8 ms 6484 KB Output is correct
11 Correct 7 ms 6436 KB Output is correct
12 Correct 15 ms 6860 KB Output is correct
13 Correct 7 ms 6100 KB Output is correct
14 Correct 15 ms 6672 KB Output is correct
15 Correct 10 ms 5844 KB Output is correct
16 Correct 12 ms 7344 KB Output is correct
17 Correct 9 ms 6740 KB Output is correct
18 Correct 7 ms 6356 KB Output is correct
19 Correct 13 ms 7092 KB Output is correct
20 Correct 9 ms 6884 KB Output is correct
21 Correct 7 ms 6696 KB Output is correct
22 Correct 9 ms 7852 KB Output is correct
23 Correct 9 ms 7508 KB Output is correct
24 Correct 12 ms 7124 KB Output is correct
25 Correct 9 ms 6868 KB Output is correct
26 Correct 11 ms 6316 KB Output is correct
27 Correct 12 ms 7380 KB Output is correct
28 Correct 14 ms 7020 KB Output is correct
29 Correct 14 ms 6728 KB Output is correct
30 Correct 14 ms 5972 KB Output is correct
31 Correct 14 ms 5932 KB Output is correct
32 Correct 11 ms 7376 KB Output is correct
33 Correct 491 ms 125780 KB Output is correct
34 Correct 372 ms 89108 KB Output is correct
35 Correct 493 ms 128680 KB Output is correct
36 Correct 353 ms 89164 KB Output is correct
37 Correct 225 ms 61332 KB Output is correct
38 Correct 201 ms 59532 KB Output is correct
39 Correct 739 ms 101256 KB Output is correct
40 Correct 410 ms 54452 KB Output is correct
41 Correct 166 ms 49436 KB Output is correct
42 Correct 747 ms 89688 KB Output is correct
43 Correct 404 ms 40140 KB Output is correct
44 Correct 474 ms 128164 KB Output is correct
45 Correct 310 ms 88060 KB Output is correct
46 Correct 156 ms 59456 KB Output is correct
47 Correct 785 ms 96648 KB Output is correct
48 Correct 312 ms 80844 KB Output is correct
49 Correct 177 ms 74724 KB Output is correct
50 Correct 319 ms 131532 KB Output is correct
51 Correct 221 ms 112848 KB Output is correct
52 Correct 809 ms 93896 KB Output is correct
53 Correct 304 ms 81092 KB Output is correct
54 Correct 430 ms 55500 KB Output is correct
55 Correct 529 ms 102496 KB Output is correct
56 Correct 636 ms 81876 KB Output is correct
57 Correct 667 ms 71264 KB Output is correct
58 Correct 723 ms 43016 KB Output is correct
59 Correct 725 ms 43012 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4972 KB Output is correct
3 Correct 3 ms 5028 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 11 ms 7440 KB Output is correct
6 Correct 9 ms 6740 KB Output is correct
7 Correct 8 ms 6464 KB Output is correct
8 Correct 12 ms 7380 KB Output is correct
9 Correct 9 ms 6796 KB Output is correct
10 Correct 8 ms 6484 KB Output is correct
11 Correct 7 ms 6436 KB Output is correct
12 Correct 15 ms 6860 KB Output is correct
13 Correct 7 ms 6100 KB Output is correct
14 Correct 15 ms 6672 KB Output is correct
15 Correct 10 ms 5844 KB Output is correct
16 Correct 12 ms 7344 KB Output is correct
17 Correct 9 ms 6740 KB Output is correct
18 Correct 7 ms 6356 KB Output is correct
19 Correct 13 ms 7092 KB Output is correct
20 Correct 9 ms 6884 KB Output is correct
21 Correct 7 ms 6696 KB Output is correct
22 Correct 9 ms 7852 KB Output is correct
23 Correct 9 ms 7508 KB Output is correct
24 Correct 12 ms 7124 KB Output is correct
25 Correct 9 ms 6868 KB Output is correct
26 Correct 11 ms 6316 KB Output is correct
27 Correct 12 ms 7380 KB Output is correct
28 Correct 14 ms 7020 KB Output is correct
29 Correct 14 ms 6728 KB Output is correct
30 Correct 14 ms 5972 KB Output is correct
31 Correct 14 ms 5932 KB Output is correct
32 Correct 11 ms 7376 KB Output is correct
33 Correct 491 ms 125780 KB Output is correct
34 Correct 372 ms 89108 KB Output is correct
35 Correct 493 ms 128680 KB Output is correct
36 Correct 353 ms 89164 KB Output is correct
37 Correct 225 ms 61332 KB Output is correct
38 Correct 201 ms 59532 KB Output is correct
39 Correct 739 ms 101256 KB Output is correct
40 Correct 410 ms 54452 KB Output is correct
41 Correct 166 ms 49436 KB Output is correct
42 Correct 747 ms 89688 KB Output is correct
43 Correct 404 ms 40140 KB Output is correct
44 Correct 474 ms 128164 KB Output is correct
45 Correct 310 ms 88060 KB Output is correct
46 Correct 156 ms 59456 KB Output is correct
47 Correct 785 ms 96648 KB Output is correct
48 Correct 312 ms 80844 KB Output is correct
49 Correct 177 ms 74724 KB Output is correct
50 Correct 319 ms 131532 KB Output is correct
51 Correct 221 ms 112848 KB Output is correct
52 Correct 809 ms 93896 KB Output is correct
53 Correct 304 ms 81092 KB Output is correct
54 Correct 430 ms 55500 KB Output is correct
55 Correct 529 ms 102496 KB Output is correct
56 Correct 636 ms 81876 KB Output is correct
57 Correct 667 ms 71264 KB Output is correct
58 Correct 723 ms 43016 KB Output is correct
59 Correct 725 ms 43012 KB Output is correct
60 Incorrect 3 ms 4948 KB Output isn't correct
61 Halted 0 ms 0 KB -