#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair <ll, ll> pll;
struct node{
ll t, l, r, x1, x2, v;
node() {}
node(ll l, ll r) : t(1), l(l), r(r) {}
node(ll l, ll r, ll x) : t(2), l(l), r(r), x1(x), x2(x), v(0) {}
pll pass(ll x)
{
if(t == 1){
if(l <= x && x <= r) return pll(x, 0);
else if(x < l) return pll(l, 0);
else return pll(r, x - r);
}
else{
ll _v = v;
if(x < l) x = l;
else if(r < x) _v += x - r, x = r;
if(x > x1) _v += x - x1;
return pll(x2, _v);
}
}
node operator + (node n)
{
if(t == 1 && n.t == 1){
if(max(l, n.l) <= min(r, n.r)){
return node(max(l, n.l), min(r, n.r));
}
else if(r < n.l) return node(l, r, n.l);
else return node(l, r, n.r);
}
else if(t == 1 && n.t == 2){
node ret = n;
ret.l = l; ret.r = r;
if(max(l, n.l) <= min(r, n.r)){
ret.l = max(l, n.l);
ret.r = min(r, n.r);
}
else if(r < n.l){
ret.x1 = n.l;
if(n.l > n.x1) ret.v += n.l - n.x1;
}
else{
ret.x1 = n.r;
if(n.r > n.x1) ret.v += n.r - n.x1;
}
return ret;
}
else if(t == 2 && n.t == 1){
node ret(l, r, x1);
ret.v = v;
if(n.l <= x2 && x2 <= n.r) ret.x2 = x2;
else if(x2 < n.l) ret.x2 = n.l;
else if(n.r < x2) ret.x2 = n.r, ret.v += x2 - n.r;
return ret;
}
else{
node ret(l, r, x1);
ll _x2, _v;
tie(_x2, _v) = n.pass(x2);
ret.x2 = _x2; ret.v = v + _v;
return ret;
}
}
};
node T1[1101010], T2[1101010];
ll n, sz;
void update(node *T, ll p)
{
p = p + sz >> 1;
for(; p; p>>=1){
T[p] = T[p << 1] + T[p << 1 | 1];
}
}
node get_sum(node *T, ll l, ll r)
{
node sl, sr;
ll fl = 0, fr = 0;
l += sz; r += sz;
for(; l<r; ){
if(l & 1){
if(!fl) sl = T[l], fl = 1;
else sl = sl + T[l];
}
if(~r & 1){
if(!fr) sr = T[r], fr = 1;
else sr = T[r] + sr;
}
l = l + 1 >> 1;
r = r - 1 >> 1;
}
if(l == r){
if(!fl) sl = T[l], fl = 1;
else sl = sl + T[l];
}
if(!fl) return sr;
else if(!fr) return sl;
else return sl + sr;
}
int main()
{
ll q, i, t, l, r, x, y, z, s, e, v;
scanf("%lld%lld", &n, &q);
for(sz=1; sz<=n; sz<<=1);
for(i=0; i<sz; i++){
T1[sz + i] = node(1, 0, 0);
T2[sz + i] = node(1, 0, 0);
}
for(i=1; i<n; i++){
scanf("%lld%lld", &l, &r);
T1[sz + i] = node(l - i, r - i - 1);
T2[sz + n - i] = node(l - n + i, r - n + i - 1);
}
for(i=sz-1; i>=1; i--){
T1[i] = T1[i << 1] + T1[i << 1 | 1];
T2[i] = T2[i << 1] + T2[i << 1 | 1];
}
for(; q--; ){
scanf("%lld", &t);
if(t == 1){
scanf("%lld%lld%lld", &i, &l, &r);
T1[sz + i] = node(l - i, r - i - 1);
T2[sz + n - i] = node(l - n + i, r - n + i - 1);
update(T1, i); update(T2, n - i);
}
else{
scanf("%lld%lld%lld%lld", &s, &x, &e, &y);
if(s == e) printf("%lld\n", max(0ll, x - y));
else if(s < e){
x -= s; y -= e;
tie(z, v) = get_sum(T1, s, e - 1).pass(x);
printf("%lld\n", v + max(0ll, z - y));
}
else{
s = n - s + 1; e = n - e + 1;
x -= s; y -= e;
tie(z, v) = get_sum(T2, s, e - 1).pass(x);
printf("%lld\n", v + max(0ll, z - y));
}
}
}
return 0;
}
Compilation message
timeleap.cpp: In function 'void update(node*, ll)':
timeleap.cpp:87:8: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
p = p + sz >> 1;
~~^~~~
timeleap.cpp: In function 'node get_sum(node*, ll, ll)':
timeleap.cpp:109:9: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
l = l + 1 >> 1;
~~^~~
timeleap.cpp:110:9: warning: suggest parentheses around '-' inside '>>' [-Wparentheses]
r = r - 1 >> 1;
~~^~~
timeleap.cpp: In function 'int main()':
timeleap.cpp:127:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld%lld", &n, &q);
~~~~~^~~~~~~~~~~~~~~~~~~~
timeleap.cpp:137:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld%lld", &l, &r);
~~~~~^~~~~~~~~~~~~~~~~~~~
timeleap.cpp:148:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld", &t);
~~~~~^~~~~~~~~~~~
timeleap.cpp:150:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld%lld%lld", &i, &l, &r);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
timeleap.cpp:156:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld%lld%lld%lld", &s, &x, &e, &y);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
640 KB |
Output is correct |
12 |
Correct |
3 ms |
640 KB |
Output is correct |
13 |
Correct |
3 ms |
640 KB |
Output is correct |
14 |
Correct |
6 ms |
640 KB |
Output is correct |
15 |
Correct |
3 ms |
640 KB |
Output is correct |
16 |
Correct |
3 ms |
512 KB |
Output is correct |
17 |
Correct |
3 ms |
640 KB |
Output is correct |
18 |
Correct |
3 ms |
512 KB |
Output is correct |
19 |
Correct |
3 ms |
512 KB |
Output is correct |
20 |
Correct |
3 ms |
512 KB |
Output is correct |
21 |
Correct |
3 ms |
640 KB |
Output is correct |
22 |
Correct |
3 ms |
512 KB |
Output is correct |
23 |
Correct |
4 ms |
612 KB |
Output is correct |
24 |
Correct |
3 ms |
640 KB |
Output is correct |
25 |
Correct |
3 ms |
640 KB |
Output is correct |
26 |
Correct |
3 ms |
512 KB |
Output is correct |
27 |
Correct |
3 ms |
512 KB |
Output is correct |
28 |
Correct |
3 ms |
640 KB |
Output is correct |
29 |
Correct |
5 ms |
512 KB |
Output is correct |
30 |
Correct |
3 ms |
640 KB |
Output is correct |
31 |
Correct |
3 ms |
512 KB |
Output is correct |
32 |
Correct |
4 ms |
512 KB |
Output is correct |
33 |
Correct |
4 ms |
640 KB |
Output is correct |
34 |
Correct |
3 ms |
640 KB |
Output is correct |
35 |
Correct |
3 ms |
640 KB |
Output is correct |
36 |
Correct |
3 ms |
512 KB |
Output is correct |
37 |
Correct |
3 ms |
512 KB |
Output is correct |
38 |
Correct |
3 ms |
512 KB |
Output is correct |
39 |
Correct |
3 ms |
512 KB |
Output is correct |
40 |
Correct |
3 ms |
512 KB |
Output is correct |
41 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
593 ms |
103004 KB |
Output is correct |
2 |
Correct |
545 ms |
69064 KB |
Output is correct |
3 |
Correct |
560 ms |
69256 KB |
Output is correct |
4 |
Correct |
559 ms |
68984 KB |
Output is correct |
5 |
Correct |
604 ms |
118488 KB |
Output is correct |
6 |
Correct |
615 ms |
118448 KB |
Output is correct |
7 |
Correct |
596 ms |
118640 KB |
Output is correct |
8 |
Correct |
617 ms |
119200 KB |
Output is correct |
9 |
Correct |
530 ms |
69120 KB |
Output is correct |
10 |
Correct |
599 ms |
118704 KB |
Output is correct |
11 |
Correct |
584 ms |
118596 KB |
Output is correct |
12 |
Correct |
625 ms |
119052 KB |
Output is correct |
13 |
Correct |
624 ms |
119280 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
640 KB |
Output is correct |
12 |
Correct |
3 ms |
640 KB |
Output is correct |
13 |
Correct |
3 ms |
640 KB |
Output is correct |
14 |
Correct |
6 ms |
640 KB |
Output is correct |
15 |
Correct |
3 ms |
640 KB |
Output is correct |
16 |
Correct |
3 ms |
512 KB |
Output is correct |
17 |
Correct |
3 ms |
640 KB |
Output is correct |
18 |
Correct |
3 ms |
512 KB |
Output is correct |
19 |
Correct |
3 ms |
512 KB |
Output is correct |
20 |
Correct |
3 ms |
512 KB |
Output is correct |
21 |
Correct |
3 ms |
640 KB |
Output is correct |
22 |
Correct |
3 ms |
512 KB |
Output is correct |
23 |
Correct |
4 ms |
612 KB |
Output is correct |
24 |
Correct |
3 ms |
640 KB |
Output is correct |
25 |
Correct |
3 ms |
640 KB |
Output is correct |
26 |
Correct |
3 ms |
512 KB |
Output is correct |
27 |
Correct |
3 ms |
512 KB |
Output is correct |
28 |
Correct |
3 ms |
640 KB |
Output is correct |
29 |
Correct |
5 ms |
512 KB |
Output is correct |
30 |
Correct |
3 ms |
640 KB |
Output is correct |
31 |
Correct |
3 ms |
512 KB |
Output is correct |
32 |
Correct |
4 ms |
512 KB |
Output is correct |
33 |
Correct |
4 ms |
640 KB |
Output is correct |
34 |
Correct |
3 ms |
640 KB |
Output is correct |
35 |
Correct |
3 ms |
640 KB |
Output is correct |
36 |
Correct |
3 ms |
512 KB |
Output is correct |
37 |
Correct |
3 ms |
512 KB |
Output is correct |
38 |
Correct |
3 ms |
512 KB |
Output is correct |
39 |
Correct |
3 ms |
512 KB |
Output is correct |
40 |
Correct |
3 ms |
512 KB |
Output is correct |
41 |
Correct |
2 ms |
384 KB |
Output is correct |
42 |
Correct |
593 ms |
103004 KB |
Output is correct |
43 |
Correct |
545 ms |
69064 KB |
Output is correct |
44 |
Correct |
560 ms |
69256 KB |
Output is correct |
45 |
Correct |
559 ms |
68984 KB |
Output is correct |
46 |
Correct |
604 ms |
118488 KB |
Output is correct |
47 |
Correct |
615 ms |
118448 KB |
Output is correct |
48 |
Correct |
596 ms |
118640 KB |
Output is correct |
49 |
Correct |
617 ms |
119200 KB |
Output is correct |
50 |
Correct |
530 ms |
69120 KB |
Output is correct |
51 |
Correct |
599 ms |
118704 KB |
Output is correct |
52 |
Correct |
584 ms |
118596 KB |
Output is correct |
53 |
Correct |
625 ms |
119052 KB |
Output is correct |
54 |
Correct |
624 ms |
119280 KB |
Output is correct |
55 |
Correct |
2 ms |
384 KB |
Output is correct |
56 |
Correct |
630 ms |
115356 KB |
Output is correct |
57 |
Correct |
587 ms |
65820 KB |
Output is correct |
58 |
Correct |
649 ms |
115704 KB |
Output is correct |
59 |
Correct |
657 ms |
115832 KB |
Output is correct |
60 |
Correct |
605 ms |
66132 KB |
Output is correct |
61 |
Correct |
653 ms |
115932 KB |
Output is correct |
62 |
Correct |
674 ms |
115968 KB |
Output is correct |
63 |
Correct |
659 ms |
115768 KB |
Output is correct |
64 |
Correct |
649 ms |
115924 KB |
Output is correct |
65 |
Correct |
651 ms |
115576 KB |
Output is correct |
66 |
Correct |
675 ms |
115780 KB |
Output is correct |
67 |
Correct |
652 ms |
115928 KB |
Output is correct |
68 |
Correct |
637 ms |
114912 KB |
Output is correct |
69 |
Correct |
639 ms |
115976 KB |
Output is correct |
70 |
Correct |
570 ms |
66056 KB |
Output is correct |
71 |
Correct |
606 ms |
65256 KB |
Output is correct |
72 |
Correct |
638 ms |
114832 KB |
Output is correct |
73 |
Correct |
656 ms |
115320 KB |
Output is correct |
74 |
Correct |
669 ms |
115480 KB |
Output is correct |
75 |
Correct |
687 ms |
115436 KB |
Output is correct |
76 |
Correct |
693 ms |
116084 KB |
Output is correct |
77 |
Correct |
2 ms |
384 KB |
Output is correct |