#include <bits/stdc++.h>
using namespace std;
// macros
typedef unsigned int ui;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef pair<ll, ll> lll;
typedef tuple<int, int, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;
typedef vector<ll> vll;
typedef vector<lll> vlll;
#define REP(a,b,c) for(int a=int(b); a<int(c); a++)
#define RE(a,c) REP(a,0,c)
#define RE1(a,c) REP(a,1,c+1)
#define REI(a,b,c) REP(a,b,c+1)
#define REV(a,b,c) for(int a=int(c-1); a>=int(b); a--)
#define FOR(a,b) for(auto& a : b)
#define all(a) a.begin(), a.end()
#define INF (3e9+10)
#define EPS 1e-9
#define pb push_back
#define popb pop_back
#define fi first
#define se second
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
// input
template<class T> void IN(T& x) {cin >> x;}
template<class H, class... T> void IN(H& h, T&... t) {IN(h); IN(t...); }
// output
template<class T1, class T2> void OUT(const pair<T1,T2>& x);
template<class T> void OUT(const vector<T>& x);
template<class T> void OUT(const T& x) {cout << x;}
template<class H, class... T> void OUT(const H& h, const T&... t) {OUT(h); OUT(t...); }
template<class T1, class T2> void OUT(const pair<T1,T2>& x) {OUT(x.fi,' ',x.se);}
template<class T> void OUT(const vector<T>& x) {RE(i,x.size()) OUT(i==0?"":" ",x[i]);}
template<class... T> void OUTL(const T&... t) {OUT(t..., "\n"); }
template<class H> void OUTLS(const H& h) {OUTL(h); }
template<class H, class... T> void OUTLS(const H& h, const T&... t) {OUT(h,' '); OUTLS(t...); }
// dp
template<class T> bool ckmin(T&a, T&b) { bool bl = a > b; a = min(a,b); return bl;}
template<class T> bool ckmax(T&a, T&b) { bool bl = a < b; a = max(a,b); return bl;}
void program();
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
program();
}
//===================//
// begin program //
//===================//
const int MX = 1e6;
const int N = (1<<21);
int n, m, p; ui b;
int c[MX], X1[MX], X2[MX], Y1[MX], Y2[MX];
// segment tree
ui SEG[N*2], LAZ[N*2];
void addSeg(int i, int j, ui v, ui lazy=0, int l=0, int r=N-1, int p=1) {
SEG[p] += lazy;
LAZ[p] += lazy;
if(j < l || i > r) return;
if(i <= l && j >= r) {
SEG[p] += v;
LAZ[p] += v;
return;
}
int m=(l+r)/2;
addSeg(i,j,v,LAZ[p],l ,m,p*2 );
addSeg(i,j,v,LAZ[p],m+1,r,p*2+1);
LAZ[p] = 0;
SEG[p] = min(SEG[p*2], SEG[p*2+1]);
}
ui getSeg(int i, int j, ui lazy=0, int l=0, int r=N-1, int p=1) {
SEG[p] += lazy;
LAZ[p] += lazy;
if(j < l || i > r) return INF;
if(i <= l && j >= r) return SEG[p];
int m=(l+r)/2;
ui a=getSeg(i,j,LAZ[p],l ,m,p*2 );
ui b=getSeg(i,j,LAZ[p],m+1,r,p*2+1);
LAZ[p] = 0;
return min(a,b);
}
typedef tuple<int, int, int, int> iiii;
typedef vector<iiii> viiii;
bool possible(int w) {
RE(i,p) X1[i] -= w-1;
RE(i,p) Y1[i] -= w-1;
// fill pq
viiii pq;
RE(i,p) {
pq.pb({max(0,X1[i] ),-c[i],max(0,Y1[i]),Y2[i]});
pq.pb({max(0,X2[i]+1), c[i],max(0,Y1[i]),Y2[i]});
}
sort(all(pq));
// clear segment tree
RE(i,N*2) SEG[i]=LAZ[i]=0;
int cx, lbY, ubY, cost, nextX, _;
tie(cx, cost, lbY, ubY) = pq[0];
if(cx > 0) {
RE(i,p) X1[i] += w-1;
RE(i,p) Y1[i] += w-1;
return 0;
}
// sweep line
RE(i,pq.size()) {
tie(cx, cost, lbY, ubY) = pq[i];
cost = -cost;
if(i != pq.size()-1)
tie(nextX, _, _, _) = pq[i+1];
addSeg(lbY, ubY, cost);
if((i == pq.size()-1 || nextX != 0) && cx <= m-w) {
if(getSeg(0,n-w) <= b) {
RE(i,p) X1[i] += w-1;
RE(i,p) Y1[i] += w-1;
return true;
}
}
}
RE(i,p) X1[i] += w-1;
RE(i,p) Y1[i] += w-1;
return false;
}
// other segment tree
int LEN[N*2], MIN[N*2], MAX[N*2], PREMIN[N*2], SUFMIN[N*2], PRELEN[N*2], SUFLEN[N*2], LAZY[N*2];
void buildSeg2(int l=0, int r=N-1, int p=1) {
LAZY [p] = 0;
if(l == r) {
LEN[p] = PREMIN[p] = PRELEN[p] = SUFMIN[p] = SUFLEN[p] = MIN[p] = MAX[p] = 1;
return;
}
int m=(l+r)/2;
buildSeg2(l ,m,p*2 );
buildSeg2(m+1,r,p*2+1);
MIN [p] = min(MIN[p*2], MIN[p*2+1]);
MAX [p] = max(MAX[p*2], MAX[p*2+1]);
if(MIN[p*2] == MIN[p*2+1]) LEN[p] = max(LEN[p*2], LEN[p*2+1]);
else LEN[p] = MIN[p*2] < MIN[p*2+1] ? LEN[p*2] : LEN[p*2+1];
if(SUFMIN[p*2] == MIN[p] && PREMIN[p*2+1] == MIN[p]) LEN[p] = max(LEN[p], SUFLEN[p*2]+PRELEN[p*2+1]);
// prefix
PREMIN[p] = PREMIN[p*2];
PRELEN[p] = PRELEN[p*2];
if(MAX[p*2] == PREMIN[p*2] && PREMIN[p*2] == PREMIN[p*2+1]) PRELEN[p] = PRELEN[p*2] + PRELEN[p*2+1];
// suffix
SUFMIN[p] = SUFMIN[p*2+1];
SUFLEN[p] = SUFLEN[p*2+1];
if(MAX[p*2+1] == SUFMIN[p*2+1] && SUFMIN[p*2+1] == SUFMIN[p*2]) SUFLEN[p] = SUFLEN[p*2] + SUFLEN[p*2+1];
}
void addSeg2(int i, int j, int v, int lazy=0, int l=0, int r=N-1, int p=1) {
PREMIN[p] += lazy;
SUFMIN[p] += lazy;
MIN [p] += lazy;
MAX [p] += lazy;
LAZY [p] += lazy;
if(j < l || i > r) return;
if(i <= l && j >= r) {
PREMIN[p] += v;
SUFMIN[p] += v;
MIN [p] += v;
MAX [p] += v;
LAZY [p] += v;
return;
}
int m=(l+r)/2;
addSeg2(i,j,v,LAZY[p],l ,m,p*2 );
addSeg2(i,j,v,LAZY[p],m+1,r,p*2+1);
LAZY [p] = 0;
MIN [p] = min(MIN[p*2], MIN[p*2+1]);
MAX [p] = max(MAX[p*2], MAX[p*2+1]);
if(MIN[p*2] == MIN[p*2+1]) LEN[p] = max(LEN[p*2], LEN[p*2+1]);
else LEN[p] = MIN[p*2] < MIN[p*2+1] ? LEN[p*2] : LEN[p*2+1];
if(SUFMIN[p*2] == MIN[p] && PREMIN[p*2+1] == MIN[p]) LEN[p] = max(LEN[p], SUFLEN[p*2]+PRELEN[p*2+1]);
// prefix
PREMIN[p] = PREMIN[p*2];
PRELEN[p] = PRELEN[p*2];
if(MAX[p*2] == PREMIN[p*2] && PREMIN[p*2] == PREMIN[p*2+1]) PRELEN[p] = PRELEN[p*2] + PRELEN[p*2+1];
// suffix
SUFMIN[p] = SUFMIN[p*2+1];
SUFLEN[p] = SUFLEN[p*2+1];
if(MAX[p*2+1] == SUFMIN[p*2+1] && SUFMIN[p*2+1] == SUFMIN[p*2]) SUFLEN[p] = SUFLEN[p*2] + SUFLEN[p*2+1];
}
int solveZero() {
buildSeg2();
addSeg2(0,n-1,-1);
priority_queue<ii,vii,greater<ii>> pqCreate, pqDestroy;
RE(i,p) {
pqCreate .push({X1[i],i});
pqDestroy.push({X2[i],i});
}
int res=0;
int r=0;
while(!pqCreate.empty()) {
ii p = pqCreate.top();
if(p.fi > r) break;
pqCreate.pop();
addSeg2(Y1[p.se], Y2[p.se], 1);
}
RE(l,m) {
while(!pqDestroy.empty()) {
ii p = pqDestroy.top();
if(p.fi >= l) break;
pqDestroy.pop();
addSeg2(Y1[p.se], Y2[p.se], -1);
}
while(r < l || (r!=m-1 && MIN[1] == 0 && LEN[1] >= r-l+1)) {
if(MIN[1] == 0 && LEN[1] >= r-l+1)
res = max(res, r-l+1);
r++;
while(!pqCreate.empty()) {
ii p = pqCreate.top();
if(p.fi > r) break;
pqCreate.pop();
addSeg2(Y1[p.se], Y2[p.se], 1);
}
}
if(MIN[1] == 0)
res = max(res, min(r-l+1, LEN[1]));
}
return res;
}
void program() {
IN(m,n,b,p);
RE(i,p) IN(X1[i],Y1[i],X2[i],Y2[i],c[i]);
RE(i,p) X1[i]--, Y1[i]--, X2[i]--, Y2[i]--;
if(b == 0) {
OUTL(solveZero());
} else {
int lb=0, ub=min(n,m);
while(lb != ub) {
int mid=(lb+ub+1)/2;
if(possible(mid)) lb=mid;
else ub=mid-1;
}
OUTL(lb);
}
}
Compilation message
pyramid_base.cpp: In function 'bool possible(int)':
pyramid_base.cpp:128:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::tuple<int, int, int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
128 | if(i != pq.size()-1)
| ~~^~~~~~~~~~~~~~
pyramid_base.cpp:132:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::tuple<int, int, int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
132 | if((i == pq.size()-1 || nextX != 0) && cx <= m-w) {
| ~~^~~~~~~~~~~~~~
pyramid_base.cpp:132:30: warning: 'nextX' may be used uninitialized in this function [-Wmaybe-uninitialized]
132 | if((i == pq.size()-1 || nextX != 0) && cx <= m-w) {
| ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
171 ms |
131640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
175 ms |
131572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
192 ms |
131708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
191 ms |
131652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
181 ms |
131680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
212 ms |
131636 KB |
Output is correct |
2 |
Correct |
220 ms |
131724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
188 ms |
131668 KB |
Output is correct |
2 |
Correct |
211 ms |
131728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
133 ms |
33680 KB |
Output is correct |
2 |
Correct |
201 ms |
33860 KB |
Output is correct |
3 |
Correct |
185 ms |
33704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
490 ms |
34472 KB |
Output is correct |
2 |
Correct |
681 ms |
34460 KB |
Output is correct |
3 |
Correct |
615 ms |
34468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
647 ms |
35248 KB |
Output is correct |
2 |
Correct |
130 ms |
35240 KB |
Output is correct |
3 |
Correct |
262 ms |
35200 KB |
Output is correct |
4 |
Correct |
859 ms |
35228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
991 ms |
35592 KB |
Output is correct |
2 |
Correct |
1505 ms |
35524 KB |
Output is correct |
3 |
Correct |
772 ms |
35596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1093 ms |
35816 KB |
Output is correct |
2 |
Correct |
1986 ms |
35916 KB |
Output is correct |
3 |
Correct |
1872 ms |
35792 KB |
Output is correct |
4 |
Correct |
2007 ms |
35768 KB |
Output is correct |
5 |
Correct |
2009 ms |
35956 KB |
Output is correct |
6 |
Correct |
887 ms |
35732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1699 ms |
138952 KB |
Output is correct |
2 |
Correct |
968 ms |
138956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2249 ms |
143864 KB |
Output is correct |
2 |
Correct |
2346 ms |
143912 KB |
Output is correct |
3 |
Correct |
1485 ms |
151796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2873 ms |
145980 KB |
Output is correct |
2 |
Correct |
3442 ms |
145932 KB |
Output is correct |
3 |
Correct |
3616 ms |
145968 KB |
Output is correct |
4 |
Correct |
3098 ms |
156872 KB |
Output is correct |
5 |
Correct |
1952 ms |
156984 KB |
Output is correct |