답안 #582953

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
582953 2022-06-24T15:57:49 Z elkernos 별자리 3 (JOI20_constellation3) C++17
100 / 100
403 ms 66720 KB
#include <bits/stdc++.h>

using namespace std;

const int oo = 1000 * 1000 * 1000;

struct Tree {
    typedef pair<int, int> T;
    static constexpr T unit = {-oo, -oo};
    T f(T a, T b) { return max(a, b); }
    vector<T> s;
    int n;
    Tree(int nn) : s(2 * nn), n(nn) {}
    void update(int pos, T val)
    {
        for(s[pos += n] = val; pos /= 2;) {
            s[pos] = f(s[pos * 2], s[pos * 2 + 1]);
        }
    }
    T query(int b, int e)
    {
        T ra = unit, rb = unit;
        for(b += n, e += n + 1; b < e; b /= 2, e /= 2) {
            if(b % 2) ra = f(ra, s[b++]);
            if(e % 2) rb = f(s[--e], rb);
        }
        return f(ra, rb);
    }
};

struct FT {
    typedef long long T;
    vector<T> s;
    FT(int n) : s(n) {}
    void update(int pos, T dif)
    {
        for(; pos < (int)s.size(); pos |= pos + 1) {
            s[pos] += dif;
        }
    }
    T query(int pos)
    {
        T res = 0;
        for(pos++; pos >= 1; pos &= pos - 1) {
            res += s[pos - 1];
        }
        return res;
    }
};

struct gwiazdeczka {
    int x, y, c;
    void read() { cin >> x >> y >> c; }
    bool operator<(const gwiazdeczka &he) const { return x < he.x; }
};

