Submission #894939

# Submission time Handle Problem Language Result Execution time Memory
894939 2023-12-29T09:08:07 Z fanwen Constellation 3 (JOI20_constellation3) C++17
100 / 100
324 ms 60572 KB
#include <bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define ll long long
#define file(name)                  \
    if(fopen(name".inp", "r"))      \
        freopen(name".inp", "r", stdin), freopen(name".out", "w", stdout);

template <class T> struct Fenwick_Tree {
    vector<T> bit;
    int n;
    Fenwick_Tree(int _n = 0) : n(_n), bit(_n + 5){}

    void clear() { fill(bit.begin(), bit.end(), T(0)); }

    void update(int u, T val) {
        for (; u <= n; u += u & -u) bit[u] += val;
    }

    void update(int l, int r, T val) {
        update(l, val);
        update(r + 1, -val);
    }

    T get(int u) {
        T ans = 0;
        for (; u; u -= u & -u) ans += bit[u];
        return ans;
    }

    T get(int l, int r) {
        return get(r) - get(l - 1);
    }
};

const int MAX = 2e5 + 5;

int n, m, a[MAX], time_in[MAX], time_out[MAX], anc[MAX][20];
vector <int> adj[MAX];
vector <pair <int, int>> stars[MAX];

long long dp[MAX], sum = 0;

void dfs(int u) {
    static int run = 0;
    for (int i = 1; i < 20; ++i) anc[u][i] = anc[anc[u][i - 1]][i - 1];
    time_in[u] = ++run;
    for (int v : adj[u]) {
        dfs(v);
    }
    time_out[u] = run;
}

void _dfs(int u) {
    static Fenwick_Tree <long long> bit(n);
    for (int v : adj[u]) {
        _dfs(v);
        dp[u] += dp[v];
    }

    if((int) adj[u].size() == 2) {
        for (int i = 0; i < 2; ++i) {
            bit.update(time_in[adj[u][i]], time_out[adj[u][i]], dp[adj[u][1 ^ i]]);
        }
    }

    for (pair <int, int> tmp : stars[u]) {
        int v, c; tie(v, c) = tmp;
        long long res = bit.get(time_in[v]) + c;
        for (int x : adj[v]) res += dp[x];
        dp[u] = max(dp[u], res);
    }
}

void you_make_it(void) {
    cin >> n;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
        static stack <int> st;
        int last = -1;
        while(!st.empty() && a[st.top()] < a[i]) {
            last = st.top();
            st.pop();
        }

        if(!st.empty()) anc[i][0] = st.top();
        else anc[i][0] = i;
        if(last != -1) anc[last][0] = i;
        st.push(i);
    }

    int rt = 1;
    for (int i = 1; i <= n; ++i) {
        if(anc[i][0] == i) rt = i;
        else adj[anc[i][0]].push_back(i);
    }

    dfs(rt);
    cin >> m; while(m--) {
        int x, y, c; cin >> x >> y >> c;
        sum += c;
        int u = x;
        for (int i = 19; i >= 0; --i) if(a[anc[u][i]] < y) {
            u = anc[u][i];
        }

        stars[u].emplace_back(x, c);
    }

    _dfs(rt);
    cout << sum - dp[rt];
}

signed main() {

#ifdef LOCAL
    freopen("TASK.inp", "r", stdin);
    freopen("TASK.out", "w", stdout);
#endif
    file("JOI20_constellation3");
    auto start_time = chrono::steady_clock::now();

    cin.tie(0), cout.tie(0) -> sync_with_stdio(0);

    you_make_it();

    auto end_time = chrono::steady_clock::now();

    cerr << "\nExecution time : " << chrono::duration_cast <chrono::milliseconds> (end_time - start_time).count() << "[ms]" << endl;

    return (0 ^ 0);
}

// Dream it. Wish it. Do it.

Compilation message

constellation3.cpp: In instantiation of 'Fenwick_Tree<T>::Fenwick_Tree(int) [with T = long long int]':
constellation3.cpp:58:42:   required from here
constellation3.cpp:14:9: warning: 'Fenwick_Tree<long long int>::n' will be initialized after [-Wreorder]
   14 |     int n;
      |         ^
