Problem:
Doping Slayer
問題
$N$ 人の鬼狩りが $M$ 両編成の列車に乗っています。$i$ ($1 \leq i \leq N$) 人目の鬼狩りの強さは $A_i$ であり、列車の $B_i$ 両目から $C_i$ 両目までの範囲を守っています。
この列車に $Q$ 匹の鬼が同時に襲撃してきます。$j$ ($1 \leq j \leq Q$) 匹目の鬼の強さは $D_j$ であり、列車の $E_j$ 両目の車両に襲撃を仕掛けます。
各 $k$ ($1 \leq k \leq M$) に対し、「$k$ 両目の車両を襲撃する鬼の強さの合計」が「$k$ 両目を含む車両を守る鬼狩りの強さの合計」を超えた場合、$k$ 両目は破壊されてしまいます。
鬼の襲撃を予知した Yoika 君は、鬼狩りをドーピングして強化することで、襲撃を阻止しようと考えました。ドーピング用のポーション $1$ 個につき、鬼狩り一人の強さを $1$ 増やすことができます。すべての襲撃を阻止するためには、最低いくつのポーションが必要でしょうか？
ただし、いくつポーションを用意しても阻止できない襲撃がある場合は、$-1$ を出力してください。
制約
$1 \leq N, M, Q \leq 10^5$
$1 \leq A_i, D_j \leq 10^9$
$1 \leq B_i \leq C_i \leq M$
$1 \leq E_j \leq M$
入力はすべて整数
入力
入力は以下の形式で標準入力から与えられます。
$N$ $M$
$A_1$ $B_1$ $C_1$
$A_2$ $B_2$ $C_2$
$\vdots$
$A_N$ $B_N$ $C_N$
$Q$
$D_1$ $E_1$
$D_2$ $E_2$
$\vdots$
$D_Q$ $E_Q$
出力
すべての襲撃を阻止するために必要なポーションの個数を出力してください。ただし、いくつポーションを用意しても阻止できない襲撃がある場合は、$-1$ を出力してください。
入力例 1
3 5
2 1 2
1 2 4
2 4 5
3
4 2
5 4
3 5
出力例 1
2
$2$ 人目と $3$ 人目の鬼狩りにポーションを $1$ 個ずつ使用します。すると、各車両における鬼狩りの強さの合計と鬼の強さの合計は以下のようになるため、すべての襲撃を阻止できます。
これがポーションの使用個数を最小化する使い方であるため、求める個数は $2$ 個です。
入力例 2
2 3
2 2 3
1 2 2
1
1 1
出力例 2
-1
どの鬼狩りをどれだけ強化しても鬼の襲撃を阻止できない場合、$-1$ を出力してください。

Submitted solutions:

code_snippet: #include <bits/stdc++.h>

#include <algorithm>

#ifdef _MSC_VER
#include <intrin.h>
#endif

namespace atcoder {

namespace internal {

// @param n `0 <= n`
// @return minimum non-negative `x` s.t. `n <= 2**x`
int ceil_pow2(int n) {
    int x = 0;
    while ((1U << x) < (unsigned int)(n)) x++;
    return x;
}

// @param n `1 <= n`
// @return minimum non-negative `x` s.t. `(n & (1 << x)) != 0`
int bsf(unsigned int n) {
#ifdef _MSC_VER
    unsigned long index;
    _BitScanForward(&index, n);
    return index;
#else
    return __builtin_ctz(n);
#endif
}

}  // namespace internal

}  // namespace atcoder

#include <cassert>
#include <iostream>
#include <vector>
namespace atcoder {

template <class S,
          S (*op)(S, S),
          S (*e)(),
          class F,
          S (*mapping)(F, S),
          F (*composition)(F, F),
          F (*id)()>
struct lazy_segtree {
  public:
    lazy_segtree() : lazy_segtree(0) {}
    lazy_segtree(int n) : lazy_segtree(std::vector<S>(n, e())) {}
    lazy_segtree(const std::vector<S>& v) : _n(int(v.size())) {
        log = internal::ceil_pow2(_n);
        size = 1 << log;
        d = std::vector<S>(2 * size, e());
        lz = std::vector<F>(size, id());
        for (int i = 0; i < _n; i++) d[size + i] = v[i];
        for (int i = size - 1; i >= 1; i--) {
            update(i);
        }
    }

    void set(int p, S x) {
        assert(0 <= p && p < _n);
        p += size;
        for (int i = log; i >= 1; i--) push(p >> i);
        d[p] = x;
        for (int i = 1; i <= log; i++) update(p >> i);
    }

    S get(int p) {
        assert(0 <= p && p < _n);
        p += size;
        for (int i = log; i >= 1; i--) push(p >> i);
        return d[p];
    }

    S prod(int l, int r) {
        assert(0 <= l && l <= r && r <= _n);
        if (l == r) return e();

        l += size;
        r += size;

        for (int i = log; i >= 1; i--) {
            if (((l >> i) << i) != l) push(l >> i);
            if (((r >> i) << i) != r) push(r >> i);
        }

        S sml = e(), smr = e();
        while (l < r) {
            if (l & 1) sml = op(sml, d[l++]);
            if (r & 1) smr = op(d[--r], smr);
            l >>= 1;
            r >>= 1;
        }

        return op(sml, smr);
    }

    S all_prod() { return d[1]; }

    void apply(int p, F f) {
        assert(0 <= p && p < _n);
        p += size;
        for (int i = log; i >= 1; i--) push(p >> i);
        d[p] = mapping(f, d[p]);
        for (int i = 1; i <= log; i++) update(p >> i);
    }
    void apply(int l, int r, F f) {
        assert(0 <= l && l <= r && r <= _n);
        if (l == r) return;

        l += size;
        r += size;

        for (int i = log; i >= 1; i--) {
            if (((l >> i) << i) != l) push(l >> i);
            if (((r >> i) << i) != r) push((r - 1) >> i);
        }

        {
            int l2 = l, r2 = r;
            while (l < r) {
                if (l & 1) all_apply(l++, f);
                if (r & 1) all_apply(--r, f);
                l >>= 1;
                r >>= 1;
            }
            l = l2;
            r = r2;
        }

        for (int i = 1; i <= log; i++) {
            if (((l >> i) << i) != l) update(l >> i);
            if (((r >> i) << i) != r) update((r - 1) >> i);
        }
    }

    template <bool (*g)(S)> int max_right(int l) {
        return max_right(l, [](S x) { return g(x); });
    }
    template <class G> int max_right(int l, G g) {
        assert(0 <= l && l <= _n);
        assert(g(e()));
        if (l == _n) return _n;
        l += size;
        for (int i = log; i >= 1; i--) push(l >> i);
        S sm = e();
        do {
            while (l % 2 == 0) l >>= 1;
            if (!g(op(sm, d[l]))) {
                while (l < size) {
                    push(l);
                    l = (2 * l);
                    if (g(op(sm, d[l]))) {
                        sm = op(sm, d[l]);
                        l++;
                    }
                }
                return l - size;
            }
            sm = op(sm, d[l]);
            l++;
        } while ((l & -l) != l);
        return _n;
    }

    template <bool (*g)(S)> int min_left(int r) {
        return min_left(r, [](S x) { return g(x); });
    }
    template <class G> int min_left(int r, G g) {
        assert(0 <= r && r <= _n);
        assert(g(e()));
        if (r == 0) return 0;
        r += size;
        for (int i = log; i >= 1; i--) push((r - 1) >> i);
        S sm = e();
        do {
            r--;
            while (r > 1 && (r % 2)) r >>= 1;
            if (!g(op(d[r], sm))) {
                while (r < size) {
                    push(r);
                    r = (2 * r + 1);
                    if (g(op(d[r], sm))) {
                        sm = op(d[r], sm);
                        r--;
                    }
                }
                return r + 1 - size;
            }
            sm = op(d[r], sm);
        } while ((r & -r) != r);
        return 0;
    }

  private:
    int _n, size, log;
    std::vector<S> d;
    std::vector<F> lz;

    void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); }
    void all_apply(int k, F f) {
        d[k] = mapping(f, d[k]);
        if (k < size) lz[k] = composition(f, lz[k]);
    }
    void push(int k) {
        all_apply(2 * k, lz[k]);
        all_apply(2 * k + 1, lz[k]);
        lz[k] = id();
    }
};

}  // namespace atcoder

using namespace atcoder;
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vd = vector<double>;
using vvd = vector<vd>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vvvb = vector<vvb>;
#define all(A) A.begin(),A.end()
#define rep(i, n) for (ll i = 0; i < (ll) (n); i++)
template<class T>
bool chmax(T& p, T q, bool C = 1) {
    if (C == 0 && p == q) {
        return 1;
    }
    if (p < q) {
        p = q;
        return 1;
    }
    else {
        return 0;
    }
}
template<class T>
bool chmin(T& p, T q, bool C = 1) {
    if (C == 0 && p == q) {
        return 1;
    }
    if (p > q) {
        p = q;
        return 1;
    }
    else {
        return 0;
    }
}
ll modPow(long long a, long long n, long long p) {
    if (n == 0) return 1; // 0乗にも対応する場合
    if (n == 1) return a % p;
    if (n % 2 == 1) return (a * modPow(a, n - 1, p)) % p;
    long long t = modPow(a, n / 2, p);
    return (t * t) % p;
}
ll gcd(ll(a), ll(b)) {
    if (a == 0)return b;
    if (b == 0)return a;
    ll c = a;
    while (a % b != 0) {
        c = a % b;
        a = b;
        b = c;
    }
    return b;
}
ll sqrtz(ll N) {
    ll L = 0;
    ll R = sqrt(N) + 10000;
    while (abs(R - L) > 1) {
        ll mid = (R + L) / 2;
        if (mid * mid <= N)L = mid;
        else R = mid;
    }
    return L;
}

struct S {
    ll a;
    int size;
};


