Submission #820144

#TimeUsernameProblemLanguageResultExecution timeMemory
820144PurpleCrayonDungeon 3 (JOI21_ho_t5)C++17
11 / 100
4066 ms3468 KiB
#include <bits/stdc++.h>
using namespace std;

#define sz(v) int(v.size())
#define ar array
typedef long long ll;
const int N = 5e2+10, MOD = 998244353;

void solve() {
    int n, m; cin >> n >> m;
    vector<ll> a(n); for (auto& x : a) cin >> x;
    vector<ll> b(n); for (auto& x : b) cin >> x;

    while (m--) {
        int l, r, cap; cin >> l >> r >> cap, --l, --r;
        deque<pair<int, ll>> st;

        ll ans = 0;
        ll sub = 0;
        for (int i = l; i < r; i++) {
            ll need = a[i], cost = b[i];
            while (sz(st) && b[st.back().first] >= cost) st.pop_back();
            st.emplace_back(i, cap + sub);

            while (sz(st) && need) {
                ll cur = min(st.front().second - sub, need); 
                ans += b[st.front().first] * cur;
                need -= cur, sub += cur;

                while (sz(st) && st.front().second == sub) st.pop_front();
            }

            if (need) {
                ans = -1;
                break;
            }
        }

        cout << ans << '\n';
    }
}

int main() {
    ios::sync_with_stdio(false); cin.tie(0);
    int T = 1;
    // cin >> T;
    while (T--) solve();
}

#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...