답안 #1038486

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1038486 2024-07-29T21:01:17 Z Wael Fire (JOI20_ho_t5) C++17
100 / 100
241 ms 73128 KB
#include <bits/stdc++.h>
using namespace std;

using i64 = long long;

struct Fenwick {
    int n;
    vector<i64> val, inc;
    Fenwick(int n) : n(n) {
        val.assign(n, 0);
        inc.assign(n, 0);
    }

    void update(int i, int w, int v, int t) {
        for (; i < n; i |= (i + 1)) {
            val[i] += 1LL * t * (w - v);
            inc[i] += (v - w);
        }
    }

    i64 query(int i, int t) {
        i64 res = 0;
        for (; i >= 0; i = (i & (i + 1)) - 1) {
            res += val[i] + inc[i] * t;
        }
        return res;
    }

    i64 get(int l, int r, int t) {
        return query(r, t) - query(l - 1, t);
    }
};

void solve() {    
    int n, q;
    cin >> n >> q;

    vector<int> a(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }

    int const lg = 20;
    vector<i64> pref(n);
    vector<vector<pair<int, int>>> sparse(n, vector<pair<int, int>>(lg));
    for (int i = 0; i < n; ++i) {
        sparse[i][0] = {a[i], i};
        pref[i] = a[i] + (i ? pref[i - 1] : 0);
    }

    for (int j = 1; j < lg; ++j) {
        for (int i = 0; i < n; ++i) {
            int ni = min(n - 1, i + (1 << (j - 1)));
            sparse[i][j] = max(sparse[i][j - 1], sparse[ni][j - 1]);
        }
    }

    vector<int> t(q), l(q), r(q);
    for (int i = 0; i < q; ++i) {
        cin >> t[i] >> l[i] >> r[i];
        --l[i], --r[i];
        t[i] = min(t[i], n - 1);
    }

    vector<vector<int>> queries(n);
    for (int i = 0; i < q; ++i) {
        queries[t[i]].push_back(i);
    }

    vector<int> stk;
    vector<vector<int>> update(n);
    for (int i = n - 1; i >= 0; --i) {
        while (stk.size() && a[i] > a[stk.back()]) {
            update[stk.back() - i - 1].push_back(i);
            stk.pop_back();
        }
        int nxt = (stk.size() ? stk.back() : n);
        update[nxt - i - 1].push_back(i);
        stk.push_back(i);
    }

    vector<i64> ans(q);
    Fenwick bit(n);

    auto query = [&](int l, int r, int t) -> i64 {
        if (l > r) {
            return 0;
        }
        i64 res = 0;
        t = min(t, r);
        int d = __lg(t + 1);
        auto [mx, index] = max(sparse[r - t][d], sparse[r - (1 << d) + 1][d]);
        res = 1LL * mx * (r - index + 1);
        res += bit.get(0, index - 1, t);
        res += (index ? pref[index - 1] : 0);
        return res;
    };

    vector<int> was(n);
    for (int t = 0; t < n; ++t) {
        for (auto i : update[t]) {
            int dif = (i + t + 1 < n && a[i + t + 1] < a[i] ? a[i] - a[i + t + 1] : 0);
            bit.update(i, was[i], dif, t);
            was[i] = dif;
        }

        for (auto i : queries[t]) {
            ans[i] = query(0, r[i], t) - query(0, l[i] - 1, t);
        }
    }

    for (int i = 0; i < q; ++i) {
        cout << ans[i] << "\n";
    }
}

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

    int t = 1;
    //cin >> t;

    while (t--) {
        solve();
    }

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 460 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 468 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 168 ms 63596 KB Output is correct
3 Correct 142 ms 68560 KB Output is correct
4 Correct 166 ms 68896 KB Output is correct
5 Correct 174 ms 70092 KB Output is correct
6 Correct 175 ms 69048 KB Output is correct
7 Correct 166 ms 69580 KB Output is correct
8 Correct 175 ms 70680 KB Output is correct
9 Correct 166 ms 69836 KB Output is correct
10 Correct 160 ms 68216 KB Output is correct
11 Correct 150 ms 70084 KB Output is correct
12 Correct 162 ms 68004 KB Output is correct
13 Correct 167 ms 69832 KB Output is correct
14 Correct 155 ms 69556 KB Output is correct
15 Correct 156 ms 69832 KB Output is correct
16 Correct 163 ms 69220 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 162 ms 64964 KB Output is correct
3 Correct 184 ms 69460 KB Output is correct
4 Correct 182 ms 72644 KB Output is correct
5 Correct 165 ms 70088 KB Output is correct
6 Correct 171 ms 70856 KB Output is correct
7 Correct 181 ms 71496 KB Output is correct
8 Correct 168 ms 70344 KB Output is correct
9 Correct 196 ms 69828 KB Output is correct
10 Correct 182 ms 69060 KB Output is correct
11 Correct 172 ms 72400 KB Output is correct
12 Correct 173 ms 71496 KB Output is correct
13 Correct 174 ms 71876 KB Output is correct
14 Correct 169 ms 69828 KB Output is correct
15 Correct 192 ms 72292 KB Output is correct
16 Correct 173 ms 71800 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 235 ms 63500 KB Output is correct
2 Correct 181 ms 63952 KB Output is correct
3 Correct 186 ms 65996 KB Output is correct
4 Correct 199 ms 63688 KB Output is correct
5 Correct 189 ms 63976 KB Output is correct
6 Correct 196 ms 64320 KB Output is correct
7 Correct 200 ms 65812 KB Output is correct
8 Correct 195 ms 65100 KB Output is correct
9 Correct 204 ms 64200 KB Output is correct
10 Correct 196 ms 64692 KB Output is correct
11 Correct 190 ms 64144 KB Output is correct
12 Correct 189 ms 64628 KB Output is correct
13 Correct 221 ms 64064 KB Output is correct
14 Correct 196 ms 64184 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 460 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 468 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 168 ms 63596 KB Output is correct
34 Correct 142 ms 68560 KB Output is correct
35 Correct 166 ms 68896 KB Output is correct
36 Correct 174 ms 70092 KB Output is correct
37 Correct 175 ms 69048 KB Output is correct
38 Correct 166 ms 69580 KB Output is correct
39 Correct 175 ms 70680 KB Output is correct
40 Correct 166 ms 69836 KB Output is correct
41 Correct 160 ms 68216 KB Output is correct
42 Correct 150 ms 70084 KB Output is correct
43 Correct 162 ms 68004 KB Output is correct
44 Correct 167 ms 69832 KB Output is correct
45 Correct 155 ms 69556 KB Output is correct
46 Correct 156 ms 69832 KB Output is correct
47 Correct 163 ms 69220 KB Output is correct
48 Correct 162 ms 64964 KB Output is correct
49 Correct 184 ms 69460 KB Output is correct
50 Correct 182 ms 72644 KB Output is correct
51 Correct 165 ms 70088 KB Output is correct
52 Correct 171 ms 70856 KB Output is correct
53 Correct 181 ms 71496 KB Output is correct
54 Correct 168 ms 70344 KB Output is correct
55 Correct 196 ms 69828 KB Output is correct
56 Correct 182 ms 69060 KB Output is correct
57 Correct 172 ms 72400 KB Output is correct
58 Correct 173 ms 71496 KB Output is correct
59 Correct 174 ms 71876 KB Output is correct
60 Correct 169 ms 69828 KB Output is correct
61 Correct 192 ms 72292 KB Output is correct
62 Correct 173 ms 71800 KB Output is correct
63 Correct 235 ms 63500 KB Output is correct
64 Correct 181 ms 63952 KB Output is correct
65 Correct 186 ms 65996 KB Output is correct
66 Correct 199 ms 63688 KB Output is correct
67 Correct 189 ms 63976 KB Output is correct
68 Correct 196 ms 64320 KB Output is correct
69 Correct 200 ms 65812 KB Output is correct
70 Correct 195 ms 65100 KB Output is correct
71 Correct 204 ms 64200 KB Output is correct
72 Correct 196 ms 64692 KB Output is correct
73 Correct 190 ms 64144 KB Output is correct
74 Correct 189 ms 64628 KB Output is correct
75 Correct 221 ms 64064 KB Output is correct
76 Correct 196 ms 64184 KB Output is correct
77 Correct 189 ms 71368 KB Output is correct
78 Correct 197 ms 72444 KB Output is correct
79 Correct 190 ms 72980 KB Output is correct
80 Correct 241 ms 71260 KB Output is correct
81 Correct 200 ms 71264 KB Output is correct
82 Correct 191 ms 72176 KB Output is correct
83 Correct 202 ms 71728 KB Output is correct
84 Correct 213 ms 70596 KB Output is correct
85 Correct 208 ms 73128 KB Output is correct
86 Correct 193 ms 71112 KB Output is correct
87 Correct 162 ms 70844 KB Output is correct
88 Correct 173 ms 70856 KB Output is correct
89 Correct 162 ms 68292 KB Output is correct
90 Correct 165 ms 70444 KB Output is correct
91 Correct 160 ms 68932 KB Output is correct
92 Correct 170 ms 68292 KB Output is correct
93 Correct 154 ms 69448 KB Output is correct
94 Correct 169 ms 71364 KB Output is correct
95 Correct 165 ms 70856 KB Output is correct
96 Correct 162 ms 69460 KB Output is correct
97 Correct 155 ms 69316 KB Output is correct
98 Correct 192 ms 68428 KB Output is correct
99 Correct 218 ms 69428 KB Output is correct
100 Correct 205 ms 69828 KB Output is correct
101 Correct 193 ms 68772 KB Output is correct
102 Correct 199 ms 70460 KB Output is correct