#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define st first
#define nd second
#define pb push_back
const ll INF = 1e18;
struct wi {
    ll fi;
    ll kiedy;
    ll mus;
    ll zapas;
};
wi wyznacz(wi a, wi b) {
    wi w;
    w.fi = a.fi;
    w.mus = a.mus;
    ll mom = a.kiedy;
    if (a.kiedy <= b.fi) {
        w.kiedy = b.kiedy;
        w.mus += b.mus;
        w.zapas = min(a.zapas, b.zapas);
        return w;
    }
    ll r = a.kiedy - b.fi;
    if (r <= b.zapas) {
        w.kiedy = b.kiedy + r;
        w.zapas = min(a.zapas, b.zapas - r);
        return w;
    }
    ll roz = r - b.zapas;
    w.mus += roz;
    w.mus += b.mus;
    w.kiedy = b.kiedy + b.zapas;
    w.zapas = 0ll;
    return w;
}
int main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int n, q;
    cin >> n >> q;
    pair<ll, ll> pom[n];
    wi T[n];
    rep(i, n - 1) {
        cin >> pom[i].st >> pom[i].nd;
        T[i].fi = pom[i].st;
        T[i].kiedy = pom[i].st + 1;
        T[i].zapas = pom[i].nd - pom[i].st - 1;
        T[i].mus = 0ll;
    }
    wi A;
    rep(cos, q) {
        int t;
        cin >> t;
        if (t == 1) {
            int p; ll s, e;
            cin >> p >> s >> e;
            p--;
            T[p].fi = s;
            T[p].kiedy = s + 1;
            T[p].zapas = e - s - 1;
            T[p].mus = 0ll;
        }
        else {
            int a, c;
            ll b, d;
            cin >> a >> b >> c >> d;
            a--;
            c--;
            wi X;
            X.fi = b;
            X.kiedy = b;
            X.mus = 0ll;
            X.zapas = INF;
            while (a > c) {
                wi droga = T[a - 1];
                X = wyznacz(X, droga);
                a--;
            }
            while (a < c) {
                wi droga = T[a];
                X = wyznacz(X, droga);
                a++;
            }
            ll ans = max(0ll, X.kiedy - d);
            ans += X.mus;
            cout << ans << '\n';
        }
    }
    return 0;
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |