Submission #63043709


Source Code Expand

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

#pragma GCC target ("avx2")
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")

#define f first
#define s second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) ((int) (x).size())
#define pb push_back
#define mp make_pair
#define int long long

using namespace std;
using namespace __gnu_pbds;

template <typename T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename T> inline bool umin(T &a, const T &b) { if(a > b) { a = b; return 1; } return 0; }
template <typename T> inline bool umax(T &a, const T &b) { if(a < b) { a = b; return 1; } return 0; }

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const ll mod = 998244353;
const ll base = 1e6 + 9;
const ll inf = 1e18;
const int MAX = 5e5 + 42;
const int LG = 20;

random_device rd;
mt19937 gen(rd());
uniform_int_distribution<ll> dis(1, inf);

int ans[MAX];
vector<pii> have[MAX];

struct DSU {
    int n;
    vector<int> p, siz;
    vector<set<int>> comp;

    DSU(int N) {
        n = N - 1;
        p = vector<int>(n + 1);
        siz = vector<int>(n + 1);
        comp = vector<set<int>>(n + 1);
    };

    void create(int x) {
        p[x] = x;
        siz[x] = 1;
        comp[x] = {x};
    }

    void init() {
        for(int v = 1; v <= n; v++) create(v);
    }

    int get(int x) {
        return p[x] = (p[x] == x? x : get(p[x]));
    }

    bool unite(int x, int y, int w) {
        x = get(x), y = get(y);
        if(x == y) return 0;
        if(siz[x] < siz[y]) swap(x, y);
        siz[x] += siz[y];
        p[y] = x;
        for(auto v : comp[y]) {
            for(auto [u, i] : have[v]) {
                if(comp[x].count(u)) {
                    ans[i] = w;
                }
            }
        }
        for(auto v : comp[y]) comp[x].insert(v);
        comp[y].clear();
        return 1;
    }
};

void solve() {
    int n, m;
    cin >> n >> m;
    vector<vector<int>> a(n, vector<int>(m));
    for(auto &i : a) {
        for(auto &j : i) {
            cin >> j;
        }
    }
    vector<vector<pii>> val(1000010);
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < m; j++) {
            val[a[i][j]].pb({i, j});
        }
    }

    auto idx = [&](int i, int j) {
        return i * m + j + 1;
    };

    int q;
    cin >> q;
    vector<array<int, 6>> qr(q);
    int IDX = 0;
    for(auto &[a, b, y, c, d, z] : qr) {
        cin >> a >> b >> y >> c >> d >> z;
        a--, b--, c--, d--;
        int u = idx(a, b);
        int v = idx(c, d);
        have[u].pb({v, IDX});
        have[v].pb({u, IDX});
        if(a == c && b == d) {
            ans[IDX] = inf;
        }
        IDX++;
    }

    DSU dsu(n * m + 1);
    dsu.init();
    vector<int> dx = {1, -1, 0, 0};
    vector<int> dy = {0, 0, 1, -1};
    for(int x = val.size() - 1; ~x; x--) {
        for(auto [i, j] : val[x]) {
            for(int k = 0; k < 4; k++) {
                int ni = i + dx[k];
                int nj = j + dy[k];
                if(ni < 0 || nj < 0 || ni == n || nj == m || a[ni][nj] < a[i][j]) continue;
                int u = idx(i, j);
                int v = idx(ni, nj);
                dsu.unite(u, v, x);
            }
        }
    }

    for(int i = 0; i < q; i++) {
        int x = qr[i][2];
        int y = qr[i][5];
        int z = ans[i];
        if(min(x, y) <= z) {
            cout << abs(x - y) << '\n';
        }
        else {
            cout << x + y - 2 * z << '\n';
        }
    }
}

signed main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    int ttt = 1;
//    cin >> ttt;
    while(ttt--) {
        solve();
    }
}

Submission Info

Submission Time
Task G - Dense Buildings
User nife
Language C++ 20 (gcc 12.2)
Score 600
Code Size 4001 Byte
Status AC
Exec Time 601 ms
Memory 92016 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 600 / 600
Status
AC × 1
AC × 38
Set Name Test Cases
Sample example_00.txt
All example_00.txt, hand_00.txt, hand_01.txt, hand_02.txt, hand_03.txt, hand_04.txt, hand_05.txt, hand_06.txt, random_00.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt, random_25.txt, random_26.txt, random_27.txt, random_28.txt, random_29.txt
Case Name Status Exec Time Memory
example_00.txt AC 13 ms 26504 KiB
hand_00.txt AC 153 ms 76940 KiB
hand_01.txt AC 181 ms 78048 KiB
hand_02.txt AC 426 ms 89108 KiB
hand_03.txt AC 290 ms 87052 KiB
hand_04.txt AC 194 ms 77284 KiB
hand_05.txt AC 66 ms 45380 KiB
hand_06.txt AC 14 ms 26500 KiB
random_00.txt AC 519 ms 90152 KiB
random_01.txt AC 531 ms 89924 KiB
random_02.txt AC 533 ms 92016 KiB
random_03.txt AC 530 ms 90308 KiB
random_04.txt AC 524 ms 90580 KiB
random_05.txt AC 359 ms 80164 KiB
random_06.txt AC 362 ms 80772 KiB
random_07.txt AC 348 ms 79708 KiB
random_08.txt AC 367 ms 81548 KiB
random_09.txt AC 363 ms 81760 KiB
random_10.txt AC 418 ms 87520 KiB
random_11.txt AC 417 ms 88480 KiB
random_12.txt AC 421 ms 87456 KiB
random_13.txt AC 514 ms 90532 KiB
random_14.txt AC 423 ms 88180 KiB
random_15.txt AC 462 ms 87876 KiB
random_16.txt AC 503 ms 90120 KiB
random_17.txt AC 511 ms 89516 KiB
random_18.txt AC 530 ms 89344 KiB
random_19.txt AC 475 ms 89088 KiB
random_20.txt AC 572 ms 89420 KiB
random_21.txt AC 572 ms 88732 KiB
random_22.txt AC 572 ms 89008 KiB
random_23.txt AC 601 ms 89392 KiB
random_24.txt AC 596 ms 90232 KiB
random_25.txt AC 594 ms 89260 KiB
random_26.txt AC 563 ms 89180 KiB
random_27.txt AC 598 ms 89664 KiB
random_28.txt AC 577 ms 89376 KiB
random_29.txt AC 575 ms 89160 KiB