結果

問題 No.2157 崖
ユーザー dyktr_06dyktr_06
提出日時 2022-12-09 22:58:27
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
MLE  
実行時間 -
コード長 5,409 bytes
コンパイル時間 2,775 ms
コンパイル使用メモリ 187,796 KB
実行使用メモリ 777,764 KB
最終ジャッジ日時 2024-04-22 22:53:45
合計ジャッジ時間 10,964 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 3 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 4 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 4 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 MLE -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'void in(T& ...)':
main.cpp:28:55: warning: fold-expressions only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
   28 | template<class... T> void in(T&... a){ (cin >> ... >> a); }
      |                                                       ^
main.cpp: In function 'void out(const T&, const Ts& ...)':
main.cpp:30:112: warning: fold-expressions only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
   30 | template<class T, class... Ts> void out(const T& a, const Ts&... b){ cout << a; (cout << ... << (cout << ' ', b)); cout << '\n'; }
      |                                                                                                                ^

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

#define rep(i,n) for(int i = 0; i < (int)(n); ++i)
#define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; --i)
#define ALL(a) a.begin(), a.end()
#define Sort(a) sort(a.begin(), a.end())
#define RSort(a) sort(a.rbegin(), a.rend())

typedef long long int ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef vector<char> vc;
typedef vector<string> vst;
typedef vector<double> vd;
typedef pair<long long, long long> P;

const long long INF = 0x1fffffffffffffff;
const long long MOD = 998244353;
const long double PI = acos(-1);
 
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T, class U> inline T vin(T& vec, U n) { vec.resize(n); for(int i = 0; i < (int) n; ++i) cin >> vec[i]; return vec; }
template<class T> inline void vout(T vec, string s = "\n"){ for(auto x : vec) cout << x << s; }
template<class... T> void in(T&... a){ (cin >> ... >> a); }
void out(){ cout << '\n'; }
template<class T, class... Ts> void out(const T& a, const Ts&... b){ cout << a; (cout << ... << (cout << ' ', b)); cout << '\n'; }
template<class T, class U> void inGraph(vector<vector<T>>& G, U n, U m, bool directed = false){ G.resize(n); for(int i = 0; i < m; i++){ int a, b; cin >> a >> b; a--, b--; G[a].push_back(b); if(!directed) G[b].push_back(a); } }

template <typename S, typename T>
struct RangeTree{
    struct BinaryIndexedTree{
        int N;
        vector<T> BIT;
        BinaryIndexedTree() {}

        void init(int size){
            N = size;
            BIT.assign(N + 1, 0);
        }

        T get(int i){
            return sum(i + 1) - sum(i);
        }

        void add(int i, T x){
            ++i;
            while(i <= N){
                BIT[i] += x;
                i += i & -i;
            }
        }

        T sum(int i) const {
            T ans = 0;
            while(i > 0){
                ans += BIT[i];
                i -= i & -i;
            }
            return ans;
        }

        T sum(int L, int R) const {
            return sum(R) - sum(L);
        }
    };

    using P = pair<S, S>;
    S N, M;
    vector<BinaryIndexedTree> bit;
    vector<vector<S>> ys;
    vector<P> ps;

    RangeTree() {}

    void add_point(S x, S y){
        ps.push_back(make_pair(x, y));
    }

    void build(){
        sort(ps.begin(), ps.end());
        ps.erase(unique(ps.begin(), ps.end()), ps.end());

        N = ps.size();
        bit.resize(N + 1);
        ys.resize(N + 1);
        for(int i = 0; i <= N; ++i){
            int j = i + 1;
            while(j <= N){
                ys[j].push_back(ps[i].second);
                j += j & -j;
            }

            sort(ys[i].begin(), ys[i].end());
            ys[i].erase(unique(ys[i].begin(), ys[i].end()), ys[i].end());
            bit[i].init(ys[i].size());
        }
    }

    int id(S x) const {
        auto f = [](const P& a, const P& b){
            return a.first < b.first;
        };
        return lower_bound(ps.begin(), ps.end(), make_pair(x, S()), f) - ps.begin();
    }

    int id(int i, S y) const {
        return lower_bound(ys[i].begin(), ys[i].end(), y) - ys[i].begin();
    }

    void add(S x, S y, T a){
        int i = lower_bound(ps.begin(), ps.end(), make_pair(x, y)) - ps.begin();
        assert(ps[i] == make_pair(x, y));
        ++i;
        while(i <= N){
            bit[i].add(id(i, y), a);
            i += i & -i;
        }
    }

    T sum(S x, S y) const {
        T ret = T();
        int a = id(x);
        while(a > 0){
            ret += bit[a].sum(id(a, y));
            a -= a & -a;
        }
        return ret;
    }

    T sum(S xl, S yl, S xr, S yr) const {
        T ret = T();
        int a = id(xl), b = id(xr);
        while(a != b) {
            if(a < b){
                ret += bit[b].sum(id(b, yl), id(b, yr));
                b -= b & -b;
            }else{
                ret -= bit[a].sum(id(a, yl), id(a, yr));
                a -= a & -a;
            }
        }
        return ret;
    }
};

ll n, m;
vector<vector<ll>> d;

void input(){
    in(n, m);
    d = vector<vector<ll>>(n, vector<ll>(m));
    rep(i, n) rep(j, m) in(d[i][j]);
}
 
void solve(){
    rep(i, n) Sort(d[i]);
    ll ans = INF;

    ll ok = 1e9, ng = -1;
    while(abs(ok - ng) > 1){
        ll mid = (ok + ng) / 2;
        RangeTree<ll, ll> tree;
        rep(i, n) rep(j, m){
            tree.add_point(i, d[i][j]);
            tree.add_point(i, d[i][j] + mid + 1);
        }
        tree.build();
        ll check = 1;
        rep(j, m){
            tree.add(0, d[0][j], 1);
            tree.add(0, d[0][j] + mid + 1, -1);
        }
        for(int i = 1; i < n; i++){
            ll cnt = 0;
            rep(j, m){
                if(tree.sum(i - 1, 0, i, d[i][j] + 1) >= 1){
                    tree.add(i, d[i][j], 1);
                    tree.add(i, d[i][j] + mid + 1, -1);
                    cnt++;
                }
            }
            if(cnt == 0){
                check = 0;
                break;
            }
        }
        if(check) ok = mid;
        else ng = mid;
    }
    chmin(ans, ok);

    if(ans >= 1e9) out(-1);
    else out(ans);
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    input();
    solve();
}
0