Submission #318989

# Submission time Handle Problem Language Result Execution time Memory
318989 2020-11-03T16:17:35 Z fivefourthreeone Abduction 2 (JOI17_abduction2) C++17
100 / 100
1676 ms 142236 KB
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
#define owo(i,a, b) for(auto i=(a);i<(b); ++i)
#define uwu(i,a, b) for(auto i=(a)-1; i>=(b); --i)
#define senpai push_back
#define ttgl pair<int, int>
#define ayaya cout<<"ayaya~"<<endl
using namespace std;
/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ordered_set tree<ttgl, null_type,less<ttgl>, rb_tree_tag,tree_order_statistics_node_update>*/
 
using ll = long long;
using ld = long double;
const ll MOD = 1000000007;
const ll root = 62;
ll binpow(ll a,ll b){ll res=1;while(b){if(b&1)res=(res*a)%MOD;a=(a*a)%MOD;b>>=1;}return res;}
ll modInv(ll a){return binpow(a, MOD-2);}
const double PI = atan(1);
const double eps = 1e-10;
const int INF = 0x3f3f3f3f;
const int NINF = 0xc0c0c0c0;
const ll INFLL = 0x3f3f3f3f3f3f3f3f;
const ll NINFLL = 0xc0c0c0c0c0c0c0c0;
const int mxN = 50001;
map<array<int, 3>, ll> mp;
int H[mxN][2][20];
int W[mxN][2][20];
int h, w, q;
ll solve(int x, int y, int dir) {
    if(mp.find({x, y, dir})!=mp.end())return mp[{x, y, dir}];
    ll res = 0;
    if(dir==0) {
        int dxl = x-1;
        int dxr = x+1;
        int mx = W[y][0][0];
        uwu(lg, 20, 0) {
            //cout<<dxl<<" "<<dxr<<" x"<<endl;
            if(H[dxl][0][lg] <= mx) {
                dxl = max(0, dxl - (1<<lg));
            }
            if(H[dxr][1][lg] <= mx) {
                dxr = min(h+1, dxr + (1<<lg));
            }
        }
        //cout<<x<<" "<<y<<" "<<dir<<" "<<mx<<" "<<dxl<<" "<<dxr<<"\n";
        if(dxl == 0) res =  max(res, (ll)x - 1);
        else res = max(res, x - dxl + solve(dxl, y, 1));
        if(dxr == h+1) res = max(res, (ll)h - x);
        else res = max(res, dxr - x + solve(dxr, y, 1));
    }else {
        int dyl = y-1;
        int dyr = y+1;
        int mx = H[x][0][0];
        uwu(lg, 20, 0) {
            //cout<<dyl<<" "<<dyr<<" y"<<endl;
            if(W[dyl][0][lg] <= mx) {
                dyl = max(0, dyl - (1<<lg));
            }
            if(W[dyr][1][lg] <= mx) {
                dyr = min(w+1, dyr + (1<<lg));
            }
        }
        if(dyl == 0) res =  max(res, (ll)y - 1);
        else res = max(res, y - dyl + solve(x, dyl, 0));
        if(dyr == w+1) res = max(res, (ll)w - y);
        else res = max(res, dyr - y + solve(x, dyr, 0));
    }
    //cout<<x<<" "<<y<<" "<<dir<<" "<<res<<"\n";
    return mp[{x, y, dir}] = res;
}
int main() {
    //freopen("file.in", "r", stdin);
    //freopen("file.out", "w", stdout);
    mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
    cin.tie(0)->sync_with_stdio(0);
    cin>>h>>w>>q;
    owo(i, 1, h+1) {
        cin>>H[i][0][0];
        H[i][1][0] = H[i][0][0];
    }
    owo(i, 1, w+1) {
        cin>>W[i][0][0];
        W[i][1][0] = W[i][0][0];
    }
    owo(lg, 1, 20) {
        owo(dir, 0, 2) {
            int sn = (dir ? 1 : -1);
            owo(i, 1, h+1) {
                int nval = i + sn*(1<<(lg-1));
                nval = max(nval, 0);
                nval = min(nval, h+1);
                H[i][dir][lg] = max(H[i][dir][lg-1], H[nval][dir][lg-1]);
                //cout<<i<<" "<<dir<<" "<<lg<<" "<<H[i][dir][lg]<<" H\n";
            }
            owo(i, 1, w+1) {
                int nval = i + sn*(1<<(lg-1));
                nval = max(nval, 0);
                nval = min(nval, w+1);
                W[i][dir][lg] = max(W[i][dir][lg-1], W[nval][dir][lg-1]);
                //cout<<i<<" "<<dir<<" "<<lg<<" "<<W[i][dir][lg]<<" W\n";
            }
        }
    }
    int a, b;
    while(q--) {
        cin>>a>>b;
        cout<<(max(solve(a, b, 0), solve(a, b, 1)))<<"\n";
        //ayaya;
    }
    return 0;
}

Compilation message

abduction2.cpp:2: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
    2 | #pragma GCC optimization ("O3")
      | 
