結果

問題 No.880 Yet Another Segment Tree Problem
ユーザー ei1333333ei1333333
提出日時 2019-09-17 23:18:08
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 620 ms / 5,000 ms
コード長 6,690 bytes
コンパイル時間 2,753 ms
コンパイル使用メモリ 215,516 KB
実行使用メモリ 15,568 KB
最終ジャッジ日時 2023-10-24 02:42:41
合計ジャッジ時間 15,004 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,372 KB
testcase_01 AC 3 ms
4,372 KB
testcase_02 AC 4 ms
4,372 KB
testcase_03 AC 4 ms
4,372 KB
testcase_04 AC 3 ms
4,372 KB
testcase_05 AC 3 ms
4,372 KB
testcase_06 AC 3 ms
4,372 KB
testcase_07 AC 3 ms
4,372 KB
testcase_08 AC 4 ms
4,372 KB
testcase_09 AC 4 ms
4,372 KB
testcase_10 AC 3 ms
4,372 KB
testcase_11 AC 546 ms
15,568 KB
testcase_12 AC 522 ms
15,568 KB
testcase_13 AC 370 ms
15,568 KB
testcase_14 AC 524 ms
15,568 KB
testcase_15 AC 562 ms
15,568 KB
testcase_16 AC 596 ms
15,568 KB
testcase_17 AC 620 ms
15,568 KB
testcase_18 AC 619 ms
15,568 KB
testcase_19 AC 262 ms
15,568 KB
testcase_20 AC 274 ms
15,568 KB
testcase_21 AC 277 ms
15,568 KB
testcase_22 AC 266 ms
15,568 KB
testcase_23 AC 278 ms
15,568 KB
testcase_24 AC 220 ms
15,568 KB
testcase_25 AC 246 ms
15,568 KB
testcase_26 AC 240 ms
15,568 KB
testcase_27 AC 241 ms
15,568 KB
testcase_28 AC 243 ms
15,568 KB
testcase_29 AC 504 ms
15,568 KB
testcase_30 AC 542 ms
15,568 KB
testcase_31 AC 580 ms
15,568 KB
testcase_32 AC 226 ms
15,568 KB
testcase_33 AC 222 ms
15,568 KB
testcase_34 AC 237 ms
15,568 KB
testcase_35 AC 212 ms
15,568 KB
testcase_36 AC 215 ms
15,568 KB
testcase_37 AC 232 ms
15,568 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using int64 = long long;
const int mod = 1e9 + 7;

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)};
}

template< typename Monoid, typename OperatorMonoid = Monoid >
struct SegmentTreeBeats {
  using F = function< Monoid(Monoid, Monoid) >;
  using G = function< Monoid(Monoid, OperatorMonoid) >;
  using H = function< OperatorMonoid(OperatorMonoid, OperatorMonoid) >;

  int sz, height;
  vector< Monoid > data;
  vector< OperatorMonoid > lazy;
  const F f;
  const G g;
  const H h;
  const Monoid M1;
  const OperatorMonoid OM0;


  SegmentTreeBeats(int n, const F f, const G g, const H h,
                   const Monoid &M1, const OperatorMonoid OM0)
      : f(f), g(g), h(h), M1(M1), OM0(OM0) {
    sz = 1;
    height = 0;
    while(sz < n) sz <<= 1, height++;
    data.assign(2 * sz, M1);
    lazy.assign(2 * sz, OM0);
  }

  void set(int k, const Monoid &x) {
    data[k + sz] = x;
  }

  void build() {
    for(int k = sz - 1; k > 0; k--) {
      data[k] = f(data[2 * k + 0], data[2 * k + 1]);
    }
  }

  inline void propagate(int k) {
    if(lazy[k] != OM0) {
      lazy[2 * k + 0] = h(lazy[2 * k + 0], lazy[k]);
      lazy[2 * k + 1] = h(lazy[2 * k + 1], lazy[k]);
      data[k] = reflect(k);
      lazy[k] = OM0;
    }
  }

  inline Monoid reflect(int k) {
    return lazy[k] == OM0 ? data[k] : g(data[k], lazy[k]);
  }

