Submission #106270

#TimeUsernameProblemLanguageResultExecution timeMemory
106270realityBitaro, who Leaps through Time (JOI19_timeleap)C++17
100 / 100
1175 ms138648 KiB
#include "bits/stdc++.h" using namespace std; #define fi first #define se second #define ll long long #define dbg(v) cerr<<#v<<" = "<<v<<'\n' #define vi vector<int> #define vl vector <ll> #define pii pair<int,int> #define vii vector < pii > #define mp make_pair #define db long double #define pb push_back #define all(s) s.begin(),s.end() template < class P , class Q > ostream& operator<<(ostream& stream, pair < P , Q > v){ stream << "(" << v.fi << ',' << v.se << ")"; return stream;} template < class T > ostream& operator<<(ostream& stream, const vector<T> v){ stream << "[ "; for (int i=0; i<(int)v.size(); i++) stream << v[i] << " "; stream << "]"; return stream;} template < class T > T smin(T &a,T b) {if (a > b) a = b;return a;} template < class T > T smax(T &a,T b) {if (a < b) a = b;return a;} const ll oo = 1e10; const ll OO = oo; const int N = 5e5 + 5; int n; struct st { ll l,r,ans,t; st(void) { l = -oo; r = oo; ans = 0; t = OO; } st(ll a,ll b) { l = a; r = b; t = OO; ans = 0; } st (ll a,ll b,ll c,ll d) { l = a; r = b; t = c; ans = d; } }; ll cst(ll v,ll r) {return max(0ll,v - r);} ll nrm(ll v,ll l,ll r) {return v > r ? r : v < l ? l : v;} st operator + (st a,st b) { st c; if (a.t != OO) { if (b.t != OO) { return st(a.l,a.r,b.t,a.ans + b.ans + cst(a.t,b.r)); } else { return st(a.l,a.r,nrm(a.t,b.l,b.r),a.ans + cst(a.t,b.r)); } } else { if (a.r < b.l) { if (b.t != OO) { return st(a.r,a.r,b.t,b.ans); } else { return st(a.r,a.r,b.l,b.ans); } } else if (a.l > b.r) { if (b.t != OO) { return st(a.l,a.l,b.t,b.ans + cst(a.l,b.r)); } else { return st(a.l,a.l,b.r,b.ans + cst(a.l,b.r)); } } else { return st(max(a.l,b.l),min(a.r,b.r),b.t,b.ans); } } } st T[2][N * 4]; void update(int l,int r,int pos,st v,int node,int id) { if (l == r) { T[id][node] = v; } else { int m = (l + r) / 2; if (pos <= m) update(l,m,pos,v,node * 2,id); else update(m+1,r,pos,v,node * 2 + 1,id); T[id][node] = T[id][node * 2] + T[id][node * 2 + 1]; } } st query(int l,int r,int l1,int r1,int node,int id) { if (l1 <= l && r <= r1) return T[id][node]; int m = (l + r) / 2; if (l1 <= m && m + 1 <= r1) return query(l,m,l1,r1,node * 2,id) + query(m + 1,r,l1,r1,node * 2 + 1,id); else if (l1 <= m) return query(l,m,l1,r1,node * 2,id); else return query(m + 1,r,l1,r1,node * 2 + 1,id); } int main(void) { int q; cin.tie(0); ios_base :: sync_with_stdio(0); cin>>n>>q; for (int i = 1;i < n;++i) { int lf,rg; cin>>lf>>rg; update(1,n,i,st(lf - i,rg - i - 1),1,0); update(1,n,n - i,st(lf - (n - i),rg - (n - i) - 1),1,1); } while (q --) { int tp; cin>>tp; if (tp == 1) { int pos,lf,rg; cin>>pos>>lf>>rg; update(1,n,pos,st(lf - pos,rg - pos - 1),1,0); update(1,n,n - pos,st(lf - (n - pos),rg - (n - pos) - 1),1,1); } else { int a,b,c,d; cin>>a>>b>>c>>d; if (a == c) { cout << max(0,b - d) << '\n'; } else if (a < c) { cout << (st(b - a,b - a) + query(1,n,a,c - 1,1,0) + st(d - c,d - c)).ans << '\n'; } else { a = n - a + 1; c = n - c + 1; cout << (st(b - a,b - a) + query(1,n,a,c - 1,1,1) + st(d - c,d - c)).ans << '\n'; } } } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...