Submission #1155768

#TimeUsernameProblemLanguageResultExecution timeMemory
1155768dnnndaDungeon 3 (JOI21_ho_t5)C++20
11 / 100
4094 ms4400 KiB
#include<bits/stdc++.h>
using namespace std;
#define S second
#define F first
#define ll long long
//#define int long long
//#pragma GCC optimize("Ofast, unroll-loop")
//#pragma GCC target("avx,avx2")
#pragma GCC optimize("O3")
const int inf=0x3f3f3f3f;
const ll inff=0x3f3f3f3f3f3f3f3f;
const int X=1000000007;

int a[200005], b[200005], r[200005];
ll pre[200005];

signed main(){
    ios::sync_with_stdio(0), cin.tie(0);
    int n, m; cin >> n >> m;
    for(int i=1 ; i<=n ; i++){
        cin >> a[i];
        pre[i]=pre[i-1]+a[i];
    }
    for(int i=1 ; i<=n ; i++) cin >> b[i];
    for(int i=1 ; i<=m ; i++){
        int s, t, u; cin >> s >> t >> u;
        bool ok=1;
        stack<int> stk;
        stk.push(t);
        for(int j=t-1 ; j>=s ; j--){
            while(stk.top()!=t&&b[stk.top()]>=b[j]) stk.pop();
            r[j]=stk.top();
            stk.push(j);
        }
        int cur=0;
        ll ans=0;
        for(int j=s ; j<t ; j++){
            int buy=min(1LL*u,pre[r[j]-1]-pre[j-1])-cur;
            buy=max(0,buy);
            cur+=buy;
            ans+=1LL*buy*b[j];
            if(cur<a[j]){
                cout << "-1\n";
                ok=0;
                break;
            }
            else cur-=a[j];
        }
        if(ok) cout << ans << '\n';
    }

    return 0;
}

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