#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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
97 ms |
125536 KB |
Output is correct |
2 |
Correct |
95 ms |
125524 KB |
Output is correct |
3 |
Correct |
109 ms |
125512 KB |
Output is correct |
4 |
Correct |
102 ms |
125604 KB |
Output is correct |
5 |
Correct |
102 ms |
125504 KB |
Output is correct |
6 |
Correct |
100 ms |
125612 KB |
Output is correct |
7 |
Correct |
97 ms |
125520 KB |
Output is correct |
8 |
Correct |
104 ms |
125560 KB |
Output is correct |
9 |
Correct |
100 ms |
125528 KB |
Output is correct |
10 |
Correct |
104 ms |
125560 KB |
Output is correct |
11 |
Correct |
108 ms |
125688 KB |
Output is correct |
12 |
Correct |
117 ms |
125560 KB |
Output is correct |
13 |
Correct |
93 ms |
125560 KB |
Output is correct |
14 |
Correct |
103 ms |
125620 KB |
Output is correct |
15 |
Correct |
101 ms |
125692 KB |
Output is correct |
16 |
Correct |
96 ms |
125532 KB |
Output is correct |
17 |
Correct |
112 ms |
125616 KB |
Output is correct |
18 |
Correct |
104 ms |
125560 KB |
Output is correct |
19 |
Correct |
103 ms |
125532 KB |
Output is correct |
20 |
Correct |
103 ms |
125560 KB |
Output is correct |
21 |
Correct |
102 ms |
125564 KB |
Output is correct |
22 |
Correct |
102 ms |
125672 KB |
Output is correct |
23 |
Correct |
98 ms |
125532 KB |
Output is correct |
24 |
Correct |
102 ms |
125532 KB |
Output is correct |
25 |
Correct |
97 ms |
125560 KB |
Output is correct |
26 |
Correct |
101 ms |
125752 KB |
Output is correct |
27 |
Correct |
106 ms |
125560 KB |
Output is correct |
28 |
Correct |
101 ms |
125532 KB |
Output is correct |
29 |
Correct |
126 ms |
125560 KB |
Output is correct |
30 |
Correct |
115 ms |
125628 KB |
Output is correct |
31 |
Correct |
105 ms |
125604 KB |
Output is correct |
32 |
Correct |
98 ms |
125560 KB |
Output is correct |
33 |
Correct |
99 ms |
125560 KB |
Output is correct |
34 |
Correct |
107 ms |
125560 KB |
Output is correct |
35 |
Correct |
102 ms |
125596 KB |
Output is correct |
36 |
Correct |
99 ms |
125560 KB |
Output is correct |
37 |
Correct |
105 ms |
125532 KB |
Output is correct |
38 |
Correct |
121 ms |
125560 KB |
Output is correct |
39 |
Correct |
102 ms |
125688 KB |
Output is correct |
40 |
Correct |
99 ms |
125532 KB |
Output is correct |
41 |
Correct |
111 ms |
125700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
870 ms |
138648 KB |
Output is correct |
2 |
Correct |
921 ms |
138104 KB |
Output is correct |
3 |
Correct |
857 ms |
137668 KB |
Output is correct |
4 |
Correct |
839 ms |
137080 KB |
Output is correct |
5 |
Correct |
930 ms |
137264 KB |
Output is correct |
6 |
Correct |
933 ms |
137208 KB |
Output is correct |
7 |
Correct |
945 ms |
137408 KB |
Output is correct |
8 |
Correct |
988 ms |
137512 KB |
Output is correct |
9 |
Correct |
907 ms |
137428 KB |
Output is correct |
10 |
Correct |
958 ms |
137464 KB |
Output is correct |
11 |
Correct |
963 ms |
137308 KB |
Output is correct |
12 |
Correct |
1043 ms |
137336 KB |
Output is correct |
13 |
Correct |
969 ms |
137252 KB |
Output is correct |
14 |
Correct |
93 ms |
125560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
97 ms |
125536 KB |
Output is correct |
2 |
Correct |
95 ms |
125524 KB |
Output is correct |
3 |
Correct |
109 ms |
125512 KB |
Output is correct |
4 |
Correct |
102 ms |
125604 KB |
Output is correct |
5 |
Correct |
102 ms |
125504 KB |
Output is correct |
6 |
Correct |
100 ms |
125612 KB |
Output is correct |
7 |
Correct |
97 ms |
125520 KB |
Output is correct |
8 |
Correct |
104 ms |
125560 KB |
Output is correct |
9 |
Correct |
100 ms |
125528 KB |
Output is correct |
10 |
Correct |
104 ms |
125560 KB |
Output is correct |
11 |
Correct |
108 ms |
125688 KB |
Output is correct |
12 |
Correct |
117 ms |
125560 KB |
Output is correct |
13 |
Correct |
93 ms |
125560 KB |
Output is correct |
14 |
Correct |
103 ms |
125620 KB |
Output is correct |
15 |
Correct |
101 ms |
125692 KB |
Output is correct |
16 |
Correct |
96 ms |
125532 KB |
Output is correct |
17 |
Correct |
112 ms |
125616 KB |
Output is correct |
18 |
Correct |
104 ms |
125560 KB |
Output is correct |
19 |
Correct |
103 ms |
125532 KB |
Output is correct |
20 |
Correct |
103 ms |
125560 KB |
Output is correct |
21 |
Correct |
102 ms |
125564 KB |
Output is correct |
22 |
Correct |
102 ms |
125672 KB |
Output is correct |
23 |
Correct |
98 ms |
125532 KB |
Output is correct |
24 |
Correct |
102 ms |
125532 KB |
Output is correct |
25 |
Correct |
97 ms |
125560 KB |
Output is correct |
26 |
Correct |
101 ms |
125752 KB |
Output is correct |
27 |
Correct |
106 ms |
125560 KB |
Output is correct |
28 |
Correct |
101 ms |
125532 KB |
Output is correct |
29 |
Correct |
126 ms |
125560 KB |
Output is correct |
30 |
Correct |
115 ms |
125628 KB |
Output is correct |
31 |
Correct |
105 ms |
125604 KB |
Output is correct |
32 |
Correct |
98 ms |
125560 KB |
Output is correct |
33 |
Correct |
99 ms |
125560 KB |
Output is correct |
34 |
Correct |
107 ms |
125560 KB |
Output is correct |
35 |
Correct |
102 ms |
125596 KB |
Output is correct |
36 |
Correct |
99 ms |
125560 KB |
Output is correct |
37 |
Correct |
105 ms |
125532 KB |
Output is correct |
38 |
Correct |
121 ms |
125560 KB |
Output is correct |
39 |
Correct |
102 ms |
125688 KB |
Output is correct |
40 |
Correct |
99 ms |
125532 KB |
Output is correct |
41 |
Correct |
111 ms |
125700 KB |
Output is correct |
42 |
Correct |
870 ms |
138648 KB |
Output is correct |
43 |
Correct |
921 ms |
138104 KB |
Output is correct |
44 |
Correct |
857 ms |
137668 KB |
Output is correct |
45 |
Correct |
839 ms |
137080 KB |
Output is correct |
46 |
Correct |
930 ms |
137264 KB |
Output is correct |
47 |
Correct |
933 ms |
137208 KB |
Output is correct |
48 |
Correct |
945 ms |
137408 KB |
Output is correct |
49 |
Correct |
988 ms |
137512 KB |
Output is correct |
50 |
Correct |
907 ms |
137428 KB |
Output is correct |
51 |
Correct |
958 ms |
137464 KB |
Output is correct |
52 |
Correct |
963 ms |
137308 KB |
Output is correct |
53 |
Correct |
1043 ms |
137336 KB |
Output is correct |
54 |
Correct |
969 ms |
137252 KB |
Output is correct |
55 |
Correct |
93 ms |
125560 KB |
Output is correct |
56 |
Correct |
1159 ms |
135628 KB |
Output is correct |
57 |
Correct |
1099 ms |
135356 KB |
Output is correct |
58 |
Correct |
1072 ms |
135544 KB |
Output is correct |
59 |
Correct |
1012 ms |
135392 KB |
Output is correct |
60 |
Correct |
1008 ms |
135236 KB |
Output is correct |
61 |
Correct |
1175 ms |
135360 KB |
Output is correct |
62 |
Correct |
1036 ms |
135388 KB |
Output is correct |
63 |
Correct |
1109 ms |
135432 KB |
Output is correct |
64 |
Correct |
1008 ms |
135348 KB |
Output is correct |
65 |
Correct |
1036 ms |
135208 KB |
Output is correct |
66 |
Correct |
1113 ms |
135672 KB |
Output is correct |
67 |
Correct |
1075 ms |
135544 KB |
Output is correct |
68 |
Correct |
1048 ms |
135596 KB |
Output is correct |
69 |
Correct |
1167 ms |
135480 KB |
Output is correct |
70 |
Correct |
1093 ms |
135528 KB |
Output is correct |
71 |
Correct |
1091 ms |
135460 KB |
Output is correct |
72 |
Correct |
999 ms |
135644 KB |
Output is correct |
73 |
Correct |
1048 ms |
135516 KB |
Output is correct |
74 |
Correct |
988 ms |
135440 KB |
Output is correct |
75 |
Correct |
1063 ms |
135388 KB |
Output is correct |
76 |
Correct |
1074 ms |
135460 KB |
Output is correct |
77 |
Correct |
97 ms |
125688 KB |
Output is correct |