結果

問題 No.1059 素敵な集合
ユーザー ei1333333ei1333333
提出日時 2020-05-22 22:17:33
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 78 ms / 2,000 ms
コード長 4,493 bytes
コンパイル時間 2,170 ms
コンパイル使用メモリ 207,616 KB
実行使用メモリ 9,388 KB
最終ジャッジ日時 2023-09-30 15:30:17
合計ジャッジ時間 3,620 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 40 ms
9,256 KB
testcase_02 AC 21 ms
5,356 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,500 KB
testcase_06 AC 17 ms
4,612 KB
testcase_07 AC 15 ms
4,552 KB
testcase_08 AC 24 ms
5,376 KB
testcase_09 AC 7 ms
4,380 KB
testcase_10 AC 26 ms
5,692 KB
testcase_11 AC 18 ms
4,600 KB
testcase_12 AC 10 ms
4,376 KB
testcase_13 AC 30 ms
6,388 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 60 ms
8,664 KB
testcase_16 AC 20 ms
5,004 KB
testcase_17 AC 19 ms
5,032 KB
testcase_18 AC 7 ms
6,920 KB
testcase_19 AC 46 ms
9,292 KB
testcase_20 AC 78 ms
9,388 KB
testcase_21 AC 67 ms
8,904 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
using int64 = long long;
const int mod = 1e9 + 7;
// const int mod = 998244353;

const int64 infll = (1LL << 62) - 1;
const int inf = (1 << 30) - 1;

struct IoSetup {
  IoSetup() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
    cerr << fixed << setprecision(10);
  }
} iosetup;


template< typename T1, typename T2 >
ostream &operator<<(ostream &os, const pair< T1, T2 > &p) {
  os << p.first << " " << p.second;
  return os;
}

template< typename T1, typename T2 >
istream &operator>>(istream &is, pair< T1, T2 > &p) {
  is >> p.first >> p.second;
  return is;
}

template< typename T >
ostream &operator<<(ostream &os, const vector< T > &v) {
  for(int i = 0; i < (int) v.size(); i++) {
    os << v[i] << (i + 1 != v.size() ? " " : "");
  }
  return os;
}

template< typename T >
istream &operator>>(istream &is, vector< T > &v) {
  for(T &in : v) is >> in;
  return is;
}

template< typename T1, typename T2 >
inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }

template< typename T1, typename T2 >
inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }

template< typename T = int64 >
vector< T > make_v(size_t a) {
  return vector< T >(a);
}

template< typename T, typename... Ts >
auto make_v(size_t a, Ts... ts) {
  return vector< decltype(make_v< T >(ts...)) >(a, make_v< T >(ts...));
}

template< typename T, typename V >
typename enable_if< is_class< T >::value == 0 >::type fill_v(T &t, const V &v) {
  t = v;
}

template< typename T, typename V >
typename enable_if< is_class< T >::value != 0 >::type fill_v(T &t, const V &v) {
  for(auto &e : t) fill_v(e, v);
}

template< typename F >
struct FixPoint : F {
  FixPoint(F &&f) : F(forward< F >(f)) {}

  template< typename... Args >
  decltype(auto) operator()(Args &&... args) const {
    return F::operator()(*this, forward< Args >(args)...);
  }
};

template< typename F >
inline decltype(auto) MFP(F &&f) {
  return FixPoint< F >{forward< F >(f)};
}

struct UnionFind {
  vector< int > data;

  UnionFind(int sz) {
    data.assign(sz, -1);
  }

  bool unite(int x, int y) {
    x = find(x), y = find(y);
    if(x == y) return (false);
    if(data[x] > data[y]) swap(x, y);
    data[x] += data[y];
    data[y] = x;
    return (true);
  }

  int find(int k) {
    if(data[k] < 0) return (k);
    return (data[k] = find(data[k]));
  }

  int size(int k) {
    return (-data[find(k)]);
  }
};

template< typename T, typename F >
T boruvka(int N, F f) {
  vector< int > rev(N), belong(N);
  UnionFind uf(N);
  T ret = T();
  while(uf.size(0) != N) {
    int ptr = 0;
    for(int i = 0; i < N; i++) {
      if(uf.find(i) == i) {
        belong[i] = ptr++;
        rev[belong[i]] = i;
      }
    }
    for(int i = 0; i < N; i++) {
      belong[i] = belong[uf.find(i)];
    }
    auto v = f(ptr, belong);
    bool update = false;
    for(int i = 0; i < ptr; i++) {
      if(~v[i].second && uf.unite(rev[i], rev[v[i].second])) {
        ret += v[i].first;
        update = true;
      }
    }
    if(!update) return -1; // notice!!
  }
  return ret;
}


int main() {
  int L, R;
  cin >> L >> R;
  auto f = [&](int sz, vector< int > belong) {
    vector< pair< int, int > > ret(sz, {inf, -1});
    vector< pair< int, int > > near(R + 2, make_pair(inf, inf));
    for(int i = R; i >= L; i--) {
      near[i] = near[i + 1];
      if(near[i].first == inf) {
        near[i].first = i;
      } else if(near[i].second == inf) {
        near[i].second = i;
      } else if(belong[near[i].first - L] == belong[i - L]) {
        near[i].first = i;
      } else {
        near[i].second = i;
      }
      if(near[i].first > near[i].second) swap(near[i].first, near[i].second);
    }
    for(int i = L; i <= R; i++) {
      for(int j = 0; j <= R; j += i) {
        if(near[j].first != inf && belong[near[j].first - L] != belong[i - L]) {
          chmin(ret[belong[i - L]], make_pair(near[j].first - j, belong[near[j].first - L]));
          chmin(ret[belong[near[j].first - L]], make_pair(near[j].first - j, belong[i - L]));
        }
        if(near[j].second != inf && belong[near[j].second - L] != belong[i - L]) {
          chmin(ret[belong[i - L]], make_pair(near[j].second - j, belong[near[j].second - L]));
          chmin(ret[belong[near[j].second - L]], make_pair(near[j].second - j, belong[i - L]));
        }
      }
    }
    return ret;
  };
  cout << boruvka< int, decltype(f) >(R - L + 1, f) << endl;
}

0