답안 #957401

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
957401 2024-04-03T16:18:15 Z peterandvoi Dungeon 3 (JOI21_ho_t5) C++17
100 / 100
463 ms 60372 KB
#include <bits/stdc++.h>

using namespace std;

#ifdef ngu
#include "debug.h"
#else
#define debug(...) 42
#endif

#define int long long

const int N = (int) 2e5 + 5;

int n, m;
int a[N], b[N], x[N], r[N], s[N], t[N], u[N], res[N];
vector<int> g[N], Q[N];

namespace st {
    int tree[4 * N];
    int idx[4 * N];

    int cmp(int i, int j) {
        return b[i] < b[j] ? i : j;
    }

    void bld(int id = 1, int l = 1, int r = n) {
        if (l == r) {
            tree[id] = a[l];
            idx[id] = l;
        } else {
            int mid = l + r >> 1;
            bld(id << 1, l, mid);
            bld(id << 1 | 1, mid + 1, r);
            idx[id] = cmp(idx[id << 1], idx[id << 1 | 1]);
            tree[id] = max(tree[id << 1], tree[id << 1 | 1]);
        }
    }

    int get(int u, int v, int id = 1, int l = 1, int r = n) {
        if (u <= x[l] && x[r] <= v) {
            return idx[id];
        }
        int mid = l + r >> 1;
        if (u <= x[mid] && x[mid] < v) {
            return cmp(get(u, v, id << 1, l, mid), get(u, v, id << 1 | 1, mid + 1, r));
        }
        if (x[mid] < u) {
            return get(u, v, id << 1 | 1, mid + 1, r);
        }
        return get(u, v, id << 1, l, mid);
    }

    int qry(int u, int v, int id = 1, int l = 1, int r = n) {
        if (u <= l && r <= v) {
            return tree[id];
        }
        int mid = l + r >> 1;
        if (u <= mid && mid < v) {
            return max(qry(u, v, id << 1, l, mid), qry(u, v, id << 1 | 1, mid + 1, r));
        }
        if (mid < u) {
            return qry(u, v, id << 1 | 1, mid + 1, r);
        }
        return qry(u, v, id << 1, l, mid);
    }
}

struct fenwick {
    int n;
    vector<int> s;

    fenwick() {};

    fenwick(int n): n(n) {
        s.resize(n);
    }

    void upd(int i, int x) {
        for (; i <= n; i += i & -i) {
            s[i - 1] += x;
        }
    }

    int get(int i) {
        int res = 0;
        for (; i; i -= i & -i) {
            res += s[i - 1];
        }
        return res;
    }

