Submission #1115468

# Submission time Handle Problem Language Result Execution time Memory
1115468 2024-11-20T13:29:07 Z Zero_OP Fire (JOI20_ho_t5) C++14
100 / 100
276 ms 50876 KB
#include <bits/stdc++.h>

using namespace std;

enum event_type {
    start_decreasing, stop_increasing, become_zero
};

struct fenwick_tree{
    vector<long long> bit;
    fenwick_tree(int n) : bit(n + 1, 0) {}

    void update(int id, long long val){
        for(; id < (int)bit.size(); id += id & (-id)) bit[id] += val;
    }

    long long query(int id){
        long long sum = 0;
        for(; id > 0; id -= id & (-id)) sum += bit[id];
        return sum;
    }
};

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int N, Q;
    cin >> N >> Q;
    vector<int> S(N + 1), L(N + 1), R(N + 1), stop_inc(N + 1), start_dec(N + 1);
    stack<int> st;

    for(int i = 1; i <= N; ++i){
        cin >> S[i];
    }

    for(int i = 1; i <= N; ++i){
        while(!st.empty() && S[st.top()] <= S[i]) st.pop();
        L[i] = (st.empty() ? -(2 * N + 1) : st.top());
        st.push(i);
    }

    while(!st.empty()) st.pop();
    for(int i = N; i >= 1; --i){
        while(!st.empty() && S[st.top()] < S[i]) st.pop();
        R[i] = (st.empty() ? + (N + 1) : st.top());
        st.push(i);
    }

    vector<vector<tuple<int, int, int>>> queries(N + 2);
    for(int i = 0; i < Q; ++i){
        int t, l, r;
        cin >> t >> l >> r;
        queries[t + 1].emplace_back(l, r, i);
    }

    vector<vector<tuple<event_type, int>>> sweep(N + 2);

    for(int i = 1; i <= N; ++i){
        { //start decreasing
            int t = i - L[i];
            start_dec[i] = t;
            if(1 <= t && t <= N + 1){
                sweep[t].emplace_back(start_decreasing, i);
            }
        }

        { //stop increasing
            int t = R[i] - i;
            stop_inc[i] = t;
            if(1 <= t && t <= N + 1){
                sweep[t].emplace_back(stop_increasing, i);
            }
        }

        { //become zero
            int t = start_dec[i] + stop_inc[i] - 1;
            if(1 <= t && t <= N + 1){
                sweep[t].emplace_back(become_zero, i);
            }
        }
    }

    int B = 32 - __builtin_clz(N);
    vector<vector<int>> prev(B, vector<int>(N + 1, -1));

    for(int i = 1; i <= N; ++i){
        prev[0][i] = L[i];
    }

    for(int i = 1; i < B; ++i){
        for(int j = 1; j <= N; ++j){
            prev[i][j] = prev[i - 1][j];
            if(prev[i][j] > 0) prev[i][j] = prev[i - 1][prev[i][j]];
        }
    }

    auto find_exact_value = [&](int i, int t) -> int{
        int cur = i;
        for(int b = B - 1; b >= 0; --b){
            if(prev[b][cur] > 0 && i - prev[b][cur] <= t){
                cur = prev[b][cur];
            }
        }
        return cur;
    };

    fenwick_tree ft1(N), ft2(N), ft3(N), ft4(N);

    for(int i = 1; i <= N; ++i){
        ft1.update(i, S[i]);
    }

    vector<long long> answer(Q);
    for(int t = 1; t <= N + 1; ++t){
        auto calculate_prefix = [&](int r) -> long long{
            int pos = find_exact_value(r, t - 1);

            long long A = ft1.query(pos - 1);
            long long B = ft2.query(pos - 1);
            long long C = ft3.query(pos - 1);
            long long D = ft4.query(pos - 1);

            long long sum_part = A * t + B;
            long long loss_part = D * t - C;
            int num_inc = L[pos] + t;
            long long extra_part = 1LL * S[pos] * (r - max(pos, num_inc) + 1);
            return sum_part - loss_part + extra_part;
        };

        for(auto [l, r, i] : queries[t]){
            answer[i] = calculate_prefix(r) - (l > 1 ? calculate_prefix(l - 1) : 0);
        }

        for(auto [e, i] : sweep[t]){
            if(e == start_decreasing){
                ft3.update(i, 1LL * S[i] * start_dec[i]);
                ft4.update(i, S[i]);
            } else if(e == stop_increasing){
                ft1.update(i, -S[i]);
                ft2.update(i, 1LL * S[i] * stop_inc[i]);
            } else if(e == become_zero){
                ft2.update(i, -1LL * S[i] * stop_inc[i]);
                ft3.update(i, -1LL * S[i] * start_dec[i]);
                ft4.update(i, -S[i]);
            } else assert(false);
        }
    }

    for(int i = 0; i < Q; ++i){
        cout << answer[i] << '\n';
    }

    return 0;
}