S op(S l, S r) { return S{l.a + r.a, l.size + r.size}; }

S e() { return S{0, 0}; }

S mapping(ll l, S r) { return S{r.a+ r.size * l, r.size}; }

ll composition(ll l, ll r) { return l+r;}

ll id() { return 0; }

bool DEB = 0;


int main() {

    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    ll N,M;
    cin>>N>>M;
    vvll B(M),C(M);
    lazy_segtree<S,op,e,ll,mapping,composition,id> seg(M);
    rep(i,M)seg.set(i,S({0,1}));
    rep(i,N){
        ll a,b,c;
        cin>>a>>b>>c;
        b--;c--;
        seg.apply(b,c+1,a);
        B[b].push_back(c);
        C[c].push_back(c);
    }
    vll R(M,-1);
    multiset<ll> MR;
    rep(i,M){
        for(auto b:B[i])MR.insert(b);
        if(!MR.empty()){
            R[i]=*(prev(MR.end()));
        }
        for(auto c:C[i])MR.erase(MR.find(c));
    }
    ll an=0;
    ll Q;
    cin>>Q;
    vll DM(M,0);
    rep(q,Q){
        ll d,e;
        cin>>d>>e;
        DM[e-1]+=d;
    }

    rep(i,M){
        ll wk=DM[i]-seg.get(i).a;
        if(wk<=0)continue;
        if(DM[i]>0&&R[i]<0){
            cout<<-1<<endl;
            return 0;
        }
        seg.apply(i,R[i]+1,wk);
        an+=wk;
    }
    cout<<an<<endl;
    
}
accuracy: 1
time_ms: 80
memory_kb: 20604
final_rank: 10

code_snippet: #include "bits/stdc++.h"


using namespace std;

using i64 = long long;

constexpr i64 mod = 998244353;


int main(){
    int n, m;
    cin >> m >> n;
    vector<i64> a(m), b(m), c(m);
    vector<vector<i64>> in(n), out(n);
    for(int i = 0; i < m; ++i){
        cin >> a[i] >> b[i] >> c[i];
        --b[i], --c[i];
        in[b[i]].emplace_back(i);
        out[c[i]].emplace_back(i);
    }
    vector<i64> sum(n, 0);
    int q;
    cin >> q;
    for(int i = 0; i < q; ++i){
        int d, e;
        cin >> d >> e;
        --e;
        sum[e] += d;
    }
    set<pair<int, int>> s;
    auto powers = a;
    i64 nowpower = 0;
    i64 pot = 0;
    for(int i = 0; i < n; ++i){
        for(auto j : in[i]){
            s.emplace(c[j], j);
            nowpower += powers[j];
        }
        if(nowpower < sum[i]){
            i64 diff = sum[i] - nowpower;
            if(s.empty()){
                cout << -1 << endl;
                return 0;
            }
            int idx = s.rbegin()->second;
            powers[idx] += diff;
            nowpower += diff;
            pot += diff;
        }
        for(auto j : out[i]){
            s.erase(make_pair(c[j], j));
            nowpower -= powers[j];
        }
    }
    cout << pot << endl;
}
accuracy: 1
time_ms: 100
memory_kb: 17764
final_rank: 11

code_snippet: /* -*- coding: utf-8 -*-
 *
 * 3267.cc: Doping Slayer
 */

#include<cstdio>
#include<vector>
#include<set>
#include<algorithm>
#include<utility>
 
using namespace std;

/* constant */

const int MAX_N = 100000;
const int MAX_M = 100000;

/* typedef */

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef set<pii> spii;

/* global variables */

ll as[MAX_N], dss[MAX_M];
int bs[MAX_N], cs[MAX_N];
vi lvs[MAX_M], rvs[MAX_M];

/* subroutines */

/* main */

