Submission #674242

# Submission time Handle Problem Language Result Execution time Memory
674242 2022-12-23T14:10:18 Z tibinyte Constellation 3 (JOI20_constellation3) C++17
100 / 100
727 ms 152152 KB
#include <bits/stdc++.h>
#define int long long
using namespace std;
struct bit
{
    vector<int> b;
    void resize(int n)
    {
        b = vector<int>(n + 2);
    }
    void update(int pos, int val)
    {
        int n = (int)b.size() - 1;
        for (int i = pos; i <= n; i += i & (-i))
        {
            b[i] += val;
        }
    }
    int query(int pos)
    {
        int ans = 0;
        for (int i = pos; i; i -= i & (-i))
        {
            ans += b[i];
        }
        return ans;
    }
    void update(int st, int dr, int val)
    {
        update(st, val);
        update(dr + 1, -val);
    }
};
struct rmq
{
    vector<vector<pair<int, int>>> rmq;
    vector<int> lg;
    void build(vector<int> a, int n)
    {
        lg = vector<int>(n + 1);
        for (int i = 2; i <= n; ++i)
        {
            lg[i] = lg[i / 2] + 1;
        }
        rmq = vector<vector<pair<int, int>>>(n + 1, vector<pair<int, int>>(lg[n] + 1));
        for (int i = 1; i <= n; ++i)
        {
            rmq[i][0] = {a[i], i};
        }
        for (int j = 1; j <= lg[n]; ++j)
        {
            for (int i = 1; i + (1 << j) - 1 <= n; ++i)
            {
                rmq[i][j] = min(rmq[i][j - 1], rmq[i + (1 << (j - 1))][j - 1]);
            }
        }
    }
    pair<int, int> query(int st, int dr)
    {
        int pow_2 = lg[dr - st + 1];
        return min(rmq[st][pow_2], rmq[dr - (1 << pow_2) + 1][pow_2]);
    }
};
struct cartesian
{
    vector<int> a;
    vector<vector<int>> g;
    vector<vector<pair<int, int>>> stars;
    vector<vector<int>> jump;
    vector<int> tin;
    vector<int> tout;
    vector<int> dp;
    int p = 0;
    int root = 0;
    int max_jump = 0;
    int n = 0;
    void build(vector<int> _a, int _n)
    {
        n = _n;
        a = _a;
        dp = tin = tout = vector<int>(n + 1);
        g = vector<vector<int>>(n + 1);
        rmq t;
        t.build(a, n);
        max_jump = t.lg[n];
        jump = vector<vector<int>>(n + 1, vector<int>(max_jump + 1));
        stars = vector<vector<pair<int, int>>>(n + 1);
        function<void(int, int, int)> construct = [&](int st, int dr, int parent)
        {
            pair<int, int> qui = t.query(st, dr);
            if (parent != 0)
            {
                g[parent].push_back(qui.second);
                g[qui.second].push_back(parent);
                jump[qui.second][0] = parent;
            }
            else
            {
                jump[qui.second][0] = qui.second;
                root = qui.second;
            }
            for (int i = 1; i <= max_jump; ++i)
            {
                jump[qui.second][i] = jump[jump[qui.second][i - 1]][i - 1];
            }
            tin[qui.second] = ++p;
            if (qui.second != st)
            {
                construct(st, qui.second - 1, qui.second);
            }
            if (qui.second != dr)
            {
                construct(qui.second + 1, dr, qui.second);
            }
            tout[qui.second] = ++p;
        };
        construct(1, n, 0);
    }
    void insert(int node, int h, int coef)
    {
        int qui = node;
        for (int i = max_jump; i >= 0; --i)
        {
            if (a[jump[node][i]] >= h)
            {
                node = jump[node][i];
            }
        }
        stars[node].push_back({qui, coef});
    }
    int solve()
    {
        bit tree;
        bit tree2;
        tree.resize(p);
        tree2.resize(p);
        function<void(int, int)> dfs = [&](int node, int parent)
        {
            for (auto i : g[node])
            {
                if (i != parent)
                {
                    dfs(i, node);
                }
            }
            int sum = 0;
            for (auto i : g[node])
            {
                if (i != parent)
                {
                    sum += dp[i];
                }
            }
            tree.update(tin[node], tout[node], sum);
            dp[node] = sum;
            for (auto chain : stars[node])
            {
                dp[node] = max(dp[node], chain.second + tree.query(tin[chain.first]) - tree2.query(tin[chain.first]));
            }
            tree2.update(tin[node], tout[node], dp[node]);
        };
        dfs(root, 0);
        return dp[root];
    }
};
int32_t main()
{
    cin.tie(nullptr)->sync_with_stdio(false);
    int n;
    cin >> n;
    vector<int> a(n + 1);
    for (int i = 1; i <= n; ++i)
    {
        cin >> a[i];
        a[i] = n - a[i];
    }
    cartesian t;
    t.build(a, n);
    int m;
    cin >> m;
    int sum = 0;
    for (int i = 1; i <= m; ++i)
    {
        int qui, wh, coef;
        cin >> qui >> wh >> coef;
        wh = n - wh + 1;
        sum += coef;
        t.insert(qui, wh, coef);
    }
    cout << sum - t.solve();
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 340 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 468 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 340 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 468 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 2 ms 1108 KB Output is correct
24 Correct 2 ms 1108 KB Output is correct
25 Correct 3 ms 1108 KB Output is correct
26 Correct 2 ms 1236 KB Output is correct
27 Correct 3 ms 1108 KB Output is correct
28 Correct 2 ms 1108 KB Output is correct
29 Correct 2 ms 1108 KB Output is correct
30 Correct 3 ms 1236 KB Output is correct
31 Correct 2 ms 1108 KB Output is correct
32 Correct 3 ms 1364 KB Output is correct
33 Correct 3 ms 1364 KB Output is correct
34 Correct 3 ms 1236 KB Output is correct
35 Correct 2 ms 1236 KB Output is correct
36 Correct 2 ms 1364 KB Output is correct
37 Correct 2 ms 1364 KB Output is correct
38 Correct 3 ms 1492 KB Output is correct
39 Correct 3 ms 1236 KB Output is correct
40 Correct 2 ms 1364 KB Output is correct
41 Correct 2 ms 1236 KB Output is correct
42 Correct 2 ms 1236 KB Output is correct
43 Correct 3 ms 1364 KB Output is correct
44 Correct 2 ms 1108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 340 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 468 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 2 ms 1108 KB Output is correct
24 Correct 2 ms 1108 KB Output is correct
25 Correct 3 ms 1108 KB Output is correct
26 Correct 2 ms 1236 KB Output is correct
27 Correct 3 ms 1108 KB Output is correct
28 Correct 2 ms 1108 KB Output is correct
29 Correct 2 ms 1108 KB Output is correct
30 Correct 3 ms 1236 KB Output is correct
31 Correct 2 ms 1108 KB Output is correct
32 Correct 3 ms 1364 KB Output is correct
33 Correct 3 ms 1364 KB Output is correct
34 Correct 3 ms 1236 KB Output is correct
35 Correct 2 ms 1236 KB Output is correct
36 Correct 2 ms 1364 KB Output is correct
37 Correct 2 ms 1364 KB Output is correct
38 Correct 3 ms 1492 KB Output is correct
39 Correct 3 ms 1236 KB Output is correct
40 Correct 2 ms 1364 KB Output is correct
41 Correct 2 ms 1236 KB Output is correct
42 Correct 2 ms 1236 KB Output is correct
43 Correct 3 ms 1364 KB Output is correct
44 Correct 2 ms 1108 KB Output is correct
45 Correct 372 ms 127208 KB Output is correct
46 Correct 341 ms 125904 KB Output is correct
47 Correct 351 ms 123396 KB Output is correct
48 Correct 396 ms 127828 KB Output is correct
49 Correct 343 ms 122792 KB Output is correct
50 Correct 353 ms 123148 KB Output is correct
51 Correct 355 ms 123244 KB Output is correct
52 Correct 373 ms 125872 KB Output is correct
53 Correct 355 ms 126280 KB Output is correct
54 Correct 697 ms 144060 KB Output is correct
55 Correct 640 ms 136160 KB Output is correct
56 Correct 687 ms 132972 KB Output is correct
57 Correct 649 ms 129788 KB Output is correct
58 Correct 496 ms 134016 KB Output is correct
59 Correct 478 ms 132852 KB Output is correct
60 Correct 344 ms 152152 KB Output is correct
61 Correct 346 ms 127660 KB Output is correct
62 Correct 727 ms 141680 KB Output is correct
63 Correct 351 ms 125672 KB Output is correct
64 Correct 337 ms 123076 KB Output is correct
65 Correct 686 ms 143812 KB Output is correct
66 Correct 357 ms 123856 KB Output is correct