Submission #1012910

# Submission time Handle Problem Language Result Execution time Memory
1012910 2024-07-02T19:54:55 Z EJIC_B_KEDAX Fire (JOI20_ho_t5) C++17
100 / 100
254 ms 73412 KB
#include <bits/stdc++.h>
#include <immintrin.h>

using namespace std;

using ll = long long;
using ld = long double;
#define x first
#define y second
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()

mt19937 mt(123);

void solve();
void init();

int32_t main() {
#ifndef LOCAL
    cin.tie(nullptr)->sync_with_stdio(false);
#endif
    cout << fixed << setprecision(30);
    init();
    int t = 1;
    // cin >> t;
    while (t--) {
        solve();
    }
}

struct segment_tree {
    vector<ll> st;
    int sz;
    segment_tree() = default;
    segment_tree(vector<int> a) {
        sz = 1;
        while (sz < a.size()) {
            sz <<= 1;
        }
        st.resize(2 * sz, 0);
        for (int i = 0; i < a.size(); i++) {
            st[sz + i] = a[i];
        }
        for (int i = sz - 1; i > 0; i--) {
            st[i] = st[2 * i] + st[2 * i + 1];
        }
    }
    void add(int i, ll x) {
        i += sz;
        while (i) {
            st[i] += x;
            i >>= 1;
        }
    }
    ll get(int l, int r) {
        l += sz;
        r += sz;
        ll res = 0;
        while (l <= r) {
            if (l & 1) {
                res += st[l++];
            }
            if (~r & 1) {
                res += st[r--];
            }
            l >>= 1;
            r >>= 1;
        }
        return res;
    }
};

struct mseg_tree {
    segment_tree def, step;
    int now_step;
    mseg_tree() = default;
    mseg_tree(int n) {
        now_step = 0;
        def = segment_tree(vector<int>(n, 0));
        step = segment_tree(vector<int>(n, 0));
    }
    void add_def(int i, int x) {
        def.add(i, x);
    }
    void add_step(int i, int x) {
        step.add(i, x);
        def.add(i, -1ll * x * now_step);
    }
    void next_step() {
        now_step++;
    }
    void set_step(int new_step) {
        now_step = new_step;
    }
    ll get(int l, int r) {
        return def.get(l, r) + now_step * step.get(l, r);
    }
};

struct sparse_max {
    using Type = pair<int, int>;
    vector<vector<Type>> sp;
    vector<int> p;
    sparse_max(const vector<Type>& a) {
        int n = a.size(), lg = 1;
        while ((1 << lg) < n) {
            lg++;
        }
        sp.resize(lg, vector<Type>(n));
        for (int i = 0; i < n; i++) {
            sp[0][i] = a[i];
        }
        for (int l = 1; l < lg; l++) {
            for (int i = 0; i <= n - (1 << l); i++) {
                sp[l][i] = max(sp[l - 1][i], sp[l - 1][i + (1 << (l - 1))]);
            }
        }
        int nw = 0;
        p.resize(n + 1, 0);
        for (int i = 1; i <= n; i++) {
            if ((1 << nw) * 2 < i) {
                nw++;
            }
            p[i] = nw;
        }
    }
    Type get(int l, int r) {
        int lev = p[r - l + 1];
        return max(sp[lev][l], sp[lev][r - (1 << lev) + 1]);
    }
};

struct sparse_min {
    using Type = pair<int, int>;
    vector<vector<Type>> sp;
    vector<int> p;
    sparse_min(const vector<Type>& a) {
        int n = a.size(), lg = 1;
        while ((1 << lg) < n) {
            lg++;
        }
        sp.resize(lg, vector<Type>(n));
        for (int i = 0; i < n; i++) {
            sp[0][i] = a[i];
        }
        for (int l = 1; l < lg; l++) {
            for (int i = 0; i <= n - (1 << l); i++) {
                sp[l][i] = min(sp[l - 1][i], sp[l - 1][i + (1 << (l - 1))]);
            }
        }
        int nw = 0;
        p.resize(n + 1, 0);
        for (int i = 1; i <= n; i++) {
            if ((1 << nw) * 2 < i) {
                nw++;
            }
            p[i] = nw;
        }
    }
    Type get(int l, int r) {
        int lev = p[r - l + 1];
        return min(sp[lev][l], sp[lev][r - (1 << lev) + 1]);
    }
};

void init() {}

