結果

問題 No.1778 括弧列クエリ / Bracketed Sequence Query
ユーザー laneguelanegue
提出日時 2021-12-24 13:14:47
言語 D
(dmd 2.105.2)
結果
AC  
実行時間 425 ms / 2,000 ms
コード長 1,441 bytes
コンパイル時間 3,051 ms
コンパイル使用メモリ 248,668 KB
実行使用メモリ 23,096 KB
最終ジャッジ日時 2023-09-04 15:31:40
合計ジャッジ時間 16,099 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 214 ms
4,592 KB
testcase_01 AC 223 ms
4,616 KB
testcase_02 AC 215 ms
4,536 KB
testcase_03 AC 198 ms
4,580 KB
testcase_04 AC 220 ms
4,568 KB
testcase_05 AC 158 ms
8,416 KB
testcase_06 AC 106 ms
5,416 KB
testcase_07 AC 6 ms
6,928 KB
testcase_08 AC 324 ms
15,316 KB
testcase_09 AC 31 ms
8,240 KB
testcase_10 AC 135 ms
12,720 KB
testcase_11 AC 119 ms
10,656 KB
testcase_12 AC 177 ms
8,768 KB
testcase_13 AC 305 ms
15,020 KB
testcase_14 AC 115 ms
12,164 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 315 ms
14,444 KB
testcase_17 AC 332 ms
15,348 KB
testcase_18 AC 332 ms
15,316 KB
testcase_19 AC 327 ms
15,244 KB
testcase_20 AC 333 ms
15,224 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 190 ms
8,280 KB
testcase_24 AC 197 ms
8,248 KB
testcase_25 AC 260 ms
9,116 KB
testcase_26 AC 425 ms
22,696 KB
testcase_27 AC 238 ms
23,096 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std;

void main(){
	auto input = readln.chomp.split(" ");
	int N = input[0].to!int;
	int Q = input[1].to!int;
	auto S = readln.chomp;
	int[] a = new int[N + 1];
	int[] parent = new int[N + 1];
	int node = 0;
	for(auto i = 1; i <= N; i++){
		if(S[i - 1]=='('){
			parent[i] = node;
			node = i;
		}else{
			a[node] = i;
			a[i] = node;
			parent[i] = parent[node];
			node = parent[node];
		}
	}
	int[][] dbl;
	dbl ~= new int[N + 1];
	auto depth = new int[N + 1];
	for(auto i = 1; i <= N; i++){
		depth[i] = depth[parent[i]] + 1;
		dbl[0][i] = parent[i];
	}
	auto max_depth = depth.maxElement;
	for(auto k = 1; k < max_depth; k *= 2){
		dbl ~= new int[N + 1];
		for(auto i = 1; i <= N; i++){
			dbl[$ - 1][i] = dbl[$ - 2][dbl[$ - 2][i]];
		}
	}
	for(auto q = 0; q < Q; q++){
		input = readln.chomp.split(" ");
		int x = input[0].to!int;
		int y = input[1].to!int;
		if(depth[x] < depth[y]){
			auto t = x;
			x = y;
			y = t;
		}
		auto d = depth[x] - depth[y];
		auto k = 0;
		while(d > 0){
			if(d & 1){
				x = dbl[k][x];
			}
			k++;
			d >>= 1;
		}
		//stderr.writeln([x,y]);
		if(x == a[y] || x == y){
			writefln("%d %d", min(x, a[x]), max(x, a[x]));
			continue;
		}
		for(k = cast(int)dbl.length - 1; k >= 0; k--){
			if(dbl[k][x] != dbl[k][y]){
				x = dbl[k][x];
				y = dbl[k][y];
			}
		}
		//stderr.writeln(">",[x,y]);
		if(parent[x] > 0){
			writefln("%d %d", parent[x], a[parent[x]]);
		}else{
			writeln(-1);
		}
	}
}
0