    void upd(int l, int r, int x) {
        upd(l, x);
        upd(r + 1, -x);
    }
};

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    #ifdef ngu
    freopen("test.inp", "r", stdin);
    freopen("test.out", "w", stdout);
    #endif
    cin >> n >> m;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
        x[i + 1] = x[i] + a[i];
    }
    for (int i = 1; i <= n; ++i) {
        cin >> b[i];
    }
    st::bld();
    vector<int> values;
    for (int i = 1; i <= m; ++i) {
        cin >> s[i] >> t[i] >> u[i];
        if (st::qry(s[i], t[i] - 1) > u[i]) {
            res[i] = -1;
        } else {
            values.emplace_back(u[i]);
            int r = st::get(max(x[s[i]], x[t[i]] - u[i]), min(x[n], x[t[i]]));
            res[i] = b[r] * (x[t[i]] - x[r]);
            Q[s[i]].emplace_back(i);
            Q[r].emplace_back(-i);
        }
    }
    sort(values.begin(), values.end());
    values.erase(unique(values.begin(), values.end()), values.end());
    auto idx = [&](int x) {
        return lower_bound(values.begin(), values.end(), x) - values.begin() + 1;
    };
    int k = (int) values.size();
    fenwick ft1(k), ft2(k);
    auto get = [&](int U) {
        int i = idx(U);
        return ft1.get(i) * U + ft2.get(i);
    };
    vector<int> st;
    for (int i = n; i >= 1; --i) {
        while (st.size() && b[st.back()] >= b[i]) {
            st.pop_back();
        }
        r[i] = st.size() ? st.back() : n + 1;
        st.emplace_back(i);
    }
    while (st.size()) {
        st.pop_back();
    }
    for (int i = 1; i <= n; ++i) {
        while (st.size() && b[st.back()] > b[i]) {
            st.pop_back();
        }
        if (st.size()) {
            g[st.back()].emplace_back(i);
        }
        st.emplace_back(i);
    }
    for (int i = n; i >= 1; --i) {
        int delta = x[r[i]] - x[i];
        int pos = idx(delta);
        if (pos > 1) {
            ft1.upd(1, pos - 1, b[i]);
        }
        if (pos <= k) {
            ft2.upd(pos, k, delta * b[i]);
        }
        for (int j : g[i]) {
            int delta = x[j] - x[i];
            int pos = idx(delta);
            if (pos <= k) {
                ft1.upd(pos, k, -b[j]);
                ft2.upd(pos, k, delta * b[j]);
            }
            delta = x[r[j]] - x[i];
            pos = idx(delta);
            if (pos <= k) {
                ft1.upd(pos, k, b[j]);
                ft2.upd(pos, k, -delta * b[j]);
            }
        }
        for (int id : Q[i]) {
            int sign = id < 0 ? -1 : 1;
            res[sign * id] += sign * get(u[sign * id]);
        }
    }
    for (int i = 1; i <= m; ++i) {
        cout << res[i] << "\n";
    }
}

Compilation message

Main.cpp: In function 'void st::bld(long long int, long long int, long long int)':
Main.cpp:32:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   32 |             int mid = l + r >> 1;
      |                       ~~^~~
Main.cpp: In function 'long long int st::get(long long int, long long int, long long int, long long int, long long int)':
Main.cpp:44:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   44 |         int mid = l + r >> 1;
      |                   ~~^~~
Main.cpp: In function 'long long int st::qry(long long int, long long int, long long int, long long int, long long int)':
Main.cpp:58:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   58 |         int mid = l + r >> 1;
      |                   ~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 21228 KB Output is correct