  inline void recalc(int k) {
    while(k >>= 1) data[k] = f(reflect(2 * k + 0), reflect(2 * k + 1));
  }

  inline void thrust(int k) {
    for(int i = height; i > 0; i--) propagate(k >> i);
  }

  void update(int a, int b, const OperatorMonoid &x) {
    thrust(a += sz);
    thrust(b += sz - 1);
    for(int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) {
      if(l & 1) lazy[l] = h(lazy[l], x), ++l;
      if(r & 1) --r, lazy[r] = h(lazy[r], x);
    }
    recalc(a);
    recalc(b);
  }

  Monoid query(int a, int b) {
    thrust(a += sz);
    thrust(b += sz - 1);
    Monoid L = M1, R = M1;
    for(int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) {
      if(l & 1) L = f(L, reflect(l++));
      if(r & 1) R = f(reflect(--r), R);
    }
    return f(L, R);
  }

  Monoid operator[](const int &k) {
    return query(k, k + 1);
  }

  template< typename Uku, typename Check, typename Func, typename X >
  void update_beats_subtree(int k, const X &x, const Uku &uku, const Check &check, const Func &func) {
    if(k >= sz) {
      auto v = reflect(k);
      if(uku(v, x)) return;
      if(check(v)) lazy[k] = func(v, x);
      return;
    }
    propagate(k);
    if(uku(data[k], x)) return;

    if(check(data[k])) {
      lazy[k] = func(data[k], x);
      return;
    }
    update_beats_subtree(k * 2 + 0, x, uku, check, func);
    update_beats_subtree(k * 2 + 1, x, uku, check, func);
    data[k] = f(reflect(2 * k + 0), reflect(2 * k + 1));
  }

  template< typename Uku, typename Check, typename Func, typename X >
  void update_beats(int a, int b, const X &x, const Uku &uku, const Check &check, const Func &func) {
    thrust(a += sz);
    thrust(b += sz - 1);
    for(int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) {
      if(l & 1) update_beats_subtree(l++, x, uku, check, func);
      if(r & 1) update_beats_subtree(--r, x, uku, check, func);
    }
    recalc(a);
    recalc(b);
  }
};


int main() {
  int N, Q;
  cin >> N >> Q;
  struct Node {
    int64 latte, malta, sum, lcm, sz;

    Node() : latte(infll), malta(-infll), sum(0), lcm(1), sz(0) {}

    Node(int64 x, int64 sz) : latte(x), malta(x), sum(x * sz), lcm(x), sz(sz) {};
  };
  auto f = [](Node a, const Node &b) {
    a.latte = min(a.latte, b.latte);
    a.malta = max(a.malta, b.malta);
    a.sum += b.sum;
    if(a.lcm >= infll || b.lcm >= infll) a.lcm = infll;
    else a.lcm = a.lcm / __gcd(a.lcm, b.lcm) * b.lcm;
    a.sz += b.sz;
    return a;
  };
  auto g = [](const Node &a, int64 b) {
    return Node(b, a.sz);
  };
  auto h = [](int64 a, int64 b) {
    return b;
  };
  SegmentTreeBeats< Node, int64 > seg(N, f, g, h, Node(), infll);
  for(int i = 0; i < N; i++) {
    int x;
    cin >> x;
    seg.set(i, Node(x, 1));
  }
  seg.build();


  auto uku = [](const Node &a, int64 x) { return x % a.lcm == 0; };
  auto check = [](const Node &a) { return a.latte == a.malta; };
  auto func = [](const Node &a, int64 x) { return __gcd(a.latte, x); };

  for(int i = 0; i < Q; i++) {
    int t;
    cin >> t;
    if(t == 1) {
      int l, r, x;
      cin >> l >> r >> x;
      --l;
      seg.update(l, r, x);
    } else if(t == 2) {
      int l, r, x;
      cin >> l >> r >> x;
      --l;
      seg.update_beats(l, r, x, uku, check, func);
    } else if(t == 3) {
      int l, r;
      cin >> l >> r;
      --l;
      cout << seg.query(l, r).malta << "\n";
    } else {
      int l, r;
      cin >> l >> r;
      --l;
      cout << seg.query(l, r).sum << "\n";
    }

  }
}

0