結果

問題 No.880 Yet Another Segment Tree Problem
ユーザー risujirohrisujiroh
提出日時 2020-02-18 22:39:04
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,248 bytes
コンパイル時間 2,134 ms
コンパイル使用メモリ 182,816 KB
実行使用メモリ 8,524 KB
最終ジャッジ日時 2024-04-16 04:18:14
合計ジャッジ時間 51,261 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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