結果

問題 No.151 セグメントフィッシング
ユーザー tnakao0123tnakao0123
提出日時 2016-03-23 00:57:06
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 55 ms / 5,000 ms
コード長 2,159 bytes
コンパイル時間 706 ms
コンパイル使用メモリ 87,028 KB
実行使用メモリ 6,952 KB
最終ジャッジ日時 2024-04-09 03:34:22
合計ジャッジ時間 2,031 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,820 KB
testcase_01 AC 1 ms
6,948 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,948 KB
testcase_04 AC 2 ms
6,952 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 8 ms
6,944 KB
testcase_09 AC 9 ms
6,944 KB
testcase_10 AC 8 ms
6,944 KB
testcase_11 AC 8 ms
6,944 KB
testcase_12 AC 32 ms
6,944 KB
testcase_13 AC 32 ms
6,944 KB
testcase_14 AC 32 ms
6,944 KB
testcase_15 AC 32 ms
6,944 KB
testcase_16 AC 31 ms
6,944 KB
testcase_17 AC 15 ms
6,948 KB
testcase_18 AC 17 ms
6,948 KB
testcase_19 AC 55 ms
6,944 KB
testcase_20 AC 55 ms
6,944 KB
testcase_21 AC 19 ms
6,948 KB
testcase_22 AC 19 ms
6,944 KB
testcase_23 AC 26 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 151.cc: No.151 セグメントフィッシング - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 20000;
const int MAX_Q = 20000;
const int MAX_E2 = 1 << 17;

/* typedef */

typedef long long ll;

template <typename T, const int MAX_E2>
struct SegTreeSum {
  int n, e2;
  T nodes[MAX_E2];

  SegTreeSum() {}

  void init(int _n) {
    n = _n;
    for (e2 = 1; e2 < n; e2 <<= 1);
    //for (int i = 0; i < MAX_E2; i++) nodes[i] = INF;
  }

  T get(int i) { return nodes[e2 - 1 + i]; }

  void add(int i, T v) {
    int j = e2 - 1 + i;
    nodes[j] += v;
    j = (j - 1) / 2;

    for (;;) {
      int j0 = j * 2 + 1, j1 = j0 + 1;
      nodes[j] = nodes[j0] + nodes[j1];
      if (j == 0) break;
      j = (j - 1) / 2;
    }
  }

  T sum_range(int r0, int r1, int k, int i0, int i1) {
    if (r1 <= i0 || i1 <= r0) return 0;
    if (r0 <= i0 && i1 <= r1) return nodes[k];

    int im = (i0 + i1) / 2;
    T v0 = sum_range(r0, r1, k * 2 + 1, i0, im);
    T v1 = sum_range(r0, r1, k * 2 + 2, im, i1);

    return v0 + v1;
  }

  T sum_range(int r0, int r1) { return sum_range(r0, r1, 0, 0, e2); }
};

/* global variables */

SegTreeSum<ll,MAX_E2> stl, str;

/* subroutines */

/* main */

int main() {
  int n, q;
  cin >> n >> q;

  stl.init(n + q + 2);
  str.init(n + q + 2);
  int tl = 0, tr = q;

  while (q--) {
    char x;
    int y, z;
    cin >> x >> y >> z;

    tl++, tr--;
    ll gl = stl.get(tl - 1);
    ll gr = str.get(tr + n);
    if (gl > 0) str.add(tr, gl);
    if (gr > 0) stl.add(tl + n - 1, gr);

    switch (x) {
    case 'L':
      stl.add(tl + y, z);
      break;
    case 'R':
      str.add(tr + y, z);
      break;
    case 'C':
      ll sum = stl.sum_range(tl + y, tl + z) + str.sum_range(tr + y, tr + z);
      printf("%lld\n", sum);
      break;
    }
  }
  return 0;
}
0