#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>;
ll n;
int base = 1<<19;
vector<pair<pair<ll, ll>, pair<ll, ll>>> T(2*base, {{-1,-1},{-1,1}}), T2(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 - (l1-r2)}};
} else {
return {{max(l1,l2), min(r1,r2)}, {c1+c2, ile1+ile2}};
}
}
void update(int v, int s, int e) {
int v2 = v;
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;
}
v=n-v2;
v+=base;
T2[v] = {{s, e-1}, {0, 1}};
v/=2;
while(v>0) {
T2[v] = merge(T2[v+v], T2[v+v+1]);
v/=2;
}
}
void pr(pair<pair<ll, ll>, pair<ll, ll>> x) {
cout << x.first.first << " " << x.first.second << " " << x.second.first << " " << x.second.second;
}
pair<pair<ll, ll>, pair<ll, ll>> query(int a, int b) {
if(a==b) return T[a];
if(a < b) {
--b;
a += base-1;
b += base+1;
pair<pair<ll, ll>, pair<ll, ll>> ans = {{-1,-1},{-1,0}};
vector<pair<pair<ll, ll>, pair<ll, ll>>> right;
while(a/2 != b/2) {
if(a%2==0) {
ans = merge(ans, T[a+1]);
// cout << "-> "; pr(T2[a+1]); cout << "\n";
// cout << "ans = "; pr(ans); cout << "\n";
}
if(b%2==1) {
// ans = merge(ans, T2[b-1]);
right.push_back(T[b-1]);
}
a/=2; b/=2;
}
while(right.size()) {
ans = merge(ans, right.back());
// cout << "-> "; pr(right.back()); cout << "\n";
right.pop_back();
}
return ans;
} else {
a = n-a+1;
b = n-b+1;
--b;
a += base-1;
b += base+1;
pair<pair<ll, ll>, pair<ll, ll>> ans = {{-1,-1},{-1,0}};
vector<pair<pair<ll, ll>, pair<ll, ll>>> right;
while(a/2 != b/2) {
if(a%2==0) {
ans = merge(ans, T2[a+1]);
// cout << "-> "; pr(T2[a+1]); cout << "\n";
// cout << "ans = "; pr(ans); cout << "\n";
}
if(b%2==1) {
// ans = merge(ans, T2[b-1]);
right.push_back(T2[b-1]);
}
a/=2; b/=2;
}
while(right.size()) {
ans = merge(ans, right.back());
// cout << "-> "; pr(right.back()); cout << "\n";
right.pop_back();
}
// cout << "\n";
return ans;
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int 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;
if(a==c) {
cout << max(0, b-d) << "\n";
continue;
}
auto[stat, stat2] = query(a, c);
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... |