結果

問題 No.880 Yet Another Segment Tree Problem
ユーザー pekempeypekempey
提出日時 2019-09-06 22:46:32
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,108 bytes
コンパイル時間 2,011 ms
コンパイル使用メモリ 169,080 KB
実行使用メモリ 24,068 KB
最終ジャッジ日時 2023-09-07 01:40:00
合計ジャッジ時間 10,072 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repr(i, n) for (int i = (n) - 1; i >= 0; i--)
#define range(a) a.begin(), a.end()

using namespace std;
using ll = long long;

constexpr int U = 1 << 17;

ll gcd(ll x, ll y) {
  while (y != 0) {
    ll r = x % y;
    x = y;
    y = r;
  }
  return x;
}

ll lcm(ll x, ll y) {
  return x / gcd(x, y) * y;
}

ll dat[U * 2];
ll dat2[U * 2];
ll sum[U * 2];
ll lazy[U * 2];
ll least[U * 2];
ll sz[U * 2];
constexpr ll INF = 1.01e9;

void apply_set(int k, ll x) {
  if (x == 0) return;
  dat[k] = x;
  sum[k] = x * sz[k];
  lazy[k] = x;
  least[k] = x;
}

void push(int k) {
  apply_set(k * 2 + 0, lazy[k]);
  apply_set(k * 2 + 1, lazy[k]);
  lazy[k] = 0;
}

void pull(int k) {
  dat[k] = max(dat[k * 2], dat[k * 2 + 1]);
  dat2[k] = min(dat[k * 2], dat[k * 2 + 1]);
  sum[k] = sum[k * 2] + sum[k * 2 + 1];
  least[k] = min(INF, lcm(least[k * 2], least[k * 2 + 1]));
}

void update_set(int a, int b, ll x, int k = 1, int l = 0, int r = U) {
  if (r <= a || b <= l) return;
  if (a <= l && r <= b) {
    apply_set(k, x);
    return;
  }
  push(k);
  int m = l + r >> 1;
  update_set(a, b, x, k * 2 + 0, l, m);
  update_set(a, b, x, k * 2 + 1, m, r);
  pull(k);
}

void update_gcd(int a, int b, ll g, int k = 1, int l = 0, int r = U) {
  if (r <= a || b <= l) return;
  if (dat[k] == dat2[k]) {
    apply_set(k, gcd(dat[k], g));
    return;
  }
  if (lcm(least[k], g) == g) return;
  if (r - l == 1) {
    dat[k] = gcd(dat[k], g);
    sum[k] = dat[k];
    least[k] = dat[k];
    return;
  }
  push(k);
  int m = l + r >> 1;
  update_gcd(a, b, g, k * 2 + 0, l, m);
  update_gcd(a, b, g, k * 2 + 1, m, r);
  pull(k);
}

ll query_max(int a, int b, int k = 1, int l = 0, int r = U) {
  if (r <= a || b <= l) return -1e18;
  if (a <= l && r <= b) return dat[k];
  push(k);
  int m = l + r >> 1;
  ll vl = query_max(a, b, k * 2 + 0, l, m);
  ll vr = query_max(a, b, k * 2 + 1, m, r);
  return max(vl, vr);
}

ll query_sum(int a, int b, int k = 1, int l = 0, int r = U) {
  if (r <= a || b <= l) return 0;
  if (a <= l && r <= b) return sum[k];
  push(k);
  int m = l + r >> 1;
  ll vl = query_sum(a, b, k * 2 + 0, l, m);
  ll vr = query_sum(a, b, k * 2 + 1, m, r);
  return vl + vr;
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int N, Q; cin >> N >> Q;
  vector<int> A(N);
  rep(i, U * 2) {
    least[i] = 1;
    dat[i] = -1e18;
    dat2[i] = 1e18;
  }
  rep(i, N) {
    cin >> A[i];
    dat[i + U] = A[i];
    dat2[i + U] = A[i];
    sum[i + U] = A[i];
    least[i + U] = A[i];
    sz[i + U] = 1;
  }
  for (int i = U - 1; i >= 1; i--) pull(i), sz[i] = sz[i * 2] + sz[i * 2 + 1];
  while (Q--) {
    int type; cin >> type;
    if (type == 1) {
      int l, r, x; cin >> l >> r >> x; l--;
      update_set(l, r, x);
    } else if (type == 2) {
      int l, r, x; cin >> l >> r >> x; l--;
      update_gcd(l, r, x);
    } else if (type == 3) {
      int l, r; cin >> l >> r; l--;
      cout << query_max(l, r) << '\n';
    } else {
      int l, r; cin >> l >> r; l--;
      cout << query_sum(l, r) << '\n';
    }
  }
}
0