#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize(2)
#define N 1<<19
int S[4*N],B[4*N],n,m,p,b,A[N],B2[N],C[N],D[N],W[N],something[2*N];
void reset(int i,int l,int r){
S[i]=B[i]=0;
if(l==r) return;
reset(i*2,l,l+r>>1);
reset(i*2+1,l+r+2>>1,r);
}
void add(int i,int l,int r,int pos,int x){
if(l==r)
return void(S[i]=B[i]+=x);
if(pos>l+r>>1)
add(i*2+1,l+r+2>>1,r,pos,x);
else add(i*2,l,l+r>>1,pos,x);
S[i]=S[i*2]+S[i*2+1];
B[i]=min(B[i*2],S[i*2]+B[i*2+1]);
}
unordered_map<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);
int c=min(C[i],n-k+1)+1;
int d=min(D[i],m-k+1)+1;
pref[a][b]+=W[i];
pref[a][d]-=W[i];
pref[c][b]-=W[i];
pref[c][d]+=W[i];
something[a]=something[c]=1;
}
reset(1,1,m-k+1);
if(!something[1]) {
for(int i=1;i<=n-k+2;i++)
if(something[i])
pref[i].clear(),
something[i]=0;
return 1;
}
for(int i=1;i<n-k+2;i++){
if(i==n-k+1){
cerr<<"HI";
}
if(something[i])
for(auto [x,w]:pref[i])
if(x<=m-k+1)
add(1,1,m-k+1,x,w);
if(B[1]<=b) {
for(int i=1;i<=n-k+2;i++)
if(something[i])
pref[i].clear(),
something[i]=0;
return 1;
}
}
for(int i=1;i<=n-k+2;i++)
if(something[i])
pref[i].clear(),
something[i]=0;
return 0;
}
signed main(){
long long XXX=chrono::steady_clock::now().time_since_epoch().count();
cin.tie(0)->sync_with_stdio(0);
cin>>n>>m>>b>>p;
b=min(b,1<<30);
for(int i=0;i<p;i++)
cin>>A[i]>>B2[i]>>C[i]>>D[i]>>W[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;
cerr<<"DONE1\n";
}
cout<<l<<'\n';
long long YYY=chrono::steady_clock::now().time_since_epoch().count();
cerr<<"Took: "<<(YYY-XXX)/1e9<<'s';
}
Compilation message
pyramid_base.cpp: In function 'void reset(int, int, int)':
pyramid_base.cpp:9:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
9 | reset(i*2,l,l+r>>1);
| ~^~
pyramid_base.cpp:10:20: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
10 | reset(i*2+1,l+r+2>>1,r);
| ~~~^~
pyramid_base.cpp: In function 'void add(int, int, int, int, int)':
pyramid_base.cpp:15:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
15 | if(pos>l+r>>1)
| ~^~
pyramid_base.cpp:16:22: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
16 | add(i*2+1,l+r+2>>1,r,pos,x);
| ~~~^~
pyramid_base.cpp:17:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
17 | else add(i*2,l,l+r>>1,pos,x);
| ~^~
pyramid_base.cpp: In function 'int main()':
pyramid_base.cpp:75:20: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
75 | int mid=l+r+1>>1;
| ~~~^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
57944 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
57944 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
57948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
58988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
39 ms |
61788 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
52 ms |
68736 KB |
Output is correct |
2 |
Correct |
147 ms |
79996 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
146 ms |
80064 KB |
Output is correct |
2 |
Correct |
61 ms |
68416 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
58980 KB |
Output is correct |
2 |
Correct |
73 ms |
60168 KB |
Output is correct |
3 |
Correct |
61 ms |
60240 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
227 ms |
70720 KB |
Output is correct |
2 |
Correct |
347 ms |
72000 KB |
Output is correct |
3 |
Correct |
336 ms |
72784 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
444 ms |
87504 KB |
Output is correct |
2 |
Correct |
208 ms |
80300 KB |
Output is correct |
3 |
Correct |
199 ms |
80456 KB |
Output is correct |
4 |
Correct |
968 ms |
112020 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
903 ms |
109256 KB |
Output is correct |
2 |
Correct |
1520 ms |
118596 KB |
Output is correct |
3 |
Correct |
169 ms |
76136 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
570 ms |
91712 KB |
Output is correct |
2 |
Correct |
1864 ms |
128156 KB |
Output is correct |
3 |
Correct |
1926 ms |
126904 KB |
Output is correct |
4 |
Correct |
1997 ms |
128064 KB |
Output is correct |
5 |
Correct |
2064 ms |
128364 KB |
Output is correct |
6 |
Correct |
88 ms |
72364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
5056 ms |
203444 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
5036 ms |
216092 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
5067 ms |
226488 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |