#include<bits/stdc++.h>
using namespace std;
int n,m,b,p;
#define N 1<<20
#define M 7<<16
struct info{
int minv,BA,BP,BS,SZ,lz;
info(){minv=lz=BA=BP=BS=0;}
info(int sz){minv=lz=0,BA=BP=BS=SZ=sz;}
info(info a,info b){
minv=min(a.minv,b.minv);
if(minv-b.minv)
*this=a,BS=0;
else if(minv-a.minv)
*this=b,BP=0;
else {
BA=max({a.BA,b.BA,a.BS+b.BS});
BP=(a.BP==a.SZ?a.BP+b.BP:a.BP);
BS=(b.BS==b.SZ?b.BS+a.BS:b.BS);
}
SZ=a.SZ+b.SZ,lz=0;
}
} T[2*N];
void pd(int i,bool b){
if(T[i].lz){
T[i].minv+=T[i].lz;
if(b)
T[i*2].lz+=T[i].lz,T[i*2+1].lz+=T[i].lz;
T[i].lz=0;
}
}
void update(int i,int l,int r,int ql,int qr,int k){
if(ql<=l&&r<=qr)
return T[i].lz+=k,pd(i,l-r);
pd(i,l-r);
if(ql>r||l>qr)
return;
update(i*2,l,l+r>>1,ql,qr,k);
update(i*2+1,l+r+2>>1,r,ql,qr,k);
T[i]=info(T[i*2],T[i*2+1]);
}
void build(int i,int l,int r){
T[i]=info(r-l+1);
if(l-r) build(i*2,l,l+r>>1),
build(i*2+1,l+r+2>>1,r);
}
vector<pair<int,int>> Add[N],Rem[N];
int calc2(){
int ans=0,l=1,r=0;
build(1,1,m);
for(int i=0;i<p;i++){
int a,b,c,d,e;
cin>>a>>b>>c>>d>>e;
Add[a].push_back({b,d});
Rem[c].push_back({b,d});
}
while(r<=n){
int A=r-l+1,B=T[1].BA*!T[1].minv;
ans=max(ans,min(A,B));
if(A<B){
r++;
for(auto[i,j]:Add[r])
update(1,1,m,i,j,1);
} else {
for(auto[i,j]:Rem[l])
update(1,1,m,i,j,-1);
l++;
}
}
return ans;
}
int S[2*N],B[2*N],A[M],B2[M],C[M],D[M],W[M],STUFF[N],SMTH[N],SMTH2[N];
void reset(int i,int l,int r){
S[i]=B[i]=0;
if(l==r) return void(STUFF[l]=i);
reset(i*2,l,l+r>>1);
reset(i*2+1,l+r+2>>1,r);
}
void add(int pos,int x){
pos=STUFF[pos];
S[pos]=B[pos]+=x;
pos/=2;
while(pos){
S[pos]=S[pos*2]+S[pos*2+1];
B[pos]=min(B[pos*2],B[pos*2+1]+S[pos*2]);
pos/=2;
}
}
vector<pair<int,int>>pref[2*N];
bool check(int k){
for(int i=0;i<p;i++){
int a=max(1,A[i]-k+1);
int b=max(1,B2[i]-k+1);
pref[a].push_back({b,W[i]});
if(D[i]<=m-k+1)
pref[a].push_back({D[i],-W[i]});
if(C[i]<=n-k+1)
pref[C[i]].push_back({b,-W[i]}),SMTH[C[i]]=1;
if(D[i]<=m-k+1&&C[i]<=n-k+1)
pref[C[i]].push_back({D[i],W[i]});
}
reset(1,1,m-k+1);
int ans=0;
for(int i=1;i<n-k+2;i++){
for(auto [x,w]:pref[i])
add(x,w);
if(B[1]<=b) {
ans=1;
break;
}
}
for(int i=1;i<=n-k+2;i++)
pref[i].clear();
return ans;
}
int calc(){
b=min(b,7000*p);
for(int i=0;i<p;i++)
cin>>A[i]>>B2[i]>>C[i]>>D[i]>>W[i],C[i]++,D[i]++;
if(!b) for(int i=0;i<p;i++)
W[i]=1;
int l=0,r=min(n,m);
while(l<r){
int mid=l+r+1>>1;
if(check(mid))
l=mid;
else
r=mid-1;
}
return l;
}
#undef N
#undef M
int read(){
int res=0;
char c=getchar();
while(c<'0'||c>'9')
c=getchar();
while(47<c&&c<58)
res=res*10+c-48,c=getchar();
return res;
}
signed main(){
//freopen("input.txt", "r", stdin);
long long XXX=chrono::steady_clock::now().time_since_epoch().count();
cin>>n>>m>>b>>p;
if(b)
cout<<calc();
else cout<<calc2();
long long YYY=chrono::steady_clock::now().time_since_epoch().count();
cerr<<fixed<<setprecision(10)<<"Took: "<<(YYY-XXX)/1e9<<'s';
}
Compilation message
pyramid_base.cpp: In function 'void update(int, int, int, int, int, int)':
pyramid_base.cpp:38:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
38 | update(i*2,l,l+r>>1,ql,qr,k);
| ~^~
pyramid_base.cpp:39:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
39 | update(i*2+1,l+r+2>>1,r,ql,qr,k);
| ~~~^~
pyramid_base.cpp: In function 'void build(int, int, int)':
pyramid_base.cpp:44:26: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
44 | if(l-r) build(i*2,l,l+r>>1),
| ~^~
pyramid_base.cpp:45:24: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
45 | build(i*2+1,l+r+2>>1,r);
| ~~~^~
pyramid_base.cpp: In function 'void reset(int, int, int)':
pyramid_base.cpp:76:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
76 | reset(i*2,l,l+r>>1);
| ~^~
pyramid_base.cpp:77:20: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
77 | reset(i*2+1,l+r+2>>1,r);
| ~~~^~
pyramid_base.cpp: In function 'int calc()':
pyramid_base.cpp:124:20: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
124 | int mid=l+r+1>>1;
| ~~~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
78 ms |
148560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
47 ms |
148740 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
42 ms |
148560 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
148652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
45 ms |
148568 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
58 ms |
148596 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
55 ms |
148760 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
149076 KB |
Output is correct |
2 |
Correct |
64 ms |
149856 KB |
Output is correct |
3 |
Correct |
63 ms |
149840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
131 ms |
153980 KB |
Output is correct |
2 |
Correct |
168 ms |
154976 KB |
Output is correct |
3 |
Correct |
163 ms |
155180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
297 ms |
165840 KB |
Output is correct |
2 |
Correct |
110 ms |
157524 KB |
Output is correct |
3 |
Correct |
155 ms |
169700 KB |
Output is correct |
4 |
Correct |
517 ms |
180564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
497 ms |
178288 KB |
Output is correct |
2 |
Correct |
854 ms |
183400 KB |
Output is correct |
3 |
Correct |
326 ms |
163020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
430 ms |
166460 KB |
Output is correct |
2 |
Correct |
1109 ms |
185632 KB |
Output is correct |
3 |
Correct |
1080 ms |
185272 KB |
Output is correct |
4 |
Correct |
1115 ms |
185872 KB |
Output is correct |
5 |
Correct |
1164 ms |
185564 KB |
Output is correct |
6 |
Correct |
235 ms |
162256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
671 ms |
160096 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
935 ms |
164940 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1108 ms |
169652 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |