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>
typedef long long ll;
using namespace std;
int a[200200], b[200200];
ll S[200200], ans;
struct Seg{
    pair<int, int> tree[400400];
    int sz;
    void init(int n){
        sz = n;
        for (int i=sz;i<sz*2;i++) tree[i] = {b[i-sz], i-sz};
        for (int i=sz-1;i;i--) tree[i] = min(tree[i<<1], tree[i<<1|1]);
    }
    pair<int, int> query(int l, int r){
        pair<int, int> ret = {2e9, 0};
        for (l+=sz, r+=sz;l<r;l>>=1, r>>=1){
            if (l&1) ret = min(ret, tree[l++]);
            if (r&1) ret = min(ret, tree[--r]);
        }
        return ret;
    }
}tree;
void dnc(int s, int t, int u, int h){
    if (ans==-1) return;
    if (s>=t) return;
    auto p = tree.query(s, t);
    ll need = min(S[t-1] - S[p.second-1], (ll)u);
    ll cur = max(h - (S[p.second-1] - S[s-1]), 0LL);
    need = max(need, cur);
    if (a[p.second]>u) {ans = -1; return;}
    ans += (need-cur) * p.first;
    //printf(" %d %d %d %d: %lld %lld / %d %d\n", s, t, u, h, need, cur, p.first, p.second);
    dnc(s, p.second, u, h);
    dnc(p.second+1, t, u, need - a[p.second]);
}
int main(){
    int n, q;
    scanf("%d %d", &n, &q);
    for (int i=1;i<=n;i++) scanf("%d", a+i);
    for (int i=1;i<=n;i++) scanf("%d", b+i);
    for (int i=1;i<=n;i++) S[i] = S[i-1] + a[i];
    tree.init(n+1);
    while(q--){
        int s, t, u;
        scanf("%d %d %d", &s, &t, &u);
        ans = 0;
        dnc(s, t, u, 0);
        printf("%lld\n", ans);
    }
    return 0;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:44:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   44 |     scanf("%d %d", &n, &q);
      |     ~~~~~^~~~~~~~~~~~~~~~~
Main.cpp:45:33: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   45 |     for (int i=1;i<=n;i++) scanf("%d", a+i);
      |                            ~~~~~^~~~~~~~~~~
Main.cpp:46:33: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   46 |     for (int i=1;i<=n;i++) scanf("%d", b+i);
      |                            ~~~~~^~~~~~~~~~~
Main.cpp:53:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   53 |         scanf("%d %d %d", &s, &t, &u);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~| # | 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... |