#include<bits/stdc++.h>
#include<ext/rope>
using namespace std;
using namespace __gnu_cxx;
#define fi first
#define se second
#define fastio ios_base::sync_with_stdio(false);cin.tie(0)
#define fopen freopen("input.txt", "r", stdin)
#define eb emplace_back
#define em emplace
#define prec(a) cout<<fixed;cout.precision(a);
#define all(a) (a).begin(), (a).end()
typedef long long ll;typedef long double ld;typedef unsigned long long ul;typedef unsigned int ui;typedef pair<int,int> pii;typedef pair<ll,ll> pll;
typedef tuple<int,int,int> tiii;
const ll INF = 2e18;
const int inf = 2e9;
template<class T>
void pr(T t) {cerr << t << " ";}
template<class T, class ...Args>
void pr(T a, Args ...args) {cerr << a << " ";pr(args...);}
template<class ...Args>
void prl(Args ...args) {pr(args...);cerr << endl;}
int n,q,in[300010][2];
struct Node{
ll b;
int x,ls,le,rs,re,si;
Node(){};
Node(ll bb,int xx,int i,int j,int k,int l){b=bb;x=xx;ls=i;le=j;rs=k;re=l;si=0;}
Node operator +(Node B){
B.x--;B.ls--;B.le--;
Node p = Node(b,x,ls,le,rs,re);
if(p.re>B.x&&B.x>=p.rs) p.x-=(p.re-B.x);
else if(p.rs>B.x){
if(p.rs!=p.re) p.x = p.rs - si;
p.b+=(p.rs-B.x);
}
p.b+=B.b;
p.si = si + B.si + 1;
if(B.re==B.rs){
p.rs=B.rs;
p.re=B.re;
return p;
}
ll t = B.re-B.rs, ss = B.rs-B.si-1, ee = B.re-B.si-1;
if(p.rs<=ss) p.rs=B.rs;
else if(p.rs>=ee) p.rs=B.re;
else p.rs = B.rs + min(t,p.rs-ss);
if(p.re<=ss) p.re=B.rs;
else if(p.re>=ee) p.re=B.re;
else p.re = B.rs + min(t,p.re-ss);
return p;
}
};
vector<Node> help;
struct SEG{
Node val[1201000];
void init(int x, int l, int r, bool t){
if(l==r){
if(t)val[x]=Node(0,in[l][1],in[l][0],in[l][1],in[l][0],in[l][1]);
else val[x]=Node(0,in[n-l][1],in[n-l][0],in[n-l][1],in[n-l][0],in[n-l][1]);
return ;
}
init(x*2,l,(l+r)/2,t);
init(x*2+1,(l+r)/2+1,r,t);
val[x]=val[x*2]+val[x*2+1];
}
void gang(int x, int l, int r, int ind, bool t){
if(r<ind||ind<l) return ;
if(l==r){
if(t)val[x]=Node(0,in[l][1],in[l][0],in[l][1],in[l][0],in[l][1]);
else val[x]=Node(0,in[n-l][1],in[n-l][0],in[n-l][1],in[n-l][0],in[n-l][1]);
return ;
}
gang(x*2,l,(l+r)/2,ind,t);
gang(x*2+1,(l+r)/2+1,r,ind,t);
val[x]=val[x*2]+val[x*2+1];
}
void query(int x, int l, int r,int s, int e){
if(r<s||e<l) return ;
if(s<=l&&r<=e){
help.eb(val[x]);return ;
}
query(x*2,l,(l+r)/2,s,e);
query(x*2+1,(l+r)/2+1,r,s,e);
}
}seg1, seg2;
ll eval(int s, int e){
for(int i=help.size()-2;i>=0;i--) help[i]=help[i]+help[i+1];
s = max(help[0].ls,s);
ll ret = help[0].b + max(s-help[0].x,0);
if(help[0].rs==help[0].re){
ret+=max(help[0].re+1-e,0);
return ret;
}
int ss = help[0].rs-help[0].si, t = help[0].rs+1;
if(s>ss) t = help[0].rs + min(help[0].re-help[0].rs,s-ss)+1;
ret+=max(t-e,0);
return ret;
}
int main(){
//fopen;
fastio;
cin>>n>>q;
for(int i=1;i<n;i++){
cin>>in[i][0]>>in[i][1];
in[i][1]--;
}
if(n==1){
while(q--){
int a,b,c,d;
cin>>a;
if(a==1){cin>>a>>b>>c;}
else if(a==2){
cin>>a>>b>>c>>d;
cout<<max(b-d,0)<<"\n";
}
}
return 0;
}
seg1.init(1,1,n-1,1);
seg2.init(1,1,n-1,0);
while(q--){
int a,b,c,d;
cin>>a;
if(a==1){
cin>>a>>b>>c;
in[a][0]=b;in[a][1]=c-1;
seg1.gang(1,1,n-1,a,1);
seg2.gang(1,1,n-1,n-a,0);
}
else{
cin>>a>>b>>c>>d;
help.clear();
ll ans=0;
if(a<c){
seg1.query(1,1,n-1,a,c-1);
ans = eval(b,d);
}
else if(a>c){
a = n-a;c = n-c;
seg2.query(1,1,n-1,a+1,c);
ans = eval(b,d);
}
else ans = max(b-d,0);
cout<<ans<<"\n";
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
380 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
6 ms |
504 KB |
Output is correct |
12 |
Correct |
6 ms |
504 KB |
Output is correct |
13 |
Correct |
6 ms |
504 KB |
Output is correct |
14 |
Correct |
6 ms |
504 KB |
Output is correct |
15 |
Correct |
6 ms |
504 KB |
Output is correct |
16 |
Correct |
6 ms |
504 KB |
Output is correct |
17 |
Correct |
6 ms |
504 KB |
Output is correct |
18 |
Correct |
6 ms |
504 KB |
Output is correct |
19 |
Correct |
6 ms |
504 KB |
Output is correct |
20 |
Correct |
6 ms |
504 KB |
Output is correct |
21 |
Correct |
6 ms |
508 KB |
Output is correct |
22 |
Correct |
6 ms |
504 KB |
Output is correct |
23 |
Correct |
6 ms |
504 KB |
Output is correct |
24 |
Correct |
6 ms |
504 KB |
Output is correct |
25 |
Correct |
7 ms |
504 KB |
Output is correct |
26 |
Correct |
6 ms |
504 KB |
Output is correct |
27 |
Correct |
6 ms |
504 KB |
Output is correct |
28 |
Correct |
6 ms |
504 KB |
Output is correct |
29 |
Correct |
6 ms |
504 KB |
Output is correct |
30 |
Correct |
6 ms |
504 KB |
Output is correct |
31 |
Correct |
6 ms |
504 KB |
Output is correct |
32 |
Correct |
6 ms |
504 KB |
Output is correct |
33 |
Correct |
6 ms |
504 KB |
Output is correct |
34 |
Correct |
6 ms |
504 KB |
Output is correct |
35 |
Correct |
6 ms |
504 KB |
Output is correct |
36 |
Correct |
6 ms |
504 KB |
Output is correct |
37 |
Correct |
6 ms |
504 KB |
Output is correct |
38 |
Correct |
6 ms |
504 KB |
Output is correct |
39 |
Correct |
6 ms |
504 KB |
Output is correct |
40 |
Correct |
6 ms |
504 KB |
Output is correct |
41 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
610 ms |
72568 KB |
Output is correct |
2 |
Correct |
582 ms |
40824 KB |
Output is correct |
3 |
Correct |
566 ms |
40732 KB |
Output is correct |
4 |
Correct |
556 ms |
40700 KB |
Output is correct |
5 |
Correct |
626 ms |
73684 KB |
Output is correct |
6 |
Correct |
632 ms |
73464 KB |
Output is correct |
7 |
Correct |
598 ms |
73752 KB |
Output is correct |
8 |
Correct |
628 ms |
74104 KB |
Output is correct |
9 |
Correct |
603 ms |
40932 KB |
Output is correct |
10 |
Correct |
623 ms |
72956 KB |
Output is correct |
11 |
Correct |
620 ms |
72952 KB |
Output is correct |
12 |
Correct |
708 ms |
72932 KB |
Output is correct |
13 |
Correct |
631 ms |
73080 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
380 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
6 ms |
504 KB |
Output is correct |
12 |
Correct |
6 ms |
504 KB |
Output is correct |
13 |
Correct |
6 ms |
504 KB |
Output is correct |
14 |
Correct |
6 ms |
504 KB |
Output is correct |
15 |
Correct |
6 ms |
504 KB |
Output is correct |
16 |
Correct |
6 ms |
504 KB |
Output is correct |
17 |
Correct |
6 ms |
504 KB |
Output is correct |
18 |
Correct |
6 ms |
504 KB |
Output is correct |
19 |
Correct |
6 ms |
504 KB |
Output is correct |
20 |
Correct |
6 ms |
504 KB |
Output is correct |
21 |
Correct |
6 ms |
508 KB |
Output is correct |
22 |
Correct |
6 ms |
504 KB |
Output is correct |
23 |
Correct |
6 ms |
504 KB |
Output is correct |
24 |
Correct |
6 ms |
504 KB |
Output is correct |
25 |
Correct |
7 ms |
504 KB |
Output is correct |
26 |
Correct |
6 ms |
504 KB |
Output is correct |
27 |
Correct |
6 ms |
504 KB |
Output is correct |
28 |
Correct |
6 ms |
504 KB |
Output is correct |
29 |
Correct |
6 ms |
504 KB |
Output is correct |
30 |
Correct |
6 ms |
504 KB |
Output is correct |
31 |
Correct |
6 ms |
504 KB |
Output is correct |
32 |
Correct |
6 ms |
504 KB |
Output is correct |
33 |
Correct |
6 ms |
504 KB |
Output is correct |
34 |
Correct |
6 ms |
504 KB |
Output is correct |
35 |
Correct |
6 ms |
504 KB |
Output is correct |
36 |
Correct |
6 ms |
504 KB |
Output is correct |
37 |
Correct |
6 ms |
504 KB |
Output is correct |
38 |
Correct |
6 ms |
504 KB |
Output is correct |
39 |
Correct |
6 ms |
504 KB |
Output is correct |
40 |
Correct |
6 ms |
504 KB |
Output is correct |
41 |
Correct |
5 ms |
376 KB |
Output is correct |
42 |
Correct |
610 ms |
72568 KB |
Output is correct |
43 |
Correct |
582 ms |
40824 KB |
Output is correct |
44 |
Correct |
566 ms |
40732 KB |
Output is correct |
45 |
Correct |
556 ms |
40700 KB |
Output is correct |
46 |
Correct |
626 ms |
73684 KB |
Output is correct |
47 |
Correct |
632 ms |
73464 KB |
Output is correct |
48 |
Correct |
598 ms |
73752 KB |
Output is correct |
49 |
Correct |
628 ms |
74104 KB |
Output is correct |
50 |
Correct |
603 ms |
40932 KB |
Output is correct |
51 |
Correct |
623 ms |
72956 KB |
Output is correct |
52 |
Correct |
620 ms |
72952 KB |
Output is correct |
53 |
Correct |
708 ms |
72932 KB |
Output is correct |
54 |
Correct |
631 ms |
73080 KB |
Output is correct |
55 |
Correct |
5 ms |
376 KB |
Output is correct |
56 |
Correct |
754 ms |
72032 KB |
Output is correct |
57 |
Correct |
715 ms |
39036 KB |
Output is correct |
58 |
Correct |
735 ms |
72056 KB |
Output is correct |
59 |
Correct |
738 ms |
72184 KB |
Output is correct |
60 |
Correct |
689 ms |
39032 KB |
Output is correct |
61 |
Correct |
723 ms |
72184 KB |
Output is correct |
62 |
Correct |
815 ms |
72184 KB |
Output is correct |
63 |
Correct |
787 ms |
72216 KB |
Output is correct |
64 |
Correct |
713 ms |
71928 KB |
Output is correct |
65 |
Correct |
740 ms |
71800 KB |
Output is correct |
66 |
Correct |
754 ms |
71672 KB |
Output is correct |
67 |
Correct |
739 ms |
71928 KB |
Output is correct |
68 |
Correct |
722 ms |
57848 KB |
Output is correct |
69 |
Correct |
689 ms |
71544 KB |
Output is correct |
70 |
Correct |
716 ms |
38496 KB |
Output is correct |
71 |
Correct |
693 ms |
38392 KB |
Output is correct |
72 |
Correct |
704 ms |
49528 KB |
Output is correct |
73 |
Execution timed out |
279 ms |
71904 KB |
Time limit exceeded (wall clock) |
74 |
Halted |
0 ms |
0 KB |
- |