Submission #1038429

# Submission time Handle Problem Language Result Execution time Memory
1038429 2024-07-29T19:18:56 Z Wael Fire (JOI20_ho_t5) C++17
100 / 100
312 ms 81596 KB
#include <bits/stdc++.h>
using namespace std;

using i64 = long long;

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

    void update(int i, int v, int t) {
        update(i, v, t, 0, 0, n - 1);
    }
    void update(int i, int v, int t, int x, int lx, int rx) {
        if (lx == rx) {
            val[x] += inc[x] * t;
            inc[x] = v;
            val[x] -= inc[x] * t;
            return;
        }
        int mid = (lx + rx) / 2;
        if (i <= mid) {
            update(i, v, t, 2 * x + 1, lx, mid); 
        } else {
            update(i, v, t, 2 * x + 2, mid + 1, rx);
        }
        val[x] = val[2 * x + 1] + val[2 * x + 2];
        inc[x] = inc[2 * x + 1] + inc[2 * x + 2];
    }

    i64 get(int l, int r, int t) {
        return get(l, r, t, 0, 0, n - 1);
    }
    i64 get(int l, int r, int t, int x, int lx, int rx) {
        if (lx > r || rx < l) {
            return 0;
        }
        if (l <= lx && rx <= r) {
            return val[x] + inc[x] * t;
        }
        int mid = (lx + rx) / 2;
        return get(l, r, t, 2 * x + 1, lx, mid) + get(l, r, t, 2 * x + 2, mid + 1, rx);
    }
};

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);
    SegmentTree st(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 += st.get(0, index - 1, t);
        res += (index ? pref[index - 1] : 0);
        return res;
    };

    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);
            st.update(i, dif, t);
        }

        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;
}
# Verdict Execution time Memory Grader output
1 Correct 1 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 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 344 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 1 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 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 1 ms 344 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 1 ms 344 KB Output is correct
24 Correct 1 ms 344 KB Output is correct
25 Correct 1 ms 452 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 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 227 ms 77516 KB Output is correct
3 Correct 192 ms 76932 KB Output is correct
4 Correct 235 ms 77340 KB Output is correct
5 Correct 237 ms 78672 KB Output is correct
6 Correct 213 ms 77372 KB Output is correct
7 Correct 250 ms 78164 KB Output is correct
8 Correct 218 ms 79120 KB Output is correct
9 Correct 213 ms 78284 KB Output is correct
10 Correct 208 ms 76304 KB Output is correct
11 Correct 201 ms 78536 KB Output is correct
12 Correct 193 ms 76196 KB Output is correct
13 Correct 227 ms 78260 KB Output is correct
14 Correct 235 ms 78032 KB Output is correct
15 Correct 224 ms 78560 KB Output is correct
16 Correct 198 ms 77448 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 272 ms 78904 KB Output is correct
3 Correct 243 ms 77640 KB Output is correct
4 Correct 243 ms 81116 KB Output is correct
5 Correct 240 ms 78372 KB Output is correct
6 Correct 227 ms 79304 KB Output is correct
7 Correct 249 ms 79912 KB Output is correct
8 Correct 226 ms 78788 KB Output is correct
9 Correct 263 ms 77856 KB Output is correct
10 Correct 238 ms 77256 KB Output is correct
11 Correct 225 ms 80836 KB Output is correct
12 Correct 225 ms 80200 KB Output is correct
13 Correct 254 ms 80608 KB Output is correct
14 Correct 231 ms 78276 KB Output is correct
15 Correct 225 ms 80964 KB Output is correct
16 Correct 224 ms 80324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 262 ms 75792 KB Output is correct
2 Correct 280 ms 76304 KB Output is correct
3 Correct 299 ms 78732 KB Output is correct
4 Correct 280 ms 76012 KB Output is correct
5 Correct 270 ms 76488 KB Output is correct
6 Correct 292 ms 76608 KB Output is correct
7 Correct 275 ms 78276 KB Output is correct
8 Correct 289 ms 77520 KB Output is correct
9 Correct 284 ms 76488 KB Output is correct
10 Correct 281 ms 77048 KB Output is correct
11 Correct 288 ms 76480 KB Output is correct
12 Correct 312 ms 77272 KB Output is correct
13 Correct 294 ms 76464 KB Output is correct
14 Correct 277 ms 76868 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 344 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 1 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 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 1 ms 344 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 1 ms 344 KB Output is correct
24 Correct 1 ms 344 KB Output is correct
25 Correct 1 ms 452 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 344 KB Output is correct
33 Correct 227 ms 77516 KB Output is correct
34 Correct 192 ms 76932 KB Output is correct
35 Correct 235 ms 77340 KB Output is correct
36 Correct 237 ms 78672 KB Output is correct
37 Correct 213 ms 77372 KB Output is correct
38 Correct 250 ms 78164 KB Output is correct
39 Correct 218 ms 79120 KB Output is correct
40 Correct 213 ms 78284 KB Output is correct
41 Correct 208 ms 76304 KB Output is correct
42 Correct 201 ms 78536 KB Output is correct
43 Correct 193 ms 76196 KB Output is correct
44 Correct 227 ms 78260 KB Output is correct
45 Correct 235 ms 78032 KB Output is correct
46 Correct 224 ms 78560 KB Output is correct
47 Correct 198 ms 77448 KB Output is correct
48 Correct 272 ms 78904 KB Output is correct
49 Correct 243 ms 77640 KB Output is correct
50 Correct 243 ms 81116 KB Output is correct
51 Correct 240 ms 78372 KB Output is correct
52 Correct 227 ms 79304 KB Output is correct
53 Correct 249 ms 79912 KB Output is correct
54 Correct 226 ms 78788 KB Output is correct
55 Correct 263 ms 77856 KB Output is correct
56 Correct 238 ms 77256 KB Output is correct
57 Correct 225 ms 80836 KB Output is correct
58 Correct 225 ms 80200 KB Output is correct
59 Correct 254 ms 80608 KB Output is correct
60 Correct 231 ms 78276 KB Output is correct
61 Correct 225 ms 80964 KB Output is correct
62 Correct 224 ms 80324 KB Output is correct
63 Correct 262 ms 75792 KB Output is correct
64 Correct 280 ms 76304 KB Output is correct
65 Correct 299 ms 78732 KB Output is correct
66 Correct 280 ms 76012 KB Output is correct
67 Correct 270 ms 76488 KB Output is correct
68 Correct 292 ms 76608 KB Output is correct
69 Correct 275 ms 78276 KB Output is correct
70 Correct 289 ms 77520 KB Output is correct
71 Correct 284 ms 76488 KB Output is correct
72 Correct 281 ms 77048 KB Output is correct
73 Correct 288 ms 76480 KB Output is correct
74 Correct 312 ms 77272 KB Output is correct
75 Correct 294 ms 76464 KB Output is correct
76 Correct 277 ms 76868 KB Output is correct
77 Correct 228 ms 79816 KB Output is correct
78 Correct 261 ms 80836 KB Output is correct
79 Correct 225 ms 81596 KB Output is correct
80 Correct 275 ms 79556 KB Output is correct
81 Correct 247 ms 79300 KB Output is correct
82 Correct 274 ms 80580 KB Output is correct
83 Correct 222 ms 80072 KB Output is correct
84 Correct 254 ms 78880 KB Output is correct
85 Correct 265 ms 81308 KB Output is correct
86 Correct 252 ms 79300 KB Output is correct
87 Correct 192 ms 79336 KB Output is correct
88 Correct 200 ms 79556 KB Output is correct
89 Correct 233 ms 76572 KB Output is correct
90 Correct 200 ms 79048 KB Output is correct
91 Correct 224 ms 77348 KB Output is correct
92 Correct 192 ms 76484 KB Output is correct
93 Correct 224 ms 77616 KB Output is correct
94 Correct 208 ms 79920 KB Output is correct
95 Correct 225 ms 79448 KB Output is correct
96 Correct 192 ms 77868 KB Output is correct
97 Correct 204 ms 77880 KB Output is correct
98 Correct 222 ms 76704 KB Output is correct
99 Correct 228 ms 77852 KB Output is correct
100 Correct 249 ms 78280 KB Output is correct
101 Correct 244 ms 77252 KB Output is correct
102 Correct 228 ms 78908 KB Output is correct