結果

問題 No.879 Range Mod 2 Query
ユーザー pekempeypekempey
提出日時 2019-09-09 01:46:42
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 244 ms / 3,000 ms
コード長 5,371 bytes
コンパイル時間 1,654 ms
コンパイル使用メモリ 171,772 KB
実行使用メモリ 16,000 KB
最終ジャッジ日時 2024-06-27 15:24:32
合計ジャッジ時間 5,366 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 10 ms
15,872 KB
testcase_01 AC 12 ms
15,744 KB
testcase_02 AC 12 ms
15,744 KB
testcase_03 AC 12 ms
15,616 KB
testcase_04 AC 13 ms
15,616 KB
testcase_05 AC 12 ms
15,616 KB
testcase_06 AC 11 ms
15,616 KB
testcase_07 AC 13 ms
15,616 KB
testcase_08 AC 13 ms
15,616 KB
testcase_09 AC 11 ms
15,616 KB
testcase_10 AC 12 ms
15,744 KB
testcase_11 AC 238 ms
15,872 KB
testcase_12 AC 140 ms
15,872 KB
testcase_13 AC 180 ms
15,872 KB
testcase_14 AC 159 ms
15,872 KB
testcase_15 AC 165 ms
15,848 KB
testcase_16 AC 230 ms
15,872 KB
testcase_17 AC 234 ms
16,000 KB
testcase_18 AC 244 ms
15,744 KB
testcase_19 AC 223 ms
15,872 KB
testcase_20 AC 218 ms
15,872 KB
testcase_21 AC 238 ms
15,744 KB
権限があれば一括ダウンロードができます

ソースコード

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;

struct semiring {
  long long x;
  semiring(long long x_ = 0) : x(x_) {}
  static semiring zero() { return semiring(0); }
  static semiring one() { return semiring(1); }
  friend semiring operator+(semiring a, semiring b) { return semiring(a.x + b.x); }
  friend semiring operator*(semiring a, semiring b) { return semiring(a.x * b.x); }
};
struct vec {
  semiring x0;
  semiring x1;
  semiring x2;
  semiring x3;
};
vec operator+(vec a, vec b) {
  a.x0 = a.x0 + b.x0;
  a.x1 = a.x1 + b.x1;
  a.x2 = a.x2 + b.x2;
  a.x3 = a.x3 + b.x3;
  return a;
}
struct mat {
  int k;
  semiring x0;
};
mat operator*(mat a, mat b) {
  mat c;
  if (a.k == 0 && b.k == 0) { c.k = 2; c.x0 = a.x0; }
  if (a.k == 0 && b.k == 1) { c.k = 0; c.x0 = a.x0; }
  if (a.k == 0 && b.k == 2) { c.k = 0; c.x0 = a.x0; }
  if (a.k == 0 && b.k == 3) { c.k = 2; c.x0 = a.x0; }
  if (a.k == 0 && b.k == 4) { c.k = 0; c.x0 = a.x0; }
  if (a.k == 0 && b.k == 5) { c.k = 2; c.x0 = a.x0; }
  if (a.k == 1 && b.k == 0) { c.k = 3; c.x0 = a.x0; }
  if (a.k == 1 && b.k == 1) { c.k = 1; c.x0 = a.x0; }
  if (a.k == 1 && b.k == 2) { c.k = 1; c.x0 = a.x0; }
  if (a.k == 1 && b.k == 3) { c.k = 3; c.x0 = a.x0; }
  if (a.k == 1 && b.k == 4) { c.k = 1; c.x0 = a.x0; }
  if (a.k == 1 && b.k == 5) { c.k = 3; c.x0 = a.x0; }
  if (a.k == 2 && b.k == 0) { c.k = 0; c.x0 = a.x0; }
  if (a.k == 2 && b.k == 1) { c.k = 2; c.x0 = a.x0; }
  if (a.k == 2 && b.k == 2) { c.k = 2; c.x0 = a.x0; }
  if (a.k == 2 && b.k == 3) { c.k = 0; c.x0 = a.x0; }
  if (a.k == 2 && b.k == 4) { c.k = 2; c.x0 = a.x0; }
  if (a.k == 2 && b.k == 5) { c.k = 0; c.x0 = a.x0; }
  if (a.k == 3 && b.k == 0) { c.k = 1; c.x0 = a.x0; }
  if (a.k == 3 && b.k == 1) { c.k = 3; c.x0 = a.x0; }
  if (a.k == 3 && b.k == 2) { c.k = 3; c.x0 = a.x0; }
  if (a.k == 3 && b.k == 3) { c.k = 1; c.x0 = a.x0; }
  if (a.k == 3 && b.k == 4) { c.k = 3; c.x0 = a.x0; }
  if (a.k == 3 && b.k == 5) { c.k = 1; c.x0 = a.x0; }
  if (a.k == 4 && b.k == 0) { c.k = 0; c.x0 = b.x0 + a.x0; }
  if (a.k == 4 && b.k == 1) { c.k = 1; c.x0 = b.x0 + a.x0; }
  if (a.k == 4 && b.k == 2) { c.k = 2; c.x0 = b.x0 + a.x0; }
  if (a.k == 4 && b.k == 3) { c.k = 3; c.x0 = b.x0 + a.x0; }
  if (a.k == 4 && b.k == 4) { c.k = 4; c.x0 = b.x0 + a.x0; }
  if (a.k == 4 && b.k == 5) { c.k = 5; c.x0 = b.x0 + a.x0; }
  if (a.k == 5 && b.k == 0) { c.k = 1; c.x0 = b.x0 + a.x0; }
  if (a.k == 5 && b.k == 1) { c.k = 0; c.x0 = b.x0 + a.x0; }
  if (a.k == 5 && b.k == 2) { c.k = 3; c.x0 = b.x0 + a.x0; }
  if (a.k == 5 && b.k == 3) { c.k = 2; c.x0 = b.x0 + a.x0; }
  if (a.k == 5 && b.k == 4) { c.k = 5; c.x0 = b.x0 + a.x0; }
  if (a.k == 5 && b.k == 5) { c.k = 4; c.x0 = b.x0 + a.x0; }
  return c;
}
vec operator*(mat a, vec b) {
  vec c;
  if (a.k == 0) { c.x0 = b.x2 + a.x0*b.x3; c.x1 = b.x2; c.x2 = b.x1; c.x3 = b.x3; }
  if (a.k == 1) { c.x0 = b.x2 + a.x0*b.x3; c.x1 = b.x1; c.x2 = b.x2; c.x3 = b.x3; }
  if (a.k == 2) { c.x0 = b.x1 + a.x0*b.x3; c.x1 = b.x1; c.x2 = b.x2; c.x3 = b.x3; }
  if (a.k == 3) { c.x0 = b.x1 + a.x0*b.x3; c.x1 = b.x2; c.x2 = b.x1; c.x3 = b.x3; }
  if (a.k == 4) { c.x0 = b.x0 + a.x0*b.x3; c.x1 = b.x1; c.x2 = b.x2; c.x3 = b.x3; }
  if (a.k == 5) { c.x0 = b.x0 + a.x0*b.x3; c.x1 = b.x2; c.x2 = b.x1; c.x3 = b.x3; }
  return c;
}
mat identity() {
  mat a;
  a.k = 4;
  a.x0 = semiring::zero();
  return a;
}
mat typeA() {
  mat a;
  a.k = 2;
  a.x0 = semiring::zero();
  return a;
}
mat typeB(semiring x0) {
  mat a;
  a.k = 5;
  a.x0 = x0;
  return a;
}
mat typeC(semiring x0) {
  mat a;
  a.k = 4;
  a.x0 = x0;
  return a;
}


