結果

問題 No.1778 括弧列クエリ / Bracketed Sequence Query
ユーザー CuriousFairy315
提出日時 2021-12-06 22:35:37
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 1,030 bytes
コンパイル時間 766 ms
コンパイル使用メモリ 71,504 KB
最終ジャッジ日時 2025-01-26 06:04:12
ジャッジサーバーID
(参考情報)
judge1 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 16 WA * 1 TLE * 10
権限があれば一括ダウンロードができます

ソースコード

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