結果

問題 No.1778 括弧列クエリ / Bracketed Sequence Query
ユーザー CuriousFairy315CuriousFairy315
提出日時 2021-12-06 22:35:37
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,030 bytes
コンパイル時間 601 ms
コンパイル使用メモリ 73,808 KB
実行使用メモリ 9,452 KB
最終ジャッジ日時 2023-09-21 15:55:25
合計ジャッジ時間 13,860 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 350 ms
4,380 KB
testcase_01 AC 350 ms
4,376 KB
testcase_02 AC 354 ms
4,380 KB
testcase_03 AC 335 ms
4,380 KB
testcase_04 AC 357 ms
4,376 KB
testcase_05 AC 752 ms
4,456 KB
testcase_06 AC 273 ms
4,376 KB
testcase_07 AC 16 ms
4,376 KB
testcase_08 TLE -
testcase_09 AC 143 ms
4,376 KB
testcase_10 AC 950 ms
4,788 KB
testcase_11 AC 704 ms
4,380 KB
testcase_12 AC 877 ms
4,380 KB
testcase_13 TLE -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
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 left[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;
				left[i] = left[i - 1] + 1;
			} else {
				pair[i] = stack[--len];
				pair[pair[i]] = i;
				left[i] = left[i - 1];
			}
		}
    }
    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;
				}
				if (i >= 30 && left[i] - left[i - 30] == 30 && pair[i - 30] < y) i -= 30;
			}
			std::cout << -1 << std::endl;
			cont:;
		}
    return 0;
}
0