void solve() {
    int n, q;
    cin >> n >> q;
    vector<int> a(n), nxt(n), prv(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    // assert(n == 1);
    mseg_tree st(n);
    for (int i = 0; i < n; i++) {
        st.add_step(i, a[i]);
    }
    vector<pair<int, int>> stack(1, {INT32_MAX, n});
    for (int i = n - 1; i >= 0; i--) {
        while (a[i] >= stack.back().x) {
            stack.pop_back();
        }
        nxt[i] = stack.back().y;
        stack.emplace_back(a[i], i);
    }
    stack.clear(); stack.emplace_back(INT32_MAX, -1);
    for (int i = 0; i < n; i++) {
        while (a[i] > stack.back().x) {
            stack.pop_back();
        }
        prv[i] = stack.back().y;
        stack.emplace_back(a[i], i);
    }
    vector<pair<int, int>> fsp(n);
    for (int i = 0; i < n; i++) {
        fsp[i] = {a[i], -i};
    }
    sparse_max sp(fsp);
    vector<vector<pair<int, int>>> ev(n + 1);
    vector<vector<int>> qu(n + 1);
    vector<pair<int, int>> seg(q);
    vector<ll> ans(q);
    for (int i = 0; i < q; i++) {
        int t, l, r;
        cin >> t >> l >> r; l--; r--;
        qu[t].push_back(i);
        seg[i] = {l, r};
    }
    for (int i = 0; i < n; i++) {
        ev[nxt[i] - i].emplace_back(i, -a[i]);
        if (prv[i] != -1) {
            ev[i - prv[i]].emplace_back(i, -a[i]);
            ev[nxt[i] - prv[i]].emplace_back(i, a[i]);
        }
    }
    for (int t = 0; t <= n; t++) {
        for (auto [i, v] : ev[t]) {
            st.add_step(i, v);
        }
        st.next_step();
        for (int i : qu[t]) {
            auto [l, r] = seg[i];
            r++;
            ll lans = 0, rans = 0;
            {
                auto [mx, mxi] = sp.get(max(0, l - t), l);
                mxi *= -1;
                lans = st.get(mxi + 1, n - 1);
                int lst = min({mxi + t, nxt[mxi] - 1});
                lans += 1ll * (lst - l + 1) * mx;
            }
            if (r < n) {
                auto [mx, mxi] = sp.get(max(0, r - t), r);
                mxi *= -1;
                rans = st.get(mxi + 1, n - 1);
                int lst = min(mxi + t, nxt[mxi] - 1);
                rans += 1ll * (lst - r + 1) * mx;
            }
            ans[i] = lans - rans;
        }
    }
    for (ll i : ans) {
        cout << i << '\n';
    }
}

Compilation message

ho_t5.cpp: In constructor 'segment_tree::segment_tree(std::vector<int>)':
ho_t5.cpp:37:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   37 |         while (sz < a.size()) {
      |                ~~~^~~~~~~~~~
ho_t5.cpp:41:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   41 |         for (int i = 0; i < a.size(); i++) {
      |                         ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 344 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 1 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 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 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 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 344 KB Output is correct
22 Correct 1 ms 600 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 1 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 1 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 344 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 139 ms 68192 KB Output is correct
3 Correct 148 ms 67660 KB Output is correct
4 Correct 141 ms 68460 KB Output is correct
5 Correct 183 ms 69944 KB Output is correct
6 Correct 156 ms 67912 KB Output is correct
7 Correct 146 ms 68440 KB Output is correct
8 Correct 128 ms 70848 KB Output is correct
9 Correct 179 ms 69704 KB Output is correct
10 Correct 131 ms 67356 KB Output is correct
11 Correct 133 ms 69784 KB Output is correct
12 Correct 137 ms 67720 KB Output is correct
13 Correct 185 ms 69348 KB Output is correct
14 Correct 158 ms 69964 KB Output is correct
15 Correct 141 ms 69524 KB Output is correct
16 Correct 126 ms 68752 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 168 ms 70352 KB Output is correct
3 Correct 166 ms 69248 KB Output is correct
4 Correct 169 ms 72592 KB Output is correct
5 Correct 163 ms 70264 KB Output is correct
6 Correct 148 ms 70484 KB Output is correct
7 Correct 163 ms 71664 KB Output is correct
8 Correct 170 ms 69836 KB Output is correct
9 Correct 153 ms 69652 KB Output is correct
10 Correct 164 ms 69532 KB Output is correct
11 Correct 211 ms 72240 KB Output is correct
12 Correct 163 ms 71740 KB Output is correct
13 Correct 158 ms 72556 KB Output is correct
14 Correct 172 ms 69740 KB Output is correct
15 Correct 170 ms 72756 KB Output is correct
16 Correct 158 ms 72256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 200 ms 65156 KB Output is correct
2 Correct 203 ms 64880 KB Output is correct
3 Correct 254 ms 66192 KB Output is correct
4 Correct 180 ms 64444 KB Output is correct
5 Correct 210 ms 64388 KB Output is correct
6 Correct 187 ms 64868 KB Output is correct
7 Correct 187 ms 66364 KB Output is correct
8 Correct 233 ms 65244 KB Output is correct
9 Correct 212 ms 65156 KB Output is correct
10 Correct 215 ms 65376 KB Output is correct
11 Correct 202 ms 64696 KB Output is correct
12 Correct 202 ms 64912 KB Output is correct
13 Correct 208 ms 65068 KB Output is correct
14 Correct 193 ms 64808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 344 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 1 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 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 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 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 344 KB Output is correct
22 Correct 1 ms 600 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 1 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 1 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 344 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 139 ms 68192 KB Output is correct
34 Correct 148 ms 67660 KB Output is correct
35 Correct 141 ms 68460 KB Output is correct
36 Correct 183 ms 69944 KB Output is correct
37 Correct 156 ms 67912 KB Output is correct
38 Correct 146 ms 68440 KB Output is correct
39 Correct 128 ms 70848 KB Output is correct
40 Correct 179 ms 69704 KB Output is correct
41 Correct 131 ms 67356 KB Output is correct
42 Correct 133 ms 69784 KB Output is correct
43 Correct 137 ms 67720 KB Output is correct
44 Correct 185 ms 69348 KB Output is correct
45 Correct 158 ms 69964 KB Output is correct
46 Correct 141 ms 69524 KB Output is correct
47 Correct 126 ms 68752 KB Output is correct
48 Correct 168 ms 70352 KB Output is correct
49 Correct 166 ms 69248 KB Output is correct
50 Correct 169 ms 72592 KB Output is correct
51 Correct 163 ms 70264 KB Output is correct
52 Correct 148 ms 70484 KB Output is correct
53 Correct 163 ms 71664 KB Output is correct
54 Correct 170 ms 69836 KB Output is correct
55 Correct 153 ms 69652 KB Output is correct
56 Correct 164 ms 69532 KB Output is correct
57 Correct 211 ms 72240 KB Output is correct
58 Correct 163 ms 71740 KB Output is correct
59 Correct 158 ms 72556 KB Output is correct
60 Correct 172 ms 69740 KB Output is correct
61 Correct 170 ms 72756 KB Output is correct
62 Correct 158 ms 72256 KB Output is correct
63 Correct 200 ms 65156 KB Output is correct
64 Correct 203 ms 64880 KB Output is correct
65 Correct 254 ms 66192 KB Output is correct
66 Correct 180 ms 64444 KB Output is correct
67 Correct 210 ms 64388 KB Output is correct
68 Correct 187 ms 64868 KB Output is correct
69 Correct 187 ms 66364 KB Output is correct
70 Correct 233 ms 65244 KB Output is correct
71 Correct 212 ms 65156 KB Output is correct
72 Correct 215 ms 65376 KB Output is correct
73 Correct 202 ms 64696 KB Output is correct
74 Correct 202 ms 64912 KB Output is correct
75 Correct 208 ms 65068 KB Output is correct
76 Correct 193 ms 64808 KB Output is correct
77 Correct 199 ms 71692 KB Output is correct
78 Correct 167 ms 71748 KB Output is correct
79 Correct 178 ms 73412 KB Output is correct
80 Correct 184 ms 71284 KB Output is correct
81 Correct 224 ms 71072 KB Output is correct
82 Correct 192 ms 72528 KB Output is correct
83 Correct 202 ms 71100 KB Output is correct
84 Correct 189 ms 70164 KB Output is correct
85 Correct 215 ms 72876 KB Output is correct
86 Correct 190 ms 70788 KB Output is correct
87 Correct 208 ms 71824 KB Output is correct
88 Correct 185 ms 71200 KB Output is correct
89 Correct 140 ms 68136 KB Output is correct
90 Correct 146 ms 70260 KB Output is correct
91 Correct 139 ms 68528 KB Output is correct
92 Correct 173 ms 67896 KB Output is correct
93 Correct 168 ms 69468 KB Output is correct
94 Correct 166 ms 71112 KB Output is correct
95 Correct 152 ms 71008 KB Output is correct
96 Correct 140 ms 69452 KB Output is correct
97 Correct 157 ms 69316 KB Output is correct
98 Correct 156 ms 68340 KB Output is correct
99 Correct 168 ms 69720 KB Output is correct
100 Correct 182 ms 70100 KB Output is correct
101 Correct 148 ms 68200 KB Output is correct
102 Correct 152 ms 70472 KB Output is correct