Submission #63068220


Source Code Expand

#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <cstring>
#include <queue>
#include <stack>
#include <cstdlib>
#include <cassert>
using namespace std;

int root[250000];
int compute_root(int a)
{
    return a == root[a] ? a : root[a] = compute_root(root[a]);
}
int unite(int a, int b)
{
    a = compute_root(a);
    b = compute_root(b);
    if (a == b)
        return 0;
    if (rand() % 2)
    {
        swap(a, b);
    }
    root[a] = b;
    return 1;
}

int h, w;
int C(int r, int c)
{
    return r * w + c;
}

vector<tuple<int, int, int>> es;
int F[500][500];

vector<pair<int, int>> g[250000];
int parent[18][250000];
int cost[18][250000];
int depth[250000];
void dfs(int r, int p, int pc,int d)
{
    parent[0][r] = p;
    cost[0][r] = pc;
    depth[r] = d;
    for (auto [i, w] : g[r])
    {
        if (i == p)
        {
            continue;
        }
        dfs(i, r, w, d+1);
    }
}

int query(int a,int b){
    assert(a != b);

    if( depth[a] > depth[b])  swap(a, b);
    int d = depth[b] - depth[a];

    int cst = 1145141919;
    for(int i = 0 ; i < 18 ; i++){
        if( d >> i & 1 ){
            cst = min(cst, cost[i][b]);
            b = parent[i][b];
        }
    }

    for(int i = 17 ; i >= 0 ; i--){
        if( parent[i][a] != parent[i][b]){
            cst = min(cst, cost[i][a]);
            cst = min(cst, cost[i][b]);
            a = parent[i][a];
            b = parent[i][b];
        }
    }
    if( a == b ){
        return cst;
    }
    cst = min(cst, cost[0][a]);
    cst = min(cst, cost[0][b]);
    a = parent[0][a];
    b = parent[0][b];
    assert(a==b);
    return cst;
}

int main()
{

    cin >> h >> w;
    int n = h * w;
    for (int i = 0; i < n; i++)
        root[i] = i;
    for (int i = 0; i < h; i++)
    {
        for (int j = 0; j < w; j++)
        {
            cin >> F[i][j];
        }
    }
    for (int i = 0; i + 1 < h; i++)
    {
        for (int j = 0; j < w; j++)
        {
            int c = min(F[i][j], F[i + 1][j]);
            es.push_back({c, C(i, j), C(i + 1, j)});
        }
    }
    for (int i = 0; i < h; i++)
    {
        for (int j = 0; j + 1 < w; j++)
        {
            int c = min(F[i][j], F[i][j + 1]);
            es.push_back({c, C(i, j), C(i, j + 1)});
        }
    }
    sort(es.rbegin(), es.rend());
    for (auto [we, i, j] : es)
    {
        if (unite(i, j))
        {
            g[i].push_back({j, we});
            g[j].push_back({i, we});
            
        }
    }
    memset(parent, -1, sizeof(parent));
    dfs(0, -1, 0, 0);
    for (int b = 0; b + 1 < 18; b++)
    {
        for (int i = 0; i < n; i++)
        {
            if (parent[b][i] != -1)
            {
                int np = parent[b][parent[b][i]];
                parent[b + 1][i] = np;
                cost[b + 1][i] = min(cost[b][parent[b][i]], cost[b][i]);
            }
        }
    }
    int Q;
    cin >> Q;
    for(int i = 0 ; i < Q ; i++){
        int a, b, y, c, d, z;
        cin >> a >> b >> y >> c >> d >> z;
        --a, --b;
        --c, --d;
        int A = C(a, b);
        int B = C(c, d);
        if( A == B ){
            cout << abs(y-z) << endl;
        }else{
            int m = query(A, B);
            if( m >= y && m >= z ){
                cout << abs(y-z) << endl;
            }else{
                cout << abs(y - m) + abs(z - m) << endl;
            }
            
        }
        
    }

}

Submission Info

Submission Time
Task G - Dense Buildings
User kyuridenamida
Language C++ 20 (gcc 12.2)
Score 600
Code Size 3612 Byte
Status AC
Exec Time 633 ms
Memory 67588 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 8 ms 21060 KiB
hand_00.txt AC 485 ms 54912 KiB
hand_01.txt AC 505 ms 55072 KiB
hand_02.txt AC 596 ms 67588 KiB
hand_03.txt AC 633 ms 54852 KiB
hand_04.txt AC 339 ms 54872 KiB
hand_05.txt AC 330 ms 21092 KiB
hand_06.txt AC 8 ms 21056 KiB
random_00.txt AC 611 ms 55968 KiB
random_01.txt AC 617 ms 56728 KiB
random_02.txt AC 629 ms 56848 KiB
random_03.txt AC 617 ms 56252 KiB
random_04.txt AC 623 ms 56548 KiB
random_05.txt AC 578 ms 56652 KiB
random_06.txt AC 571 ms 56748 KiB
random_07.txt AC 570 ms 56428 KiB
random_08.txt AC 573 ms 56212 KiB
random_09.txt AC 575 ms 56756 KiB
random_10.txt AC 608 ms 56268 KiB
random_11.txt AC 607 ms 56012 KiB
random_12.txt AC 605 ms 55668 KiB
random_13.txt AC 612 ms 56780 KiB
random_14.txt AC 609 ms 56524 KiB
random_15.txt AC 610 ms 56500 KiB
random_16.txt AC 620 ms 56552 KiB
random_17.txt AC 613 ms 56100 KiB
random_18.txt AC 618 ms 56756 KiB
random_19.txt AC 617 ms 56448 KiB
random_20.txt AC 617 ms 55920 KiB
random_21.txt AC 618 ms 56552 KiB
random_22.txt AC 617 ms 56744 KiB
random_23.txt AC 618 ms 56820 KiB
random_24.txt AC 617 ms 56348 KiB
random_25.txt AC 615 ms 56152 KiB
random_26.txt AC 626 ms 56868 KiB
random_27.txt AC 617 ms 56012 KiB
random_28.txt AC 630 ms 56740 KiB
random_29.txt AC 620 ms 56684 KiB