結果

問題 No.1778 括弧列クエリ / Bracketed Sequence Query
ユーザー CuriousFairy315CuriousFairy315
提出日時 2021-12-06 22:23:37
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 875 bytes
コンパイル時間 613 ms
コンパイル使用メモリ 72,288 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-09-21 15:55:03
合計ジャッジ時間 20,186 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 354 ms
4,380 KB
testcase_01 AC 359 ms
4,380 KB
testcase_02 AC 357 ms
4,380 KB
testcase_03 AC 344 ms
4,380 KB
testcase_04 AC 351 ms
4,380 KB
testcase_05 AC 362 ms
4,380 KB
testcase_06 AC 190 ms
4,376 KB
testcase_07 AC 9 ms
4,376 KB
testcase_08 AC 954 ms
4,376 KB
testcase_09 AC 65 ms
4,380 KB
testcase_10 AC 369 ms
4,380 KB
testcase_11 AC 300 ms
4,376 KB
testcase_12 AC 415 ms
4,500 KB
testcase_13 AC 1,278 ms
4,380 KB
testcase_14 AC 416 ms
4,380 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 1,111 ms
4,472 KB
testcase_17 AC 1,072 ms
4,380 KB
testcase_18 AC 1,153 ms
4,504 KB
testcase_19 AC 1,835 ms
4,376 KB
testcase_20 AC 1,146 ms
4,380 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 TLE -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<cstring>
#include<algorithm>

int MAX = 200020;

int main() {
    int N, Q;
    std::string S;
    std::cin >> N >> Q >> S;
    int pair[MAX];
    {
        int stack[MAX >> 1];
		int len = 1;
		for (int i = 1;i <= S.length();++ i) {
			char c = S[i - 1];
			if (c == '(') {
				stack[len++] = i;
			} else {
				pair[i] = stack[--len];
				pair[pair[i]] = i;
			}
		}
    }
    while(Q --> 0) {
			int x, y;
			std::cin >> x >> y;
			if (x < y) {
				int l = std::min(x, pair[x]);
				int r = std::max(y, pair[y]);
				x = l;
				y = r;
			} else {
				int l = std::min(y, pair[y]);
				int r = std::max(x, pair[x]);
				x = l;
				y = r;
			}
			for (int i = x;i >= 0; -- i) {
				if (pair[i] >= y) {
				    std::cout << i << ' ' << pair[i] << std::endl;
					goto cont;
				}
			}
			std::cout << -1 << std::endl;
			cont:;
		}
    return 0;
}
0