#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#define AquA cin.tie(0);ios_base::sync_with_stdio(0);
#define fs first
#define sc second
#define p_q priority_queue
#define int long long
using namespace std;
const int inf=1e9;
vector<pair<int,int> > gg[2];
struct ST{
int tp;
struct no{
int ax,ay,bx;
// <=ax : ay
// >=bx : by=bx+(ay-ax)
int dx,dy;
// <=dx : dy
// >=dx : linear
};
vector<no> st;
pair<int,int> get(no& a,int x){
pair<int,int> ret;
if(x<=a.ax){
ret.fs=a.ay;
}
else if(x>=a.bx){
ret.fs=a.bx+a.ay-a.ax;
}
else{
ret.fs=a.ay+(x-a.ax);
}
if(x<=a.dx){
ret.sc=a.dy;
}
else{
ret.sc=a.dy+x-a.dx;
}
return ret;
}
void upd(int v,int l,int r){
st[v].ax=l;
st[v].ay=l+1;
st[v].bx=r;
st[v].dx=r;
st[v].dy=0;
}
void pull(int v){
auto p=st[2*v+1],q=st[2*v+2];
if(get(q,p.ay).fs==get(q,p.bx+p.ay-p.ax).fs){
st[v].ax=st[v].bx=0;
st[v].ay=get(q,p.ay).fs;
}
else{
st[v].ax=p.ax+max(0ll,q.ax-p.ay);
st[v].ay=get(q,get(p,st[v].ax).fs).fs;
st[v].bx=p.bx-max(0ll,p.bx+p.ay-p.ax-q.bx);
}
int c=get(p,0).sc+get(q,get(p,0).fs).sc,d=get(p,inf).sc+get(q,get(p,inf).fs).sc;
st[v].dx=inf-(d-c);
st[v].dy=c;
}
void build(int v,int L,int R){
if(L==R){
upd(v,gg[tp][L].fs,gg[tp][L].sc);
return;
}
int m=(L+R)/2;
build(2*v+1,L,m);
build(2*v+2,m+1,R);
pull(v);
}
void print(int v){
cout << v << ":" << "\n";
cout << st[v].ax << " " << st[v].ay << " " << st[v].bx << "\n";
cout << st[v].dx << " " << st[v].dy << "\n\n";
}
void init(int x){
st.resize(4*x);
build(0,0,x-1);
}
void update(int v,int L,int R,int p,int l,int r){
if(L==R){
upd(v,l,r);
return;
}
int m=(L+R)/2;
if(p<=m){
update(2*v+1,L,m,p,l,r);
}
else{
update(2*v+2,m+1,R,p,l,r);
}
pull(v);
}
pair<int,int> query(int v,int L,int R,int l,int r,int x){
if(L==l && R==r){
return get(st[v],x);
}
int m=(L+R)/2;
if(r<=m){
return query(2*v+1,L,m,l,r,x);
}
else if(l>m){
return query(2*v+2,m+1,R,l,r,x);
}
else{
auto h=query(2*v+1,L,m,l,m,x);
auto y=query(2*v+2,m+1,R,m+1,r,h.fs);
return {y.fs,y.sc+h.sc};
}
}
}st[2];
signed main(){
AquA;
int n,q;
cin >> n >> q;
n--;
if(n==0){
for(int i=0;i<q;i++){
int t;
cin >> t;
int a,b,c,d;
cin >> a >> b >> c >> d;
cout << max(0ll,b-d) << "\n";
}
return 0;
}
gg[0].resize(n);
gg[1].resize(n);
for(int i=0;i<n;i++){
cin >> gg[0][i].fs >> gg[0][i].sc;
gg[0][i].sc--;
}
gg[1]=gg[0];
reverse(gg[1].begin(),gg[1].end());
st[0].tp=0;
st[1].tp=1;
st[0].init(n);
st[1].init(n);
for(int i=0;i<4*n;i++){
//st[1].print(i);
}
//cout << st.st[0].ax << " " << st.st[0].ay << " " << st.st[0].bx << " " << st.st[0].dx << " " << st.st[0].dy << '\n';
//cout << st.get(st.st[0],3).fs << " " << st.get(st.st[0],3).sc << "\n";
for(int i=0;i<q;i++){
int t;
cin >> t;
if(t==1){
int a,l,r;
cin >> a >> l >> r;
a--;
r--;
st[0].update(0,0,n-1,a,l,r);
st[1].update(0,0,n-1,n-a-1,l,r);
}
else{
int a,b,c,d;
cin >> a >> b >> c >> d;
if(a==c){
cout << max(0ll,b-d) << "\n";
continue;
}
if(a<c){
a--;
c--;
c--;
auto h=st[0].query(0,0,n-1,a,c,b);
cout << h.sc+(max(0ll,h.fs-d)) << "\n";
}
else{
a--;
c--;
a=n-a;
c=n-c;
c--;
//cout << a << " " << c << endl;
auto h=st[1].query(0,0,n-1,a,c,b);
cout << h.sc+(max(0ll,h.fs-d)) << "\n";
}
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
324 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
316 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
256 KB |
Output is correct |
10 |
Correct |
1 ms |
324 KB |
Output is correct |
11 |
Correct |
2 ms |
596 KB |
Output is correct |
12 |
Correct |
2 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
2 ms |
596 KB |
Output is correct |
15 |
Correct |
2 ms |
716 KB |
Output is correct |
16 |
Correct |
2 ms |
596 KB |
Output is correct |
17 |
Correct |
2 ms |
584 KB |
Output is correct |
18 |
Correct |
2 ms |
596 KB |
Output is correct |
19 |
Correct |
2 ms |
596 KB |
Output is correct |
20 |
Correct |
2 ms |
724 KB |
Output is correct |
21 |
Correct |
2 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
588 KB |
Output is correct |
23 |
Correct |
2 ms |
588 KB |
Output is correct |
24 |
Correct |
2 ms |
596 KB |
Output is correct |
25 |
Correct |
2 ms |
596 KB |
Output is correct |
26 |
Correct |
2 ms |
588 KB |
Output is correct |
27 |
Correct |
2 ms |
596 KB |
Output is correct |
28 |
Correct |
2 ms |
596 KB |
Output is correct |
29 |
Correct |
2 ms |
596 KB |
Output is correct |
30 |
Correct |
3 ms |
596 KB |
Output is correct |
31 |
Correct |
2 ms |
644 KB |
Output is correct |
32 |
Correct |
3 ms |
596 KB |
Output is correct |
33 |
Correct |
2 ms |
596 KB |
Output is correct |
34 |
Correct |
2 ms |
648 KB |
Output is correct |
35 |
Correct |
2 ms |
596 KB |
Output is correct |
36 |
Correct |
2 ms |
584 KB |
Output is correct |
37 |
Correct |
2 ms |
648 KB |
Output is correct |
38 |
Correct |
2 ms |
596 KB |
Output is correct |
39 |
Correct |
2 ms |
596 KB |
Output is correct |
40 |
Correct |
2 ms |
596 KB |
Output is correct |
41 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
427 ms |
99384 KB |
Output is correct |
2 |
Correct |
427 ms |
94412 KB |
Output is correct |
3 |
Correct |
406 ms |
95540 KB |
Output is correct |
4 |
Correct |
414 ms |
92492 KB |
Output is correct |
5 |
Correct |
428 ms |
99692 KB |
Output is correct |
6 |
Correct |
400 ms |
97856 KB |
Output is correct |
7 |
Correct |
468 ms |
101168 KB |
Output is correct |
8 |
Correct |
432 ms |
105260 KB |
Output is correct |
9 |
Correct |
389 ms |
93056 KB |
Output is correct |
10 |
Correct |
419 ms |
100664 KB |
Output is correct |
11 |
Correct |
427 ms |
99460 KB |
Output is correct |
12 |
Correct |
415 ms |
106576 KB |
Output is correct |
13 |
Correct |
434 ms |
108024 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
324 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
316 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
256 KB |
Output is correct |
10 |
Correct |
1 ms |
324 KB |
Output is correct |
11 |
Correct |
2 ms |
596 KB |
Output is correct |
12 |
Correct |
2 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
2 ms |
596 KB |
Output is correct |
15 |
Correct |
2 ms |
716 KB |
Output is correct |
16 |
Correct |
2 ms |
596 KB |
Output is correct |
17 |
Correct |
2 ms |
584 KB |
Output is correct |
18 |
Correct |
2 ms |
596 KB |
Output is correct |
19 |
Correct |
2 ms |
596 KB |
Output is correct |
20 |
Correct |
2 ms |
724 KB |
Output is correct |
21 |
Correct |
2 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
588 KB |
Output is correct |
23 |
Correct |
2 ms |
588 KB |
Output is correct |
24 |
Correct |
2 ms |
596 KB |
Output is correct |
25 |
Correct |
2 ms |
596 KB |
Output is correct |
26 |
Correct |
2 ms |
588 KB |
Output is correct |
27 |
Correct |
2 ms |
596 KB |
Output is correct |
28 |
Correct |
2 ms |
596 KB |
Output is correct |
29 |
Correct |
2 ms |
596 KB |
Output is correct |
30 |
Correct |
3 ms |
596 KB |
Output is correct |
31 |
Correct |
2 ms |
644 KB |
Output is correct |
32 |
Correct |
3 ms |
596 KB |
Output is correct |
33 |
Correct |
2 ms |
596 KB |
Output is correct |
34 |
Correct |
2 ms |
648 KB |
Output is correct |
35 |
Correct |
2 ms |
596 KB |
Output is correct |
36 |
Correct |
2 ms |
584 KB |
Output is correct |
37 |
Correct |
2 ms |
648 KB |
Output is correct |
38 |
Correct |
2 ms |
596 KB |
Output is correct |
39 |
Correct |
2 ms |
596 KB |
Output is correct |
40 |
Correct |
2 ms |
596 KB |
Output is correct |
41 |
Correct |
1 ms |
212 KB |
Output is correct |
42 |
Correct |
427 ms |
99384 KB |
Output is correct |
43 |
Correct |
427 ms |
94412 KB |
Output is correct |
44 |
Correct |
406 ms |
95540 KB |
Output is correct |
45 |
Correct |
414 ms |
92492 KB |
Output is correct |
46 |
Correct |
428 ms |
99692 KB |
Output is correct |
47 |
Correct |
400 ms |
97856 KB |
Output is correct |
48 |
Correct |
468 ms |
101168 KB |
Output is correct |
49 |
Correct |
432 ms |
105260 KB |
Output is correct |
50 |
Correct |
389 ms |
93056 KB |
Output is correct |
51 |
Correct |
419 ms |
100664 KB |
Output is correct |
52 |
Correct |
427 ms |
99460 KB |
Output is correct |
53 |
Correct |
415 ms |
106576 KB |
Output is correct |
54 |
Correct |
434 ms |
108024 KB |
Output is correct |
55 |
Correct |
0 ms |
212 KB |
Output is correct |
56 |
Correct |
570 ms |
110184 KB |
Output is correct |
57 |
Correct |
482 ms |
103120 KB |
Output is correct |
58 |
Correct |
500 ms |
113244 KB |
Output is correct |
59 |
Correct |
530 ms |
114080 KB |
Output is correct |
60 |
Correct |
496 ms |
106104 KB |
Output is correct |
61 |
Correct |
520 ms |
117276 KB |
Output is correct |
62 |
Correct |
551 ms |
116780 KB |
Output is correct |
63 |
Correct |
530 ms |
116576 KB |
Output is correct |
64 |
Correct |
539 ms |
117608 KB |
Output is correct |
65 |
Correct |
520 ms |
111636 KB |
Output is correct |
66 |
Correct |
477 ms |
112528 KB |
Output is correct |
67 |
Correct |
507 ms |
116928 KB |
Output is correct |
68 |
Correct |
512 ms |
107584 KB |
Output is correct |
69 |
Correct |
533 ms |
120184 KB |
Output is correct |
70 |
Correct |
503 ms |
106960 KB |
Output is correct |
71 |
Correct |
482 ms |
102808 KB |
Output is correct |
72 |
Correct |
469 ms |
107052 KB |
Output is correct |
73 |
Correct |
512 ms |
117540 KB |
Output is correct |
74 |
Correct |
518 ms |
118180 KB |
Output is correct |
75 |
Correct |
518 ms |
119920 KB |
Output is correct |
76 |
Correct |
494 ms |
120920 KB |
Output is correct |
77 |
Correct |
1 ms |
212 KB |
Output is correct |