//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define F first
#define S second
#define all(x) x.begin(),x.end()
#define _ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
void setIO(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
struct node{
ll s1=0,s2=0,e1=0,e2=0,c1=0,c2=0,sz=0;
};
const int mxn=3e5+5;
vector<vector<node>> segtree(2);
int n,q;
ll L[mxn],R[mxn];
node trash;
node merge(const node &a,const node &b){
if(a.sz==-1) return b;
if(b.sz==-1) return a;
node ans;
ans.sz=a.sz+b.sz;
ans.s1=a.s1;
ans.s2=a.s2;
{
ans.c1=a.c1;
if(a.e1>b.s1){
ans.c1+=b.c1+(a.e1-b.s1);
ans.e1=b.e1;
}
else if(a.e1>=b.s2){
ll dif=b.c1-b.c2;
ans.c1+=b.c2+max(0ll,dif-(b.s1-a.e1));
ll res=a.e1+b.sz;
if(res>b.e1){
ans.e1=b.e1;
}
else{
ans.e1=max(res,b.e2);
}
}
else{
ans.c1+=b.c2;
ans.e1=b.e2;
}
}
{
ans.c2=a.c2;
if(a.e2>b.s1){
ans.c2+=b.c1+(a.e2-b.s1);
ans.e2=b.e1;
}
else if(a.e2>=b.s2){
ll dif=b.c1-b.c2;
ans.c2+=b.c2+max(0ll,dif-(b.s1-a.e2));
ll res=a.e2+b.sz;
if(res>b.e1){
ans.e2=b.e1;
}
else{
ans.e2=max(res,b.e2);
}
}
else{
ans.c2+=b.c2;
ans.e2=b.e2;
}
}
return ans;
}
void build(int id,int l=1,int r=n,int v=1){
if(l==r){
segtree[id][v].s1=R[l]-1;
segtree[id][v].s2=L[l];
segtree[id][v].e1=R[l];
segtree[id][v].e2=L[l]+1;
segtree[id][v].c1=segtree[id][v].c2=0;
segtree[id][v].sz=1;
return;
}
int mid=(l+r)/2;
build(id,l,mid,v*2);
build(id,mid+1,r,v*2+1);
segtree[id][v]=merge(segtree[id][v*2],segtree[id][v*2+1]);
}
void update(int id,int pos,int s,int e,int l=1,int r=n,int v=1){
if(l==r){
segtree[id][v].s1=e-1;
segtree[id][v].s2=s;
segtree[id][v].e1=e;
segtree[id][v].e2=s+1;
return;
}
int mid=(l+r)/2;
if(pos<=mid) update(id,pos,s,e,l,mid,v*2);
else update(id,pos,s,e,mid+1,r,v*2+1);
segtree[id][v]=merge(segtree[id][v*2],segtree[id][v*2+1]);
}
node query(int id,int tl,int tr,int l=1,int r=n,int v=1){
if(r<tl or tr<l){
return trash;
}
if(tl<=l and r<=tr){
return segtree[id][v];
}
int mid=(l+r)/2;
return merge(query(id,tl,min(mid,tr),l,mid,v*2),query(id,max(mid+1,tl),tr,mid+1,r,v*2+1));
}
signed main() {_
trash.sz=-1;
cin>>n>>q;
for(int i=1;i<n;i++){
cin>>L[i]>>R[i];
}
segtree[0].resize(n*4);
segtree[1].resize(n*4);
build(0);
reverse(L+1,L+n);
reverse(R+1,R+n);
build(1);
reverse(L+1,L+n);
reverse(R+1,R+n);
for(int i=0;i<q;i++){
int t;
cin>>t;
if(t==1){
ll p,s,e;
cin>>p>>s>>e;
update(0,p,s,e);
update(1,n-p,s,e);
}
else{
ll a,b,c,d;
cin>>a>>b>>c>>d;
if(a==c){
cout<<max(0ll,b-d)<<'\n';
continue;
}
node tmp;
if(a<c){
tmp=query(0,a,c-1);
}
else{
a=n-a+1;
c=n-c+1;
tmp=query(1,a,c-1);
}
node cur;
cur.e1=b;
node fin=merge(cur,tmp);
tmp.e2;
cout<<fin.c1+max(0ll,fin.e1-d)<<'\n';;
}
}
return 0;
}
//maybe its multiset not set
Compilation message
timeleap.cpp: In function 'int main()':
timeleap.cpp:163:17: warning: statement has no effect [-Wunused-value]
163 | tmp.e2;
| ~~~~^~
timeleap.cpp: In function 'void setIO(std::string)':
timeleap.cpp:12:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
12 | freopen((s + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
timeleap.cpp:13:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
13 | freopen((s + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2516 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
2 ms |
2908 KB |
Output is correct |
13 |
Correct |
2 ms |
2652 KB |
Output is correct |
14 |
Correct |
2 ms |
2652 KB |
Output is correct |
15 |
Correct |
2 ms |
2908 KB |
Output is correct |
16 |
Correct |
1 ms |
2908 KB |
Output is correct |
17 |
Correct |
2 ms |
2908 KB |
Output is correct |
18 |
Correct |
2 ms |
2908 KB |
Output is correct |
19 |
Correct |
2 ms |
2908 KB |
Output is correct |
20 |
Correct |
1 ms |
2908 KB |
Output is correct |
21 |
Correct |
1 ms |
2908 KB |
Output is correct |
22 |
Correct |
1 ms |
2652 KB |
Output is correct |
23 |
Correct |
2 ms |
2908 KB |
Output is correct |
24 |
Correct |
2 ms |
2908 KB |
Output is correct |
25 |
Correct |
2 ms |
2908 KB |
Output is correct |
26 |
Correct |
2 ms |
2716 KB |
Output is correct |
27 |
Correct |
2 ms |
2652 KB |
Output is correct |
28 |
Correct |
1 ms |
2908 KB |
Output is correct |
29 |
Correct |
2 ms |
2908 KB |
Output is correct |
30 |
Correct |
2 ms |
2908 KB |
Output is correct |
31 |
Correct |
2 ms |
2904 KB |
Output is correct |
32 |
Correct |
2 ms |
2908 KB |
Output is correct |
33 |
Correct |
2 ms |
2652 KB |
Output is correct |
34 |
Correct |
2 ms |
2908 KB |
Output is correct |
35 |
Correct |
1 ms |
2908 KB |
Output is correct |
36 |
Correct |
2 ms |
2908 KB |
Output is correct |
37 |
Correct |
2 ms |
2908 KB |
Output is correct |
38 |
Correct |
2 ms |
2908 KB |
Output is correct |
39 |
Correct |
2 ms |
2908 KB |
Output is correct |
40 |
Correct |
2 ms |
2908 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
495 ms |
129264 KB |
Output is correct |
2 |
Correct |
484 ms |
121988 KB |
Output is correct |
3 |
Correct |
480 ms |
123684 KB |
Output is correct |
4 |
Correct |
484 ms |
119888 KB |
Output is correct |
5 |
Correct |
491 ms |
128852 KB |
Output is correct |
6 |
Correct |
488 ms |
126804 KB |
Output is correct |
7 |
Correct |
503 ms |
131016 KB |
Output is correct |
8 |
Correct |
484 ms |
136272 KB |
Output is correct |
9 |
Correct |
458 ms |
120660 KB |
Output is correct |
10 |
Correct |
505 ms |
130428 KB |
Output is correct |
11 |
Correct |
500 ms |
129148 KB |
Output is correct |
12 |
Correct |
505 ms |
138620 KB |
Output is correct |
13 |
Correct |
517 ms |
141152 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2516 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
2 ms |
2908 KB |
Output is correct |
13 |
Correct |
2 ms |
2652 KB |
Output is correct |
14 |
Correct |
2 ms |
2652 KB |
Output is correct |
15 |
Correct |
2 ms |
2908 KB |
Output is correct |
16 |
Correct |
1 ms |
2908 KB |
Output is correct |
17 |
Correct |
2 ms |
2908 KB |
Output is correct |
18 |
Correct |
2 ms |
2908 KB |
Output is correct |
19 |
Correct |
2 ms |
2908 KB |
Output is correct |
20 |
Correct |
1 ms |
2908 KB |
Output is correct |
21 |
Correct |
1 ms |
2908 KB |
Output is correct |
22 |
Correct |
1 ms |
2652 KB |
Output is correct |
23 |
Correct |
2 ms |
2908 KB |
Output is correct |
24 |
Correct |
2 ms |
2908 KB |
Output is correct |
25 |
Correct |
2 ms |
2908 KB |
Output is correct |
26 |
Correct |
2 ms |
2716 KB |
Output is correct |
27 |
Correct |
2 ms |
2652 KB |
Output is correct |
28 |
Correct |
1 ms |
2908 KB |
Output is correct |
29 |
Correct |
2 ms |
2908 KB |
Output is correct |
30 |
Correct |
2 ms |
2908 KB |
Output is correct |
31 |
Correct |
2 ms |
2904 KB |
Output is correct |
32 |
Correct |
2 ms |
2908 KB |
Output is correct |
33 |
Correct |
2 ms |
2652 KB |
Output is correct |
34 |
Correct |
2 ms |
2908 KB |
Output is correct |
35 |
Correct |
1 ms |
2908 KB |
Output is correct |
36 |
Correct |
2 ms |
2908 KB |
Output is correct |
37 |
Correct |
2 ms |
2908 KB |
Output is correct |
38 |
Correct |
2 ms |
2908 KB |
Output is correct |
39 |
Correct |
2 ms |
2908 KB |
Output is correct |
40 |
Correct |
2 ms |
2908 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |
42 |
Correct |
495 ms |
129264 KB |
Output is correct |
43 |
Correct |
484 ms |
121988 KB |
Output is correct |
44 |
Correct |
480 ms |
123684 KB |
Output is correct |
45 |
Correct |
484 ms |
119888 KB |
Output is correct |
46 |
Correct |
491 ms |
128852 KB |
Output is correct |
47 |
Correct |
488 ms |
126804 KB |
Output is correct |
48 |
Correct |
503 ms |
131016 KB |
Output is correct |
49 |
Correct |
484 ms |
136272 KB |
Output is correct |
50 |
Correct |
458 ms |
120660 KB |
Output is correct |
51 |
Correct |
505 ms |
130428 KB |
Output is correct |
52 |
Correct |
500 ms |
129148 KB |
Output is correct |
53 |
Correct |
505 ms |
138620 KB |
Output is correct |
54 |
Correct |
517 ms |
141152 KB |
Output is correct |
55 |
Correct |
0 ms |
348 KB |
Output is correct |
56 |
Correct |
531 ms |
140188 KB |
Output is correct |
57 |
Correct |
514 ms |
131808 KB |
Output is correct |
58 |
Correct |
524 ms |
143972 KB |
Output is correct |
59 |
Correct |
528 ms |
144976 KB |
Output is correct |
60 |
Correct |
565 ms |
134884 KB |
Output is correct |
61 |
Correct |
563 ms |
149208 KB |
Output is correct |
62 |
Correct |
551 ms |
148780 KB |
Output is correct |
63 |
Correct |
545 ms |
148224 KB |
Output is correct |
64 |
Correct |
554 ms |
149916 KB |
Output is correct |
65 |
Correct |
528 ms |
142164 KB |
Output is correct |
66 |
Correct |
504 ms |
143048 KB |
Output is correct |
67 |
Correct |
510 ms |
148920 KB |
Output is correct |
68 |
Correct |
488 ms |
137184 KB |
Output is correct |
69 |
Correct |
527 ms |
152916 KB |
Output is correct |
70 |
Correct |
515 ms |
136020 KB |
Output is correct |
71 |
Correct |
515 ms |
130944 KB |
Output is correct |
72 |
Correct |
517 ms |
136572 KB |
Output is correct |
73 |
Correct |
532 ms |
149588 KB |
Output is correct |
74 |
Correct |
551 ms |
150636 KB |
Output is correct |
75 |
Correct |
564 ms |
152920 KB |
Output is correct |
76 |
Correct |
556 ms |
153672 KB |
Output is correct |
77 |
Correct |
0 ms |
348 KB |
Output is correct |