結果

問題 No.880 Yet Another Segment Tree Problem
ユーザー risujirohrisujiroh
提出日時 2020-02-18 22:22:36
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 2,198 bytes
コンパイル時間 2,218 ms
コンパイル使用メモリ 182,084 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-16 04:17:13
合計ジャッジ時間 14,782 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 AC 4,187 ms
5,376 KB
testcase_33 RE -
testcase_34 RE -
testcase_35 RE -
testcase_36 RE -
testcase_37 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

constexpr int sz = 128;

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n, q;
  cin >> n >> q;
  vector<int> a(n);
  for (auto&& e : a) {
    cin >> e;
  }
  int m = (n + sz - 1) / sz;
  struct block {
    int gcd = 0;
    int assign = -1;
    long long sum = 0;
    map<int, int> mp;
  };
  vector<block> b(m);
  auto build = [&](int k) {
    b[k].gcd = 0;
    b[k].assign = -1;
    b[k].sum = 0;
    for (int i = k * sz; i < min((k + 1) * sz, n); ++i) {
      ++b[k].mp[a[i]];
      b[k].sum += a[i];
    }
  };
  auto push = [&](int k) {
    for (int i = k * sz; i < min((k + 1) * sz, n); ++i) {
      a[i] = b[k].assign == -1 ? __gcd(a[i], b[k].gcd) : b[k].assign;
    }
  };
  auto run = [&](int l, int r, auto fa, auto fb) {
    if (l % sz) {
      push(l / sz);
      while (l < r and l % sz) fa(l++);
      build((l - 1) / sz);
    }
    if (r % sz) {
      push(r / sz);
      while (l < r and r % sz) fa(--r);
      build(r / sz);
    }
    for (; l < r; l += sz) fb(l / sz);
  };
  while (q--) {
    int tp, l, r;
    cin >> tp >> l >> r;
    --l;
    if (tp == 1) {
      int x;
      cin >> x;
      run(l, r, [&](int i) { a[i] = x; }, [&](int k) {
        b[k].assign = x;
        b[k].mp.clear();
        int len = min(sz, n - k * sz);
        b[k].mp[x] = len;
        b[k].sum = (long long)x * len;
      });
    } else if (tp == 2) {
      int x;
      cin >> x;
      run(l, r, [&](int i) { a[i] = __gcd(a[i], x); }, [&](int k) {
        b[k].gcd = __gcd(b[k].gcd, x);
        map<int, int> nmp;
        for (auto e : b[k].mp) {
          nmp[__gcd(e.first, x)] += e.second;
        }
        swap(b[k].mp, nmp);
        b[k].sum = 0;
        for (auto e : b[k].mp) {
          b[k].sum += (long long)e.first * e.second;
        }
      });
    } else if (tp == 3) {
      int res = -1;
      run(l, r, [&](int i) { res = max(res, a[i]); }, [&](int k) {
        res = max(res, rbegin(b[k].mp)->first);
      });
      cout << res << '\n';
    } else {
      long long res = 0;
      run(l, r, [&](int i) { res += a[i]; }, [&](int k) {
        res += b[k].sum;
      });
      cout << res << '\n';
    }
  }
}
0