Submission #597668

# Submission time Handle Problem Language Result Execution time Memory
597668 2022-07-16T13:54:25 Z piOOE Dungeon 3 (JOI21_ho_t5) C++17
100 / 100
1034 ms 129128 KB
#include <bits/stdc++.h>

using namespace std;

struct segtree {
    segtree *left, *right;
    long long sum, lazy, count;

    segtree(int l, int r, vector<long long>& a) {
        lazy = 0;
        if (l + 1 == r) {
            count = a[l + 1] - a[l];
            sum = 0;
        } else {
            int m = (l + r) / 2;
            left = new segtree(l, m, a);
            right = new segtree(m, r, a);
            pull();
        }
    }

    void apply(long long x) {
        sum += count * x, lazy += x;
    }

    void pull() {
        sum = left->sum + right->sum;
        count = left->count + right->count;
    }

    void push() {
        if (lazy != 0) {
            left->apply(lazy);
            right->apply(lazy);
            lazy = 0;
        }
    }

    void modify(int a, int b, long long x, int l, int r) {
        if (a == b || b <= l || r <= a) {
            return;
        } else if (a <= l && r <= b) {
            apply(x);
        } else {
            push();
            int m = (l + r) / 2;
            left->modify(a, b, x, l, m);
            right->modify(a, b, x, m, r);
            pull();
        }
    }

    long long get(int a, int b, int l, int r) {
        if (a == b || b <= l || r <= a) {
            return 0;
        } else if (a <= l && r <= b) {
            return sum;
        } else {
            push();
            int m = (l + r) / 2;
            return left->get(a, b, l, m) + right->get(a, b, m, r);
        }
    }
};

template<typename T, class F = function<T(const T &, const T &)>>
class SparseTable {
public:
    int n;
    vector<vector<T>> st;
    F func;

    SparseTable(const vector<T> &a, const F &f) : func(f) {
        n = static_cast<int>(a.size());
        int max_log = 32 - __builtin_clz(n);
        st.resize(max_log);
        st[0] = a;
        for (int j = 1; j < max_log; ++j) {
            st[j].resize(n - (1 << j) + 1);
            for (int i = 0; i <= n - (1 << j); ++i) {
                st[j][i] = func(st[j - 1][i], st[j - 1][i + (1 << (j - 1))]);
            }
        }
    }

    SparseTable() = default;

    T get(int L, int R) const {
        assert(0 <= L && L < R && R <= n);
        int lg = __lg(R - L);
        return func(st[lg][L], st[lg][R - (1 << lg)]);
    }
};

using ll = long long;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, m;
    cin >> n >> m;

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

    vector<ll> pref(n + 1);
    for (int i = 1; i <= n; ++i) {
        pref[i] = pref[i - 1] + a[i - 1];
    }//prefix sums

    vector<int> ord(n);
    iota(ord.begin(), ord.end(), 0);
    SparseTable<int> mn(ord, [&b](int i, int j) { return b[i] < b[j] ? i : j; }),
            mx(a, [](int i, int j) { return max(i, j); });

    vector<ll> ans(m);

    vector<vector<array<ll, 3>>> updates(n), queries(n);
    for (int i = 0; i < m; ++i) {
        int s, t, u;
        cin >> s >> t >> u;
        --s, --t;
        if (mx.get(s, t) <= u) {
            int _t = lower_bound(pref.begin() + s, pref.begin() + t, pref[t] - u) - pref.begin();
            _t = mn.get(_t, t);
            queries[s].push_back({i, 1, u});
            queries[_t].push_back({i, -1, u});
            ans[i] = (pref[t] - pref[_t]) * (ll) b[_t];
        } else {
            ans[i] = -1;
        }
    }

    vector<int> R(n, n), L(n, -1), stk;
    for (int i = 0; i < n; ++i) {
        while (!stk.empty() && b[stk.back()] > b[i]) {
            R[stk.back()] = i;
            stk.pop_back();
        }
        L[i] = stk.empty() ? -1 : stk.back();
        stk.push_back(i);
    }

    vector<ll> yy;
    for (int i = n - 1; i > -1; --i) {
        updates[i].push_back({0, pref[R[i]] - pref[i], b[i]});
        if (L[i] > -1) {
            updates[L[i]].push_back({pref[i] - pref[L[i]], pref[R[i]] - pref[L[i]], -b[i]});
        }

        for (auto [j, t, u]: queries[i]) {
            yy.push_back(u);
        }

        for (auto [l, r, val]: updates[i]) {
            yy.push_back(l);
            yy.push_back(r);
        }
    }

    sort(yy.begin(), yy.end());
    yy.resize(unique(yy.begin(), yy.end()) - yy.begin());

    auto get = [&yy](ll x) -> int {
        assert(binary_search(yy.begin(), yy.end(), x));
        return lower_bound(yy.begin(), yy.end(), x) - yy.begin();
    };

    segtree fn(0, yy.size(), yy);

    for (int i = n - 1; i > -1; --i) {
        for (auto [l, r, v]: updates[i]) {
            fn.modify(get(l), get(r), v, 0, yy.size());
        }

        for (auto [j, t, u]: queries[i]) {
            ans[j] += t * fn.get(0, get(u), 0, yy.size());
        }
    }

    for (int i = 0; i < m; ++i) {
        cout << ans[i] << '\n';
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 10 ms 2256 KB Output is correct
2 Correct 10 ms 2120 KB Output is correct
3 Correct 6 ms 1736 KB Output is correct
4 Correct 10 ms 2248 KB Output is correct
5 Correct 9 ms 2128 KB Output is correct
6 Correct 7 ms 1876 KB Output is correct
7 Correct 10 ms 2136 KB Output is correct
8 Correct 10 ms 2128 KB Output is correct
9 Correct 7 ms 1708 KB Output is correct
10 Correct 9 ms 2128 KB Output is correct
11 Correct 11 ms 2236 KB Output is correct
12 Correct 7 ms 1748 KB Output is correct
13 Correct 10 ms 2248 KB Output is correct
14 Correct 9 ms 2132 KB Output is correct
15 Correct 8 ms 2132 KB Output is correct
16 Correct 9 ms 2204 KB Output is correct
17 Correct 8 ms 1704 KB Output is correct
18 Correct 8 ms 1912 KB Output is correct
19 Correct 6 ms 1356 KB Output is correct
20 Correct 7 ms 1744 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 118 ms 28796 KB Output is correct
2 Correct 149 ms 29700 KB Output is correct
3 Correct 145 ms 30492 KB Output is correct
4 Correct 107 ms 22824 KB Output is correct
5 Correct 179 ms 29804 KB Output is correct
6 Correct 517 ms 110524 KB Output is correct
7 Correct 662 ms 107032 KB Output is correct
8 Correct 715 ms 115632 KB Output is correct
9 Correct 547 ms 86008 KB Output is correct
10 Correct 602 ms 102260 KB Output is correct
11 Correct 607 ms 100332 KB Output is correct
12 Correct 458 ms 74632 KB Output is correct
13 Correct 652 ms 87588 KB Output is correct
14 Correct 640 ms 87052 KB Output is correct
15 Correct 417 ms 98636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1034 ms 110472 KB Output is correct
2 Correct 821 ms 121704 KB Output is correct
3 Correct 696 ms 98728 KB Output is correct
4 Correct 856 ms 121300 KB Output is correct
5 Correct 651 ms 122832 KB Output is correct
6 Correct 831 ms 117596 KB Output is correct
7 Correct 673 ms 92692 KB Output is correct
8 Correct 634 ms 108056 KB Output is correct
9 Correct 479 ms 80148 KB Output is correct
10 Correct 459 ms 100156 KB Output is correct
11 Correct 671 ms 117104 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 2256 KB Output is correct
2 Correct 10 ms 2120 KB Output is correct
3 Correct 6 ms 1736 KB Output is correct
4 Correct 10 ms 2248 KB Output is correct
5 Correct 9 ms 2128 KB Output is correct
6 Correct 7 ms 1876 KB Output is correct
7 Correct 10 ms 2136 KB Output is correct
8 Correct 10 ms 2128 KB Output is correct
9 Correct 7 ms 1708 KB Output is correct
10 Correct 9 ms 2128 KB Output is correct
11 Correct 11 ms 2236 KB Output is correct
12 Correct 7 ms 1748 KB Output is correct
13 Correct 10 ms 2248 KB Output is correct
14 Correct 9 ms 2132 KB Output is correct
15 Correct 8 ms 2132 KB Output is correct
16 Correct 9 ms 2204 KB Output is correct
17 Correct 8 ms 1704 KB Output is correct
18 Correct 8 ms 1912 KB Output is correct
19 Correct 6 ms 1356 KB Output is correct
20 Correct 7 ms 1744 KB Output is correct
21 Correct 118 ms 28796 KB Output is correct
22 Correct 149 ms 29700 KB Output is correct
23 Correct 145 ms 30492 KB Output is correct
24 Correct 107 ms 22824 KB Output is correct
25 Correct 179 ms 29804 KB Output is correct
26 Correct 517 ms 110524 KB Output is correct
27 Correct 662 ms 107032 KB Output is correct
28 Correct 715 ms 115632 KB Output is correct
29 Correct 547 ms 86008 KB Output is correct
30 Correct 602 ms 102260 KB Output is correct
31 Correct 607 ms 100332 KB Output is correct
32 Correct 458 ms 74632 KB Output is correct
33 Correct 652 ms 87588 KB Output is correct
34 Correct 640 ms 87052 KB Output is correct
35 Correct 417 ms 98636 KB Output is correct
36 Correct 1034 ms 110472 KB Output is correct
37 Correct 821 ms 121704 KB Output is correct
38 Correct 696 ms 98728 KB Output is correct
39 Correct 856 ms 121300 KB Output is correct
40 Correct 651 ms 122832 KB Output is correct
41 Correct 831 ms 117596 KB Output is correct
42 Correct 673 ms 92692 KB Output is correct
43 Correct 634 ms 108056 KB Output is correct
44 Correct 479 ms 80148 KB Output is correct
45 Correct 459 ms 100156 KB Output is correct
46 Correct 671 ms 117104 KB Output is correct
47 Correct 898 ms 99028 KB Output is correct
48 Correct 989 ms 129128 KB Output is correct
49 Correct 648 ms 85956 KB Output is correct
50 Correct 1008 ms 121880 KB Output is correct
51 Correct 724 ms 109116 KB Output is correct
52 Correct 886 ms 112460 KB Output is correct
53 Correct 774 ms 92332 KB Output is correct
54 Correct 750 ms 108576 KB Output is correct
55 Correct 563 ms 79832 KB Output is correct
56 Correct 475 ms 97116 KB Output is correct
57 Correct 768 ms 115512 KB Output is correct