結果

問題 No.3601 Queen Dist Sum with One Wall
コンテスト
ユーザー trqs
提出日時 2026-07-24 21:47:57
言語 C++17
(gcc 15.2.0 + boost 1.90.0)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 17 ms / 2,000 ms
+ 47µs
コード長 4,762 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,160 ms
コンパイル使用メモリ 214,004 KB
実行使用メモリ 5,888 KB
最終ジャッジ日時 2026-07-24 21:48:03
合計ジャッジ時間 4,517 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 22
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll mod = 1e9 + 7;
const int N = 200005;
const int INF = 0x3f3f3f3f;
namespace FastIO {
const int SIZ = 1 << 20;
char ibuf[SIZ], *iS, *iT;
char obuf[SIZ], *oS = obuf, *oT = obuf + SIZ - 1;
inline char getch() {
    return (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZ, stdin), (iS == iT ? EOF : *iS++)) : *iS++);
}
template <typename T>
inline void read(T& x) {
    char c = getch();
    x = 0;
    bool f = 0;
    while (!isdigit(c)) {
        if (c == '-') f = 1;
        c = getch();
    }
    while (isdigit(c)) {
        x = (x << 1) + (x << 3) + (c ^ 48);
        c = getch();
    }
    if (f) x = -x;
}
inline void read(char& c) {
    while ((c = getch()) <= 32);
}
inline void read(char* s) {
    char c = getch();
    while (c <= 32) c = getch();
    while (c > 32) {
        *s++ = c;
        c = getch();
    }
    *s = '\0';
}
inline void read(string& s) {
    s.clear();
    char c = getch();
    while (c <= 32) c = getch();
    while (c > 32) {
        s += c;
        c = getch();
    }
}
inline void flush() {
    fwrite(obuf, 1, oS - obuf, stdout);
    oS = obuf;
}
inline void putch(char x) {
    *oS++ = x;
    if (oS == oT) flush();
}
template <typename T>
inline void write(T x) {
    if (x < 0) {
        putch('-');
        x = -x;
    }
    if (x > 9) write(x / 10);
    putch(x % 10 + 48);
}
inline void write(const char* s) {
    while (*s) putch(*s++);
}
inline void write(const string& s) {
    for (char c : s) putch(c);
}
struct IO {
    ~IO() {
        flush();
    }
    template <typename T>
    IO& operator>>(T& x) {
        read(x);
        return *this;
    }
    template <typename T>
    IO& operator<<(const T& x) {
        write(x);
        return *this;
    }
} io;
}  // namespace FastIO
using namespace FastIO;
using i128 = __int128;
using u128 = __uint128_t;
using ull = unsigned long long;
template <typename T>
constexpr std::make_unsigned_t<T> gcd(T a, T b) {
    using U = std::make_unsigned_t<T>;
    U m = a < 0 ? U(-a) : U(a);
    U n = b < 0 ? U(-b) : U(b);
    if (m == 0) return n;
    if (n == 0) return m;
    int i = __builtin_ctz(m);
    m >>= i;
    int j = __builtin_ctz(n);
    n >>= j;
    int k = min(i, j);
    while (true) {
        if (m > n) swap(m, n);
        n -= m;
        if (n == 0) return m << k;
        n >>= __builtin_ctz(n);
    }
}
inline int lg(int __n) {
    return sizeof(int) * __CHAR_BIT__ - 1 - __builtin_clz(__n);
}
ll powmod(ll x, ll y, ll m = mod) {
    ll res = 1;
    while (y) {
        if (y & 1) res = res * x % m;
        y >>= 1;
        x = x * x % m;
    }
    return res;
}
ll calc(ll x, ll y) {
    if (x < 1) return 0;
    return (min(x, y) + 1) / 2 + min(x, 2 * y) / 2;
}
void solve() {
    ll n, m, h, w, x, y;
    io >> n >> m >> h >> w >> x >> y;
    ll c1 = (m - 1) + (n - 1) + min(h - 1, w - 1) + min(n - h, m - w) + min(n - h, w - 1) + min(h - 1, m - w);
    ll c2 = n * m - c1 - 1;
    if (x == h) {
        if (y > w) {
            ll l1 = y - w + 1, l2 = m - w;
            if (m > y) {
                io << c1 + 2 * c2 + 2 * (m - y) - calc(l2, max(n - h, h - 1)) + calc(l1 - 1, max(n - h, h - 1)) - 1 << "\n";
            } else {
                io << c1 + 2 * c2 - 1 << "\n";
            }
        } else {
            ll l1 = w - y + 1, l2 = w - 1;
            if (y > 1) {
                io << c1 + 2 * c2 + 2 * (y - 1) - calc(l2, max(n - h, h - 1)) + calc(l1 - 1, max(n - h, h - 1)) - 1 << "\n";
            } else {
                io << c1 + 2 * c2 - 1 << "\n";
            }
        }
    } else if (y == w) {
        if (x > h) {
            ll l1 = x - h + 1, l2 = n - h;
            if (n > x) {
                io << c1 + 2 * c2 + 2 * (n - x) - calc(l2, max(m - w, w - 1)) + calc(l1 - 1, max(m - w, w - 1)) - 1 << "\n";
            } else {
                io << c1 + 2 * c2 - 1 << "\n";
            }
        } else {
            ll l1 = h - x + 1, l2 = h - 1;
            if (x > 1) {
                io << c1 + 2 * c2 + 2 * (x - 1) - calc(l2, max(m - w, w - 1)) + calc(l1 - 1, max(m - w, w - 1)) - 1 << "\n";
            } else {
                io << c1 + 2 * c2 - 1 << "\n";
            }
        }
    } else if (x - h == y - w) {
        if (x > h) {
            io << c1 + 2 * c2 + min(n - x, m - y) - 1 << "\n";
        } else {
            io << c1 + 2 * c2 + min(x - 1, y - 1) - 1 << "\n";
        }
    } else if (x - h == w - y) {
        if (x > h) {
            io << c1 + 2 * c2 + min(n - x, y - 1) - 1 << "\n";
        } else {
            io << c1 + 2 * c2 + min(x - 1, m - y) - 1 << "\n";
        }
    } else {
        io << c1 + 2 * c2 - 2 << "\n";
    }
}
int main() {
    int _;
    io >> _;
    while (_--) solve();
    return 0;
}
0