結果

問題 No.1778 括弧列クエリ / Bracketed Sequence Query
ユーザー さかぽんさかぽん
提出日時 2021-12-19 18:48:14
言語 C#(csc)
(csc 3.9.0)
結果
WA  
実行時間 -
コード長 1,511 bytes
コンパイル時間 2,419 ms
コンパイル使用メモリ 113,112 KB
実行使用メモリ 56,364 KB
最終ジャッジ日時 2023-10-13 18:57:31
合計ジャッジ時間 15,495 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 73 ms
16,172 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 71 ms
16,076 KB
testcase_22 AC 70 ms
15,964 KB
testcase_23 AC 489 ms
40,172 KB
testcase_24 AC 502 ms
42,756 KB
testcase_25 AC 589 ms
48,880 KB
testcase_26 AC 540 ms
50,080 KB
testcase_27 AC 433 ms
44,776 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.Collections.Generic;
using System.Linq;

class G
{
	static int[] Read() => Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
	static (int x, int y) Read2() { var a = Read(); return (a[0], a[1]); }
	static void Main() => Console.WriteLine(Solve());
	static object Solve()
	{
		var (n, qc) = Read2();
		var s = Console.ReadLine();
		var qs = Array.ConvertAll(new bool[qc], _ => Read2());

		// 対
		var pair = new int[n + 1];
		var st = new Stack<int>();

		for (int i = 1; i <= n; i++)
		{
			if (s[i - 1] == '(')
			{
				st.Push(i);
			}
			else
			{
				var l = st.Pop();
				pair[l] = i;
				pair[i] = l;
			}
		}

		for (int qi = 0; qi < qc; qi++)
		{
			var (x, y) = qs[qi];

			var xl = Math.Min(x, pair[x]);
			var xr = Math.Max(x, pair[x]);
			var yl = Math.Min(y, pair[y]);
			var yr = Math.Max(y, pair[y]);

			qs[qi] = (Math.Min(xl, yl), Math.Max(xr, yr));
		}

		var map = Array.ConvertAll(new bool[n + 1], _ => new List<int>());
		foreach (var qi in Enumerable.Range(0, qc).OrderBy(qi => qs[qi].x))
		{
			var y = qs[qi].y;
			map[y].Add(qi);
		}

		var r = new (int l, int r)[qc];

		for (int i = 1; i <= n; i++)
		{
			if (s[i - 1] == '(') continue;

			foreach (var qi in map[i])
				st.Push(qi);

			while (st.Count > 0)
			{
				var qi = st.Peek();

				if (pair[i] <= qs[qi].x)
				{
					st.Pop();
					r[qi] = (pair[i], i);
				}
				else
				{
					break;
				}
			}
		}

		return string.Join("\n", r.Select(p => p.l == 0 ? "-1" : $"{p.l} {p.r}"));
	}
}
0