int main() {
  int n, m;
  scanf("%d%d", &n, &m);

  for (int i = 0; i < n; i++) {
    scanf("%lld%d%d", as + i, bs + i, cs + i);
    bs[i]--, cs[i]--;
    lvs[bs[i]].push_back(i);
    rvs[cs[i]].push_back(i);
  }

  int qn;
  scanf("%d", &qn);

  while (qn--) {
    int di, ei;
    scanf("%d%d", &di, &ei), ei--;
    dss[ei] += di;
  }

  ll sum = 0, asum = 0;
  spii ps;
  for (int i = 0; i < m; i++) {
    for (auto u: lvs[i]) {
      asum += as[u];
      ps.insert(pii(cs[u], u));
    }

    if (dss[i] > 0) {
      if (ps.empty()) { puts("-1"); return 0; }
      if (dss[i] > asum) {
	ll dd = dss[i] - asum;
	//printf(" i=%d, dd=%lld\n", i, dd);

	sum += dd;
	asum += dd;
	auto sit = ps.end(); sit--;
	as[sit->second] += dd;
      }
    }

    for (auto u: rvs[i]) {
      asum -= as[u];
      ps.erase(pii(cs[u], u));
    }
  }

  printf("%lld\n", sum);

  return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 16140
final_rank: 6

code_snippet: #include <bits/stdc++.h>
using namespace std;

template <typename T>
struct LazySegmentTree {
    int n;
    vector<T> dat;
    vector<T>lazy;
    LazySegmentTree() {};
    LazySegmentTree(int N) {
        n = 1;
        while(n < N) {
            n *= 2;
        }
        dat.resize(n*2-1,0);
        lazy.resize(n*2-1,0);
    }
    void eval(int k,int l,int r) {
        if(lazy[k] == 0) return;
        if(k < n-1) {
            lazy[k*2+1] += lazy[k];
            lazy[k*2+2] += lazy[k];
        }
        dat[k] += lazy[k]*(r-l);
        lazy[k] = 0;
    }
    void update(int a, int b, T x, int k, int l, int r) {
        eval(k,l,r);
        if(a <= l && r <= b) {
            lazy[k] += x;
            eval(k,l,r);
        }
        else if(a < r && l < b) {
            update(a, b, x, k*2+1, l, (l+r)/2);
            update(a, b, x, k*2+2, (l+r)/2, r);
            dat[k] = dat[k*2+1]+dat[k*2+2];
        }
    }
    void update(int a, int b, T x) {//[a,b)
        update(a, b, x, 0, 0, n);
    }
    T query_sub(int a, int b, int k, int l, int r) {
        eval(k,l,r);
        if (r <= a || b <= l) {
            return 0;
        }
        else if (a <= l && r <= b) {
            return dat[k];
        }
        else {
            T vl = query_sub(a, b, k*2+1, l, (l+r)/2);
            T vr = query_sub(a, b, k*2+2, (l+r)/2, r);
            return vl+vr;
        }
    }
    T query(int a, int b) {//[a,b)
        return query_sub(a, b, 0, 0, n);
    }
};

int main() {
    int N,M;
    cin >> N >> M;
    vector<int>A(N),B(N),C(N);
    vector<int>nxt(M,-1);
    LazySegmentTree<long long>Seg(M);
    for(int i = 0; i < N; i++) {
        cin >> A[i] >> B[i] >> C[i];
        B[i]--;
        C[i]--;
        Seg.update(B[i],C[i]+1,A[i]);
        nxt[B[i]] = max(nxt[B[i]],C[i]);
    }
    int Q;
    cin >> Q;
    while (Q--) {
        int D,E;
        cin >> D >> E;
        E--;
        Seg.update(E,E+1,-D);
    }
    int mx = -1;
    long long ans = 0;
    for(int i = 0; i < M; i++) {
        mx = max(mx,nxt[i]);
        long long ret = 0-Seg.query(i,i+1);
        if(ret > 0 && mx < i) {
            cout << -1 << endl;
            return 0;
        }
        else if(ret > 0) {
            ans += ret;
            Seg.update(i,mx+1,ret);
        }
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 120
memory_kb: 8464
final_rank: 8

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
#define chmin(x,y) x = min(x,y)
#define chmax(x,y) x = max(x,y)
#define BIG_NUM 2000000000
#define HUGE_NUM 4000000000000000000 //オーバーフローに注意
#define MOD 1000000007
#define EPS 0.000000001



#define SIZE 100005

struct Info{
	Info(ll arg_R,ll arg_value){
		R = arg_R;
		value = arg_value;
	}
	bool operator<(const struct Info& arg) const{

		return R < arg.R; //(PQ)
	}
	ll R,value;
};


ll A[SIZE],B[SIZE],C[SIZE];
ll rui[SIZE],enemy[SIZE];
vector<pair<ll,ll>> V[SIZE];

int main(){

	ll N,M;
	scanf("%lld %lld",&N,&M);

	for(ll i = 0; i < N; i++){

		scanf("%lld %lld %lld",&A[i],&B[i],&C[i]);

		V[B[i]].push_back(make_pair(C[i],A[i]));
	}


	int numQ;
	scanf("%d",&numQ);

	ll D,E;

	for(int loop = 0; loop < numQ; loop++){

		scanf("%lld %lld",&D,&E);
		enemy[E] += D;
	}

	ll ans = 0;

	priority_queue<Info> Q;
	for(int i = 1; i <= M; i++){
		for(int a = 0; a < V[i].size(); a++){

			Q.push(Info(V[i][a].first,V[i][a].second));

			rui[i] += V[i][a].second;
			rui[V[i][a].first+1] -= V[i][a].second;
		}

		rui[i] += rui[i-1];

		if(enemy[i] > 0 && rui[i] == 0){ //カバー範囲外

			printf("-1\n");
			return 0;
		}
		if(enemy[i] <= rui[i])continue;

		ll add = enemy[i]-rui[i];

		rui[i] += add;
		ans += add;
		rui[Q.top().R+1] -= add;
	}

	printf("%lld\n",ans);

	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 14340
final_rank: 4

code_snippet: #include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define drep(i,j,n) for(int i=0;i<(int)(n-1);i++)for(int j=i+1;j<(int)(n);j++)
#define trep(i,j,k,n) for(int i=0;i<(int)(n-2);i++)for(int j=i+1;j<(int)(n-1);j++)for(int k=j+1;k<(int)(n);k++)
#define codefor int test;scanf("%d",&test);while(test--)
#define INT(...) int __VA_ARGS__;in(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)
#define yes(ans) if(ans)printf("yes\n");else printf("no\n")
#define Yes(ans) if(ans)printf("Yes\n");else printf("No\n")
#define YES(ans) if(ans)printf("YES\n");else printf("NO\n")
#define popcount(v) __builtin_popcountll(v)
#define vector2d(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__))
#define vector3d(type,name,h,w,...) vector<vector<vector<type>>>name(h,vector<vector<type>>(w,vector<type>(__VA_ARGS__)))
#define vector4d(type,name,h,w,d,...) vector<vector<vector<vector<type>>>>name(h,vector<vector<vector<type>>>(w,vector<vector<type>>(d,vector<type>(__VA_ARGS__))))
using namespace std;
using ll = long long;
template<class T> using rpriority_queue = priority_queue<T, vector<T>, greater<T>>;
const int MOD=1000000007;
const int MOD2=998244353;
const int INF=1<<30;
const ll INF2=1LL<<60;
void scan(int& a){scanf("%d",&a);}
void scan(long long& a){scanf("%lld",&a);}
template<class T,class L>void scan(pair<T, L>& p){scan(p.first);scan(p.second);}
template<class T,class U,class V>void scan(tuple<T,U,V>& p){scan(get<0>(p));scan(get<1>(p));scan(get<2>(p));}
template<class T, size_t size> void scan(array<T, size>& a){ for(auto&& i : a) scan(i);}
template<class T> void scan(T& a){cin>>a;}
template<class T> void scan(vector<T>& vec){for(auto&& it:vec)scan(it);}
void in(){}
template <class Head, class... Tail> void in(Head& head, Tail&... tail){scan(head);in(tail...);}
void print(const int& a){printf("%d",a);}
void print(const long long& a){printf("%lld",a);}
void print(const double& a){printf("%.15lf",a);}
template<class T,class L>void print(const pair<T, L>& p){print(p.first);putchar(' ');print(p.second);}
template<class T> void print(const T& a){cout<<a;}
template<class T> void print(const vector<T>& vec){if(vec.empty())return;print(vec[0]);for(auto it=vec.begin();++it!= vec.end();){putchar(' ');print(*it);}}
void out(){putchar('\n');}
template<class T> void out(const T& t){print(t);putchar('\n');}
template <class Head, class... Tail> void out(const Head& head,const Tail&... tail){print(head);putchar(' ');out(tail...);}
template<class T> void dprint(const T& a){cerr<<a;}
template<class T> void dprint(const vector<T>& vec){if(vec.empty())return;cerr<<vec[0];for(auto it=vec.begin();++it!= vec.end();){cerr<<" "<<*it;}}
void debug(){cerr<<'\n';}
template<class T> void debug(const T& t){dprint(t);cerr<<endl;}
template <class Head, class... Tail> void debug(const Head& head, const Tail&... tail){dprint(head);cerr<<" ";debug(tail...);}
ll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; }
ll modpow(ll a, ll b, ll p){ ll ans = 1; while(b){ if(b & 1) (ans *= a) %= p; (a *= a) %= p; b /= 2; } return ans; }
ll modinv(ll a, ll m) {ll b = m, u = 1, v = 0;while (b) {ll t = a / b;a -= t * b; swap(a, b);u -= t * v; swap(u, v);}u %= m;if (u < 0) u += m;return u;}
ll updivide(ll a,ll b){return (a+b-1)/b;}
template<class T> void chmax(T &a,const T b){if(b>a)a=b;}
template<class T> void chmin(T &a,const T b){if(b<a)a=b;}

int main(){
    INT(n,m);
    vector<ll> imos(m+1);
    vector<int> maxv(m+1,-1);
    int l,r,x;
    rep(i,n){
        in(x,l,r);
        l--;
        imos[l]+=x;
        imos[r]-=x;
        chmax(maxv[l],r);
    }
    INT(q);
    rep(i,q){
        in(x,l);
        l--;
        imos[l]-=x;
        imos[l+1]+=x;
    }
    r=-1;
    ll ans=0,d;
    for(int i=0;i<m;i++){
        chmax(r,maxv[i]);
        if(imos[i]<0&&r<=i){
            out(-1);
            return 0;
        }
        if(imos[i]<0){
            d=-imos[i];
            ans+=d;
            imos[r]-=d;
            imos[i]+=d;
        }
        imos[i+1]+=imos[i];
    }
    out(ans);
}
accuracy: 1
time_ms: 20
memory_kb: 4316
final_rank: 1

code_snippet: #include <algorithm>
#include <iostream>
#include <iomanip>
#include <limits.h>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#include <stack>

using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i < n + 1; i++)
#define all(A) A.begin(), A.end()
#define itr(A, l, r) A.begin() + l, A.begin() + r
#define debug(var) cout << #var << " = " << var << endl;
typedef long long ll;

#ifndef ATCODER_LAZYSEGTREE_HPP
#define ATCODER_LAZYSEGTREE_HPP 1

#include <algorithm>
#include <cassert>
#include <iostream>
#include <vector>

#ifndef ATCODER_INTERNAL_BITOP_HPP
#define ATCODER_INTERNAL_BITOP_HPP 1

#ifdef _MSC_VER
#include <intrin.h>
#endif

namespace atcoder
{

    namespace internal
    {

        // @param n `0 <= n`
        // @return minimum non-negative `x` s.t. `n <= 2**x`
        int ceil_pow2(int n)
        {
            int x = 0;
            while ((1U << x) < (unsigned int)(n))
                x++;
            return x;
        }

        // @param n `1 <= n`
        // @return minimum non-negative `x` s.t. `(n & (1 << x)) != 0`
        constexpr int bsf_constexpr(unsigned int n)
        {
            int x = 0;
            while (!(n & (1 << x)))
                x++;
            return x;
        }

        // @param n `1 <= n`
        // @return minimum non-negative `x` s.t. `(n & (1 << x)) != 0`
        int bsf(unsigned int n)
        {
#ifdef _MSC_VER
            unsigned long index;
            _BitScanForward(&index, n);
            return index;
#else
            return __builtin_ctz(n);
#endif
        }

    } // namespace internal

} // namespace atcoder

#endif // ATCODER_INTERNAL_BITOP_HPP

namespace atcoder
{

    template <class S,
              S (*op)(S, S),
              S (*e)(),
              class F,
              S (*mapping)(F, S),
              F (*composition)(F, F),
              F (*id)()>
    struct lazy_segtree
    {
    public:
        lazy_segtree() : lazy_segtree(0) {}
        explicit lazy_segtree(int n) : lazy_segtree(std::vector<S>(n, e())) {}
        explicit lazy_segtree(const std::vector<S> &v) : _n(int(v.size()))
        {
            log = internal::ceil_pow2(_n);
            size = 1 << log;
            d = std::vector<S>(2 * size, e());
            lz = std::vector<F>(size, id());
            for (int i = 0; i < _n; i++)
                d[size + i] = v[i];
            for (int i = size - 1; i >= 1; i--)
            {
                update(i);
            }
        }

        void set(int p, S x)
        {
            assert(0 <= p && p < _n);
            p += size;
            for (int i = log; i >= 1; i--)
                push(p >> i);
            d[p] = x;
            for (int i = 1; i <= log; i++)
                update(p >> i);
        }

        S get(int p)
        {
            assert(0 <= p && p < _n);
            p += size;
            for (int i = log; i >= 1; i--)
                push(p >> i);
            return d[p];
        }

        S prod(int l, int r)
        {
            assert(0 <= l && l <= r && r <= _n);
            if (l == r)
                return e();

            l += size;
            r += size;

            for (int i = log; i >= 1; i--)
            {
                if (((l >> i) << i) != l)
                    push(l >> i);
                if (((r >> i) << i) != r)
                    push((r - 1) >> i);
            }

            S sml = e(), smr = e();
            while (l < r)
            {
                if (l & 1)
                    sml = op(sml, d[l++]);
                if (r & 1)
                    smr = op(d[--r], smr);
                l >>= 1;
                r >>= 1;
            }

            return op(sml, smr);
        }

        S all_prod() { return d[1]; }

        void apply(int p, F f)
        {
            assert(0 <= p && p < _n);
            p += size;
            for (int i = log; i >= 1; i--)
                push(p >> i);
            d[p] = mapping(f, d[p]);
            for (int i = 1; i <= log; i++)
                update(p >> i);
        }
        void apply(int l, int r, F f)
        {
            assert(0 <= l && l <= r && r <= _n);
            if (l == r)
                return;

            l += size;
            r += size;

            for (int i = log; i >= 1; i--)
            {
                if (((l >> i) << i) != l)
                    push(l >> i);
                if (((r >> i) << i) != r)
                    push((r - 1) >> i);
            }

            {
                int l2 = l, r2 = r;
                while (l < r)
                {
                    if (l & 1)
                        all_apply(l++, f);
                    if (r & 1)
                        all_apply(--r, f);
                    l >>= 1;
                    r >>= 1;
                }
                l = l2;
                r = r2;
            }

            for (int i = 1; i <= log; i++)
            {
                if (((l >> i) << i) != l)
                    update(l >> i);
                if (((r >> i) << i) != r)
                    update((r - 1) >> i);
            }
        }

        template <bool (*g)(S)>
        int max_right(int l)
        {
            return max_right(l, [](S x)
                             { return g(x); });
        }
        template <class G>
        int max_right(int l, G g)
        {
            assert(0 <= l && l <= _n);
            assert(g(e()));
            if (l == _n)
                return _n;
            l += size;
            for (int i = log; i >= 1; i--)
                push(l >> i);
            S sm = e();
            do
            {
                while (l % 2 == 0)
                    l >>= 1;
                if (!g(op(sm, d[l])))
                {
                    while (l < size)
                    {
                        push(l);
                        l = (2 * l);
                        if (g(op(sm, d[l])))
                        {
                            sm = op(sm, d[l]);
                            l++;
                        }
                    }
                    return l - size;
                }
                sm = op(sm, d[l]);
                l++;
            } while ((l & -l) != l);
            return _n;
        }

        template <bool (*g)(S)>
        int min_left(int r)
        {
            return min_left(r, [](S x)
                            { return g(x); });
        }
        template <class G>
        int min_left(int r, G g)
        {
            assert(0 <= r && r <= _n);
            assert(g(e()));
            if (r == 0)
                return 0;
            r += size;
            for (int i = log; i >= 1; i--)
                push((r - 1) >> i);
            S sm = e();
            do
            {
                r--;
                while (r > 1 && (r % 2))
                    r >>= 1;
                if (!g(op(d[r], sm)))
                {
                    while (r < size)
                    {
                        push(r);
                        r = (2 * r + 1);
                        if (g(op(d[r], sm)))
                        {
                            sm = op(d[r], sm);
                            r--;
                        }
                    }
                    return r + 1 - size;
                }
                sm = op(d[r], sm);
            } while ((r & -r) != r);
            return 0;
        }

    private:
        int _n, size, log;
        std::vector<S> d;
        std::vector<F> lz;

        void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); }
        void all_apply(int k, F f)
        {
            d[k] = mapping(f, d[k]);
            if (k < size)
                lz[k] = composition(f, lz[k]);
        }
        void push(int k)
        {
            all_apply(2 * k, lz[k]);
            all_apply(2 * k + 1, lz[k]);
            lz[k] = id();
        }
    };

} // namespace atcoder

