結果

問題 No.151 セグメントフィッシング
ユーザー koba-e964koba-e964
提出日時 2015-06-23 15:27:15
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 54 ms / 5,000 ms
コード長 2,320 bytes
コンパイル時間 856 ms
コンパイル使用メモリ 76,020 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-22 00:14:56
合計ジャッジ時間 3,089 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 8 ms
4,376 KB
testcase_09 AC 9 ms
4,380 KB
testcase_10 AC 8 ms
4,380 KB
testcase_11 AC 8 ms
4,376 KB
testcase_12 AC 34 ms
4,380 KB
testcase_13 AC 33 ms
4,380 KB
testcase_14 AC 32 ms
4,376 KB
testcase_15 AC 33 ms
4,380 KB
testcase_16 AC 32 ms
4,376 KB
testcase_17 AC 18 ms
4,376 KB
testcase_18 AC 19 ms
4,376 KB
testcase_19 AC 54 ms
4,376 KB
testcase_20 AC 52 ms
4,376 KB
testcase_21 AC 23 ms
4,376 KB
testcase_22 AC 22 ms
4,376 KB
testcase_23 AC 25 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <vector>
/**
 * Segment Tree. This data structure is useful for fast folding on intervals of an array
 * whose elements are elements of monoid M. Note that constructing this tree requires the identity
 * element of M and the operation of M.
 * Header requirement: vector
 */
template<class I, class BiOp = I (*) (I, I)>
class SegTree {
  int n;
  std::vector<I> dat;
  BiOp op;
  I e;
public:
  SegTree(int n_, BiOp op, I e) : op(op), e(e) {
    n = 1;
    while (n < n_) { n *= 2; } // n is a power of 2
    dat.resize(2 * n);
    for (int i = 0; i < 2 * n - 1; i++) {
      dat[i] = e;
    }
  }
  /* ary[k] <- v */
  void update(int k, I v) {
    k += n - 1;
    dat[k] = v;
    while (k > 0) {
      k = (k - 1) / 2;
      dat[k] = op(dat[2 * k + 1], dat[2 * k + 2]);
    }
  }
  void update_array(int k, int len, const I *vals) {
    for (int i = 0; i < len; ++i) {
      update(k + i, vals[i]);
    }
  }
  /* l,r are for simplicity */
  I querySub(int a, int b, int k, int l, int r) const {
    // [a,b) and  [l,r) intersects?
    if (r <= a || b <= l) return e;
    if (a <= l && r <= b) return dat[k];
    I vl = querySub(a, b, 2 * k + 1, l, (l + r) / 2);
    I vr = querySub(a, b, 2 * k + 2, (l + r) / 2, r);
    return op(vl, vr);
  }
  /* [a, b] (note: inclusive) */
  I query(int a, int b) const {
    return querySub(a, b + 1, 0, 0, n);
  }
};

#include <iostream>
#include <algorithm>

#define REP(i,s,n) for(int i=(int)(s);i<(int)(n);i++)

using namespace std;
typedef long long int ll;
typedef vector<int> VI;

ll tt(const SegTree<ll, plus<ll> > &st, int x, int y, int n) {
  if (x / n == y / n) {
    return st.query(x % n, y % n);
  }
  return st.query(x % n, n - 1) + st.query(0, y % n);
}

int main(void){
  int n, q;
  cin >> n >> q;
  SegTree<ll, plus<ll> > st(2 * n, plus<ll>(), 0);
  REP(t, 0, q) {
    int u = t % (2 * n);
    string x;
    ll y, z;
    cin >> x >> y >> z;
    switch(x[0]) {
    case 'L':
      {
	int pos = (y + t) % (2 * n);
	st.update(pos, st.query(pos, pos) + z);
	break;
      }
    case 'R':
      {
	int pos = (-y + t + 2 * n - 1) % (2 * n);
	st.update(pos, st.query(pos, pos) + z);
	break;
      }
    case 'C':
      {
	ll q = tt(st, y + t, z + t - 1, 2 * n);
	q += tt(st, - z + t + 2 * n, -y + t + 2 * n - 1, 2 * n);
	cout << q << endl;
      }
    }
  }
}
0