結果

問題 No.880 Yet Another Segment Tree Problem
ユーザー risujirohrisujiroh
提出日時 2020-02-18 23:26:20
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2,165 ms / 5,000 ms
コード長 2,940 bytes
コンパイル時間 2,497 ms
コンパイル使用メモリ 192,720 KB
実行使用メモリ 8,424 KB
最終ジャッジ日時 2023-10-24 02:49:10
合計ジャッジ時間 40,025 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 8 ms
4,372 KB
testcase_04 AC 6 ms
4,372 KB
testcase_05 AC 6 ms
4,372 KB
testcase_06 AC 4 ms
4,372 KB
testcase_07 AC 6 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,110 ms
7,356 KB
testcase_12 AC 1,084 ms
7,604 KB
testcase_13 AC 755 ms
7,156 KB
testcase_14 AC 1,403 ms
8,172 KB
testcase_15 AC 1,447 ms
8,184 KB
testcase_16 AC 1,532 ms
8,344 KB
testcase_17 AC 1,659 ms
8,424 KB
testcase_18 AC 1,652 ms
8,424 KB
testcase_19 AC 1,104 ms
8,172 KB
testcase_20 AC 1,141 ms
8,408 KB
testcase_21 AC 1,159 ms
8,080 KB
testcase_22 AC 1,108 ms
8,344 KB
testcase_23 AC 1,167 ms
8,080 KB
testcase_24 AC 858 ms
6,012 KB
testcase_25 AC 886 ms
6,232 KB
testcase_26 AC 905 ms
5,968 KB
testcase_27 AC 862 ms
6,232 KB
testcase_28 AC 923 ms
5,968 KB
testcase_29 AC 1,364 ms
8,172 KB
testcase_30 AC 1,413 ms
8,184 KB
testcase_31 AC 1,499 ms
8,344 KB
testcase_32 AC 2,165 ms
4,372 KB
testcase_33 AC 1,557 ms
4,372 KB
testcase_34 AC 1,631 ms
4,372 KB
testcase_35 AC 1,565 ms
4,372 KB
testcase_36 AC 1,700 ms
4,372 KB
testcase_37 AC 1,656 ms
4,372 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

struct Stopwatch {
  clock_t t = clock();
  void restart() { t = clock(); }
  int elapsed() const { return (clock() - t) * 1000 / CLOCKS_PER_SEC; }
  friend string to_string(Stopwatch sw) {
    return "Time: " + to_string(sw.elapsed()) + " ms";
  }
} sw;

#pragma GCC target ("avx")
int bsf(int a) { return __builtin_ctz(a); }
int gcd(int a, int b) {
  if (a == 0) return b;
  if (b == 0) return a;
  int shift = bsf(a | b);
  a >>= bsf(a);
  do {
    b >>= bsf(b);
    if (a > b) swap(a, b);
    b -= a;
  } while (b);
  return (a << shift);
}

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';
    }
  }
  cerr << to_string(sw) << '\n';
}
0