2 Correct 8 ms 21336 KB Output is correct
3 Correct 7 ms 21340 KB Output is correct
4 Correct 8 ms 21340 KB Output is correct
5 Correct 8 ms 21340 KB Output is correct
6 Correct 7 ms 21340 KB Output is correct
7 Correct 8 ms 21340 KB Output is correct
8 Correct 8 ms 21340 KB Output is correct
9 Correct 8 ms 21232 KB Output is correct
10 Correct 7 ms 21340 KB Output is correct
11 Correct 8 ms 21340 KB Output is correct
12 Correct 8 ms 21180 KB Output is correct
13 Correct 8 ms 21380 KB Output is correct
14 Correct 8 ms 21340 KB Output is correct
15 Correct 7 ms 21340 KB Output is correct
16 Correct 7 ms 21500 KB Output is correct
17 Correct 7 ms 21184 KB Output is correct
18 Correct 7 ms 21340 KB Output is correct
19 Correct 7 ms 21236 KB Output is correct
20 Correct 6 ms 21340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 54 ms 31188 KB Output is correct
2 Correct 57 ms 30924 KB Output is correct
3 Correct 56 ms 32196 KB Output is correct
4 Correct 70 ms 30904 KB Output is correct
5 Correct 55 ms 31020 KB Output is correct
6 Correct 286 ms 53644 KB Output is correct
7 Correct 288 ms 53768 KB Output is correct
8 Correct 300 ms 58364 KB Output is correct
9 Correct 326 ms 52832 KB Output is correct
10 Correct 357 ms 57372 KB Output is correct
11 Correct 346 ms 57156 KB Output is correct
12 Correct 344 ms 52292 KB Output is correct
13 Correct 322 ms 52396 KB Output is correct
14 Correct 336 ms 52364 KB Output is correct
15 Correct 170 ms 53488 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 363 ms 55464 KB Output is correct
2 Correct 321 ms 59984 KB Output is correct
3 Correct 288 ms 53552 KB Output is correct
4 Correct 340 ms 55888 KB Output is correct
5 Correct 266 ms 54736 KB Output is correct
6 Correct 291 ms 59184 KB Output is correct
7 Correct 303 ms 52904 KB Output is correct
8 Correct 278 ms 57140 KB Output is correct
9 Correct 254 ms 50992 KB Output is correct
10 Correct 159 ms 54084 KB Output is correct
11 Correct 239 ms 52228 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 21228 KB Output is correct
2 Correct 8 ms 21336 KB Output is correct
3 Correct 7 ms 21340 KB Output is correct
4 Correct 8 ms 21340 KB Output is correct
5 Correct 8 ms 21340 KB Output is correct
6 Correct 7 ms 21340 KB Output is correct
7 Correct 8 ms 21340 KB Output is correct
8 Correct 8 ms 21340 KB Output is correct
9 Correct 8 ms 21232 KB Output is correct
10 Correct 7 ms 21340 KB Output is correct
11 Correct 8 ms 21340 KB Output is correct
12 Correct 8 ms 21180 KB Output is correct
13 Correct 8 ms 21380 KB Output is correct
14 Correct 8 ms 21340 KB Output is correct
15 Correct 7 ms 21340 KB Output is correct
16 Correct 7 ms 21500 KB Output is correct
17 Correct 7 ms 21184 KB Output is correct
18 Correct 7 ms 21340 KB Output is correct
19 Correct 7 ms 21236 KB Output is correct
20 Correct 6 ms 21340 KB Output is correct
21 Correct 54 ms 31188 KB Output is correct
22 Correct 57 ms 30924 KB Output is correct
23 Correct 56 ms 32196 KB Output is correct
24 Correct 70 ms 30904 KB Output is correct
25 Correct 55 ms 31020 KB Output is correct
26 Correct 286 ms 53644 KB Output is correct
27 Correct 288 ms 53768 KB Output is correct
28 Correct 300 ms 58364 KB Output is correct
29 Correct 326 ms 52832 KB Output is correct
30 Correct 357 ms 57372 KB Output is correct
31 Correct 346 ms 57156 KB Output is correct
32 Correct 344 ms 52292 KB Output is correct
33 Correct 322 ms 52396 KB Output is correct
34 Correct 336 ms 52364 KB Output is correct
35 Correct 170 ms 53488 KB Output is correct
36 Correct 363 ms 55464 KB Output is correct
37 Correct 321 ms 59984 KB Output is correct
38 Correct 288 ms 53552 KB Output is correct
39 Correct 340 ms 55888 KB Output is correct
40 Correct 266 ms 54736 KB Output is correct
41 Correct 291 ms 59184 KB Output is correct
42 Correct 303 ms 52904 KB Output is correct
43 Correct 278 ms 57140 KB Output is correct
44 Correct 254 ms 50992 KB Output is correct
45 Correct 159 ms 54084 KB Output is correct
46 Correct 239 ms 52228 KB Output is correct
47 Correct 463 ms 54676 KB Output is correct
48 Correct 435 ms 60372 KB Output is correct
49 Correct 366 ms 53308 KB Output is correct
50 Correct 418 ms 56304 KB Output is correct
51 Correct 445 ms 55124 KB Output is correct
52 Correct 412 ms 59516 KB Output is correct
53 Correct 422 ms 53352 KB Output is correct
54 Correct 379 ms 57584 KB Output is correct
55 Correct 344 ms 52032 KB Output is correct
56 Correct 204 ms 54036 KB Output is correct
57 Correct 358 ms 53172 KB Output is correct