結果

問題 No.1802 Range Score Query for Bracket Sequence
ユーザー tnakao0123tnakao0123
提出日時 2022-01-15 18:42:00
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 130 ms / 2,000 ms
コード長 2,029 bytes
コンパイル時間 653 ms
コンパイル使用メモリ 54,292 KB
実行使用メモリ 4,788 KB
最終ジャッジ日時 2023-09-12 14:17:31
合計ジャッジ時間 4,105 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 114 ms
4,684 KB
testcase_02 AC 113 ms
4,704 KB
testcase_03 AC 116 ms
4,768 KB
testcase_04 AC 115 ms
4,764 KB
testcase_05 AC 112 ms
4,688 KB
testcase_06 AC 112 ms
4,680 KB
testcase_07 AC 111 ms
4,676 KB
testcase_08 AC 114 ms
4,788 KB
testcase_09 AC 114 ms
4,708 KB
testcase_10 AC 114 ms
4,660 KB
testcase_11 AC 90 ms
4,748 KB
testcase_12 AC 92 ms
4,760 KB
testcase_13 AC 91 ms
4,708 KB
testcase_14 AC 130 ms
4,728 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 1 ms
4,384 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 1802.cc:  No.1802 Range Score Query for Bracket Sequence - yukicoder
 */

#include<cstdio>
#include<vector>
#include<algorithm>
 
using namespace std;

/* constant */

const int MAX_N = 200000;

/* typedef */

template <typename T>
struct SegTree {
  int e2;
  vector<T> nodes;
  T defv;
  SegTree() {}

  void init(int n, T _defv) {
    defv = _defv;
    for (e2 = 1; e2 < n; e2 <<= 1);
    nodes.assign(e2 * 2, defv);
  }

  T &geti(int i) { return nodes[e2 - 1 + i]; }
  void seti(int i, T v) { geti(i) = v; }

  void setall() {
    for (int j = e2 - 2; j >= 0; j--)
      nodes[j] = nodes[j * 2 + 1] + nodes[j * 2 + 2];
  }

  void set(int i, T v) {
    int j = e2 - 1 + i;
    nodes[j] = v;
    while (j > 0) {
      j = (j - 1) / 2;
      nodes[j] = nodes[j * 2 + 1] + nodes[j * 2 + 2];
    }
  }

  T sum_range(int r0, int r1, int k, int i0, int i1) {
    if (r1 <= i0 || i1 <= r0) return defv;
    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 */

char s[MAX_N + 4];
SegTree<int> st;

/* subroutines */

/* main */

int main() {
  int n, qn;
  scanf("%d%d%s", &n, &qn, s);

  st.init(n, 0);
  for (int i = 0; i + 1 < n; i++)
    if (s[i] == '(' && s[i + 1] == ')') st.seti(i, 1);
  st.setall();

  while (qn--) {
    int op;
    scanf("%d", &op);

    if (op == 1) {
      int i;
      scanf("%d", &i), i--;

      if (s[i] == '(') {
	s[i] = ')';
	if (i + 1 < n && s[i + 1] == ')') st.set(i, 0);
	if (i > 0 && s[i - 1] == '(') st.set(i - 1, 1);
      }
      else { // s[i] == ')'
	s[i] = '(';
	if (i + 1 < n && s[i + 1] == ')') st.set(i, 1);
	if (i > 0 && s[i - 1] == '(') st.set(i - 1, 0);
      }
    }
    else {
      int l, r;
      scanf("%d%d", &l, &r);
      l--, r--;

      printf("%d\n", st.sum_range(l, r));
    }
  }
  
  return 0;
}
0