int main()
{
    cin.tie(nullptr)->sync_with_stdio(0);
    int n;
    cin >> n;
    vector<int> a(n + 1);
    Tree t1(n + 123);
    for(int i = 1; i <= n; i++) {
        cin >> a[i];
        t1.update(i, {a[i], i});
    }
    int m;
    cin >> m;
    vector<gwiazdeczka> stars(m + 1);
    long long sum = 0;
    for(int i = 1; i <= m; i++) {
        stars[i].read();
        sum += stars[i].c;
    }
    sort(stars.begin() + 1, stars.end());
    Tree t2(m + 123);
    for(int i = 1; i <= m; i++) {
        t2.update(i, {stars[i].y, i});
    }
    int now = 0;
    vector<vector<int>> g(n + 1), paths(n + 1);
    vector<int> ends(n + 1);
    function<void(int, int, int)> rek = [&](int l, int r, int p) {
        if(l > r) {
            return;
        }
        now++;
        if(p) {
            g[p].push_back(now);
        }
        pair<int, int> split = t1.query(l, r);
        ends[split.second] = now;
        int lo = lower_bound(stars.begin() + 1, stars.end(), gwiazdeczka{l, 0, 0}) - stars.begin();
        int hi = upper_bound(stars.begin() + 1, stars.end(), gwiazdeczka{r, 0, 0}) - stars.begin() - 1;
        while(1) {
            pair<int, int> maybe = t2.query(lo, hi);
            if(maybe.first <= split.first) {
                break;
            }
            paths[now].push_back(maybe.second);
            t2.update(maybe.second, pair<int, int>{-oo, -oo});
        }
        rek(l, split.second - 1, ends[split.second]); //! JA PIERDOLE XDDD
        rek(split.second + 1, r, ends[split.second]);
    };
    rek(1, n, 0);
    vector<long long> dp(n + 1);
    int len = 0;
    vector<int> pre(n + 1), post(n + 1);
    FT tsum(n + 123);
    function<void(int)> dfs = [&](int u) {
        pre[u] = ++len;
        for(int to : g[u]) {
            dfs(to);
            dp[u] += dp[to];
        }
        post[u] = len;
        tsum.update(pre[u], dp[u]);
        tsum.update(post[u] + 1, -dp[u]);
        for(int i : paths[u]) {
            dp[u] = max(dp[u], tsum.query(pre[ends[stars[i].x]]) + stars[i].c);
        }
        tsum.update(pre[u], -dp[u]);
        tsum.update(post[u] + 1, dp[u]);
    };
    dfs(1);
    cout << sum - dp[1] << '\n';
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 356 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 328 KB Output is correct
8 Correct 1 ms 328 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 320 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 328 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 2 ms 320 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 356 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 328 KB Output is correct
8 Correct 1 ms 328 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 320 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 328 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 2 ms 320 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 3 ms 596 KB Output is correct
24 Correct 4 ms 596 KB Output is correct
25 Correct 3 ms 656 KB Output is correct
26 Correct 3 ms 596 KB Output is correct
27 Correct 3 ms 596 KB Output is correct
28 Correct 3 ms 596 KB Output is correct
29 Correct 4 ms 596 KB Output is correct
30 Correct 3 ms 596 KB Output is correct
31 Correct 3 ms 596 KB Output is correct
32 Correct 4 ms 724 KB Output is correct
33 Correct 4 ms 724 KB Output is correct
34 Correct 4 ms 724 KB Output is correct
35 Correct 3 ms 724 KB Output is correct
36 Correct 3 ms 724 KB Output is correct
37 Correct 3 ms 724 KB Output is correct
38 Correct 3 ms 852 KB Output is correct
39 Correct 3 ms 596 KB Output is correct
40 Correct 3 ms 852 KB Output is correct
41 Correct 4 ms 596 KB Output is correct
42 Correct 4 ms 596 KB Output is correct
43 Correct 5 ms 844 KB Output is correct
44 Correct 3 ms 596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 356 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 328 KB Output is correct
8 Correct 1 ms 328 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 320 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 328 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 2 ms 320 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 3 ms 596 KB Output is correct
24 Correct 4 ms 596 KB Output is correct
25 Correct 3 ms 656 KB Output is correct
26 Correct 3 ms 596 KB Output is correct
27 Correct 3 ms 596 KB Output is correct
28 Correct 3 ms 596 KB Output is correct
29 Correct 4 ms 596 KB Output is correct
30 Correct 3 ms 596 KB Output is correct
31 Correct 3 ms 596 KB Output is correct
32 Correct 4 ms 724 KB Output is correct
33 Correct 4 ms 724 KB Output is correct
34 Correct 4 ms 724 KB Output is correct
35 Correct 3 ms 724 KB Output is correct
36 Correct 3 ms 724 KB Output is correct
37 Correct 3 ms 724 KB Output is correct
38 Correct 3 ms 852 KB Output is correct
39 Correct 3 ms 596 KB Output is correct
40 Correct 3 ms 852 KB Output is correct
41 Correct 4 ms 596 KB Output is correct
42 Correct 4 ms 596 KB Output is correct
43 Correct 5 ms 844 KB Output is correct
44 Correct 3 ms 596 KB Output is correct
45 Correct 301 ms 37088 KB Output is correct
46 Correct 315 ms 36604 KB Output is correct
47 Correct 286 ms 36616 KB Output is correct
48 Correct 298 ms 36852 KB Output is correct
49 Correct 282 ms 36224 KB Output is correct
50 Correct 290 ms 36008 KB Output is correct
51 Correct 300 ms 36284 KB Output is correct
52 Correct 292 ms 36976 KB Output is correct
53 Correct 279 ms 36672 KB Output is correct
54 Correct 372 ms 55432 KB Output is correct
55 Correct 394 ms 49624 KB Output is correct
56 Correct 354 ms 46164 KB Output is correct
57 Correct 382 ms 44160 KB Output is correct
58 Correct 347 ms 49200 KB Output is correct
59 Correct 330 ms 49328 KB Output is correct
60 Correct 292 ms 66720 KB Output is correct
61 Correct 285 ms 35940 KB Output is correct
62 Correct 389 ms 57952 KB Output is correct
63 Correct 313 ms 35564 KB Output is correct
64 Correct 297 ms 34892 KB Output is correct
65 Correct 403 ms 58336 KB Output is correct
66 Correct 315 ms 35352 KB Output is correct