結果

問題 No.1802 Range Score Query for Bracket Sequence
ユーザー ks2mks2m
提出日時 2022-01-07 21:59:36
言語 Java21
(openjdk 21)
結果
AC  
実行時間 809 ms / 2,000 ms
コード長 2,750 bytes
コンパイル時間 2,693 ms
コンパイル使用メモリ 76,208 KB
実行使用メモリ 65,632 KB
最終ジャッジ日時 2023-09-12 14:10:25
合計ジャッジ時間 15,437 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
49,384 KB
testcase_01 AC 781 ms
64,660 KB
testcase_02 AC 769 ms
64,496 KB
testcase_03 AC 768 ms
64,604 KB
testcase_04 AC 766 ms
65,276 KB
testcase_05 AC 763 ms
65,164 KB
testcase_06 AC 739 ms
64,156 KB
testcase_07 AC 775 ms
64,952 KB
testcase_08 AC 778 ms
64,464 KB
testcase_09 AC 764 ms
64,796 KB
testcase_10 AC 781 ms
64,752 KB
testcase_11 AC 741 ms
65,128 KB
testcase_12 AC 732 ms
65,064 KB
testcase_13 AC 743 ms
64,800 KB
testcase_14 AC 809 ms
65,632 KB
testcase_15 AC 43 ms
49,436 KB
testcase_16 AC 43 ms
49,464 KB
testcase_17 AC 44 ms
49,568 KB
testcase_18 AC 43 ms
49,364 KB
testcase_19 AC 44 ms
49,580 KB
testcase_20 AC 44 ms
49,528 KB
testcase_21 AC 43 ms
49,488 KB
testcase_22 AC 45 ms
49,532 KB
testcase_23 AC 44 ms
47,284 KB
testcase_24 AC 45 ms
49,580 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;

public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] sa = br.readLine().split(" ");
		int n = Integer.parseInt(sa[0]);
		int q = Integer.parseInt(sa[1]);
		char[] s = br.readLine().toCharArray();

		int n1 = n - 1;
		FenwickTree ft = new FenwickTree(n1);
		for (int i = 0; i < n1; i++) {
			if (s[i] == '(' && s[i + 1] == ')') {
				ft.add(i, 1);
			}
		}
		PrintWriter pw = new PrintWriter(System.out);
		for (int z = 0; z < q; z++) {
			sa = br.readLine().split(" ");
			if (sa[0].equals("1")) {
				int i = Integer.parseInt(sa[1]) - 1;
				if (s[i] == '(') {
					if (i < n1 && s[i + 1] == ')') {
						ft.add(i, -1);
					}
					s[i] = ')';
					if (i > 0 && s[i - 1] == '(') {
						ft.add(i - 1, 1);
					}
				} else {
					if (i > 0 && s[i - 1] == '(') {
						ft.add(i - 1, -1);
					}
					s[i] = '(';
					if (i < n1 && s[i + 1] == ')') {
						ft.add(i, 1);
					}
				}
			} else {
				int l = Integer.parseInt(sa[1]) - 1;
				int r = Integer.parseInt(sa[2]) - 1;
				pw.println(ft.sum(l, r));
			}
		}
		pw.flush();
		br.close();
	}
}

class FenwickTree {
	private int n;
	private long[] data;

	/**
	 * 長さnの配列(a[0]~a[n-1])を作る。初期値は全て0。<br>
	 * O(n)
	 * 
	 * @param n 配列の長さ
	 */
	public FenwickTree(int n) {
		this.n = n;
		data = new long[n];
	}

	/**
	 * 初期データを元にFenwick Treeを構成する。<br>
	 * O(n)
	 * 
	 * @param data 初期データ
	 */
	public FenwickTree(long[] data) {
		this(data.length);
		build(data);
	}

	/**
	 * a[p] += x を行う。<br>
	 * O(log n)
	 * 
	 * @param p 加算位置(0≦p<n)
	 * @param x 加算値
	 */
	void add(int p, long x) {
		assert 0 <= p && p < n : "p=" + p;

		p++;
		while (p <= n) {
			data[p - 1] += x;
			p += p & -p;
		}
	}

	/**
	 * a[l] + ... + a[r-1] を返す。<br>
	 * O(log n)
	 * 
	 * @param l 開始位置(含む)    (0≦l≦r≦n)
	 * @param r 終了位置(含まない)(0≦l≦r≦n)
	 */
	long sum(int l, int r) {
		assert 0 <= l && l <= r && r <= n : "l=" + l + ", r=" + r;

		return sum(r) - sum(l);
	}

	/**
	 * a[0] + ... + a[r-1] を返す。<br>
	 * O(log n)
	 * 
	 * @param r 終了位置(含まない)(0≦r≦n)
	 */
	long sum(int r) {
		assert 0 <= r && r <= n : "r=" + r;

		long s = 0;
		while (r > 0) {
			s += data[r - 1];
			r -= r & -r;
		}
		return s;
	}

	private void build(long[] dat) {
		System.arraycopy(dat, 0, data, 0, n);
		for (int i = 1; i <= n; i++) {
			int p = i + (i & -i);
			if (p <= n) {
				data[p - 1] += data[i - 1];
			}
		}
	}
}
0