#include<bits/stdc++.h>
using namespace std;
#define all(fl) fl.begin(),fl.end()
#define pb push_back
#define fi first
#define se second
#define for1(i,j,k) for(int i=j;i<=k;i++)
#define for2(i,j,k) for(int i=j;i>=k;i--)
#define for3(i,j,k,l) for(int i=j;i<=k;i+=l)
#define lb lower_bound
#define ub upper_bound
#define sz(a) (int)a.size()
#define pii pair<int,int>
#define pli pair<long long,int>
#define gcd __gcd
#define lcm(x,y) x*y/__gcd(x,y)
#define pil pair<int,long long>
const int inf=1e9;
struct node{
int type,l,r;
long long cost;
};
node operator + (const node &p,const node &q){
node r;
r.cost=p.cost+q.cost;
//need to divide into 2 types because type 1 will be 2 interval length 1 and type 0 will be interval [l,r]
if (!p.type&&!q.type){//both in case 1
if (max(p.l,q.l)<=min(p.r,q.r)){//case 1 again
r.l=max(p.l,q.l);
r.r=min(p.r,q.r);
r.type=0;
//no cost need
}
else {
//now the node will be 2 interval length 1 is [r.l,r.l] and [r.r,r.r]
if (p.l>q.r){//case 3
r.cost+=p.l-q.r;
r.l=p.l,r.r=q.r;
r.type=1;//no more intersect
}
else {//case 2
r.type=1;
//no more intersect we can go to time p.r and then go to q.l
r.cost+=0;//just need to wait no need to use skill
r.l=p.r;
r.r=q.l;
}
}
}
else if (!p.type){
if (p.l>q.l){
r.cost+=p.l-q.l;
r.l=p.l;//best is back to p.l
r.r=q.r;
r.type=1;
}
else if (p.r<q.l){
r.cost+=0;//just need to wait
r.l=p.r;//best to wait and go in p.r;
r.r=q.r;
r.type=1;
}
else {
r.cost+=0;
r.l=q.l;//change to [q.l,q.l]
r.r=q.r;
r.type=1;
}
}
else if (!q.type){
r.l=p.l;//just length 1
if (p.r<q.l){
r.cost+=0;//just need to wait
r.r=q.l;
r.type=1;
}
else if (p.r>q.r){
r.cost+=p.r-q.r;//use skill to back time
r.r=q.r;
r.type=1;
}
else {//q.l<=p.r<=q.r;
r.r=p.r;
r.cost+=0;
r.type=1;
}
}
else {
r.type=1;
r.l=p.l;
r.r=q.r;
if (p.r>q.l){
r.cost+=p.r-q.l;//backtime
}
else if (p.r<=q.l){
r.cost+=0;//just wait
}
}
return r;
}
struct IT{
vector<node>st;
void resz(int n){
st.resize(4*n+9);
}
void update(int id,int l,int r,int u,int x,int y){
if (l>u||r<u)return;
if (l==r){
st[id].type=0;
st[id].l=x,st[id].r=y;
st[id].cost=0;
return;
}
int mid=(l+r)/2;
update(id*2,l,mid,u,x,y);
update(id*2+1,mid+1,r,u,x,y);
st[id]=st[id*2]+st[id*2+1];
}
node get(int id,int l,int r,int u,int v){
if (l>v||r<u||u>v)return {0,-inf,inf,0};
if (u<=l&&r<=v)return st[id];
int mid=(l+r)/2;
return get(id*2,l,mid,u,v)+get(id*2+1,mid+1,r,u,v);
}
}st1,st2;
int n,q;
void update(int id,int l,int r){
st1.update(1,1,n-1,id,l-id,r-id-1);
id=n-id;
st2.update(1,1,n-1,id,l-id,r-id-1);
}
long long get(int x,int y,int u,int v){
if (x==y)return max(0,u-v);
if (x<y){
y--;
node in={0,u-x,u-x,0};
node tmp=st1.get(1,1,n-1,x,y);
node out={0,v-y-1,v-y-1,0};
//cout<<tmp.l<<" "<<tmp.r<<" "<<tmp.cost<<'\n';
node ans=in+tmp+out;
return ans.cost;
}
else {
y=n-y;
x=n+1-x;
node in={0,u-x,u-x,0};
node tmp=st2.get(1,1,n-1,x,y);
node out={0,v-y-1,v-y-1,0};
node ans=in+tmp+out;
return ans.cost;
}
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
//freopen("temp.INP","r",stdin);
//freopen("temp.OUT","w",stdout);
cin>>n>>q;
st1.resz(n-1),st2.resz(n-1);
for1(i,1,n-1){
int l,r;
cin>>l>>r;
update(i,l,r);
}
for1(i,1,q){
int type;
cin>>type;
if (type==1){
int id,l,r;
cin>>id>>l>>r;
update(id,l,r);
}
else {
int x,y,u,v;
cin>>x>>u>>y>>v;
cout<<get(x,y,u,v)<<'\n';
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
460 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
456 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
2 ms |
604 KB |
Output is correct |
12 |
Correct |
2 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
424 KB |
Output is correct |
14 |
Correct |
1 ms |
600 KB |
Output is correct |
15 |
Correct |
2 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
2 ms |
604 KB |
Output is correct |
18 |
Correct |
2 ms |
604 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
2 ms |
604 KB |
Output is correct |
21 |
Correct |
2 ms |
668 KB |
Output is correct |
22 |
Correct |
1 ms |
664 KB |
Output is correct |
23 |
Correct |
2 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
2 ms |
604 KB |
Output is correct |
26 |
Correct |
2 ms |
604 KB |
Output is correct |
27 |
Correct |
2 ms |
604 KB |
Output is correct |
28 |
Correct |
2 ms |
604 KB |
Output is correct |
29 |
Correct |
2 ms |
604 KB |
Output is correct |
30 |
Correct |
2 ms |
604 KB |
Output is correct |
31 |
Correct |
2 ms |
604 KB |
Output is correct |
32 |
Correct |
2 ms |
604 KB |
Output is correct |
33 |
Correct |
2 ms |
604 KB |
Output is correct |
34 |
Correct |
1 ms |
604 KB |
Output is correct |
35 |
Correct |
2 ms |
604 KB |
Output is correct |
36 |
Correct |
1 ms |
604 KB |
Output is correct |
37 |
Correct |
2 ms |
604 KB |
Output is correct |
38 |
Correct |
1 ms |
604 KB |
Output is correct |
39 |
Correct |
2 ms |
608 KB |
Output is correct |
40 |
Correct |
2 ms |
608 KB |
Output is correct |
41 |
Correct |
0 ms |
352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
564 ms |
71572 KB |
Output is correct |
2 |
Correct |
542 ms |
68464 KB |
Output is correct |
3 |
Correct |
554 ms |
69016 KB |
Output is correct |
4 |
Correct |
551 ms |
67136 KB |
Output is correct |
5 |
Correct |
540 ms |
71420 KB |
Output is correct |
6 |
Correct |
568 ms |
70328 KB |
Output is correct |
7 |
Correct |
557 ms |
72604 KB |
Output is correct |
8 |
Correct |
558 ms |
75104 KB |
Output is correct |
9 |
Correct |
534 ms |
67928 KB |
Output is correct |
10 |
Correct |
537 ms |
72276 KB |
Output is correct |
11 |
Correct |
542 ms |
71508 KB |
Output is correct |
12 |
Correct |
579 ms |
75900 KB |
Output is correct |
13 |
Correct |
586 ms |
77112 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
460 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
456 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
2 ms |
604 KB |
Output is correct |
12 |
Correct |
2 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
424 KB |
Output is correct |
14 |
Correct |
1 ms |
600 KB |
Output is correct |
15 |
Correct |
2 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
2 ms |
604 KB |
Output is correct |
18 |
Correct |
2 ms |
604 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
2 ms |
604 KB |
Output is correct |
21 |
Correct |
2 ms |
668 KB |
Output is correct |
22 |
Correct |
1 ms |
664 KB |
Output is correct |
23 |
Correct |
2 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
2 ms |
604 KB |
Output is correct |
26 |
Correct |
2 ms |
604 KB |
Output is correct |
27 |
Correct |
2 ms |
604 KB |
Output is correct |
28 |
Correct |
2 ms |
604 KB |
Output is correct |
29 |
Correct |
2 ms |
604 KB |
Output is correct |
30 |
Correct |
2 ms |
604 KB |
Output is correct |
31 |
Correct |
2 ms |
604 KB |
Output is correct |
32 |
Correct |
2 ms |
604 KB |
Output is correct |
33 |
Correct |
2 ms |
604 KB |
Output is correct |
34 |
Correct |
1 ms |
604 KB |
Output is correct |
35 |
Correct |
2 ms |
604 KB |
Output is correct |
36 |
Correct |
1 ms |
604 KB |
Output is correct |
37 |
Correct |
2 ms |
604 KB |
Output is correct |
38 |
Correct |
1 ms |
604 KB |
Output is correct |
39 |
Correct |
2 ms |
608 KB |
Output is correct |
40 |
Correct |
2 ms |
608 KB |
Output is correct |
41 |
Correct |
0 ms |
352 KB |
Output is correct |
42 |
Correct |
564 ms |
71572 KB |
Output is correct |
43 |
Correct |
542 ms |
68464 KB |
Output is correct |
44 |
Correct |
554 ms |
69016 KB |
Output is correct |
45 |
Correct |
551 ms |
67136 KB |
Output is correct |
46 |
Correct |
540 ms |
71420 KB |
Output is correct |
47 |
Correct |
568 ms |
70328 KB |
Output is correct |
48 |
Correct |
557 ms |
72604 KB |
Output is correct |
49 |
Correct |
558 ms |
75104 KB |
Output is correct |
50 |
Correct |
534 ms |
67928 KB |
Output is correct |
51 |
Correct |
537 ms |
72276 KB |
Output is correct |
52 |
Correct |
542 ms |
71508 KB |
Output is correct |
53 |
Correct |
579 ms |
75900 KB |
Output is correct |
54 |
Correct |
586 ms |
77112 KB |
Output is correct |
55 |
Correct |
0 ms |
344 KB |
Output is correct |
56 |
Correct |
559 ms |
67840 KB |
Output is correct |
57 |
Correct |
528 ms |
63924 KB |
Output is correct |
58 |
Correct |
557 ms |
69456 KB |
Output is correct |
59 |
Correct |
556 ms |
70036 KB |
Output is correct |
60 |
Correct |
553 ms |
65500 KB |
Output is correct |
61 |
Correct |
583 ms |
72052 KB |
Output is correct |
62 |
Correct |
563 ms |
71744 KB |
Output is correct |
63 |
Correct |
577 ms |
71332 KB |
Output is correct |
64 |
Correct |
572 ms |
72040 KB |
Output is correct |
65 |
Correct |
588 ms |
68632 KB |
Output is correct |
66 |
Correct |
549 ms |
69116 KB |
Output is correct |
67 |
Correct |
563 ms |
71504 KB |
Output is correct |
68 |
Correct |
570 ms |
66264 KB |
Output is correct |
69 |
Correct |
597 ms |
73720 KB |
Output is correct |
70 |
Correct |
533 ms |
65872 KB |
Output is correct |
71 |
Correct |
571 ms |
63312 KB |
Output is correct |
72 |
Correct |
538 ms |
65872 KB |
Output is correct |
73 |
Correct |
570 ms |
71780 KB |
Output is correct |
74 |
Correct |
577 ms |
72216 KB |
Output is correct |
75 |
Correct |
587 ms |
73296 KB |
Output is correct |
76 |
Correct |
581 ms |
73924 KB |
Output is correct |
77 |
Correct |
0 ms |
348 KB |
Output is correct |