Compilation message

ho_t5.cpp: In function 'int main()':
ho_t5.cpp:131:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  131 |         for(auto [l, r, i] : queries[t]){
      |                  ^
ho_t5.cpp:135:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  135 |         for(auto [e, i] : sweep[t]){
      |                  ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 504 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 504 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 1 ms 336 KB Output is correct
19 Correct 1 ms 336 KB Output is correct
20 Correct 1 ms 380 KB Output is correct
21 Correct 1 ms 336 KB Output is correct
22 Correct 1 ms 336 KB Output is correct
23 Correct 1 ms 336 KB Output is correct
24 Correct 1 ms 336 KB Output is correct
25 Correct 1 ms 336 KB Output is correct
26 Correct 1 ms 336 KB Output is correct
27 Correct 1 ms 336 KB Output is correct
28 Correct 1 ms 336 KB Output is correct
29 Correct 1 ms 336 KB Output is correct
30 Correct 1 ms 336 KB Output is correct
31 Correct 1 ms 336 KB Output is correct
32 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 155 ms 46636 KB Output is correct
3 Correct 155 ms 46264 KB Output is correct
4 Correct 185 ms 46352 KB Output is correct
5 Correct 177 ms 49492 KB Output is correct
6 Correct 178 ms 46520 KB Output is correct
7 Correct 175 ms 46264 KB Output is correct
8 Correct 173 ms 49596 KB Output is correct
9 Correct 165 ms 47032 KB Output is correct
10 Correct 164 ms 46008 KB Output is correct
11 Correct 161 ms 49072 KB Output is correct
12 Correct 195 ms 45932 KB Output is correct
13 Correct 200 ms 49328 KB Output is correct
14 Correct 167 ms 48812 KB Output is correct
15 Correct 188 ms 49328 KB Output is correct
16 Correct 168 ms 46520 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 257 ms 47040 KB Output is correct
3 Correct 215 ms 46264 KB Output is correct
4 Correct 205 ms 50300 KB Output is correct
5 Correct 211 ms 46776 KB Output is correct
6 Correct 209 ms 47176 KB Output is correct
7 Correct 185 ms 49840 KB Output is correct
8 Correct 220 ms 47036 KB Output is correct
9 Correct 233 ms 46596 KB Output is correct
10 Correct 222 ms 46264 KB Output is correct
11 Correct 201 ms 50352 KB Output is correct
12 Correct 181 ms 49752 KB Output is correct
13 Correct 181 ms 49840 KB Output is correct
14 Correct 222 ms 46520 KB Output is correct
15 Correct 177 ms 50104 KB Output is correct
16 Correct 170 ms 49800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 197 ms 44984 KB Output is correct
2 Correct 206 ms 45496 KB Output is correct
3 Correct 216 ms 45976 KB Output is correct
4 Correct 243 ms 44916 KB Output is correct
5 Correct 252 ms 44728 KB Output is correct
6 Correct 225 ms 45596 KB Output is correct
7 Correct 241 ms 45632 KB Output is correct
8 Correct 244 ms 46008 KB Output is correct
9 Correct 241 ms 44480 KB Output is correct
10 Correct 230 ms 45756 KB Output is correct
11 Correct 257 ms 44984 KB Output is correct
12 Correct 244 ms 45168 KB Output is correct
13 Correct 261 ms 45496 KB Output is correct
14 Correct 250 ms 44944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 504 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 504 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 1 ms 336 KB Output is correct
19 Correct 1 ms 336 KB Output is correct
20 Correct 1 ms 380 KB Output is correct
21 Correct 1 ms 336 KB Output is correct
22 Correct 1 ms 336 KB Output is correct
23 Correct 1 ms 336 KB Output is correct
24 Correct 1 ms 336 KB Output is correct
25 Correct 1 ms 336 KB Output is correct
26 Correct 1 ms 336 KB Output is correct
27 Correct 1 ms 336 KB Output is correct
28 Correct 1 ms 336 KB Output is correct
29 Correct 1 ms 336 KB Output is correct
30 Correct 1 ms 336 KB Output is correct
31 Correct 1 ms 336 KB Output is correct
32 Correct 1 ms 336 KB Output is correct
33 Correct 155 ms 46636 KB Output is correct
34 Correct 155 ms 46264 KB Output is correct
35 Correct 185 ms 46352 KB Output is correct
36 Correct 177 ms 49492 KB Output is correct
37 Correct 178 ms 46520 KB Output is correct
38 Correct 175 ms 46264 KB Output is correct
39 Correct 173 ms 49596 KB Output is correct
40 Correct 165 ms 47032 KB Output is correct
41 Correct 164 ms 46008 KB Output is correct
42 Correct 161 ms 49072 KB Output is correct
43 Correct 195 ms 45932 KB Output is correct
44 Correct 200 ms 49328 KB Output is correct
45 Correct 167 ms 48812 KB Output is correct
46 Correct 188 ms 49328 KB Output is correct
47 Correct 168 ms 46520 KB Output is correct
48 Correct 257 ms 47040 KB Output is correct
49 Correct 215 ms 46264 KB Output is correct
50 Correct 205 ms 50300 KB Output is correct
51 Correct 211 ms 46776 KB Output is correct
52 Correct 209 ms 47176 KB Output is correct
53 Correct 185 ms 49840 KB Output is correct
54 Correct 220 ms 47036 KB Output is correct
55 Correct 233 ms 46596 KB Output is correct
56 Correct 222 ms 46264 KB Output is correct
57 Correct 201 ms 50352 KB Output is correct
58 Correct 181 ms 49752 KB Output is correct
59 Correct 181 ms 49840 KB Output is correct
60 Correct 222 ms 46520 KB Output is correct
61 Correct 177 ms 50104 KB Output is correct
62 Correct 170 ms 49800 KB Output is correct
63 Correct 197 ms 44984 KB Output is correct
64 Correct 206 ms 45496 KB Output is correct
65 Correct 216 ms 45976 KB Output is correct
66 Correct 243 ms 44916 KB Output is correct
67 Correct 252 ms 44728 KB Output is correct
68 Correct 225 ms 45596 KB Output is correct
69 Correct 241 ms 45632 KB Output is correct
70 Correct 244 ms 46008 KB Output is correct
71 Correct 241 ms 44480 KB Output is correct
72 Correct 230 ms 45756 KB Output is correct
73 Correct 257 ms 44984 KB Output is correct
74 Correct 244 ms 45168 KB Output is correct
75 Correct 261 ms 45496 KB Output is correct
76 Correct 250 ms 44944 KB Output is correct
77 Correct 276 ms 47800 KB Output is correct
78 Correct 232 ms 48432 KB Output is correct
79 Correct 235 ms 50876 KB Output is correct
80 Correct 239 ms 47800 KB Output is correct
81 Correct 223 ms 47708 KB Output is correct
82 Correct 225 ms 48312 KB Output is correct
83 Correct 237 ms 48056 KB Output is correct
84 Correct 234 ms 47688 KB Output is correct
85 Correct 187 ms 50864 KB Output is correct
86 Correct 222 ms 47692 KB Output is correct
87 Correct 174 ms 50524 KB Output is correct
88 Correct 173 ms 50752 KB Output is correct
89 Correct 193 ms 46860 KB Output is correct
90 Correct 173 ms 50352 KB Output is correct
91 Correct 187 ms 47288 KB Output is correct
92 Correct 210 ms 46808 KB Output is correct
93 Correct 251 ms 47584 KB Output is correct
94 Correct 210 ms 50800 KB Output is correct
95 Correct 190 ms 50608 KB Output is correct
96 Correct 214 ms 47544 KB Output is correct
97 Correct 218 ms 47648 KB Output is correct
98 Correct 247 ms 47040 KB Output is correct
99 Correct 218 ms 47672 KB Output is correct
100 Correct 224 ms 47856 KB Output is correct
101 Correct 230 ms 47292 KB Output is correct
102 Correct 199 ms 50288 KB Output is correct