constexpr int U = 1 << 17;
vec dat[U * 2];
mat lazy[U * 2];

void apply(int k, mat v) {
  lazy[k] = v * lazy[k];
  dat[k] = v * dat[k];
}

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

void pull(int k) {
  dat[k] = dat[k * 2] + dat[k * 2 + 1];
}

void update(int a, int b, mat v, int k = 1, int l = 0, int r = U) {
  if (r <= a || b <= l) return;
  if (a <= l && r <= b) {
    apply(k, v);
    return;
  }
  push(k);
  int m = (l + r) / 2;
  update(a, b, v, k * 2 + 0, l, m);
  update(a, b, v, k * 2 + 1, m, r);
  pull(k);
}

vec query(int a, int b, int k = 1, int l = 0, int r = U) {
  if (r <= a || b <= l) return vec{};
  if (a <= l && r <= b) return dat[k];
  push(k);
  int m = (l + r) / 2;
  vec vl = query(a, b, k * 2 + 0, l, m);
  vec vr = query(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) lazy[i] = identity();
  rep(i, N) {
    cin >> A[i];
    dat[i + U].x0 = A[i];
    dat[i + U].x1 = A[i] % 2;
    dat[i + U].x2 = (A[i] + 1) % 2;
    dat[i + U].x3 = 1;
  }
  for (int i = U - 1; i >= 1; i--) pull(i);
  while (Q--) {
    int type; cin >> type;
    if (type == 1) {
      int l, r; cin >> l >> r; l--;
      update(l, r, typeA());
    } else if (type == 2) {
      int l, r, x; cin >> l >> r >> x; l--;
      if (x % 2 == 1) {
        update(l, r, typeB(x));
      } else {
        update(l, r, typeC(x));
      }
    } else {
      int l, r; cin >> l >> r; l--;
      vec ans = query(l, r);
      cout << ans.x0.x << '\n';
    }
  }
}
0