abduction2.cpp:3: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
    3 | #pragma GCC optimization ("unroll-loops")
      |
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 360 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 360 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 2 ms 1004 KB Output is correct
13 Correct 2 ms 1004 KB Output is correct
14 Correct 2 ms 1004 KB Output is correct
15 Correct 2 ms 1004 KB Output is correct
16 Correct 2 ms 1004 KB Output is correct
17 Correct 2 ms 1004 KB Output is correct
18 Correct 2 ms 1144 KB Output is correct
19 Correct 4 ms 1260 KB Output is correct
20 Correct 4 ms 1388 KB Output is correct
21 Correct 4 ms 1260 KB Output is correct
22 Correct 6 ms 1664 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 360 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 2 ms 1004 KB Output is correct
13 Correct 2 ms 1004 KB Output is correct
14 Correct 2 ms 1004 KB Output is correct
15 Correct 2 ms 1004 KB Output is correct
16 Correct 2 ms 1004 KB Output is correct
17 Correct 2 ms 1004 KB Output is correct
18 Correct 2 ms 1144 KB Output is correct
19 Correct 4 ms 1260 KB Output is correct
20 Correct 4 ms 1388 KB Output is correct
21 Correct 4 ms 1260 KB Output is correct
22 Correct 6 ms 1664 KB Output is correct
23 Correct 82 ms 16996 KB Output is correct
24 Correct 84 ms 16996 KB Output is correct
25 Correct 81 ms 17004 KB Output is correct
26 Correct 81 ms 17156 KB Output is correct
27 Correct 83 ms 16996 KB Output is correct
28 Correct 94 ms 22628 KB Output is correct
29 Correct 82 ms 17764 KB Output is correct
30 Correct 140 ms 23524 KB Output is correct
31 Correct 163 ms 25316 KB Output is correct
32 Correct 84 ms 17124 KB Output is correct
33 Correct 97 ms 19044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 1260 KB Output is correct
2 Correct 4 ms 1132 KB Output is correct
3 Correct 4 ms 1260 KB Output is correct
4 Correct 4 ms 1260 KB Output is correct
5 Correct 4 ms 1260 KB Output is correct
6 Correct 3 ms 1260 KB Output is correct
7 Correct 3 ms 1260 KB Output is correct
8 Correct 6 ms 1516 KB Output is correct
9 Correct 6 ms 1516 KB Output is correct
10 Correct 6 ms 1388 KB Output is correct
11 Correct 7 ms 1516 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 360 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 2 ms 1004 KB Output is correct
13 Correct 2 ms 1004 KB Output is correct
14 Correct 2 ms 1004 KB Output is correct
15 Correct 2 ms 1004 KB Output is correct
16 Correct 2 ms 1004 KB Output is correct
17 Correct 2 ms 1004 KB Output is correct
18 Correct 2 ms 1144 KB Output is correct
19 Correct 4 ms 1260 KB Output is correct
20 Correct 4 ms 1388 KB Output is correct
21 Correct 4 ms 1260 KB Output is correct
22 Correct 6 ms 1664 KB Output is correct
23 Correct 82 ms 16996 KB Output is correct
24 Correct 84 ms 16996 KB Output is correct
25 Correct 81 ms 17004 KB Output is correct
26 Correct 81 ms 17156 KB Output is correct
27 Correct 83 ms 16996 KB Output is correct
28 Correct 94 ms 22628 KB Output is correct
29 Correct 82 ms 17764 KB Output is correct
30 Correct 140 ms 23524 KB Output is correct
31 Correct 163 ms 25316 KB Output is correct
32 Correct 84 ms 17124 KB Output is correct
33 Correct 97 ms 19044 KB Output is correct
34 Correct 4 ms 1260 KB Output is correct
35 Correct 4 ms 1132 KB Output is correct
36 Correct 4 ms 1260 KB Output is correct
37 Correct 4 ms 1260 KB Output is correct
38 Correct 4 ms 1260 KB Output is correct
39 Correct 3 ms 1260 KB Output is correct
40 Correct 3 ms 1260 KB Output is correct
41 Correct 6 ms 1516 KB Output is correct
42 Correct 6 ms 1516 KB Output is correct
43 Correct 6 ms 1388 KB Output is correct
44 Correct 7 ms 1516 KB Output is correct
45 Correct 83 ms 17380 KB Output is correct
46 Correct 87 ms 17252 KB Output is correct
47 Correct 84 ms 17252 KB Output is correct
48 Correct 85 ms 17256 KB Output is correct
49 Correct 86 ms 17252 KB Output is correct
50 Correct 102 ms 21988 KB Output is correct
51 Correct 103 ms 22572 KB Output is correct
52 Correct 194 ms 27488 KB Output is correct
53 Correct 195 ms 26852 KB Output is correct
54 Correct 182 ms 26468 KB Output is correct
55 Correct 263 ms 31844 KB Output is correct
56 Correct 1676 ms 142236 KB Output is correct
57 Correct 468 ms 49380 KB Output is correct
58 Correct 446 ms 47716 KB Output is correct
59 Correct 455 ms 47588 KB Output is correct