#endif // ATCODER_LAZYSEGTREE_HPP

using namespace atcoder;

struct S
{
    long long value;
    int size;
};

using F = long long;

S op(S a, S b) { return {a.value + b.value, a.size + b.size}; }
S e() { return {0, 0}; }
S mapping(F f, S x) { return {x.value + f * x.size, x.size}; }
F composition(F f, F g) { return f + g; }
F id() { return 0; }

int main(void)
{
    int n, m;
    cin >> n >> m;
    vector<S> v(m, {0, 1});
    lazy_segtree<S, op, e, F, mapping, composition, id> seg(v);
    set<pair<ll, ll>> greedy;
    rep(i, n)
    {
        ll a, b, c;
        cin >> a >> b >> c;
        b--;
        seg.apply(b, c, a);
        greedy.insert({b, -c});
    }
    int q;
    cin >> q;
    rep(i, q)
    {
        ll d, e;
        cin >> d >> e;
        e--;
        if (seg.get(e).value == 0)
        {
            cout << -1 << endl;
            return 0;
        }
        seg.apply(e, -d);
    }
    ll ans = 0;
    rep(i, m)
    {
        if(seg.get(i).value >= 0){
            continue;
        }
        auto dope_guard = greedy.lower_bound({i, -1e18});
        ll dope_from = dope_guard->first;
        ll dope_end = -(dope_guard->second);
        ans += -seg.get(i).value;
        seg.apply(dope_from, dope_end, -seg.get(i).value);
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 140
memory_kb: 15848
final_rank: 13

code_snippet: #include <algorithm>
#include <iostream>
#include <iomanip>
#include <limits.h>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#include <stack>

using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i < n + 1; i++)
#define all(A) A.begin(), A.end()
#define itr(A, l, r) A.begin() + l, A.begin() + r
#define debug(var) cout << #var << " = " << var << endl;
typedef long long ll;

#ifndef ATCODER_LAZYSEGTREE_HPP
#define ATCODER_LAZYSEGTREE_HPP 1

#include <algorithm>
#include <cassert>
#include <iostream>
#include <vector>

#ifndef ATCODER_INTERNAL_BITOP_HPP
#define ATCODER_INTERNAL_BITOP_HPP 1

#ifdef _MSC_VER
#include <intrin.h>
#endif

namespace atcoder
{

    namespace internal
    {

        // @param n `0 <= n`
        // @return minimum non-negative `x` s.t. `n <= 2**x`
        int ceil_pow2(int n)
        {
            int x = 0;
            while ((1U << x) < (unsigned int)(n))
                x++;
            return x;
        }

        // @param n `1 <= n`
        // @return minimum non-negative `x` s.t. `(n & (1 << x)) != 0`
        constexpr int bsf_constexpr(unsigned int n)
        {
            int x = 0;
            while (!(n & (1 << x)))
                x++;
            return x;
        }

        // @param n `1 <= n`
        // @return minimum non-negative `x` s.t. `(n & (1 << x)) != 0`
        int bsf(unsigned int n)
        {
#ifdef _MSC_VER
            unsigned long index;
            _BitScanForward(&index, n);
            return index;
#else
            return __builtin_ctz(n);
#endif
        }

    } // namespace internal

} // namespace atcoder

#endif // ATCODER_INTERNAL_BITOP_HPP

namespace atcoder
{

    template <class S,
              S (*op)(S, S),
              S (*e)(),
              class F,
              S (*mapping)(F, S),
              F (*composition)(F, F),
              F (*id)()>
    struct lazy_segtree
    {
    public:
        lazy_segtree() : lazy_segtree(0) {}
        explicit lazy_segtree(int n) : lazy_segtree(std::vector<S>(n, e())) {}
        explicit lazy_segtree(const std::vector<S> &v) : _n(int(v.size()))
        {
            log = internal::ceil_pow2(_n);
            size = 1 << log;
            d = std::vector<S>(2 * size, e());
            lz = std::vector<F>(size, id());
            for (int i = 0; i < _n; i++)
                d[size + i] = v[i];
            for (int i = size - 1; i >= 1; i--)
            {
                update(i);
            }
        }

        void set(int p, S x)
        {
            assert(0 <= p && p < _n);
            p += size;
            for (int i = log; i >= 1; i--)
                push(p >> i);
            d[p] = x;
            for (int i = 1; i <= log; i++)
                update(p >> i);
        }

        S get(int p)
        {
            assert(0 <= p && p < _n);
            p += size;
            for (int i = log; i >= 1; i--)
                push(p >> i);
            return d[p];
        }

        S prod(int l, int r)
        {
            assert(0 <= l && l <= r && r <= _n);
            if (l == r)
                return e();

            l += size;
            r += size;

            for (int i = log; i >= 1; i--)
            {
                if (((l >> i) << i) != l)
                    push(l >> i);
                if (((r >> i) << i) != r)
                    push((r - 1) >> i);
            }

            S sml = e(), smr = e();
            while (l < r)
            {
                if (l & 1)
                    sml = op(sml, d[l++]);
                if (r & 1)
                    smr = op(d[--r], smr);
                l >>= 1;
                r >>= 1;
            }

            return op(sml, smr);
        }

        S all_prod() { return d[1]; }

        void apply(int p, F f)
        {
            assert(0 <= p && p < _n);
            p += size;
            for (int i = log; i >= 1; i--)
                push(p >> i);
            d[p] = mapping(f, d[p]);
            for (int i = 1; i <= log; i++)
                update(p >> i);
        }
        void apply(int l, int r, F f)
        {
            assert(0 <= l && l <= r && r <= _n);
            if (l == r)
                return;

            l += size;
            r += size;

            for (int i = log; i >= 1; i--)
            {
                if (((l >> i) << i) != l)
                    push(l >> i);
                if (((r >> i) << i) != r)
                    push((r - 1) >> i);
            }

            {
                int l2 = l, r2 = r;
                while (l < r)
                {
                    if (l & 1)
                        all_apply(l++, f);
                    if (r & 1)
                        all_apply(--r, f);
                    l >>= 1;
                    r >>= 1;
                }
                l = l2;
                r = r2;
            }

            for (int i = 1; i <= log; i++)
            {
                if (((l >> i) << i) != l)
                    update(l >> i);
                if (((r >> i) << i) != r)
                    update((r - 1) >> i);
            }
        }

        template <bool (*g)(S)>
        int max_right(int l)
        {
            return max_right(l, [](S x)
                             { return g(x); });
        }
        template <class G>
        int max_right(int l, G g)
        {
            assert(0 <= l && l <= _n);
            assert(g(e()));
            if (l == _n)
                return _n;
            l += size;
            for (int i = log; i >= 1; i--)
                push(l >> i);
            S sm = e();
            do
            {
                while (l % 2 == 0)
                    l >>= 1;
                if (!g(op(sm, d[l])))
                {
                    while (l < size)
                    {
                        push(l);
                        l = (2 * l);
                        if (g(op(sm, d[l])))
                        {
                            sm = op(sm, d[l]);
                            l++;
                        }
                    }
                    return l - size;
                }
                sm = op(sm, d[l]);
                l++;
            } while ((l & -l) != l);
            return _n;
        }

        template <bool (*g)(S)>
        int min_left(int r)
        {
            return min_left(r, [](S x)
                            { return g(x); });
        }
        template <class G>
        int min_left(int r, G g)
        {
            assert(0 <= r && r <= _n);
            assert(g(e()));
            if (r == 0)
                return 0;
            r += size;
            for (int i = log; i >= 1; i--)
                push((r - 1) >> i);
            S sm = e();
            do
            {
                r--;
                while (r > 1 && (r % 2))
                    r >>= 1;
                if (!g(op(d[r], sm)))
                {
                    while (r < size)
                    {
                        push(r);
                        r = (2 * r + 1);
                        if (g(op(d[r], sm)))
                        {
                            sm = op(d[r], sm);
                            r--;
                        }
                    }
                    return r + 1 - size;
                }
                sm = op(d[r], sm);
            } while ((r & -r) != r);
            return 0;
        }

    private:
        int _n, size, log;
        std::vector<S> d;
        std::vector<F> lz;

        void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); }
        void all_apply(int k, F f)
        {
            d[k] = mapping(f, d[k]);
            if (k < size)
                lz[k] = composition(f, lz[k]);
        }
        void push(int k)
        {
            all_apply(2 * k, lz[k]);
            all_apply(2 * k + 1, lz[k]);
            lz[k] = id();
        }
    };

} // namespace atcoder

