結果

問題 No.880 Yet Another Segment Tree Problem
ユーザー risujirohrisujiroh
提出日時 2020-09-11 16:43:07
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,834 bytes
コンパイル時間 3,161 ms
コンパイル使用メモリ 266,400 KB
実行使用メモリ 11,724 KB
最終ジャッジ日時 2023-08-27 00:17:54
合計ジャッジ時間 14,793 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
11,724 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 3 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 3 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 176 ms
7,400 KB
testcase_12 AC 173 ms
7,236 KB
testcase_13 AC 122 ms
7,132 KB
testcase_14 AC 165 ms
7,140 KB
testcase_15 AC 179 ms
7,196 KB
testcase_16 AC 193 ms
7,136 KB
testcase_17 AC 159 ms
7,220 KB
testcase_18 AC 154 ms
7,164 KB
testcase_19 AC 114 ms
7,124 KB
testcase_20 AC 116 ms
7,176 KB
testcase_21 AC 119 ms
7,268 KB
testcase_22 AC 114 ms
7,124 KB
testcase_23 AC 118 ms
7,240 KB
testcase_24 AC 99 ms
7,236 KB
testcase_25 AC 100 ms
7,164 KB
testcase_26 AC 102 ms
7,132 KB
testcase_27 AC 98 ms
7,268 KB
testcase_28 AC 103 ms
7,132 KB
testcase_29 AC 163 ms
7,224 KB
testcase_30 AC 178 ms
7,168 KB
testcase_31 AC 191 ms
7,272 KB
testcase_32 AC 55 ms
7,120 KB
testcase_33 TLE -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/extc++.h>

struct beats {
  struct node {
    int64_t mx, sum;
    friend node operator*(node a, node b) {
      return {std::max(a.mx, b.mx), a.sum + b.sum};
    }
  };

  int sz;
  std::vector<node> tree;

  beats(int n) {
    for (sz = 1; sz < n;) sz *= 2;
    tree.resize(2 * sz);
    for (int i = 0; i < n; ++i) {
      int a;
      std::cin >> a;
      tree[sz + i] = {a, a};
    }
    for (int i = sz; i-- > 1;) pull(i);
  }

  void pull(int i) { tree[i] = tree[2 * i] * tree[2 * i + 1]; }
  void push(int i, int j, int len) {
    if (tree[i].mx * len == tree[i].sum) {
      tree[j].mx = tree[i].mx;
      tree[j].sum = tree[i].sum / 2;
    }
  }
  void assign(int l, int r, int x, int i, int a, int b) {
    if (r <= a or b <= l) return;
    if (l <= a and b <= r) {
      tree[i].mx = x;
      tree[i].sum = tree[i].mx * (b - a);
      return;
    }
    push(i, 2 * i, b - a), push(i, 2 * i + 1, b - a);
    int m = (a + b) >> 1;
    assign(l, r, x, 2 * i, a, m), assign(l, r, x, 2 * i + 1, m, b);
    pull(i);
  }
  void assign(int l, int r, int x) { assign(l, r, x, 1, 0, sz); }
  void gcd(int l, int r, int x, int i, int a, int b) {
    if (r <= a or b <= l) return;
    if (l <= a and b <= r and tree[i].mx * (b - a) == tree[i].sum) {
      tree[i].mx = std::gcd(tree[i].mx, x);
      tree[i].sum = tree[i].mx * (b - a);
      return;
    }
    push(i, 2 * i, b - a), push(i, 2 * i + 1, b - a);
    int m = (a + b) >> 1;
    gcd(l, r, x, 2 * i, a, m), gcd(l, r, x, 2 * i + 1, m, b);
    pull(i);
  }
  void gcd(int l, int r, int x) { gcd(l, r, x, 1, 0, sz); }
  int max(int l, int r, int i, int a, int b) {
    if (r <= a or b <= l) return 0;
    if (l <= a and b <= r) return tree[i].mx;
    push(i, 2 * i, b - a), push(i, 2 * i + 1, b - a);
    int m = (a + b) >> 1;
    return std::max(max(l, r, 2 * i, a, m), max(l, r, 2 * i + 1, m, b));
  }
  int max(int l, int r) { return max(l, r, 1, 0, sz); }
  int64_t sum(int l, int r, int i, int a, int b) {
    if (r <= a or b <= l) return 0;
    if (l <= a and b <= r) return tree[i].sum;
    push(i, 2 * i, b - a), push(i, 2 * i + 1, b - a);
    int m = (a + b) >> 1;
    return sum(l, r, 2 * i, a, m) + sum(l, r, 2 * i + 1, m, b);
  }
  int64_t sum(int l, int r) { return sum(l, r, 1, 0, sz); }
};

int main() {
  using namespace std;
  cin.tie(nullptr)->sync_with_stdio(false);
  int n, q;
  cin >> n >> q;
  beats b(n);
  while (q--) {
    int t, l, r;
    cin >> t >> l >> r;
    --l;
    if (t == 1) {
      int x;
      cin >> x;
      b.assign(l, r, x);
    } else if (t == 2) {
      int x;
      cin >> x;
      b.gcd(l, r, x);
    } else if (t == 3) {
      cout << b.max(l, r) << '\n';
    } else {
      cout << b.sum(l, r) << '\n';
    }
  }
}
0