Submission #593065

# Submission time Handle Problem Language Result Execution time Memory
593065 2022-07-10T10:37:14 Z piOOE Fire (JOI20_ho_t5) C++17
100 / 100
304 ms 30296 KB
#include <bits/stdc++.h>

using namespace std;

template<typename T>
struct Fenwick {
    vector<T> t;
    int n;

    Fenwick(int x) {
        n = x;
        t.assign(x + 1, 0);
    }

    Fenwick() = default;

    T get(int i) {
        T ans = 0;
        for (; i > 0; i -= i & -i)
            ans += t[i];
        return ans;
    }

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

    void modify(int i, T v) {
        assert(i > 0);
        for (; i <= n; i += i & -i)
            t[i] += v;
    }

};

using ll = long long;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, q;
    cin >> n >> q;
    vector<int> s(n + 1);
    vector<ll> ans(q);
    vector<array<int, 3>> queries[n + 1];
    for (int i = 1; i <= n; ++i) {
        cin >> s[i];
    }
    for (int i = 0; i < q; ++i) {
        int t, l, r;
        cin >> t >> l >> r;
        queries[l - 1].push_back({t, -1, i});
        queries[r].push_back({t, 1, i});
    }
    Fenwick<ll> add_l(n), add_q(n), sub_l(n), sub_q(n);
    vector<int> stk;
    ll pref = 0;
    for (int i = 1; i <= n; ++i) {
        auto upd = [&](int len, ll k) {
            add_l.modify(i - len, k);
            add_q.modify(i - len, k * (n - i + len));
            if (len > 1) {
                sub_l.modify(len - 1, k);
                sub_q.modify(len - 1, (len - 1) * k);
            }
        };
        while (!stk.empty() && s[stk.back()] <= s[i]) {
            int b = stk.back();
            stk.pop_back();
            if (!stk.empty()) {
                upd(i - stk.back(), s[b] - s[stk.back()]);
            }
        }
        if (!stk.empty()) {
            upd(i - stk.back(), s[stk.back()] - s[i]);
        }
        stk.push_back(i);
        pref += s[i];
        for (auto [len, k, idx]: queries[i]) {
            ll sum = pref;
            sum += add_q.get(i - len + 1, i + 1) - (n - i) * (add_l.get(i - len + 1, i + 1));
            sum += len * (add_l.get(i - len) - (sub_l.get(n) - sub_l.get(len)));
            sum -= sub_q.get(len);
            ans[idx] += sum * k;
        }
    }
    for (int i = 0; i < q; ++i) {
        cout << ans[i] << '\n';
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 320 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 320 KB Output is correct
7 Correct 1 ms 316 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 324 KB Output is correct
13 Correct 0 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 0 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 0 ms 352 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 0 ms 320 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 324 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 246 ms 29644 KB Output is correct
3 Correct 223 ms 29520 KB Output is correct
4 Correct 233 ms 29820 KB Output is correct
5 Correct 235 ms 29960 KB Output is correct
6 Correct 238 ms 29396 KB Output is correct
7 Correct 228 ms 30024 KB Output is correct
8 Correct 231 ms 29900 KB Output is correct
9 Correct 249 ms 30032 KB Output is correct
10 Correct 227 ms 29520 KB Output is correct
11 Correct 226 ms 30172 KB Output is correct
12 Correct 229 ms 29308 KB Output is correct
13 Correct 244 ms 29644 KB Output is correct
14 Correct 223 ms 29428 KB Output is correct
15 Correct 218 ms 29604 KB Output is correct
16 Correct 226 ms 29456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 251 ms 28944 KB Output is correct
3 Correct 237 ms 28440 KB Output is correct
4 Correct 261 ms 29272 KB Output is correct
5 Correct 242 ms 28356 KB Output is correct
6 Correct 256 ms 28748 KB Output is correct
7 Correct 234 ms 28712 KB Output is correct
8 Correct 247 ms 29004 KB Output is correct
9 Correct 244 ms 28600 KB Output is correct
10 Correct 238 ms 28280 KB Output is correct
11 Correct 253 ms 29192 KB Output is correct
12 Correct 235 ms 28620 KB Output is correct
13 Correct 249 ms 29160 KB Output is correct
14 Correct 261 ms 28620 KB Output is correct
15 Correct 237 ms 28904 KB Output is correct
16 Correct 233 ms 28620 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 226 ms 26180 KB Output is correct
2 Correct 224 ms 26192 KB Output is correct
3 Correct 217 ms 26856 KB Output is correct
4 Correct 219 ms 26444 KB Output is correct
5 Correct 237 ms 26444 KB Output is correct
6 Correct 209 ms 26188 KB Output is correct
7 Correct 243 ms 26552 KB Output is correct
8 Correct 224 ms 26548 KB Output is correct
9 Correct 226 ms 26412 KB Output is correct
10 Correct 230 ms 26428 KB Output is correct
11 Correct 217 ms 26660 KB Output is correct
12 Correct 229 ms 26820 KB Output is correct
13 Correct 234 ms 26476 KB Output is correct
14 Correct 221 ms 26572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 320 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 320 KB Output is correct
7 Correct 1 ms 316 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 324 KB Output is correct
13 Correct 0 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 0 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 0 ms 352 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 0 ms 320 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 324 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 246 ms 29644 KB Output is correct
34 Correct 223 ms 29520 KB Output is correct
35 Correct 233 ms 29820 KB Output is correct
36 Correct 235 ms 29960 KB Output is correct
37 Correct 238 ms 29396 KB Output is correct
38 Correct 228 ms 30024 KB Output is correct
39 Correct 231 ms 29900 KB Output is correct
40 Correct 249 ms 30032 KB Output is correct
41 Correct 227 ms 29520 KB Output is correct
42 Correct 226 ms 30172 KB Output is correct
43 Correct 229 ms 29308 KB Output is correct
44 Correct 244 ms 29644 KB Output is correct
45 Correct 223 ms 29428 KB Output is correct
46 Correct 218 ms 29604 KB Output is correct
47 Correct 226 ms 29456 KB Output is correct
48 Correct 251 ms 28944 KB Output is correct
49 Correct 237 ms 28440 KB Output is correct
50 Correct 261 ms 29272 KB Output is correct
51 Correct 242 ms 28356 KB Output is correct
52 Correct 256 ms 28748 KB Output is correct
53 Correct 234 ms 28712 KB Output is correct
54 Correct 247 ms 29004 KB Output is correct
55 Correct 244 ms 28600 KB Output is correct
56 Correct 238 ms 28280 KB Output is correct
57 Correct 253 ms 29192 KB Output is correct
58 Correct 235 ms 28620 KB Output is correct
59 Correct 249 ms 29160 KB Output is correct
60 Correct 261 ms 28620 KB Output is correct
61 Correct 237 ms 28904 KB Output is correct
62 Correct 233 ms 28620 KB Output is correct
63 Correct 226 ms 26180 KB Output is correct
64 Correct 224 ms 26192 KB Output is correct
65 Correct 217 ms 26856 KB Output is correct
66 Correct 219 ms 26444 KB Output is correct
67 Correct 237 ms 26444 KB Output is correct
68 Correct 209 ms 26188 KB Output is correct
69 Correct 243 ms 26552 KB Output is correct
70 Correct 224 ms 26548 KB Output is correct
71 Correct 226 ms 26412 KB Output is correct
72 Correct 230 ms 26428 KB Output is correct
73 Correct 217 ms 26660 KB Output is correct
74 Correct 229 ms 26820 KB Output is correct
75 Correct 234 ms 26476 KB Output is correct
76 Correct 221 ms 26572 KB Output is correct
77 Correct 263 ms 29664 KB Output is correct
78 Correct 260 ms 30144 KB Output is correct
79 Correct 288 ms 29792 KB Output is correct
80 Correct 255 ms 29644 KB Output is correct
81 Correct 259 ms 29392 KB Output is correct
82 Correct 268 ms 29668 KB Output is correct
83 Correct 274 ms 29648 KB Output is correct
84 Correct 254 ms 29608 KB Output is correct
85 Correct 268 ms 29664 KB Output is correct
86 Correct 304 ms 29756 KB Output is correct
87 Correct 270 ms 30192 KB Output is correct
88 Correct 264 ms 29920 KB Output is correct
89 Correct 253 ms 29460 KB Output is correct
90 Correct 243 ms 29752 KB Output is correct
91 Correct 253 ms 29400 KB Output is correct
92 Correct 250 ms 29260 KB Output is correct
93 Correct 277 ms 29568 KB Output is correct
94 Correct 281 ms 30296 KB Output is correct
95 Correct 269 ms 30156 KB Output is correct
96 Correct 288 ms 29728 KB Output is correct
97 Correct 282 ms 29760 KB Output is correct
98 Correct 243 ms 29112 KB Output is correct
99 Correct 267 ms 29220 KB Output is correct
100 Correct 295 ms 29508 KB Output is correct
101 Correct 278 ms 29200 KB Output is correct
102 Correct 269 ms 29868 KB Output is correct