#endif // ATCODER_LAZYSEGTREE_HPP

using namespace atcoder;

struct S
{
    long long value;
    int size;
};

using F = long long;

S op(S a, S b) { return {a.value + b.value, a.size + b.size}; }
S e() { return {0, 0}; }
S mapping(F f, S x) { return {x.value + f * x.size, x.size}; }
F composition(F f, F g) { return f + g; }
F id() { return 0; }

int main(void)
{
    int n, m;
    cin >> n >> m;
    vector<ll> guard(m);
    vector<ll> imos(m + 1);
    set<pair<ll, ll>> greedy;
    rep(i, n)
    {
        ll a, b, c;
        cin >> a >> b >> c;
        b--;
        imos[b] += a;
        imos[c] -= a;
        greedy.insert({b, -c});
    }
    auto print_vector = [](auto v)
    {
        for (auto i : v)
        {
            cout << i << ',';
        }
        cout << endl;
    };
    auto print_vector_pair = [](auto v)
    {
        for (auto i : v)
        {
            cout << "(" << i.first << "," << i.second << ")"
                 << " ";
        }
        cout << endl;
    };
    // print_vector(imos);
    rep(i, m)
    {
        guard[i] = imos[i];
        if (i > 0)
        {
            guard[i] += guard[i - 1];
        }
    }
    // print_vector(guard);
    int q;
    cin >> q;
    vector<ll> demons(m);
    rep(i, q)
    {
        ll d, e;
        cin >> d >> e;
        e--;
        demons[e] += d;
    }
    // print_vector(demons);
    vector<S> v(m, {0, 1});
    rep(i, m)
    {
        v[i] = {guard[i], 1};
    }
    lazy_segtree<S, op, e, F, mapping, composition, id> seg(v);
    // print_vector_pair(greedy);
    ll ans = 0;
    rep(i, m)
    {
        ll now_guard = seg.get(i).value;
        if (now_guard >= demons[i])
        {
            continue;
        }
        if (now_guard == 0)
        {
            cout << -1 << endl;
            return 0;
        }
        auto dope_guard = greedy.lower_bound({i, -1e18});
        ll dope_from = dope_guard->first;
        ll dope_end = -(dope_guard->second);
        ll dope_amount = demons[i] - now_guard;
        seg.apply(dope_from, dope_end, dope_amount);
        ans += dope_amount;
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 110
memory_kb: 18176
final_rank: 12

code_snippet: #include <algorithm>
#include <iostream>
#include <iomanip>
#include <limits.h>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#include <stack>

using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i < n + 1; i++)
#define all(A) A.begin(), A.end()
#define itr(A, l, r) A.begin() + l, A.begin() + r
#define debug(var) cout << #var << " = " << var << endl;
typedef long long ll;

#ifndef ATCODER_LAZYSEGTREE_HPP
#define ATCODER_LAZYSEGTREE_HPP 1

#include <algorithm>
#include <cassert>
#include <iostream>
#include <vector>

#ifndef ATCODER_INTERNAL_BITOP_HPP
#define ATCODER_INTERNAL_BITOP_HPP 1

#ifdef _MSC_VER
#include <intrin.h>
#endif

namespace atcoder
{

    namespace internal
    {

        // @param n `0 <= n`
        // @return minimum non-negative `x` s.t. `n <= 2**x`
        int ceil_pow2(int n)
        {
            int x = 0;
            while ((1U << x) < (unsigned int)(n))
                x++;
            return x;
        }

        // @param n `1 <= n`
        // @return minimum non-negative `x` s.t. `(n & (1 << x)) != 0`
        constexpr int bsf_constexpr(unsigned int n)
        {
            int x = 0;
            while (!(n & (1 << x)))
                x++;
            return x;
        }

        // @param n `1 <= n`
        // @return minimum non-negative `x` s.t. `(n & (1 << x)) != 0`
        int bsf(unsigned int n)
        {
#ifdef _MSC_VER
            unsigned long index;
            _BitScanForward(&index, n);
            return index;
#else
            return __builtin_ctz(n);
#endif
        }

    } // namespace internal

} // namespace atcoder

#endif // ATCODER_INTERNAL_BITOP_HPP

namespace atcoder
{

    template <class S,
              S (*op)(S, S),
              S (*e)(),
              class F,
              S (*mapping)(F, S),
              F (*composition)(F, F),
              F (*id)()>
    struct lazy_segtree
    {
    public:
        lazy_segtree() : lazy_segtree(0) {}
        explicit lazy_segtree(int n) : lazy_segtree(std::vector<S>(n, e())) {}
        explicit lazy_segtree(const std::vector<S> &v) : _n(int(v.size()))
        {
            log = internal::ceil_pow2(_n);
            size = 1 << log;
            d = std::vector<S>(2 * size, e());
            lz = std::vector<F>(size, id());
            for (int i = 0; i < _n; i++)
                d[size + i] = v[i];
            for (int i = size - 1; i >= 1; i--)
            {
                update(i);
            }
        }

        void set(int p, S x)
        {
            assert(0 <= p && p < _n);
            p += size;
            for (int i = log; i >= 1; i--)
                push(p >> i);
            d[p] = x;
            for (int i = 1; i <= log; i++)
                update(p >> i);
        }

        S get(int p)
        {
            assert(0 <= p && p < _n);
            p += size;
            for (int i = log; i >= 1; i--)
                push(p >> i);
            return d[p];
        }

        S prod(int l, int r)
        {
            assert(0 <= l && l <= r && r <= _n);
            if (l == r)
                return e();

            l += size;
            r += size;

            for (int i = log; i >= 1; i--)
            {
                if (((l >> i) << i) != l)
                    push(l >> i);
                if (((r >> i) << i) != r)
                    push((r - 1) >> i);
            }

            S sml = e(), smr = e();
            while (l < r)
            {
                if (l & 1)
                    sml = op(sml, d[l++]);
                if (r & 1)
                    smr = op(d[--r], smr);
                l >>= 1;
                r >>= 1;
            }

            return op(sml, smr);
        }

        S all_prod() { return d[1]; }

        void apply(int p, F f)
        {
            assert(0 <= p && p < _n);
            p += size;
            for (int i = log; i >= 1; i--)
                push(p >> i);
            d[p] = mapping(f, d[p]);
            for (int i = 1; i <= log; i++)
                update(p >> i);
        }
        void apply(int l, int r, F f)
        {
            assert(0 <= l && l <= r && r <= _n);
            if (l == r)
                return;

            l += size;
            r += size;

            for (int i = log; i >= 1; i--)
            {
                if (((l >> i) << i) != l)
                    push(l >> i);
                if (((r >> i) << i) != r)
                    push((r - 1) >> i);
            }

            {
                int l2 = l, r2 = r;
                while (l < r)
                {
                    if (l & 1)
                        all_apply(l++, f);
                    if (r & 1)
                        all_apply(--r, f);
                    l >>= 1;
                    r >>= 1;
                }
                l = l2;
                r = r2;
            }

            for (int i = 1; i <= log; i++)
            {
                if (((l >> i) << i) != l)
                    update(l >> i);
                if (((r >> i) << i) != r)
                    update((r - 1) >> i);
            }
        }

        template <bool (*g)(S)>
        int max_right(int l)
        {
            return max_right(l, [](S x)
                             { return g(x); });
        }
        template <class G>
        int max_right(int l, G g)
        {
            assert(0 <= l && l <= _n);
            assert(g(e()));
            if (l == _n)
                return _n;
            l += size;
            for (int i = log; i >= 1; i--)
                push(l >> i);
            S sm = e();
            do
            {
                while (l % 2 == 0)
                    l >>= 1;
                if (!g(op(sm, d[l])))
                {
                    while (l < size)
                    {
                        push(l);
                        l = (2 * l);
                        if (g(op(sm, d[l])))
                        {
                            sm = op(sm, d[l]);
                            l++;
                        }
                    }
                    return l - size;
                }
                sm = op(sm, d[l]);
                l++;
            } while ((l & -l) != l);
            return _n;
        }

        template <bool (*g)(S)>
        int min_left(int r)
        {
            return min_left(r, [](S x)
                            { return g(x); });
        }
        template <class G>
        int min_left(int r, G g)
        {
            assert(0 <= r && r <= _n);
            assert(g(e()));
            if (r == 0)
                return 0;
            r += size;
            for (int i = log; i >= 1; i--)
                push((r - 1) >> i);
            S sm = e();
            do
            {
                r--;
                while (r > 1 && (r % 2))
                    r >>= 1;
                if (!g(op(d[r], sm)))
                {
                    while (r < size)
                    {
                        push(r);
                        r = (2 * r + 1);
                        if (g(op(d[r], sm)))
                        {
                            sm = op(d[r], sm);
                            r--;
                        }
                    }
                    return r + 1 - size;
                }
                sm = op(d[r], sm);
            } while ((r & -r) != r);
            return 0;
        }

    private:
        int _n, size, log;
        std::vector<S> d;
        std::vector<F> lz;

        void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); }
        void all_apply(int k, F f)
        {
            d[k] = mapping(f, d[k]);
            if (k < size)
                lz[k] = composition(f, lz[k]);
        }
        void push(int k)
        {
            all_apply(2 * k, lz[k]);
            all_apply(2 * k + 1, lz[k]);
            lz[k] = id();
        }
    };

} // namespace atcoder

