This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define int ll
#define rep(n) rep1(i,n)
#define rep1(i,n) rep2(i,0,n)
#define rep2(i,a,b) for(int i=a; i<(b); ++i)
#define rep3(i,a,b) for(int i=a; i>=(b); --i)
#define pii pair<int,int>
#define pb push_back
#define all(a) a.begin(),a.end()
#define sz(a) ((int)a.size())
#define pow2(x) (1ll<<(x))
#define inf 1000'000'000'000'000'000
#ifdef i_am_noob
#define bug(...) cerr << "#" << __LINE__ << ": " << #__VA_ARGS__ << "- ",_do(__VA_ARGS__)
template<typename T> void _do(T x){cerr << x << endl;}
template<typename T, typename ... S> void _do(T x, S ...y){cerr << x << ", ";_do(y...);}
#else
#define bug(...) 49
#endif
const int maxn=200005;
int n,m,a[maxn],b[maxn];
deque<pii> dq;
signed main(){
    ios_base::sync_with_stdio(0),cin.tie(0);
    cin >> n >> m;
    rep(n) cin >> a[i];
    rep(n) cin >> b[i];
    if(n<=3000&&m<=3000){
        rep1(_,m){
            int s,t,u;
            cin >> s >> t >> u;
            s--,t--;
            dq.clear();
            dq.pb({0,u});
            int val=0;
            bool bad=0;
            rep3(i,t-1,s){
                if(a[i]>u){
                    bad=1;
                    break;
                }
                int to_delete=a[i];
                while(sz(dq)&&dq.back().second<=to_delete) to_delete-=dq.back().second,dq.pop_back();
                if(sz(dq)) dq.back().second-=to_delete;
                int to_add=a[i];
                while(sz(dq)&&dq.front().first>=b[i]){
                    val-=dq.front().first*dq.front().second;
                    to_add+=dq.front().second;
                    dq.pop_front();
                }
                dq.push_front({b[i],to_add});
                val+=b[i]*to_add;
            }
            if(bad){
                cout << "-1\n";
                continue;
            }
            cout << val << "\n";
        }
    }
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |