結果

問題 No.880 Yet Another Segment Tree Problem
ユーザー risujirohrisujiroh
提出日時 2020-02-18 23:01:23
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 4,946 ms / 5,000 ms
コード長 2,371 bytes
コンパイル時間 3,619 ms
コンパイル使用メモリ 189,116 KB
実行使用メモリ 8,292 KB
最終ジャッジ日時 2023-10-24 02:47:45
合計ジャッジ時間 49,475 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,372 KB
testcase_01 AC 5 ms
4,372 KB
testcase_02 AC 7 ms
4,372 KB
testcase_03 AC 7 ms
4,372 KB
testcase_04 AC 6 ms
4,372 KB
testcase_05 AC 5 ms
4,372 KB
testcase_06 AC 4 ms
4,372 KB
testcase_07 AC 5 ms
4,372 KB
testcase_08 AC 6 ms
4,372 KB
testcase_09 AC 7 ms
4,372 KB
testcase_10 AC 6 ms
4,372 KB
testcase_11 AC 1,618 ms
7,224 KB
testcase_12 AC 1,580 ms
7,472 KB
testcase_13 AC 1,096 ms
7,024 KB
testcase_14 AC 2,135 ms
8,040 KB
testcase_15 AC 2,197 ms
8,052 KB
testcase_16 AC 2,331 ms
8,168 KB
testcase_17 AC 4,946 ms
8,292 KB
testcase_18 AC 4,929 ms
8,292 KB
testcase_19 AC 812 ms
8,040 KB
testcase_20 AC 843 ms
8,276 KB
testcase_21 AC 852 ms
7,904 KB
testcase_22 AC 813 ms
8,168 KB
testcase_23 AC 856 ms
7,940 KB
testcase_24 AC 775 ms
5,880 KB
testcase_25 AC 800 ms
6,056 KB
testcase_26 AC 817 ms
5,812 KB
testcase_27 AC 779 ms
6,116 KB
testcase_28 AC 815 ms
5,832 KB
testcase_29 AC 2,046 ms
8,040 KB
testcase_30 AC 2,113 ms
8,052 KB
testcase_31 AC 2,246 ms
8,168 KB
testcase_32 AC 2,194 ms
4,372 KB
testcase_33 AC 1,289 ms
4,372 KB
testcase_34 AC 1,377 ms
4,372 KB
testcase_35 AC 1,331 ms
4,372 KB
testcase_36 AC 1,326 ms
4,372 KB
testcase_37 AC 1,289 ms
4,372 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

constexpr int sz = 256;

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;
    b[k].mp.clear();
    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);
  };
  for (int k = 0; k < m; ++k) build(k);
  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) {
        if (b[k].assign == -1) {
          b[k].gcd = __gcd(b[k].gcd, x);
        } else {
          b[k].assign = __gcd(b[k].assign, 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, prev(end(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