#endif // ATCODER_LAZYSEGTREE_HPP

using namespace atcoder;

struct S
{
    long long value;
    int size;
};

using F = long long;

S op(S a, S b) { return {a.value + b.value, a.size + b.size}; }
S e() { return {0, 0}; }
S mapping(F f, S x) { return {x.value + f * x.size, x.size}; }
F composition(F f, F g) { return f + g; }
F id() { return 0; }

int main(void)
{
    int n, m;
    cin >> n >> m;
    vector<ll> guard(m);
    vector<ll> imos(m + 1);
    set<pair<ll, ll>> greedy;
    rep(i, n)
    {
        ll a, b, c;
        cin >> a >> b >> c;
        b--;
        imos[b] += a;
        imos[c] -= a;
        greedy.insert({b, -c});
    }
    auto print_vector = [](auto v)
    {
        for (auto i : v)
        {
            cout << i << ',';
        }
        cout << endl;
    };
    auto print_vector_pair = [](auto v)
    {
        for (auto i : v)
        {
            cout << "(" << i.first << "," << i.second << ")"
                 << " ";
        }
        cout << endl;
    };
    // print_vector(imos);
    rep(i, m)
    {
        guard[i] = imos[i];
        if (i > 0)
        {
            guard[i] += guard[i - 1];
        }
    }
    // print_vector(guard);
    int q;
    cin >> q;
    vector<ll> demons(m);
    rep(i, q)
    {
        ll d, e;
        cin >> d >> e;
        e--;
        demons[e] += d;
    }
    // print_vector(demons);
    vector<S> v(m, {0, 1});
    rep(i, m)
    {
        v[i] = {guard[i], 1};
    }
    lazy_segtree<S, op, e, F, mapping, composition, id> seg(v);
    // print_vector_pair(greedy);
    ll ans = 0;
    rep(i, m)
    {
        ll now_guard = seg.get(i).value;
        if (now_guard == 0)
        {
            cout << -1 << endl;
            return 0;
        }
        if (now_guard >= demons[i])
        {
            continue;
        }
        auto dope_guard = greedy.lower_bound({i, -1e18});
        ll dope_from = dope_guard->first;
        ll dope_end = -(dope_guard->second);
        ll dope_amount = demons[i] - now_guard;
        seg.apply(dope_from, dope_end, dope_amount);
        ans += dope_amount;
    }
    cout << ans << endl;
}
accuracy: 0.9642857142857143
time_ms: 110
memory_kb: 18144
final_rank: 14

code_snippet: #include <algorithm>
#include <iostream>
#include <iomanip>
#include <limits.h>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#include <stack>

using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i < n + 1; i++)
#define all(A) A.begin(), A.end()
#define itr(A, l, r) A.begin() + l, A.begin() + r
#define debug(var) cout << #var << " = " << var << endl;
typedef long long ll;

#ifndef ATCODER_LAZYSEGTREE_HPP
#define ATCODER_LAZYSEGTREE_HPP 1

#include <algorithm>
#include <cassert>
#include <iostream>
#include <vector>

#ifndef ATCODER_INTERNAL_BITOP_HPP
#define ATCODER_INTERNAL_BITOP_HPP 1

#ifdef _MSC_VER
#include <intrin.h>
#endif

namespace atcoder
{

    namespace internal
    {

        // @param n `0 <= n`
        // @return minimum non-negative `x` s.t. `n <= 2**x`
        int ceil_pow2(int n)
        {
            int x = 0;
            while ((1U << x) < (unsigned int)(n))
                x++;
            return x;
        }

        // @param n `1 <= n`
        // @return minimum non-negative `x` s.t. `(n & (1 << x)) != 0`
        constexpr int bsf_constexpr(unsigned int n)
        {
            int x = 0;
            while (!(n & (1 << x)))
                x++;
            return x;
        }

        // @param n `1 <= n`
        // @return minimum non-negative `x` s.t. `(n & (1 << x)) != 0`
        int bsf(unsigned int n)
        {
#ifdef _MSC_VER
            unsigned long index;
            _BitScanForward(&index, n);
            return index;
#else
            return __builtin_ctz(n);
#endif
        }

    } // namespace internal

} // namespace atcoder

#endif // ATCODER_INTERNAL_BITOP_HPP

namespace atcoder
{

    template <class S,
              S (*op)(S, S),
              S (*e)(),
              class F,
              S (*mapping)(F, S),
              F (*composition)(F, F),
              F (*id)()>
    struct lazy_segtree
    {
    public:
        lazy_segtree() : lazy_segtree(0) {}
        explicit lazy_segtree(int n) : lazy_segtree(std::vector<S>(n, e())) {}
        explicit lazy_segtree(const std::vector<S> &v) : _n(int(v.size()))
        {
            log = internal::ceil_pow2(_n);
            size = 1 << log;
            d = std::vector<S>(2 * size, e());
            lz = std::vector<F>(size, id());
            for (int i = 0; i < _n; i++)
                d[size + i] = v[i];
            for (int i = size - 1; i >= 1; i--)
            {
                update(i);
            }
        }

        void set(int p, S x)
        {
            assert(0 <= p && p < _n);
            p += size;
            for (int i = log; i >= 1; i--)
                push(p >> i);
            d[p] = x;
            for (int i = 1; i <= log; i++)
                update(p >> i);
        }

        S get(int p)
        {
            assert(0 <= p && p < _n);
            p += size;
            for (int i = log; i >= 1; i--)
                push(p >> i);
            return d[p];
        }

        S prod(int l, int r)
        {
            assert(0 <= l && l <= r && r <= _n);
            if (l == r)
                return e();

            l += size;
            r += size;

            for (int i = log; i >= 1; i--)
            {
                if (((l >> i) << i) != l)
                    push(l >> i);
                if (((r >> i) << i) != r)
                    push((r - 1) >> i);
            }

            S sml = e(), smr = e();
            while (l < r)
            {
                if (l & 1)
                    sml = op(sml, d[l++]);
                if (r & 1)
                    smr = op(d[--r], smr);
                l >>= 1;
                r >>= 1;
            }

            return op(sml, smr);
        }

        S all_prod() { return d[1]; }

        void apply(int p, F f)
        {
            assert(0 <= p && p < _n);
            p += size;
            for (int i = log; i >= 1; i--)
                push(p >> i);
            d[p] = mapping(f, d[p]);
            for (int i = 1; i <= log; i++)
                update(p >> i);
        }
        void apply(int l, int r, F f)
        {
            assert(0 <= l && l <= r && r <= _n);
            if (l == r)
                return;

            l += size;
            r += size;

            for (int i = log; i >= 1; i--)
            {
                if (((l >> i) << i) != l)
                    push(l >> i);
                if (((r >> i) << i) != r)
                    push((r - 1) >> i);
            }

            {
                int l2 = l, r2 = r;
                while (l < r)
                {
                    if (l & 1)
                        all_apply(l++, f);
                    if (r & 1)
                        all_apply(--r, f);
                    l >>= 1;
                    r >>= 1;
                }
                l = l2;
                r = r2;
            }

            for (int i = 1; i <= log; i++)
            {
                if (((l >> i) << i) != l)
                    update(l >> i);
                if (((r >> i) << i) != r)
                    update((r - 1) >> i);
            }
        }

        template <bool (*g)(S)>
        int max_right(int l)
        {
            return max_right(l, [](S x)
                             { return g(x); });
        }
        template <class G>
        int max_right(int l, G g)
        {
            assert(0 <= l && l <= _n);
            assert(g(e()));
            if (l == _n)
                return _n;
            l += size;
            for (int i = log; i >= 1; i--)
                push(l >> i);
            S sm = e();
            do
            {
                while (l % 2 == 0)
                    l >>= 1;
                if (!g(op(sm, d[l])))
                {
                    while (l < size)
                    {
                        push(l);
                        l = (2 * l);
                        if (g(op(sm, d[l])))
                        {
                            sm = op(sm, d[l]);
                            l++;
                        }
                    }
                    return l - size;
                }
                sm = op(sm, d[l]);
                l++;
            } while ((l & -l) != l);
            return _n;
        }

        template <bool (*g)(S)>
        int min_left(int r)
        {
            return min_left(r, [](S x)
                            { return g(x); });
        }
        template <class G>
        int min_left(int r, G g)
        {
            assert(0 <= r && r <= _n);
            assert(g(e()));
            if (r == 0)
                return 0;
            r += size;
            for (int i = log; i >= 1; i--)
                push((r - 1) >> i);
            S sm = e();
            do
            {
                r--;
                while (r > 1 && (r % 2))
                    r >>= 1;
                if (!g(op(d[r], sm)))
                {
                    while (r < size)
                    {
                        push(r);
                        r = (2 * r + 1);
                        if (g(op(d[r], sm)))
                        {
                            sm = op(d[r], sm);
                            r--;
                        }
                    }
                    return r + 1 - size;
                }
                sm = op(d[r], sm);
            } while ((r & -r) != r);
            return 0;
        }

    private:
        int _n, size, log;
        std::vector<S> d;
        std::vector<F> lz;

        void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); }
        void all_apply(int k, F f)
        {
            d[k] = mapping(f, d[k]);
            if (k < size)
                lz[k] = composition(f, lz[k]);
        }
        void push(int k)
        {
            all_apply(2 * k, lz[k]);
            all_apply(2 * k + 1, lz[k]);
            lz[k] = id();
        }
    };

} // namespace atcoder

#endif // ATCODER_LAZYSEGTREE_HPP

using namespace atcoder;

struct S
{
    long long value;
    int size;
};

using F = long long;

S op(S a, S b) { return {a.value + b.value, a.size + b.size}; }
S e() { return {0, 0}; }
S mapping(F f, S x) { return {x.value + f * x.size, x.size}; }
F composition(F f, F g) { return f + g; }
F id() { return 0; }

