Submission #63220047
Source Code Expand
// 期待外れでいたいだなんて
// いつから願ってしまった?
// 名も知れぬほうがいいなんて
// いつからか願ってしまった
// ココロもネジ巻出して
// 意味を失ってしまった
// 何ひとつも動かせない今日と
// 押しつぶすように広がる
// 澄んだ機械仕掛けの空
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
// Pragmas
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// Namespaces
using namespace std;
using namespace __gnu_pbds;
// Data types
using si = short int;
using ll = long long;
using ull = unsigned long long;
using lll = __int128;
using ld = long double;
// Pairs
using pii = pair<int, int>;
using psi = pair<si, si>;
using pll = pair<ll, ll>;
using plll = pair<lll, lll>;
using pld = pair<ld, ld>;
#define fi first
#define se second
// PBDS
template<typename Z>
using ordered_set = tree<Z, null_type, less<Z>, rb_tree_tag, tree_order_statistics_node_update>;
// Various outputs and debug
template<typename Z, typename = void> struct is_iterable : false_type {};
template<typename Z> struct is_iterable<Z, void_t<decltype(begin(declval<Z>())),decltype(end(declval<Z>()))>> : true_type {};
template<typename Z> typename enable_if<is_iterable<Z>::value&&!is_same<Z, string>::value,ostream&>::type operator<<(ostream &os, const Z &v);
template<typename Y, typename Z> ostream& operator<<(ostream &os, const pair<Y, Z> &p) {
return os << "(" << p.fi << ", " << p.se << ")";
}
template<class TupType, size_t... I> void printTuple(ostream& os, const TupType& _tup, index_sequence<I...>) {
os << "(";
(..., (os << (I == 0? "" : ", ") << get<I>(_tup)));
os << ")";
}
template<class... Z> ostream& operator<<(ostream& os, const tuple<Z...>& _tup) {
printTuple(os, _tup, make_index_sequence<sizeof...(Z)>());
return os;
}
template<typename Z> typename enable_if<is_iterable<Z>::value&&!is_same<Z, string>::value,ostream&>::type operator<<(ostream &os, const Z &v) {
os << "[";
for (auto it = v.begin(); it != v.end();) { os << *it; if (++it != v.end()) os << ", "; }
return os << "]";
}
#define debug(...) logger(cout, #__VA_ARGS__, __VA_ARGS__)
#define debugV(v, x) vlogger(cout, #v, x, v[x]);
#define rrebug(...) logger(cerr, #__VA_ARGS__, __VA_ARGS__)
#define rrebugV(v, x) vlogger(cerr, #v, x, v[x]);
template <typename... Args>
void logger(ostream& os, string vars, Args &&...values) {
os << vars << " = "; string delim = "";
(..., (os << delim << values, delim = ", "));
os << "\n";
}
template<class Y, class Z>
void vlogger(ostream& os, string var, Y idx, Z val) {
os << var << "[" << idx << "] = " << val << "\n";
}
// Various macros
#define All(x) x.begin(), x.end()
#define Sort(x) sort(All(x))
#define Reverse(x) reverse(All(x))
#define Uniqueify(x) Sort(x); x.erase(unique(All(x)), x.end())
#define RandomSeed chrono::steady_clock::now().time_since_epoch().count()
#define MultipleTestcases int _tc; cin >> _tc; for (int _cur_tc = 1; _cur_tc <= _tc; _cur_tc++)
// Chmin & chmax
template<typename Z> bool chmin(Z &a, Z b) { return (b < a) ? a = b, true : false; }
template<typename Z> bool chmax(Z &a, Z b) { return (b > a) ? a = b, true : false; }
// Modular arithmetic
template<int MOD>
class ModInt {
public:
int v;
ModInt() : v(0) {}
ModInt(long long _v) {
v = int((-MOD < _v && _v < MOD) ? (_v) : (_v % MOD));
if (v < 0) v += MOD;
}
friend bool operator==(const ModInt &a, const ModInt &b) { return a.v == b.v; }
friend bool operator!=(const ModInt &a, const ModInt &b) { return a.v != b.v; }
friend bool operator< (const ModInt &a, const ModInt &b) { return a.v < b.v; }
friend bool operator<=(const ModInt &a, const ModInt &b) { return a.v <= b.v; }
friend bool operator> (const ModInt &a, const ModInt &b) { return a.v > b.v; }
friend bool operator>=(const ModInt &a, const ModInt &b) { return a.v >= b.v; }
ModInt &operator+=(const ModInt &a) { v = (long long)(v + a.v) % MOD; return *this; }
ModInt &operator-=(const ModInt &a) { v = (long long)(v - a.v + MOD) % MOD; return *this; }
ModInt &operator*=(const ModInt &a) { v = 1ll * v * a.v % MOD; return *this; }
ModInt &operator/=(const ModInt &a) { return (*this) *= inverse(a); }
friend ModInt pow(ModInt a, long long x) {
ModInt res = 1;
for (; x; x /= 2, a *= a) if (x & 1) res *= a;
return res;
}
friend ModInt inverse(ModInt a) { return pow(a, MOD - 2); }
ModInt operator+ () const { return ModInt( v); }
ModInt operator- () const { return ModInt(-v); }
ModInt operator++() const { return *this += 1; }
ModInt operator--() const { return *this -= 1; }
friend ModInt operator+(ModInt a, const ModInt &b) { return a += b; }
friend ModInt operator-(ModInt a, const ModInt &b) { return a -= b; }
friend ModInt operator*(ModInt a, const ModInt &b) { return a *= b; }
friend ModInt operator/(ModInt a, const ModInt &b) { return a /= b; }
friend istream &operator>>(istream &is, ModInt &v) { return is >> v.v; }
friend ostream &operator<<(ostream &os, const ModInt &v) { return os << v.v; }
};
const int ModA = 998244353;
const int ModC = 1e9 + 7;
using MintA = ModInt<ModA>;
using MintC = ModInt<ModC>;
// Other constants
const ll INF = 1e18;
const ll iINF = 1e9;
const ld EPS = 1e-9;
const ld iEPS = 1e-6;
struct DSU {
int n, q;
vector<int> par;
vector<set<int>> queries;
vector<int> link_time;
DSU(int n) : n(n), q(0), par(n), queries(n) {
iota(par.begin(), par.end(), 0);
}
void addQuery(int u, int v) {
if (u == v) {
link_time.push_back(iINF);
} else {
link_time.push_back(-1);
queries[u].insert(q);
queries[v].insert(q);
}
q++;
}
int rep(int x) {
return ((x == par[x]) ? (x) : (par[x] = rep(par[x])));
}
bool check(int x, int y) {
return (rep(x) == rep(y));
}
void join(int x, int y, int t) {
int a = rep(x), b = rep(y);
if (a == b) return;
// debug(a, b, t);
if (queries[a].size() < queries[b].size()) swap(a, b);
par[b] = a;
for (auto q : queries[b]) {
if (queries[a].count(q)) {
link_time[q] = t;
// debug(q, t);
queries[a].erase(q);
} else {
queries[a].insert(q);
}
}
queries[b].clear();
}
};
const int maxN = 523;
const int maxQ = 200'023;
const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, 1, -1};
int h, w;
vector<pair<int, pii>> by_f;
int q;
int node_u[maxQ], node_v[maxQ], h_u[maxQ], h_v[maxQ];
bool active[maxN][maxN];
int convert(int u, int v) { return u*w + v; }
pii convert(int x) { return {x/w, x%w}; }
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
cin >> h >> w;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
static int f;
cin >> f;
by_f.push_back({f, {i, j}});
}
}
cin >> q;
for (int a, b, y, c, d, z, i = 0; i < q; i++) {
cin >> a >> b >> y >> c >> d >> z;
a--, b--, c--, d--;
node_u[i] = convert(a, b);
h_u[i] = y;
node_v[i] = convert(c, d);
h_v[i] = z;
}
DSU dsu(h*w);
for (int i = 0; i < q; i++) dsu.addQuery(node_u[i], node_v[i]);
sort(All(by_f), greater<pair<int, pii>>());
for (auto [f, p] : by_f) {
auto [r, c] = p;
active[r][c] = true;
for (int k = 0; k < 4; k++) {
int nr = r + dx[k], nc = c + dy[k];
if (nr >= 0 && nr < h && nc >= 0 && nc < w && active[nr][nc]) {
dsu.join(convert(r, c), convert(nr, nc), f);
}
}
}
vector<int> ans(q);
for (int i = 0; i < q; i++) {
int t = dsu.link_time[i];
// debug(i, t);
if (t >= min(h_u[i], h_v[i])) ans[i] = abs(h_u[i] - h_v[i]);
else {
ans[i] = h_u[i] + h_v[i] - 2*t;
}
}
for (int i = 0; i < q; i++) cout << ans[i] << "\n";
}
// dibisakan
Submission Info
Submission Time |
|
Task |
G - Dense Buildings |
User |
Zanite |
Language |
C++ 20 (gcc 12.2) |
Score |
600 |
Code Size |
8612 Byte |
Status |
AC |
Exec Time |
582 ms |
Memory |
41900 KiB |
Judge Result
Set Name |
Sample |
All |
Score / Max Score |
0 / 0 |
600 / 600 |
Status |
|
|
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 |
4 ms |
3408 KiB |
hand_00.txt |
AC |
165 ms |
41692 KiB |
hand_01.txt |
AC |
338 ms |
41720 KiB |
hand_02.txt |
AC |
469 ms |
41788 KiB |
hand_03.txt |
AC |
314 ms |
41648 KiB |
hand_04.txt |
AC |
153 ms |
31072 KiB |
hand_05.txt |
AC |
48 ms |
7956 KiB |
hand_06.txt |
AC |
1 ms |
3400 KiB |
random_00.txt |
AC |
479 ms |
41488 KiB |
random_01.txt |
AC |
484 ms |
41512 KiB |
random_02.txt |
AC |
494 ms |
41808 KiB |
random_03.txt |
AC |
481 ms |
41732 KiB |
random_04.txt |
AC |
486 ms |
41468 KiB |
random_05.txt |
AC |
365 ms |
41484 KiB |
random_06.txt |
AC |
379 ms |
41444 KiB |
random_07.txt |
AC |
362 ms |
41748 KiB |
random_08.txt |
AC |
368 ms |
41496 KiB |
random_09.txt |
AC |
373 ms |
41436 KiB |
random_10.txt |
AC |
504 ms |
41632 KiB |
random_11.txt |
AC |
503 ms |
41248 KiB |
random_12.txt |
AC |
540 ms |
41444 KiB |
random_13.txt |
AC |
494 ms |
41900 KiB |
random_14.txt |
AC |
518 ms |
41612 KiB |
random_15.txt |
AC |
561 ms |
41512 KiB |
random_16.txt |
AC |
493 ms |
41568 KiB |
random_17.txt |
AC |
486 ms |
41716 KiB |
random_18.txt |
AC |
491 ms |
41804 KiB |
random_19.txt |
AC |
554 ms |
41892 KiB |
random_20.txt |
AC |
509 ms |
41504 KiB |
random_21.txt |
AC |
582 ms |
41524 KiB |
random_22.txt |
AC |
574 ms |
41696 KiB |
random_23.txt |
AC |
536 ms |
41356 KiB |
random_24.txt |
AC |
513 ms |
41204 KiB |
random_25.txt |
AC |
537 ms |
41212 KiB |
random_26.txt |
AC |
563 ms |
41656 KiB |
random_27.txt |
AC |
518 ms |
40936 KiB |
random_28.txt |
AC |
569 ms |
41312 KiB |
random_29.txt |
AC |
564 ms |
41076 KiB |