#include<bits/stdc++.h>
using namespace std;
#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],STUFF[2*N],something[2*N],something2[2*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]}),something[C[i]]=1;
if(D[i]<=m-k+1&&C[i]<=n-k+1)
pref[C[i]].push_back({D[i],W[i]});
something[a]=1;
}
reset(1,1,m-k+1);
int ans=0;
if(!something[1])
ans=1;
else for(int i=1;i<n-k+2;i++){
if(something[i]) {
for(auto [x,w]:pref[i])
something2[x]+=w;
for(auto [x,w]:pref[i])
if(something2[x]) {
w=STUFF[x];
S[w]=B[w]+=something2[x];
something2[x]=0;
w/=2;
while(w){
S[w]=S[w*2]+S[w*2+1];
B[w]=min(B[w*2],B[w*2+1]+S[w*2]);
w/=2;
}
}
}
if(B[1]<=b) {
ans=1;
break;
}
}
for(int i=1;i<=n-k+2;i++)
if(something[i])
pref[i].clear(),
something[i]=0;
return ans;
}
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(){
long long XXX=chrono::steady_clock::now().time_since_epoch().count();
cin>>n>>m>>b>>p;
b=min(b,1<<28);
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;
cerr<<"DONE1\n";
}
cout<<l<<'\n';
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 reset(int, int, int)':
pyramid_base.cpp:8:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
8 | reset(i*2,l,l+r>>1);
| ~^~
pyramid_base.cpp:9:20: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
9 | reset(i*2+1,l+r+2>>1,r);
| ~~~^~
pyramid_base.cpp: In function 'int main()':
pyramid_base.cpp:86:20: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
86 | int mid=l+r+1>>1;
| ~~~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
43612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
43612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
43612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
43868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
54364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
58200 KB |
Output is correct |
2 |
Correct |
120 ms |
64340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
121 ms |
64592 KB |
Output is correct |
2 |
Correct |
42 ms |
58120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
43864 KB |
Output is correct |
2 |
Correct |
30 ms |
44380 KB |
Output is correct |
3 |
Correct |
29 ms |
44380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
99 ms |
56600 KB |
Output is correct |
2 |
Correct |
142 ms |
57420 KB |
Output is correct |
3 |
Correct |
135 ms |
57448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
232 ms |
63024 KB |
Output is correct |
2 |
Correct |
68 ms |
50504 KB |
Output is correct |
3 |
Correct |
132 ms |
63176 KB |
Output is correct |
4 |
Correct |
494 ms |
73104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
440 ms |
72216 KB |
Output is correct |
2 |
Correct |
893 ms |
75240 KB |
Output is correct |
3 |
Correct |
101 ms |
60616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
343 ms |
64012 KB |
Output is correct |
2 |
Correct |
1131 ms |
77292 KB |
Output is correct |
3 |
Correct |
1070 ms |
76948 KB |
Output is correct |
4 |
Correct |
1153 ms |
77320 KB |
Output is correct |
5 |
Correct |
1184 ms |
77520 KB |
Output is correct |
6 |
Correct |
66 ms |
59852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4654 ms |
111684 KB |
Output is correct |
2 |
Correct |
903 ms |
88528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5010 ms |
122040 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5038 ms |
128316 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |