답안 #597665

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
597665 2022-07-16T13:53:15 Z piOOE Dungeon 3 (JOI21_ho_t5) C++17
100 / 100
1209 ms 164120 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;

#define int long long

signed 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);
    partial_sum(a.begin(), a.end(), pref.begin() + 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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 2384 KB Output is correct
2 Correct 9 ms 2384 KB Output is correct
3 Correct 7 ms 1940 KB Output is correct
4 Correct 10 ms 2436 KB Output is correct
5 Correct 10 ms 2384 KB Output is correct
6 Correct 7 ms 2052 KB Output is correct
7 Correct 10 ms 2368 KB Output is correct
8 Correct 9 ms 2420 KB Output is correct
9 Correct 9 ms 1956 KB Output is correct
10 Correct 11 ms 2440 KB Output is correct
11 Correct 9 ms 2424 KB Output is correct
12 Correct 7 ms 2004 KB Output is correct
13 Correct 10 ms 2384 KB Output is correct
14 Correct 8 ms 2388 KB Output is correct
15 Correct 8 ms 2364 KB Output is correct
16 Correct 9 ms 2384 KB Output is correct
17 Correct 8 ms 1876 KB Output is correct
18 Correct 10 ms 2132 KB Output is correct
19 Correct 6 ms 1620 KB Output is correct
20 Correct 7 ms 2000 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 120 ms 34556 KB Output is correct
2 Correct 159 ms 36608 KB Output is correct
3 Correct 177 ms 37428 KB Output is correct
4 Correct 121 ms 29452 KB Output is correct
5 Correct 201 ms 36616 KB Output is correct
6 Correct 562 ms 142952 KB Output is correct
7 Correct 701 ms 139204 KB Output is correct
8 Correct 781 ms 150172 KB Output is correct
9 Correct 587 ms 119632 KB Output is correct
10 Correct 624 ms 135704 KB Output is correct
11 Correct 632 ms 133072 KB Output is correct
12 Correct 471 ms 107828 KB Output is correct
13 Correct 652 ms 120968 KB Output is correct
14 Correct 717 ms 120660 KB Output is correct
15 Correct 463 ms 132336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1159 ms 137792 KB Output is correct
2 Correct 874 ms 155972 KB Output is correct
3 Correct 732 ms 132456 KB Output is correct
4 Correct 999 ms 153900 KB Output is correct
5 Correct 745 ms 155380 KB Output is correct
6 Correct 915 ms 150288 KB Output is correct
7 Correct 824 ms 125680 KB Output is correct
8 Correct 713 ms 140576 KB Output is correct
9 Correct 513 ms 112676 KB Output is correct
10 Correct 528 ms 133732 KB Output is correct
11 Correct 812 ms 150952 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 2384 KB Output is correct
2 Correct 9 ms 2384 KB Output is correct
3 Correct 7 ms 1940 KB Output is correct
4 Correct 10 ms 2436 KB Output is correct
5 Correct 10 ms 2384 KB Output is correct
6 Correct 7 ms 2052 KB Output is correct
7 Correct 10 ms 2368 KB Output is correct
8 Correct 9 ms 2420 KB Output is correct
9 Correct 9 ms 1956 KB Output is correct
10 Correct 11 ms 2440 KB Output is correct
11 Correct 9 ms 2424 KB Output is correct
12 Correct 7 ms 2004 KB Output is correct
13 Correct 10 ms 2384 KB Output is correct
14 Correct 8 ms 2388 KB Output is correct
15 Correct 8 ms 2364 KB Output is correct
16 Correct 9 ms 2384 KB Output is correct
17 Correct 8 ms 1876 KB Output is correct
18 Correct 10 ms 2132 KB Output is correct
19 Correct 6 ms 1620 KB Output is correct
20 Correct 7 ms 2000 KB Output is correct
21 Correct 120 ms 34556 KB Output is correct
22 Correct 159 ms 36608 KB Output is correct
23 Correct 177 ms 37428 KB Output is correct
24 Correct 121 ms 29452 KB Output is correct
25 Correct 201 ms 36616 KB Output is correct
26 Correct 562 ms 142952 KB Output is correct
27 Correct 701 ms 139204 KB Output is correct
28 Correct 781 ms 150172 KB Output is correct
29 Correct 587 ms 119632 KB Output is correct
30 Correct 624 ms 135704 KB Output is correct
31 Correct 632 ms 133072 KB Output is correct
32 Correct 471 ms 107828 KB Output is correct
33 Correct 652 ms 120968 KB Output is correct
34 Correct 717 ms 120660 KB Output is correct
35 Correct 463 ms 132336 KB Output is correct
36 Correct 1159 ms 137792 KB Output is correct
37 Correct 874 ms 155972 KB Output is correct
38 Correct 732 ms 132456 KB Output is correct
39 Correct 999 ms 153900 KB Output is correct
40 Correct 745 ms 155380 KB Output is correct
41 Correct 915 ms 150288 KB Output is correct
42 Correct 824 ms 125680 KB Output is correct
43 Correct 713 ms 140576 KB Output is correct
44 Correct 513 ms 112676 KB Output is correct
45 Correct 528 ms 133732 KB Output is correct
46 Correct 812 ms 150952 KB Output is correct
47 Correct 1135 ms 132560 KB Output is correct
48 Correct 1183 ms 164120 KB Output is correct
49 Correct 832 ms 120688 KB Output is correct
50 Correct 1209 ms 157168 KB Output is correct
51 Correct 840 ms 143648 KB Output is correct
52 Correct 1119 ms 147992 KB Output is correct
53 Correct 965 ms 128112 KB Output is correct
54 Correct 996 ms 145168 KB Output is correct
55 Correct 647 ms 115980 KB Output is correct
56 Correct 518 ms 133644 KB Output is correct
57 Correct 958 ms 152052 KB Output is correct