Submission #674237

# Submission time Handle Problem Language Result Execution time Memory
674237 2022-12-23T14:01:08 Z tibinyte Constellation 3 (JOI20_constellation3) C++17
100 / 100
740 ms 153648 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;
    int max_jump;
    int n;
    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 324 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 468 KB Output is correct
12 Correct 1 ms 456 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 320 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 448 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 324 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 468 KB Output is correct
12 Correct 1 ms 456 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 320 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 448 KB Output is correct
23 Correct 3 ms 1236 KB Output is correct
24 Correct 3 ms 1236 KB Output is correct
25 Correct 2 ms 1144 KB Output is correct
26 Correct 2 ms 1228 KB Output is correct
27 Correct 2 ms 1236 KB Output is correct
28 Correct 2 ms 1236 KB Output is correct
29 Correct 3 ms 1184 KB Output is correct
30 Correct 3 ms 1224 KB Output is correct
31 Correct 3 ms 1236 KB Output is correct
32 Correct 2 ms 1364 KB Output is correct
33 Correct 2 ms 1356 KB Output is correct
34 Correct 2 ms 1236 KB Output is correct
35 Correct 2 ms 1364 KB Output is correct
36 Correct 2 ms 1364 KB Output is correct
37 Correct 2 ms 1352 KB Output is correct
38 Correct 2 ms 1492 KB Output is correct
39 Correct 3 ms 1236 KB Output is correct
40 Correct 3 ms 1364 KB Output is correct
41 Correct 3 ms 1236 KB Output is correct
42 Correct 3 ms 1272 KB Output is correct
43 Correct 3 ms 1364 KB Output is correct
44 Correct 2 ms 1236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 324 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 468 KB Output is correct
12 Correct 1 ms 456 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 320 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 448 KB Output is correct
23 Correct 3 ms 1236 KB Output is correct
24 Correct 3 ms 1236 KB Output is correct
25 Correct 2 ms 1144 KB Output is correct
26 Correct 2 ms 1228 KB Output is correct
27 Correct 2 ms 1236 KB Output is correct
28 Correct 2 ms 1236 KB Output is correct
29 Correct 3 ms 1184 KB Output is correct
30 Correct 3 ms 1224 KB Output is correct
31 Correct 3 ms 1236 KB Output is correct
32 Correct 2 ms 1364 KB Output is correct
33 Correct 2 ms 1356 KB Output is correct
34 Correct 2 ms 1236 KB Output is correct
35 Correct 2 ms 1364 KB Output is correct
36 Correct 2 ms 1364 KB Output is correct
37 Correct 2 ms 1352 KB Output is correct
38 Correct 2 ms 1492 KB Output is correct
39 Correct 3 ms 1236 KB Output is correct
40 Correct 3 ms 1364 KB Output is correct
41 Correct 3 ms 1236 KB Output is correct
42 Correct 3 ms 1272 KB Output is correct
43 Correct 3 ms 1364 KB Output is correct
44 Correct 2 ms 1236 KB Output is correct
45 Correct 378 ms 128600 KB Output is correct
46 Correct 364 ms 127288 KB Output is correct
47 Correct 359 ms 124948 KB Output is correct
48 Correct 406 ms 129204 KB Output is correct
49 Correct 355 ms 124144 KB Output is correct
50 Correct 377 ms 124468 KB Output is correct
51 Correct 389 ms 124600 KB Output is correct
52 Correct 389 ms 127308 KB Output is correct
53 Correct 362 ms 127572 KB Output is correct
54 Correct 723 ms 145592 KB Output is correct
55 Correct 676 ms 137568 KB Output is correct
56 Correct 661 ms 134192 KB Output is correct
57 Correct 631 ms 131196 KB Output is correct
58 Correct 502 ms 135472 KB Output is correct
59 Correct 475 ms 134180 KB Output is correct
60 Correct 326 ms 153648 KB Output is correct
61 Correct 353 ms 129208 KB Output is correct
62 Correct 712 ms 143264 KB Output is correct
63 Correct 348 ms 127204 KB Output is correct
64 Correct 353 ms 124496 KB Output is correct
65 Correct 740 ms 145372 KB Output is correct
66 Correct 358 ms 125492 KB Output is correct