結果

問題 No.1778 括弧列クエリ / Bracketed Sequence Query
ユーザー CuriousFairy315CuriousFairy315
提出日時 2021-11-17 16:20:03
言語 Java21
(openjdk 21)
結果
AC  
実行時間 827 ms / 2,000 ms
コード長 10,348 bytes
コンパイル時間 5,495 ms
コンパイル使用メモリ 80,284 KB
実行使用メモリ 93,104 KB
最終ジャッジ日時 2023-09-16 22:10:08
合計ジャッジ時間 21,428 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 353 ms
58,788 KB
testcase_01 AC 375 ms
58,276 KB
testcase_02 AC 370 ms
58,192 KB
testcase_03 AC 343 ms
56,632 KB
testcase_04 AC 362 ms
58,356 KB
testcase_05 AC 373 ms
65,148 KB
testcase_06 AC 294 ms
56,948 KB
testcase_07 AC 139 ms
56,632 KB
testcase_08 AC 636 ms
88,068 KB
testcase_09 AC 214 ms
61,184 KB
testcase_10 AC 376 ms
73,588 KB
testcase_11 AC 574 ms
65,884 KB
testcase_12 AC 389 ms
65,420 KB
testcase_13 AC 706 ms
82,804 KB
testcase_14 AC 438 ms
67,872 KB
testcase_15 AC 66 ms
51,988 KB
testcase_16 AC 744 ms
86,312 KB
testcase_17 AC 741 ms
88,156 KB
testcase_18 AC 827 ms
93,104 KB
testcase_19 AC 695 ms
88,096 KB
testcase_20 AC 780 ms
88,664 KB
testcase_21 AC 80 ms
52,404 KB
testcase_22 AC 66 ms
50,792 KB
testcase_23 AC 433 ms
77,580 KB
testcase_24 AC 479 ms
84,828 KB
testcase_25 AC 642 ms
87,492 KB
testcase_26 AC 751 ms
88,216 KB
testcase_27 AC 512 ms
86,884 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package yukicoder_4516;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.BitSet;
import java.util.NoSuchElementException;

public class Main {

	public static void main(String[] args) {
		new Main();
	}

	public Main() {
		InputChecker ic = new InputChecker(System.in);
		java.io.PrintWriter out = new java.io.PrintWriter(System.out);
		solve(ic, out);
		out.flush();
	}

