Submission #1077330

# Submission time Handle Problem Language Result Execution time Memory
1077330 2024-08-27T05:28:09 Z ainta Cultivation (JOI17_cultivation) C++17
30 / 100
168 ms 1712 KB
#include <bits/stdc++.h>
using namespace std;

#define rng(i,a,b) for(int i=int(a);i<=int(b);i++)
#define rep(i,b) rng(i,0,b-1)
#define gnr(i,b,a) for(int i=int(b);i>=int(a);i--)
#define per(i,b) gnr(i,b-1,0)
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define bg begin()
#define ed end()
#define all(x) x.bg,x.ed
#define si(x) int(x.size())
template<class t> using vc=vector<t>;
template<class t> using vvc=vc<vc<t>>;
typedef long long ll;
using pii=pair<int,int>;
using vi=vc<int>;
using uint=unsigned;
using ull=unsigned long long;
using pil=pair<int,ll>;
using pli=pair<ll,int>;
using pll=pair<ll,ll>;
using t3=tuple<int,int,int>;

ll rand_int(ll l, ll r) { //[l, r]
	#ifdef LOCAL
	static mt19937_64 gen;
	#else
    static mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count());
    #endif
    return uniform_int_distribution<ll>(l, r)(gen);
}


template <uint MD> struct ModInt {
    using M = ModInt;
    const static M G;
    uint v;
    ModInt(ll _v = 0) { set_v(_v % MD + MD); }
    M& set_v(uint _v) {
        v = (_v < MD) ? _v : _v - MD;
        return *this;
    }
    explicit operator bool() const { return v != 0; }
    M operator-() const { return M() - *this; }
    M operator+(const M& r) const { return M().set_v(v + r.v); }
    M operator-(const M& r) const { return M().set_v(v + MD - r.v); }
    M operator*(const M& r) const { return M().set_v(ull(v) * r.v % MD); }
    M operator/(const M& r) const { return *this * r.inv(); }
    M& operator+=(const M& r) { return *this = *this + r; }
    M& operator-=(const M& r) { return *this = *this - r; }
    M& operator*=(const M& r) { return *this = *this * r; }
    M& operator/=(const M& r) { return *this = *this / r; }
    bool operator==(const M& r) const { return v == r.v; }
    M pow(ll n) const {
        M x = *this, r = 1;
        while (n) {
            if (n & 1) r *= x;
            x *= x;
            n >>= 1;
        }
        return r;
    }
    M inv() const { return pow(MD - 2); }
    friend ostream& operator<<(ostream& os, const M& r) { return os << r.v; }
};
using Mint = ModInt<998244353>;
template<> const Mint Mint::G = Mint(3);

using t4 = tuple<int,int,int,int>;

int H, W, n;
int res=2e9;