constellation3.cpp:13:15: warning:   'std::vector<long long int, std::allocator<long long int> > Fenwick_Tree<long long int>::bit' [-Wreorder]
   13 |     vector<T> bit;
      |               ^~~
constellation3.cpp:15:5: warning:   when initialized here [-Wreorder]
   15 |     Fenwick_Tree(int _n = 0) : n(_n), bit(_n + 5){}
      |     ^~~~~~~~~~~~
constellation3.cpp: In function 'int main()':
constellation3.cpp:10:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   10 |         freopen(name".inp", "r", stdin), freopen(name".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
constellation3.cpp:123:5: note: in expansion of macro 'file'
  123 |     file("JOI20_constellation3");
      |     ^~~~
constellation3.cpp:10:49: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   10 |         freopen(name".inp", "r", stdin), freopen(name".out", "w", stdout);
      |                                          ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
constellation3.cpp:123:5: note: in expansion of macro 'file'
  123 |     file("JOI20_constellation3");
      |     ^~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 16732 KB Output is correct
2 Correct 3 ms 16728 KB Output is correct
3 Correct 3 ms 16984 KB Output is correct
4 Correct 3 ms 16732 KB Output is correct
5 Correct 3 ms 16732 KB Output is correct
6 Correct 3 ms 16732 KB Output is correct
7 Correct 3 ms 16732 KB Output is correct
8 Correct 3 ms 16732 KB Output is correct
9 Correct 3 ms 16732 KB Output is correct
10 Correct 3 ms 16732 KB Output is correct
11 Correct 3 ms 16732 KB Output is correct
12 Correct 3 ms 16732 KB Output is correct
13 Correct 4 ms 16732 KB Output is correct
14 Correct 3 ms 16732 KB Output is correct
15 Correct 3 ms 16732 KB Output is correct
16 Correct 3 ms 16732 KB Output is correct
17 Correct 3 ms 16732 KB Output is correct
18 Correct 4 ms 16732 KB Output is correct
19 Correct 3 ms 16732 KB Output is correct
20 Correct 3 ms 16732 KB Output is correct
21 Correct 3 ms 16732 KB Output is correct
22 Correct 3 ms 16732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 16732 KB Output is correct
2 Correct 3 ms 16728 KB Output is correct
3 Correct 3 ms 16984 KB Output is correct
4 Correct 3 ms 16732 KB Output is correct
5 Correct 3 ms 16732 KB Output is correct
6 Correct 3 ms 16732 KB Output is correct
7 Correct 3 ms 16732 KB Output is correct
8 Correct 3 ms 16732 KB Output is correct
9 Correct 3 ms 16732 KB Output is correct
10 Correct 3 ms 16732 KB Output is correct
11 Correct 3 ms 16732 KB Output is correct
12 Correct 3 ms 16732 KB Output is correct
13 Correct 4 ms 16732 KB Output is correct
14 Correct 3 ms 16732 KB Output is correct
15 Correct 3 ms 16732 KB Output is correct
16 Correct 3 ms 16732 KB Output is correct
17 Correct 3 ms 16732 KB Output is correct
18 Correct 4 ms 16732 KB Output is correct
19 Correct 3 ms 16732 KB Output is correct
20 Correct 3 ms 16732 KB Output is correct
21 Correct 3 ms 16732 KB Output is correct
22 Correct 3 ms 16732 KB Output is correct
23 Correct 4 ms 16988 KB Output is correct
24 Correct 4 ms 16988 KB Output is correct
25 Correct 4 ms 16988 KB Output is correct
26 Correct 4 ms 16988 KB Output is correct
27 Correct 4 ms 16988 KB Output is correct
28 Correct 4 ms 16984 KB Output is correct
29 Correct 4 ms 16988 KB Output is correct
30 Correct 4 ms 16988 KB Output is correct
31 Correct 5 ms 16876 KB Output is correct
32 Correct 5 ms 16988 KB Output is correct
33 Correct 5 ms 16988 KB Output is correct
34 Correct 4 ms 16988 KB Output is correct
35 Correct 4 ms 16988 KB Output is correct
36 Correct 4 ms 16988 KB Output is correct
37 Correct 4 ms 16988 KB Output is correct
38 Correct 4 ms 17076 KB Output is correct
39 Correct 4 ms 16872 KB Output is correct
40 Correct 4 ms 16992 KB Output is correct
41 Correct 4 ms 16736 KB Output is correct
42 Correct 4 ms 16988 KB Output is correct
43 Correct 5 ms 16984 KB Output is correct
44 Correct 4 ms 16988 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 16732 KB Output is correct
2 Correct 3 ms 16728 KB Output is correct
3 Correct 3 ms 16984 KB Output is correct
4 Correct 3 ms 16732 KB Output is correct
5 Correct 3 ms 16732 KB Output is correct
6 Correct 3 ms 16732 KB Output is correct
7 Correct 3 ms 16732 KB Output is correct
8 Correct 3 ms 16732 KB Output is correct
9 Correct 3 ms 16732 KB Output is correct
10 Correct 3 ms 16732 KB Output is correct
11 Correct 3 ms 16732 KB Output is correct
12 Correct 3 ms 16732 KB Output is correct
13 Correct 4 ms 16732 KB Output is correct
14 Correct 3 ms 16732 KB Output is correct
15 Correct 3 ms 16732 KB Output is correct
16 Correct 3 ms 16732 KB Output is correct
17 Correct 3 ms 16732 KB Output is correct
18 Correct 4 ms 16732 KB Output is correct
19 Correct 3 ms 16732 KB Output is correct
20 Correct 3 ms 16732 KB Output is correct
21 Correct 3 ms 16732 KB Output is correct
22 Correct 3 ms 16732 KB Output is correct
23 Correct 4 ms 16988 KB Output is correct
24 Correct 4 ms 16988 KB Output is correct
25 Correct 4 ms 16988 KB Output is correct
26 Correct 4 ms 16988 KB Output is correct
27 Correct 4 ms 16988 KB Output is correct
28 Correct 4 ms 16984 KB Output is correct
29 Correct 4 ms 16988 KB Output is correct
30 Correct 4 ms 16988 KB Output is correct
31 Correct 5 ms 16876 KB Output is correct
32 Correct 5 ms 16988 KB Output is correct
33 Correct 5 ms 16988 KB Output is correct
34 Correct 4 ms 16988 KB Output is correct
35 Correct 4 ms 16988 KB Output is correct
36 Correct 4 ms 16988 KB Output is correct
37 Correct 4 ms 16988 KB Output is correct
38 Correct 4 ms 17076 KB Output is correct
39 Correct 4 ms 16872 KB Output is correct
40 Correct 4 ms 16992 KB Output is correct
41 Correct 4 ms 16736 KB Output is correct
42 Correct 4 ms 16988 KB Output is correct
43 Correct 5 ms 16984 KB Output is correct
44 Correct 4 ms 16988 KB Output is correct
45 Correct 183 ms 43608 KB Output is correct
46 Correct 191 ms 44116 KB Output is correct
47 Correct 178 ms 44120 KB Output is correct
48 Correct 187 ms 44012 KB Output is correct
49 Correct 195 ms 43860 KB Output is correct
50 Correct 176 ms 43836 KB Output is correct
51 Correct 189 ms 43860 KB Output is correct
52 Correct 192 ms 44180 KB Output is correct
53 Correct 185 ms 44116 KB Output is correct
54 Correct 324 ms 54360 KB Output is correct
55 Correct 241 ms 51308 KB Output is correct
56 Correct 258 ms 49364 KB Output is correct
57 Correct 304 ms 48244 KB Output is correct
58 Correct 186 ms 52060 KB Output is correct
59 Correct 164 ms 51832 KB Output is correct
60 Correct 121 ms 60572 KB Output is correct
61 Correct 168 ms 43088 KB Output is correct
62 Correct 241 ms 57080 KB Output is correct
63 Correct 187 ms 42820 KB Output is correct
64 Correct 153 ms 42728 KB Output is correct
65 Correct 308 ms 57440 KB Output is correct
66 Correct 154 ms 42924 KB Output is correct