int main(void)
{
    int n, m;
    cin >> n >> m;
    vector<ll> guard(m);
    vector<ll> imos(m + 1);
    set<pair<ll, ll>> greedy;
    rep(i, n)
    {
        ll a, b, c;
        cin >> a >> b >> c;
        b--;
        imos[b] += a;
        imos[c] -= a;
        greedy.insert({b, -c});
    }
    auto print_vector = [](auto v)
    {
        for (auto i : v)
        {
            cout << i << ',';
        }
        cout << endl;
    };
    auto print_vector_pair = [](auto v)
    {
        for (auto i : v)
        {
            cout << "(" << i.first << "," << i.second << ")"
                 << " ";
        }
        cout << endl;
    };
    // print_vector(imos);
    rep(i, m)
    {
        guard[i] = imos[i];
        if (i > 0)
        {
            guard[i] += guard[i - 1];
        }
    }
    // print_vector(guard);
    int q;
    cin >> q;
    vector<ll> demons(m);
    rep(i, q)
    {
        ll d, e;
        cin >> d >> e;
        e--;
        demons[e] += d;
    }
    // print_vector(demons);
    vector<S> v(m, {0, 1});
    rep(i, m)
    {
        v[i] = {guard[i], 1};
    }
    lazy_segtree<S, op, e, F, mapping, composition, id> seg(v);
    // print_vector_pair(greedy);
    ll ans = 0;
    rep(i, m)
    {
        ll now_guard = seg.get(i).value;
        if (now_guard == 0)
        {
            cout << -1 << endl;
            return 0;
        }
        if (now_guard >= demons[i])
        {
            continue;
        }
        auto dope_guard = greedy.lower_bound({i, -1e18});
        ll dope_from = dope_guard->first;
        ll dope_end = -(dope_guard->second);
        ll dope_amount = demons[i] - now_guard;
        seg.apply(dope_from, dope_end, dope_amount);
        ans += dope_amount;
    }
    cout << ans << endl;
}
accuracy: 0.9642857142857143
time_ms: 110
memory_kb: 18324
final_rank: 15

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll=long long;
using ld=long double;
ll inf=10010010010010010;
ll mod=998244353 ;
int main(){
    ll n,m;
    cin >> n >> m;
    vector<ll>c(n);
    vector<ll>imos(m+2,0),last(m+2,-1);
    for (ll i = 0; i < n; i++)
    {
        ll a,b;
        cin >> c[i] >> a >> b;
        imos[a-1]+=c[i];
        imos[b]-=c[i];
        last[a-1]=b-1;
        last[b-1]=b-1;
    }
    for (ll i = 1; i <=m; i++)
    {
        last[i]=max(last[i-1],last[i]);
    }
    ll q;
    cin >> q;
    vector<ll>memo(m+1,0);
    for (ll i = 0; i < q; i++)
    {
        ll d,e;
        cin >> d >> e;
        memo[e-1]+=d;
    }
    ll ans=0;
    for (ll i = 0; i <m; i++)
    {
        if (i==0)
        {
            if (imos[i]<memo[i])
            {
                if (last[i]<i)
                {
                    cout << -1 << endl;
                    return 0;
                }else{
                    ll k=memo[i]-imos[i];
                    imos[i]+=k;
                    imos[last[i]+1]-=k;
                    ans+=k;
                }
            }
            continue;
        }
        imos[i]+=imos[i-1];
        if (imos[i]<memo[i])
        {
            if (last[i]<i)
            {
                cout << -1 << endl;
                return 0;
            }else{
                ll k=memo[i]-imos[i];
                imos[i]+=k;
                imos[last[i]+1]-=k;
                ans+=k;
            }
        }
    }
    cout << ans << endl;
}
accuracy: 0.07142857142857142
time_ms: 40
memory_kb: 3572
final_rank: 18

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll=long long;
using ld=long double;
ll inf=10010010010010010;
ll mod=998244353 ;
int main(){
    ll n,m;
    cin >> n >> m;
    vector<ll>c(n);
    vector<ll>imos(m+1,0),last(m+1,-1);
    for (ll i = 0; i < n; i++)
    {
        ll a,b;
        cin >> c[i] >> a >> b;
        imos[a-1]+=c[i];
        imos[b]-=c[i];
        last[a-1]=b-1;
        last[b-1]=b-1;
    }
    for (ll i = 1; i <=m; i++)
    {
        last[i]=max(last[i-1],last[i]);
    }
    ll q;
    cin >> q;
    vector<ll>memo(m+1,0);
    for (ll i = 0; i < q; i++)
    {
        ll d,e;
        cin >> d >> e;
        memo[e-1]+=d;
    }
    ll ans=0;
    for (ll i = 0; i <m; i++)
    {
        if (i==0)
        {
            if (imos[i]<memo[i])
            {
                if (last[i]<i)
                {
                    cout << -1 << endl;
                    return 0;
                }else{
                    ll k=memo[i]-imos[i];
                    imos[i]+=k;
                    imos[last[i]+1]-=k;
                    ans+=k;
                }
            }
            continue;
        }
        imos[i]+=imos[i-1];
        if (imos[i]<memo[i])
        {
            if (last[i]<i)
            {
                cout << -1 << endl;
                return 0;
            }else{
                ll k=memo[i]-imos[i];
                imos[i]+=k;
                imos[last[i]+1]-=k;
                ans+=k;
            }
        }
    }
    cout << ans << endl;
}
accuracy: 0.07142857142857142
time_ms: 40
memory_kb: 3780
final_rank: 19

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, x, n) for (int i = x; i <= n; i++)
#define rep3(i, x, n) for (int i = x; i >= n; i--)
#define each(e, v) for (auto &e : v)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) (int)x.size()
using ll = long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;

template <typename T>
bool chmax(T &x, const T &y) {
    return (x < y) ? (x = y, true) : false;
}

template <typename T>
bool chmin(T &x, const T &y) {
    return (x > y) ? (x = y, true) : false;
}

template <typename T>
int flg(T x, int i) {
    return (x >> i) & 1;
}

template <typename T>
void print(const vector<T> &v, T x = 0) {
    int n = v.size();
    for (int i = 0; i < n; i++) cout << v[i] + x << (i == n - 1 ? '\n' : ' ');
    if (v.empty()) cout << '\n';
}

template <typename T>
void printn(const vector<T> &v, T x = 0) {
    int n = v.size();
    for (int i = 0; i < n; i++) cout << v[i] + x << '\n';
}

template <typename T>
int lb(const vector<T> &v, T x) {
    return lower_bound(begin(v), end(v), x) - begin(v);
}

template <typename T>
int ub(const vector<T> &v, T x) {
    return upper_bound(begin(v), end(v), x) - begin(v);
}

template <typename T>
void rearrange(vector<T> &v) {
    sort(begin(v), end(v));
    v.erase(unique(begin(v), end(v)), end(v));
}

template <typename T>
vector<int> id_sort(const vector<T> &v, bool greater = false) {
    int n = v.size();
    vector<int> ret(n);
    iota(begin(ret), end(ret), 0);
    sort(begin(ret), end(ret), [&](int i, int j) { return greater ? v[i] > v[j] : v[i] < v[j]; });
    return ret;
}

template <typename S, typename T>
pair<S, T> operator+(const pair<S, T> &p, const pair<S, T> &q) {
    return make_pair(p.first + q.first, p.second + q.second);
}

template <typename S, typename T>
pair<S, T> operator-(const pair<S, T> &p, const pair<S, T> &q) {
    return make_pair(p.first - q.first, p.second - q.second);
}

template <typename S, typename T>
istream &operator>>(istream &is, pair<S, T> &p) {
    S a;
    T b;
    is >> a >> b;
    p = make_pair(a, b);
    return is;
}

template <typename S, typename T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
    return os << p.first << ' ' << p.second;
}

struct io_setup {
    io_setup() {
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        cout << fixed << setprecision(15);
    }
} io_setup;

const int inf = (1 << 30) - 1;
const ll INF = (1LL << 60) - 1;
const int MOD = 1000000007;
// const int MOD = 998244353;

int main() {
    int N, M;
    cin >> N >> M;

    vector<ll> A(N);
    vector<int> L(N), R(N);
    vector<vector<int>> ids1(M + 1), ids2(M + 1);

    rep(i, N) {
        cin >> A[i] >> L[i] >> R[i];
        L[i]--;
        ids1[L[i]].eb(i), ids2[R[i]].eb(i);
    }

    vector<ll> D(M, 0);

    int Q;
    cin >> Q;
    while (Q--) {
        ll x;
        int i;
        cin >> x >> i;
        i--;
        D[i] += x;
    }

    ll S = 0, ans = 0;
    priority_queue<pli> que;

    rep(i, M) {
        each(e, ids2[i]) S -= A[e];
        while (!empty(que) && que.top().first >= i) que.pop();
        each(e, ids1[i]) {
            S += A[e];
            que.emplace(R[e], e);
        }

        if (S >= D[i]) continue;
        if (empty(que)) {
            cout << "-1\n";
            return 0;
        }
        ll R = D[i] - S;
        auto [r, j] = que.top();
        S += R;
        A[j] += R;
        ans += R;
    }

    cout << ans << '\n';
}
accuracy: 1
time_ms: 30
memory_kb: 16340
final_rank: 5