	public void solve(InputChecker ic, java.io.PrintWriter out) {
		int N = ic.nextInt(1, exponent10(2, 5));
		ic.nextChar(' ');
		int Q = ic.nextInt(1, exponent10(2, 5));
		ic.readNewLine();
		final CharSet BRACKET = new CharSet();
		BRACKET.add('(', ')');
		char[] S = ic.next(BRACKET).toCharArray();
		if (S.length != N) throw new AssertionError();
		ic.readNewLine();

		LCA lca;
		int[] pair = new int[N + 2];
		{
			int[] stack = new int[N + 2 >> 1];
			int[] parent = new int[N + 2];
			int[] depth = new int[N + 2];
			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;
					parent[i] = parent[pair[i]] = stack[len - 1];
					depth[i] = depth[pair[i]] = len;
				}
			}
			lca = new LCA(parent, depth);
		}
		while(Q --> 0) {
			int x = ic.nextInt(1, N);
			ic.nextChar(' ');
			int y = ic.nextInt(1, N);
			ic.readNewLine();

			x = Math.min(x, pair[x]);
			y = Math.min(y, pair[y]);
			int ans = lca.lca(x, y);
			if (ans == 0) out.println(-1);
			else out.println(ans + " " + pair[ans]);
		}
		ic.checkEOF();
	}

	public static class LCA {
		int[][] parent;
		int[] depth;
		public LCA(int[] parent, int[] depth) {
			int N = 32 - Integer.numberOfLeadingZeros(parent.length - 1);
			this.parent = new int[N][];
			this.depth = depth.clone();
			this.parent[0] = parent.clone();
			for (int i = 1;i < N;++ i) {
				int[] last = this.parent[i - 1], next = new int[parent.length];
				for (int j = 0;j < parent.length;++ j) next[j] = last[last[j]];
				this.parent[i] = next;
			}
		}
		int lca(int i, int j) {
			if (depth[i] > depth[j]) {
				 int up = depth[i] - depth[j];
				 for (int x = 31 - Integer.numberOfLeadingZeros(up);up != 0;-- x) {
					 if ((up >> x & 1) != 0) {
						 i = parent[x][i];
						 up ^= 1 << x;
					 }
				 }
				 if (i == j) return i;
				 for (int x = 31 - Integer.numberOfLeadingZeros(depth[j]);x >= 0;-- x) {
					 int pi = parent[x][i], pj = parent[x][j];
					 if (pi != pj) {
						 i = pi;
						 j = pj;
					 }
				 }
			} else {
				int up = depth[j] - depth[i];
				 for (int x = 31 - Integer.numberOfLeadingZeros(up);up != 0;-- x) {
					 if ((up >> x & 1) != 0) {
						 j = parent[x][j];
						 up ^= 1 << x;
					 }
				 }
				 if (i == j) return i;
				 for (int x = 31 - Integer.numberOfLeadingZeros(depth[i]);x >= 0;-- x) {
					 int pi = parent[x][i], pj = parent[x][j];
					 if (pi != pj) {
						 i = pi;
						 j = pj;
					 }
				 }
			}
			 return parent[0][i];
		}
	}


	public static int exponent10(int n, int e) {
		return n * pow(10, e);
	}

	public static long exponent10L(int n, int e) {
		return n * pow(10L, e);
	}

	public static int pow(int a, int b) {
		int ans = 1;
		for (int mul = a;b > 0;b >>= 1, mul *= mul) if ((b & 1) != 0) ans *= mul;
		return ans;
	}

	public static long pow(long a, long b) {
		long ans = 1;
		for (long mul = a;b > 0;b >>= 1, mul *= mul) if ((b & 1) != 0) ans *= mul;
		return ans;
	}

	public static class CharSet {
		private final BitSet set = new BitSet(256);

		public void add(char... c) {
			for (char i : c) set.set(i);
		}

		public void add(CharSet... s) {
			for (CharSet i : s) set.or(i.set);
		}

		public boolean contains(char... c) {
			for (char i : c) if (!set.get(i)) return false;
			return true;
	 	}

		public boolean contains(String s) {
			return contains(s.toCharArray());
	 	}

		private static final class Chars extends CharSet {
			public Chars(char... c) {
				super.add(c);
			}
			public Chars(CharSet... s) {
				super.add(s);
			}
			@Override
			public void add(char... c) {
				throw new UnsupportedOperationException();
			}
			@Override
			public void add(CharSet... s) {
				throw new UnsupportedOperationException();
			}
		}

		public static final CharSet NUMBER = new Chars('0','1','2','3','4','5','6','7','8','9');
		public static final CharSet LOWER = new Chars('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
		public static final CharSet UPPER = new Chars('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
		public static final CharSet ALPHABET = new Chars(LOWER, UPPER);

	}
	public static class InputChecker {
		private InputStream in;
		private final byte[] buffer = new byte[1024];
		private final byte[] undo = new byte[1024];
		private int undoSize = 0;
		private int read = 0;
		private int length = 0;

		public InputChecker(InputStream in) {
			this.in = in;
		}

		public final void setInputStream(InputStream in) {
			this.in = in;
		}

		public final void setInputStream(File in) {
			try {
				this.in = new FileInputStream(in);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		}

		private boolean hasNextByte() {
			if (undoSize > 0) return true;
			if (read < length) return true;
			read = 0;
			try {
				length = in.read(buffer);
			} catch (IOException e) {
				e.printStackTrace();
			}
			return length > 0;
		}

		private byte readByte() {
			if (hasNextByte()) return undoSize > 0 ? undo[--undoSize] : buffer[read++];
			throw new NoSuchElementException();
		}

		private void undo(byte b) {
			undo[undoSize ++] = b;
		}

		private void undo(char c) {
			if ((c & 0xFF80) == 0) {
				undo((byte)c);
				return;
			}
			undo((byte)(c & 0x3F | 0x80));
			if ((c & 0xF800) == 0) {
				undo((byte)(c >> 6 & 0x1F | 0xC0));
			} else {
				undo((byte)(c >> 6 & 0x3F | 0x80));
				undo((byte)(c >> 12 | 0xE0));
			}
		}

		public final boolean hasNext() {
			return hasNextByte();
		}

		public final char nextChar() {
			byte b = readByte();
			if ((b & 0x80) == 0) return (char)b;
			if ((b & 0x20) == 0) return (char)((b & 0x1F) << 6 | readByte() & 0x3F);
			return (char)((b & 0xF) << 12 | (readByte() & 0x3F) << 6 | readByte() & 0x3F);
		}

		public final char nextChar(char estimate) {
			char c = nextChar();
			if (estimate == c) return estimate;
			undo(c);
			throw new AssertionError();
		}

		public final char nextChar(CharSet estimate) {
			char c = nextChar();
			if (estimate.contains(c)) return c;
			undo(c);
			throw new AssertionError();
		}

		public final void readNewLine() {
			char c = nextChar();
			if (c == '\r') {
				c = nextChar();
				if (c != '\n') undo(c);
				return;
			} else if (c == '\n') return;
			undo(c);
			throw new AssertionError();
		}

		public final void checkEOF() {
			if (hasNextByte()) throw new AssertionError();
		}

		public final String next(CharSet contains) {
			StringBuilder sb = new StringBuilder();
			try {
				do {
					char c = nextChar();
					if (!contains.contains(c)) {
						undo(c);
						return sb.toString();
					}
					sb.append(c);
				} while(true);
			} catch (NoSuchElementException e) {
				if (sb.length() <= 0) throw new AssertionError();
				return sb.toString();
			}
		}

		public final int nextInt() {
			byte b = readByte();
			int n = 0;
			if (b == '-') {
				if (!isNumber(b = readByte())) {
					undo(b);
					throw new NumberFormatException();
				}
				try {
					if (b == '0') {
						if (!isNumber(b = readByte())) {
							undo(b);
							return 0;
						}
						throw new NumberFormatException();
					}
					do n = Math.addExact(Math.multiplyExact(n, 10), '0' - b); while(isNumber(b = readByte()));
					undo(b);
				} catch (NoSuchElementException e) {
				}
				return n;
			}
			if (!isNumber(b)) {
				undo(b);
				throw new NumberFormatException();
			}
			try {
				if (b == '0') {
					if (!isNumber(b = readByte())) {
						undo(b);
						return 0;
					}
					throw new NumberFormatException();
				}
				do n = Math.addExact(Math.multiplyExact(n, 10), b - '0'); while(isNumber(b = readByte()));
				undo(b);
			} catch (NoSuchElementException e) {
			}
			return n;
		}

		public final int nextInt(int min, int max) {
			int n = nextInt();
			if (min <= n && n <= max) return n;
			throw new NumberFormatException();
		}

		private static boolean isNumber(byte c) {
			return '0' <= c && c <= '9';
		}

		public final long nextLong() {
			byte b = readByte();
			long n = 0;
			if (b == '-') {
				if (!isNumber(b = readByte())) {
					undo(b);
					throw new NumberFormatException();
				}
				try {
					if (b == '0') {
						if (!isNumber(b = readByte())) {
							undo(b);
							return 0;
						}
						throw new NumberFormatException();
					}
					do n = Math.addExact(Math.multiplyExact(n, 10), '0' - b); while(isNumber(b = readByte()));
					undo(b);
				} catch (NoSuchElementException e) {
				}
				return n;
			}
			if (!isNumber(b)) {
				undo(b);
				throw new NumberFormatException();
			}
			try {
				if (b == '0') {
					if (!isNumber(b = readByte())) {
						undo(b);
						return 0;
					}
					throw new NumberFormatException();
				}
				do n = Math.addExact(Math.multiplyExact(n, 10), b - '0'); while(isNumber(b = readByte()));
				undo(b);
			} catch (NoSuchElementException e) {
			}
			return n;
		}

		public final long nextLong(long min, long max) {
			long n = nextLong();
			if (min <= n && n <= max) return n;
			throw new NumberFormatException();
		}

		public final double nextDouble() {
			StringBuilder sb = new StringBuilder();
			byte b = readByte();
			if (b == '-') {
				sb.append(b);
				b = readByte();
			}
			if (b == '0') {
				sb.append(b);
				b = readByte();
			} else {
				while(isNumber(b)) {
					sb.append(b);
					b = readByte();
				}
			}
			if (b == '.') {
				sb.append(b);
				b = readByte();
				while(isNumber(b)) {
					sb.append(b);
					b = readByte();
				}
			}
			if (b == 'e' || b == 'E') {
				sb.append(b);
				b = readByte();
				if (b == '-' || b == '+') {
					sb.append(b);
					b = readByte();
				}
				while(isNumber(b)) {
					sb.append(b);
					b = readByte();
				}
			}
			undo(b);
			return Double.parseDouble(sb.toString());
		}
	}
}
0