제출 #73005

#제출 시각아이디문제언어결과실행 시간메모리
73005Benq유괴 2 (JOI17_abduction2)C++14
100 / 100
1466 ms193652 KiB
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")

#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/rope>

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
 
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;

typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;

template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define F0R(i, a) for (int i=0; i<(a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)

#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()

const int MOD = 1000000007;
const ll INF = 1e18;
const int MX = 50005;

template<class T, int SZ> struct RMQ {
    T stor[SZ][32-__builtin_clz(SZ)];
    
    T comb(T a, T b) {
        return max(a,b);
    }
    
    void build(vector<T>& x) {
        F0R(i,sz(x)) stor[i][0] = x[i];
        FOR(j,1,32-__builtin_clz(SZ)) F0R(i,SZ-(1<<(j-1))) 
            stor[i][j] = comb(stor[i][j-1],
                        stor[i+(1<<(j-1))][j-1]);
    }
    
    T query(int l, int r) {
        int x = 31-__builtin_clz(r-l+1);
        return comb(stor[l][x],stor[r-(1<<x)+1][x]);
    }
    
    int getLst(int pos, int val) {
        int lo = 0, hi = pos-1;
        while (lo < hi) {
            int mid = (lo+hi+1)/2;
            if (query(mid,pos-1) > val) lo = mid;
            else hi = mid-1;
        }
        return lo;
    }
    
    int getFst(int pos, int val) {
        int lo = pos+1, hi = SZ-1;
        while (lo < hi) {
            int mid = (lo+hi)/2;
            if (query(pos+1,mid) > val) hi = mid;
            else lo = mid+1;
        }
        return lo;
    }
};

namespace mapOp {
    const int tmp = chrono::high_resolution_clock::now().time_since_epoch().count();

    template<class T> struct hsh {
        size_t operator()(const T& x) const { 
            return hash<T>{}(x)^tmp; // avoid anti-hash tests?
        }
    };

    template<class a, class b> using um = gp_hash_table<a,b,hsh<a>>;

    template<class a, class b> b get(um<a,b>& u, a x) {
        if (u.find(x) == u.end()) return 0;
        return u[x];
    }
}

using namespace mapOp;

RMQ<int,MX> R[2];
um<int,ll> dp[2][MX];

int H,W,Q;
vi A,B;

ll get(int ind, int r, int c) {
    if (dp[ind][r].find(c) != dp[ind][r].end()) return dp[ind][r][c];
    ll res = 0;
    if (ind == 0) {
        int r0 = R[0].getLst(r,B[c]), r1 = R[0].getFst(r,B[c]);
        res = max(res,r0 >= 1?get(1,r0,c)+r-r0:r-1);
        res = max(res,r1 <= H?get(1,r1,c)+r1-r:H-r);
    } else {
        int c0 = R[1].getLst(c,A[r]), c1 = R[1].getFst(c,A[r]);
        res = max(res,c0 >= 1?get(0,r,c0)+c-c0:c-1);
        res = max(res,c1 <= W?get(0,r,c1)+c1-c:W-c);
    }
    return dp[ind][r][c] = res;
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin >> H >> W >> Q;
    A.resize(H+1), B.resize(W+1);
    FOR(i,1,H+1) cin >> A[i];
    FOR(i,1,W+1) cin >> B[i];
    R[0].build(A), R[1].build(B);
    F0R(i,Q) {
        int S,T; cin >> S >> T;
        cout << max(get(0,S,T),get(1,S,T)) << "\n";
    }
}

/* Look for:
* the exact constraints (multiple sets are too slow for n=10^6 :( ) 
* special cases (n=1?)
* overflow (ll vs int?)
* array bounds
* if you have no idea just guess the appropriate well-known algo instead of doing nothing :/
*/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...