code_snippet: #include <bits/stdc++.h>
using namespace std;
template <typename T>
struct dual_segment_tree{
	int N;
	vector<T> ST;
	dual_segment_tree(int n){
		N = 1;
		while (N < n){
			N *= 2;
		}
		ST = vector<T>(N * 2 - 1, 0);
	}
	T operator [](int k){
		k += N - 1;
		T ans = ST[k];
		while (k > 0){
			k = (k - 1) / 2;
			ans += ST[k];
		}
		return ans;
	}
	void range_add(int L, int R, T x, int i, int l, int r){
		if (R <= l || r <= L){
			return;
		} else if (L <= l && r <= R){
			ST[i] += x;
			return;
		} else {
			int m = (l + r) / 2;
			range_add(L, R, x, i * 2 + 1, l, m);
			range_add(L, R, x, i * 2 + 2, m, r);
			return;
		}
	}
	void range_add(int L, int R, T x){
		range_add(L, R, x, 0, 0, N);
	}
};
int main(){
  int N, M;
  cin >> N >> M;
  vector<int> A(N), B(N), C(N);
  for (int i = 0; i < N; i++){
    cin >> A[i] >> B[i] >> C[i];
    B[i]--;
  }
  int Q;
  cin >> Q;
  vector<int> D(Q), E(Q);
  for (int i = 0; i < Q; i++){
    cin >> D[i] >> E[i];
    E[i]--;
  }
  dual_segment_tree<long long> ST(M);
  for (int i = 0; i < N; i++){
    ST.range_add(B[i], C[i], A[i]);
  }
  for (int i = 0; i < Q; i++){
    ST.range_add(E[i], E[i] + 1, -D[i]);
  }
  vector<vector<int>> add(M + 1);
  for (int i = 0; i < N; i++){
    add[B[i]].push_back(C[i]);
  }
  bool ok = true;
  long long ans = 0;
  priority_queue<int> pq;
  for (int i = 0; i < M; i++){
    for (int x : add[i]){
      pq.push(x);
    }
    while (!pq.empty()){
      int x = pq.top();
      if (x == i){
        pq.pop();
      } else {
        break;
      }
    }
    if (ST[i] < 0){
      long long x = -ST[i];
      if (pq.empty()){
        ok = false;
        break;
      } else {
        int p = pq.top();
        ans += x;
        ST.range_add(i, p, x);
      }
    }
  }
  if (!ok){
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }
}
accuracy: 1
time_ms: 100
memory_kb: 13080
final_rank: 9

code_snippet: #include<bits/stdc++.h>
//#include<atcoder/all>

using namespace std;
//using namespace atcoder;
using ll = long long;

int main() {
    int n, m; cin >> n >> m;
    vector<ll> imos(m + 1), r(m, -1);
    for (int i = 0; i < n; ++i) {
        ll a, b, c; cin >> a >> b >> c; --b; --c;
        imos[b] += -a;
        imos[c + 1] += a;
        r[b] = max(r[b], c);
    }
    for (int i = 0; i < m; ++i) {
        imos[i + 1] += imos[i];
    }

    int q; cin >> q;
    for (int i = 0; i < q; ++i) {
        ll d, e; cin >> d >> e; --e;
        imos[e] += d;
    }

    ll ans = 0, rM = -1, now = 0;
    vector<ll> dec(m + 1);
    for (int i = 0; i < m; ++i) {
        rM = max(rM, r[i]);
        now += dec[i];
        if (imos[i] > now) {
            if (rM < i) {
                cout << -1 << endl;
                return 0;
            }
            ll dif = imos[i] - now;
            ans += dif;
            now += dif;
            dec[rM + 1] += -dif;
        }
    }

    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 5412
final_rank: 3

code_snippet: #include<bits/stdc++.h>
//#include<atcoder/all>

using namespace std;
//using namespace atcoder;
using ll = long long;

int main() {
    int n, m; cin >> n >> m;
    vector<ll> imos(m + 1), r(m, -1);
    for (int i = 0; i < n; ++i) {
        ll a, b, c; cin >> a >> b >> c; --b; --c;
        imos[b] += -a;
        imos[c + 1] += a;
        r[b] = max(r[b], c);
    }
    for (int i = 0; i < m; ++i) {
        imos[i + 1] += imos[i];
    }

    int q; cin >> q;
    for (int i = 0; i < q; ++i) {
        ll d, e; cin >> d >> e; --e;
        imos[e] += d;
    }

    ll ans = 0, rM = -1, now = 0;
    vector<ll> dec(m + 1);
    for (int i = 0; i < m; ++i) {
        rM = max(rM, r[i]);
        now += dec[i];
        if (imos[i] > now) {
            if (rM < i) {
                cout << -1 << endl;
                return 0;
            }
            ans += imos[i] - now;
            now += imos[i] - now;
            dec[rM + 1] += -(imos[i] - now);
        }
    }

    cout << ans << endl;
    return 0;
}
accuracy: 0.42857142857142855
time_ms: 70
memory_kb: 5408
final_rank: 16

code_snippet: #include<bits/stdc++.h>
//#include<atcoder/all>

using namespace std;
//using namespace atcoder;
using ll = long long;

int main() {
    int n, m; cin >> n >> m;
    vector<ll> imos(m + 1), r(m, -1);
    for (int i = 0; i < n; ++i) {
        ll a, b, c; cin >> a >> b >> c; --b; --c;
        imos[b] = -a;
        imos[c + 1] = a;
        r[b] = max(r[b], c);
    }
    for (int i = 0; i < m; ++i) {
        imos[i + 1] += imos[i];
    }

    int q; cin >> q;
    for (int i = 0; i < q; ++i) {
        ll d, e; cin >> d >> e; --e;
        imos[e] += d;
    }

    ll ans = 0, rM = -1, now = 0;
    vector<ll> dec(m + 1);
    for (int i = 0; i < m; ++i) {
        rM = max(rM, r[i]);
        now += dec[i];
        if (imos[i] > now) {
            if (rM < i) {
                cout << -1 << endl;
                return 0;
            }
            ans += imos[i] - now;
            now += imos[i] - now;
            dec[rM + 1] += -(imos[i] - now);
        }
    }

    cout << ans << endl;
    return 0;
}
accuracy: 0.07142857142857142
time_ms: 40
memory_kb: 3356
final_rank: 17

code_snippet: #include<bits/stdc++.h>
//#include<atcoder/all>

using namespace std;
//using namespace atcoder;
using ll = long long;

int main() {
    int n, m; cin >> n >> m;
    vector<ll> imos(m + 1), r(m, -1);
    for (int i = 0; i < n; ++i) {
        ll a, b, c; cin >> a >> b >> c; --b; --c;
        imos[b] =-a;
        imos[c + 1] = a;
        r[b] = max(r[b], c);
    }
    for (int i = 0; i < m; ++i) {
        imos[i + 1] += imos[i];
    }

    int q; cin >> q;
    for (int i = 0; i < q; ++i) {
        ll d, e; cin >> d >> e; --e;
        imos[e] += d;
    }

    ll ans = 0, rM = -1, now = 0;
    vector<ll> dec(m + 1);
    for (int i = 0; i < m; ++i) {
        rM = max(rM, r[i]);
        now += dec[i];
        if (imos[i] > now) {
            if (rM < i) {
                cout << -1 << endl;
                return 0;
            }
            ans += imos[i] - now;
            now += imos[i];
            dec[rM + 1] += -(imos[i] - now);
        }
    }

    cout << ans << endl;
    return 0;
}
accuracy: 0.07142857142857142
time_ms: 50
memory_kb: 3332
final_rank: 20

code_snippet: #pragma GCC optimize("Ofast")
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <cstdio>
#include <ctime>
#include <assert.h>
#include <chrono>
#include <random>
#include <numeric>
#include <set>
#include <deque>
#include <stack>
#include <sstream>
#include <utility>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <tuple>
#include <array>
#include <bitset>
using namespace std;
typedef long long int ll;
typedef unsigned long long ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
    return (ull)rng() % B;
}
inline ll time() {
    return static_cast<long double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;
}


int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n,m; cin >> n >> m;
    vector<ll> sum(m+2);
    vector<int> r(m+2);
    for(int i=0;i<n;i++){
        int a,b,c; cin >> a >> b >> c;
        r[b] = max(r[b],c);
        sum[b] += a;
        sum[c+1] -= a;
    }
    ll res = 0;
    int q; cin >> q;
    vector<ll> need(m+1);
    while(q--){
        int d,e; cin >> d >> e;
        need[e] += d;
    }
    int R = 1;
    for(int i=1;i<=m;i++){
        sum[i] += sum[i-1];
        if(need[i] > 0 and sum[i] == 0){
            cout << -1 << endl;
            return 0;
        }
        R = max(R, r[i]);
        ll u = max(0LL,need[i]-sum[i]);
        sum[i] += u;
        sum[R+1] -= u;
        res += u;
    }
    cout << res << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 5096
final_rank: 2

code_snippet: #include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#define int long long
#define rep(i, n) for(i = 0; i < n; i++)
using namespace std;
typedef pair<int, int> P;

int n, m;
vector<P> rval[100000];
int q, oni[100000];

signed main() {
	int i, j;
	
	cin >> n >> m;
	rep(i, n) {
		int a, b, c;
		cin >> a >> b >> c; b--; c--;
		rval[b].push_back(P(c, a));
	}
	
	cin >> q;
	rep(i, q) {
		int d, e;
		cin >> d >> e; e--;
		oni[e] += d;
	}
	
	multiset<P> dict;
	int dsum = 0;
	int ans = 0;
	rep(i, m) {
		rep(j, rval[i].size()) {
			P val = rval[i][j];
			dict.insert(val);
			dsum += val.second;
		}
		if (dsum < oni[i]) {
			if (dict.size() == 0) { cout << -1 << endl; return 0; }
			else {
				P res = *dict.rbegin();
				int diff = oni[i] - dsum;
				dict.erase(prev(dict.end()));
				ans += diff;
				dsum = oni[i];
				dict.insert(P(res.first, res.second + diff));
			}
		}
		
		set<P>::iterator it = dict.begin();
		while (it != dict.end()) {
			if (it->first == i) {
				dsum -= it->second;
				it = dict.erase(it);
			}
			else {
				break;
			}
		}
	}
	
	cout << ans << endl;
	return 0;
}
accuracy: 1
time_ms: 90
memory_kb: 10848
final_rank: 7