結果

問題 No.2251 Marking Grid
ユーザー kumakumakumakuma
提出日時 2023-03-13 19:31:30
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 948 ms / 3,000 ms
コード長 3,095 bytes
コンパイル時間 2,831 ms
コンパイル使用メモリ 223,808 KB
実行使用メモリ 65,944 KB
最終ジャッジ日時 2023-10-18 11:15:02
合計ジャッジ時間 11,152 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 1 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 1 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 2 ms
4,348 KB
testcase_22 AC 767 ms
57,400 KB
testcase_23 AC 942 ms
65,944 KB
testcase_24 AC 17 ms
6,132 KB
testcase_25 AC 948 ms
65,664 KB
testcase_26 AC 123 ms
17,580 KB
testcase_27 AC 462 ms
48,920 KB
testcase_28 AC 11 ms
4,820 KB
testcase_29 AC 525 ms
49,208 KB
testcase_30 AC 108 ms
16,444 KB
testcase_31 AC 67 ms
11,348 KB
testcase_32 AC 24 ms
6,356 KB
testcase_33 AC 579 ms
49,548 KB
testcase_34 AC 33 ms
9,860 KB
testcase_35 AC 243 ms
26,704 KB
testcase_36 AC 310 ms
29,344 KB
testcase_37 AC 443 ms
48,756 KB
testcase_38 AC 14 ms
5,040 KB
testcase_39 AC 9 ms
4,756 KB
testcase_40 AC 204 ms
26,520 KB
testcase_41 AC 71 ms
16,064 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
// #include <atcoder/modint>
#define rng(a) a.begin(),a.end()
#define rrng(a) a.rbegin(),a.rend()
#define INF 2000000000000000000
#define ll long long
#define ull unsigned long long
#define ld long double
#define pll pair<ll, ll>
using namespace std;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
const double PI = 3.141592653589793;
// ============union_find_tree===============
struct union_find {
  vector<ll> par;
  vector<ll> tree_rank;
  vector<ll> num;

  union_find(ll n)
  {
    par = vector<ll>(n);
    num = vector<ll>(n, 1);
    tree_rank = vector<ll>(n);
    for (ll i = 0; i < n; ++i)
    {
      par.at(i) = i;
      tree_rank.at(i) = 0;
    }
  }

  ll find(ll x)
  {
    if (par.at(x) == x)
    {
      return x;
    }
    else
    {
      return par.at(x) = find(par.at(x));
    }
  }

  ll get_num(ll x) {
    return num.at(find(x));
  }

  void unite(ll x, ll y)
  {
    x = find(x);
    y = find(y);
    if (x == y)
    {
      return;
    }
    if (tree_rank.at(x) < tree_rank.at(y))
    {
      par.at(x) = y;
      num.at(y) = num.at(x) + num.at(y);
    }
    else
    {
      par.at(y) = x;
      num.at(x) = num.at(x) + num.at(y);
      if (tree_rank.at(x) == tree_rank.at(y))
      {
        tree_rank.at(x)++;
      }
    }
  }

  bool same(ll x, ll y)
  {
    return find(x) == find(y);
  }
};
// =======================================


ll mod = 998244353;
//=============modinv============================
ll modinv(ll a, ll m = mod) {
    ll b = m, u = 1, v = 0;
    while (b) {
        ll t = a / b;
        a -= t * b; swap(a, b);
        u -= t * v; swap(u, v);
    }
    u %= m;
    if (u < 0) u += m;
    return u;
}
//=================================================

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll H, W;
  cin >> H >> W;
  vector<ll> power(H + W + 1, 1);
  for (ll i = 0; i < H + W; ++i) {
    power.at(i + 1) = power.at(i) * 2 % mod;
  }
  vector<vector<ll>> A(H, vector<ll>(W));
  vector<vector<ll>> num;
  for (ll i = 0; i < H; ++i) {
    for (ll j = 0; j < W; ++j) {
      cin >> A.at(i).at(j);
      num.push_back({A.at(i).at(j), i, j});
    }
  }
  sort(rrng(num));
  ll r_num = W, c_num = H;
  vector<bool> row(W, false);
  vector<bool> col(H, false);
  ll ans = 0;
  vector<vector<ll>> already(H);
  union_find uf(W);
  for (ll i = 0; i < num.size(); ++i) {
    vector<ll> n = num.at(i);
    ll h = n.at(1), w = n.at(2);
    if (already.at(h).size() != 0) {
      ll nw = already.at(h).at((ll)already.at(h).size() - 1);
      if (!uf.same(w, nw)) {
        r_num -= 1;
      }
      else {
        continue;
      }
      uf.unite(w, nw);
    }
    already.at(h).push_back(w);
    if (!col.at(h)) {
      col.at(h) = true;
      c_num -= 1;
    }
    ans += (n.at(0) * power.at(r_num) % mod) * power.at(c_num) % mod;
    ans %= mod;
  }
  ans *= modinv(2);
  ans %= mod;
  cout << ans << "\n";
}
//0011
//1100
//1100
//0011

//0101
//1010
//1010
//0101
0