void Solve(){
    scanf("%d%d",&H,&W);
    scanf("%d",&n);
    vc<pii>w(n);
    set<int>SX, SY;
    rep(i,n){
        scanf("%d%d",&w[i].fi,&w[i].se);
        w[i].fi-=H,w[i].se-=W;
        SX.insert(w[i].fi);SX.insert(w[i].fi+H);
        SY.insert(w[i].se);SY.insert(w[i].se+W);
    }
    vi X, Y;
    for(auto &t: SX)X.pb(t);
    for(auto &t: SY)Y.pb(t);

    rep(i,n){
        w[i].fi = lower_bound(all(X), w[i].fi) - X.begin();
        w[i].se = lower_bound(all(Y), w[i].se) - Y.begin();
    }
    vi DX, DY;
    rep(i,si(X)){
        rng(j,i+1,si(X)-1)DX.pb(X[j]-X[i]);
    }
    rep(i,si(Y)){
        rng(j,i+1,si(Y)-1)DY.pb(Y[j]-Y[i]);
    }
    sort(all(DX));
    DX.resize(unique(all(DX)) - DX.begin());
    sort(all(DY));
    DY.resize(unique(all(DY)) - DY.begin());

    int nX = si(X), nY = si(Y);
    vi PX(n), PY(n);
    vc<vi> S(nX,vi(nY));
    rep(i,n){
        PX[i] = w[i].fi;
        PY[i] = nY-1;
    }
    vc<priority_queue<int>>PQ1(nX), PQ2(nX);

    int mY = lower_bound(all(Y),1) - Y.begin();

    auto add0 = [&](int x, int y){
        if(y<=mY) PQ1[x].push(y);
        else PQ2[x].push(-y);
    };

    auto getLeft = [&](int x){
        while(!PQ1[x].empty()){
            int y = PQ1[x].top();
            if(!S[x][y])return y;
            PQ1[x].pop();
        }
        return 0;
    };
    auto getRight = [&](int x){
        while(!PQ2[x].empty()){
            int y = -PQ2[x].top();
            if(!S[x][y])return y;
            PQ2[x].pop();
        }
        return nY;
    };
    int ans = 2e9;
    rng(i,1,nX-1){
        rng(j,1,nY-1){
            add0(i,j);
        }
    }
    vi LL(nX), RR(nX), dL(nX), dR(nX);
    int pdy = si(DY)-1, dy = DY.back();
    for(auto &dx : DX){
        rep(i,n){
            int bx = w[i].fi;
            int by = w[i].se;
            while(PX[i] < nX - 1 && X[PX[i]+1] - X[bx] <= dx){
                PX[i]++;
                rng(j,by+1,PY[i]){
                    S[PX[i]][j]++;
                }
            }
        }
        while(1){
            /*printf("! %d %d\n",dx,dy);
            rng(i,1,nX-1){
                rng(j,1,nY-1){
                    printf("%d",S[i][j]);
                }
                puts("");
            }*/
            rng(i,1,nX-1){
                LL[i] = getLeft(i);
                RR[i] = getRight(i);
                if(LL[i]==mY)RR[i]=mY;
                RR[i]--;
            }
            int hL=1, tL=0;
            int hR=1, tR=0, CK=0;
            rng(i,1,nX-1){
                while(hL <= tL && X[i] - X[dL[hL]] >= H)hL++;
                while(hL <= tL && LL[dL[tL]] <= LL[i])tL--;
                dL[++tL] = i;
                int l = LL[dL[hL]];

                while(hR <= tR && X[i] - X[dR[hR]] >= H)hR++;
                while(hR <= tR && RR[dR[tR]] >= RR[i])tR--;
                dR[++tR] = i;
                int r = RR[dR[hR]];

                //printf("%d %d %d %d\n",X[i],X[0],Y[r],Y[l]);

                if(X[i]-X[0] >= H && Y[r]-Y[l] >= W){
                    //printf("%d %d %d %d\n",dx,dy,Y[l],Y[r]);
                    ans = min(ans, dx+dy-2);
                    CK=1;
                    break;
                }
            }
            if(!CK)break;
            pdy--;
            if(pdy<0)break;
            dy = DY[pdy];
            rep(i,n){
                int bx = w[i].fi;
                int by = w[i].se;
                while(Y[PY[i]] - Y[by] > dy){
                    rng(j,bx+1,PX[i]){
                        S[j][PY[i]]--;
                        if(!S[j][PY[i]])add0(j,PY[i]);
                    }
                    PY[i]--;
                }
            }
        }
        if(pdy<0)break;
    }
    printf("%d\n",ans);
}

int main(){
	int TC=1;
	while(TC--){
		Solve();
	}
}

Compilation message

cultivation.cpp: In function 'void Solve()':
cultivation.cpp:79:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   79 |     scanf("%d%d",&H,&W);
      |     ~~~~~^~~~~~~~~~~~~~
cultivation.cpp:80:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   80 |     scanf("%d",&n);
      |     ~~~~~^~~~~~~~~
cultivation.cpp:84:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   84 |         scanf("%d%d",&w[i].fi,&w[i].se);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 600 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 1 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 600 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 1 ms 344 KB Output is correct
33 Correct 86 ms 1492 KB Output is correct
34 Correct 168 ms 1692 KB Output is correct
35 Correct 10 ms 984 KB Output is correct
36 Correct 156 ms 1712 KB Output is correct
37 Correct 151 ms 1576 KB Output is correct
38 Correct 155 ms 1488 KB Output is correct
39 Correct 118 ms 1708 KB Output is correct
40 Correct 158 ms 1492 KB Output is correct
41 Correct 102 ms 1492 KB Output is correct
42 Correct 15 ms 984 KB Output is correct
43 Correct 155 ms 1492 KB Output is correct
44 Correct 44 ms 980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 492 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Incorrect 1 ms 348 KB Output isn't correct
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 492 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Incorrect 1 ms 348 KB Output isn't correct
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 600 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 1 ms 344 KB Output is correct
33 Correct 86 ms 1492 KB Output is correct
34 Correct 168 ms 1692 KB Output is correct
35 Correct 10 ms 984 KB Output is correct
36 Correct 156 ms 1712 KB Output is correct
37 Correct 151 ms 1576 KB Output is correct
38 Correct 155 ms 1488 KB Output is correct
39 Correct 118 ms 1708 KB Output is correct
40 Correct 158 ms 1492 KB Output is correct
41 Correct 102 ms 1492 KB Output is correct
42 Correct 15 ms 984 KB Output is correct
43 Correct 155 ms 1492 KB Output is correct
44 Correct 44 ms 980 KB Output is correct
45 Correct 1 ms 348 KB Output is correct
46 Correct 1 ms 348 KB Output is correct
47 Correct 1 ms 492 KB Output is correct
48 Correct 1 ms 348 KB Output is correct
49 Correct 1 ms 348 KB Output is correct
50 Correct 1 ms 348 KB Output is correct
51 Incorrect 1 ms 348 KB Output isn't correct
52 Halted 0 ms 0 KB -