Submission #386578

# Submission time Handle Problem Language Result Execution time Memory
386578 2021-04-07T00:38:36 Z timmyfeng Dungeon 3 (JOI21_ho_t5) C++17
100 / 100
1242 ms 140384 KB
#include <bits/stdc++.h>
using namespace std;

const int N = 200001;
const int L = __lg(N) + 1;

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

    segtree(int l, int r, 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 update(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->update(a, b, x, l, m);
            right->update(a, b, x, m, r);
            pull();
        }
    }

    long long query(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->query(a, b, l, m) + right->query(a, b, m, r);
        }
    }
};

int max_a[L][N], b[N], nxt[N], prv[N], k;
long long a[N], ans[N], values[6 * N];
array<int, 2> min_b[L][N];

vector<array<long long, 3>> updates[N];
vector<array<int, 3>> queries[N];

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, m;
    cin >> n >> m;

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

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

    for (int i = 1; i <= __lg(n); ++i) {
        for (int j = 0; j + (1 << i) <= n; ++j) {
            max_a[i][j] = max(max_a[i - 1][j], max_a[i - 1][j + (1 << (i - 1))]);
            min_b[i][j] = min(min_b[i - 1][j], min_b[i - 1][j + (1 << (i - 1))]);
        }
    }

    partial_sum(a, a + n, a);
    rotate(a, a + n, a + n + 1);

    for (int i = 0; i < m; ++i) {
        int s, t, u;
        cin >> s >> t >> u;
        --s, --t;

        int l = __lg(t - s);

        if (max(max_a[l][s], max_a[l][t - (1 << l)]) <= u) {
            int v = lower_bound(a + s, a + t, a[t] - u) - a;
            l = __lg(t - v);
            v = min(min_b[l][v], min_b[l][t - (1 << l)])[1];
            ans[i] = (a[t] - a[v]) * b[v];
            queries[s].push_back({u, 1, i});
            queries[v].push_back({u, -1, i});
        } else {
            ans[i] = -1;
        }
    }

    vector<int> stack;
    for (int i = 0; i < n; ++i) {
        while (!stack.empty() && b[stack.back()] > b[i]) {
            nxt[stack.back()] = i;
            stack.pop_back();
        }
        prv[i] = stack.empty() ? -1 : stack.back();
        stack.push_back(i);
    }
    for (auto i : stack) {
        nxt[i] = n;
    }

    for (int i = n - 1; i >= 0; --i) {
        updates[i].push_back({0, a[nxt[i]] - a[i], b[i]});
        if (prv[i] >= 0) {
            updates[prv[i]].push_back({a[i] - a[prv[i]], a[nxt[i]] - a[prv[i]], -b[i]});
        }

        for (auto &j : updates[i]) {
            values[k++] = j[0], values[k++] = j[1];
        }

        for (auto &j : queries[i]) {
            values[k++] = j[0];
        }
    }

    sort(values, values + k);
    k = unique(values, values + k) - values;

    segtree *sum = new segtree(0, k, values);

    for (int i = n - 1; i >= 0; --i) {
        for (auto [l, r, x] : updates[i]) {
            l = lower_bound(values, values + k, l) - values;
            r = lower_bound(values, values + k, r) - values;
            sum->update(l, r, x, 0, k);
        }

        for (auto [u, t, j] : queries[i]) {
            u = lower_bound(values, values + k, u) - values;
            ans[j] += t * sum->query(0, u, 0, k);
        }
    }

    for (int i = 0; i < m; ++i) {
        cout << ans[i] << "\n";
    }
}
# Verdict Execution time Memory Grader output
1 Correct 17 ms 11648 KB Output is correct
2 Correct 16 ms 11628 KB Output is correct
3 Correct 14 ms 11116 KB Output is correct
4 Correct 18 ms 11648 KB Output is correct
5 Correct 16 ms 11648 KB Output is correct
6 Correct 14 ms 11244 KB Output is correct
7 Correct 17 ms 11500 KB Output is correct
8 Correct 16 ms 11628 KB Output is correct
9 Correct 13 ms 11116 KB Output is correct
10 Correct 17 ms 11628 KB Output is correct
11 Correct 17 ms 11648 KB Output is correct
12 Correct 14 ms 11244 KB Output is correct
13 Correct 17 ms 11628 KB Output is correct
14 Correct 16 ms 11500 KB Output is correct
15 Correct 16 ms 11500 KB Output is correct
16 Correct 16 ms 11628 KB Output is correct
17 Correct 15 ms 11244 KB Output is correct
18 Correct 16 ms 11372 KB Output is correct
19 Correct 13 ms 10880 KB Output is correct
20 Correct 14 ms 11116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 131 ms 36384 KB Output is correct
2 Correct 166 ms 36716 KB Output is correct
3 Correct 169 ms 37348 KB Output is correct
4 Correct 128 ms 29676 KB Output is correct
5 Correct 212 ms 36716 KB Output is correct
6 Correct 623 ms 114000 KB Output is correct
7 Correct 778 ms 116756 KB Output is correct
8 Correct 866 ms 125636 KB Output is correct
9 Correct 685 ms 96300 KB Output is correct
10 Correct 727 ms 111776 KB Output is correct
11 Correct 710 ms 109796 KB Output is correct
12 Correct 517 ms 84332 KB Output is correct
13 Correct 724 ms 97884 KB Output is correct
14 Correct 729 ms 97812 KB Output is correct
15 Correct 462 ms 108572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1204 ms 115672 KB Output is correct
2 Correct 977 ms 132676 KB Output is correct
3 Correct 842 ms 110620 KB Output is correct
4 Correct 1016 ms 132412 KB Output is correct
5 Correct 782 ms 134424 KB Output is correct
6 Correct 991 ms 128800 KB Output is correct
7 Correct 764 ms 104924 KB Output is correct
8 Correct 738 ms 119584 KB Output is correct
9 Correct 552 ms 92188 KB Output is correct
10 Correct 515 ms 109640 KB Output is correct
11 Correct 831 ms 130020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 11648 KB Output is correct
2 Correct 16 ms 11628 KB Output is correct
3 Correct 14 ms 11116 KB Output is correct
4 Correct 18 ms 11648 KB Output is correct
5 Correct 16 ms 11648 KB Output is correct
6 Correct 14 ms 11244 KB Output is correct
7 Correct 17 ms 11500 KB Output is correct
8 Correct 16 ms 11628 KB Output is correct
9 Correct 13 ms 11116 KB Output is correct
10 Correct 17 ms 11628 KB Output is correct
11 Correct 17 ms 11648 KB Output is correct
12 Correct 14 ms 11244 KB Output is correct
13 Correct 17 ms 11628 KB Output is correct
14 Correct 16 ms 11500 KB Output is correct
15 Correct 16 ms 11500 KB Output is correct
16 Correct 16 ms 11628 KB Output is correct
17 Correct 15 ms 11244 KB Output is correct
18 Correct 16 ms 11372 KB Output is correct
19 Correct 13 ms 10880 KB Output is correct
20 Correct 14 ms 11116 KB Output is correct
21 Correct 131 ms 36384 KB Output is correct
22 Correct 166 ms 36716 KB Output is correct
23 Correct 169 ms 37348 KB Output is correct
24 Correct 128 ms 29676 KB Output is correct
25 Correct 212 ms 36716 KB Output is correct
26 Correct 623 ms 114000 KB Output is correct
27 Correct 778 ms 116756 KB Output is correct
28 Correct 866 ms 125636 KB Output is correct
29 Correct 685 ms 96300 KB Output is correct
30 Correct 727 ms 111776 KB Output is correct
31 Correct 710 ms 109796 KB Output is correct
32 Correct 517 ms 84332 KB Output is correct
33 Correct 724 ms 97884 KB Output is correct
34 Correct 729 ms 97812 KB Output is correct
35 Correct 462 ms 108572 KB Output is correct
36 Correct 1204 ms 115672 KB Output is correct
37 Correct 977 ms 132676 KB Output is correct
38 Correct 842 ms 110620 KB Output is correct
39 Correct 1016 ms 132412 KB Output is correct
40 Correct 782 ms 134424 KB Output is correct
41 Correct 991 ms 128800 KB Output is correct
42 Correct 764 ms 104924 KB Output is correct
43 Correct 738 ms 119584 KB Output is correct
44 Correct 552 ms 92188 KB Output is correct
45 Correct 515 ms 109640 KB Output is correct
46 Correct 831 ms 130020 KB Output is correct
47 Correct 1048 ms 109420 KB Output is correct
48 Correct 1242 ms 140384 KB Output is correct
49 Correct 774 ms 97692 KB Output is correct
50 Correct 1177 ms 134564 KB Output is correct
51 Correct 846 ms 121500 KB Output is correct
52 Correct 1026 ms 124644 KB Output is correct
53 Correct 902 ms 106304 KB Output is correct
54 Correct 915 ms 122820 KB Output is correct
55 Correct 642 ms 94444 KB Output is correct
56 Correct 507 ms 109628 KB Output is correct
57 Correct 932 ms 130668 KB Output is correct