#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
typedef long long ll;
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
int base = 1<<19;
vector<pair<pair<ll, ll>, pair<ll, ll>>> T(2*base, {{-1,-1},{-1,1}});
pair<pair<ll, ll>, pair<ll, ll>> merge(pair<pair<ll, ll>, pair<ll, ll>> a, pair<pair<ll, ll>, pair<ll, ll>> b) {
auto[l1,r1] = a.first;
auto[l2,r2] = b.first;
auto[c1, ile1] = a.second;
auto[c2, ile2] = b.second;
if(l1==-1) return b;
if(l2==-1) return a;
l2 -= ile1; r2 -= ile1;
if(l2 > r1) {
return {{r1,r1}, {c1+c2, ile1+ile2 + l2-r1}};
} else if(l1 > r2) {
return {{l1,l1}, {c1+c2+l1-r2, ile1+ile2}};
} else {
if(l2 < l1) {
swap(l1,l2);
swap(r1,r2);
}
return {{min(l2,r2), max(l1,r1)}, {c1+c2, ile1+ile2}};
}
}
void update(int v, int s, int e) {
v += base;
T[v] = {{s, e-1}, {0, 1}};
v/=2;
while(v>0) {
T[v] = merge(T[v+v], T[v+v+1]);
v/=2;
}
}
pair<pair<ll, ll>, pair<ll, ll>> query(int a, int b) {
if(a==b) return T[a];
a += base-1;
b += base+1;
pair<pair<ll, ll>, pair<ll, ll>> ans = {{-1,-1},{-1,0}};
while(a/2 != b/2) {
if(a%2==0) ans = merge(T[a+1], ans);
if(b%2==1) ans = merge(ans, T[b-1]);
a/=2; b/=2;
}
return ans;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n, q;
cin >> n >> q;
for(int i=1; i<n; ++i) {
int s, e;
cin >> s >> e;
update(i, s, e);
}
while(q--) {
int t; cin >> t;
if(t==1) {
int p, s, e;
cin >> p >> s >> e;
update(p,s,e);
} else {
int a,b,c,d;
cin >> a >> b >> c >> d;
auto[stat, stat2] = query(a, c-1);
auto[l, r] = stat;
auto[cost, ile] = stat2;
// cout << l << " " << r << " " << cost << " " << ile << "\n";
ll time = b;
ll res = 0;
if(time < l) time = l;
else if(time > r) {
res += time-r;
time = r;
}
res += cost;
time += ile;
res += max(0LL, time-d);
cout << res << "\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... |