結果

問題 No.2308 [Cherry 5th Tune B] もしかして、真?
ユーザー ks2mks2m
提出日時 2023-05-19 22:44:23
言語 Java21
(openjdk 21)
結果
AC  
実行時間 946 ms / 2,000 ms
コード長 3,244 bytes
コンパイル時間 3,182 ms
コンパイル使用メモリ 79,032 KB
実行使用メモリ 85,972 KB
最終ジャッジ日時 2024-05-10 06:12:03
合計ジャッジ時間 38,283 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 53 ms
37,256 KB
testcase_01 AC 767 ms
63,524 KB
testcase_02 AC 889 ms
70,900 KB
testcase_03 AC 801 ms
73,776 KB
testcase_04 AC 739 ms
73,592 KB
testcase_05 AC 833 ms
73,864 KB
testcase_06 AC 800 ms
72,180 KB
testcase_07 AC 877 ms
73,640 KB
testcase_08 AC 881 ms
85,972 KB
testcase_09 AC 916 ms
82,916 KB
testcase_10 AC 891 ms
70,800 KB
testcase_11 AC 861 ms
71,720 KB
testcase_12 AC 890 ms
71,364 KB
testcase_13 AC 882 ms
73,604 KB
testcase_14 AC 879 ms
73,784 KB
testcase_15 AC 789 ms
73,912 KB
testcase_16 AC 778 ms
71,864 KB
testcase_17 AC 849 ms
71,488 KB
testcase_18 AC 868 ms
71,424 KB
testcase_19 AC 895 ms
71,352 KB
testcase_20 AC 859 ms
71,660 KB
testcase_21 AC 856 ms
83,564 KB
testcase_22 AC 903 ms
83,444 KB
testcase_23 AC 879 ms
83,040 KB
testcase_24 AC 901 ms
83,280 KB
testcase_25 AC 916 ms
83,992 KB
testcase_26 AC 861 ms
83,172 KB
testcase_27 AC 883 ms
83,636 KB
testcase_28 AC 946 ms
81,496 KB
testcase_29 AC 913 ms
81,324 KB
testcase_30 AC 889 ms
83,432 KB
testcase_31 AC 698 ms
83,932 KB
testcase_32 AC 742 ms
84,132 KB
testcase_33 AC 735 ms
83,884 KB
testcase_34 AC 740 ms
83,648 KB
testcase_35 AC 764 ms
83,296 KB
testcase_36 AC 757 ms
83,776 KB
testcase_37 AC 191 ms
43,936 KB
testcase_38 AC 903 ms
72,800 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;

public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int t = Integer.parseInt(br.readLine());
		PrintWriter pw = new PrintWriter(System.out);
		for (int z = 0; z < t; z++) {
			int n = Integer.parseInt(br.readLine());
			Obj[] arr = new Obj[n];
			String[] sa = br.readLine().split(" ");
			for (int i = 0; i < n; i++) {
				Obj o = new Obj();
				o.i = i;
				o.r = i + 1;
				if (sa[i].equals("True")) {
					o.val = true;
				}
				arr[i] = o;
			}
			sa = br.readLine().split(" ");
			for (int i = 0; i < n - 1; i++) {
				arr[i].y = sa[i];
			}
			sa = br.readLine().split(" ");
			int[] s = new int[n - 1];
			for (int i = 0; i < n - 1; i++) {
				s[i] = Integer.parseInt(sa[i]) - 1;
			}

			FenwickTree ft = new FenwickTree(n);
			for (int i = 0; i < n - 1; i++) {
				int ok = s[i];
				int ng = n;
				while (Math.abs(ok - ng) > 1) {
					int mid = (ok + ng) / 2;
					int idx = (int) (mid - ft.sum(mid));
					if (idx <= s[i]) {
						ok = mid;
					} else {
						ng = mid;
					}
				}
				Obj o1 = arr[ok];
				Obj o2 = arr[o1.r];
				if (o1.y.equals("and")) {
					o1.val = o1.val && o2.val;
				} else if (o1.y.equals("or")) {
					o1.val = o1.val || o2.val;
				} else if (o1.y.equals("xor")) {
					o1.val = o1.val ^ o2.val;
				} else {
					if (o1.val) {
						o1.val = o2.val;
					} else {
						o1.val = true;
					}
				}
				o1.r = o2.r;
				o1.y = o2.y;
				ft.add(o2.i, 1);
			}
			pw.println(arr[0].val ? "True" : "False");
		}
		pw.flush();
		br.close();
	}

	static class Obj {
		int i, r;
		boolean val;
		String y;
	}
}

class FenwickTree {
	private int n;
	private long[] data;

	/**
	 * 長さnの配列(a[0]~a[n-1])を作る。初期値は全て0。<br>
	 * O(n)
	 * 
	 * @param n 配列の長さ
	 */
	public FenwickTree(int n) {
		this.n = n;
		data = new long[n];
	}

	/**
	 * 初期データを元にFenwick Treeを構成する。<br>
	 * O(n)
	 * 
	 * @param data 初期データ
	 */
	public FenwickTree(long[] data) {
		this(data.length);
		build(data);
	}

	/**
	 * a[p] += x を行う。<br>
	 * O(log n)
	 * 
	 * @param p 加算位置(0≦p<n)
	 * @param x 加算値
	 */
	void add(int p, long x) {
		assert 0 <= p && p < n : "p=" + p;

		p++;
		while (p <= n) {
			data[p - 1] += x;
			p += p & -p;
		}
	}

	/**
	 * a[l] + ... + a[r-1] を返す。<br>
	 * O(log n)
	 * 
	 * @param l 開始位置(含む)    (0≦l≦r≦n)
	 * @param r 終了位置(含まない)(0≦l≦r≦n)
	 */
	long sum(int l, int r) {
		assert 0 <= l && l <= r && r <= n : "l=" + l + ", r=" + r;

		return sum(r) - sum(l);
	}

	/**
	 * a[0] + ... + a[r-1] を返す。<br>
	 * O(log n)
	 * 
	 * @param r 終了位置(含まない)(0≦r≦n)
	 */
	long sum(int r) {
		assert 0 <= r && r <= n : "r=" + r;

		long s = 0;
		while (r > 0) {
			s += data[r - 1];
			r -= r & -r;
		}
		return s;
	}

	private void build(long[] dat) {
		System.arraycopy(dat, 0, data, 0, n);
		for (int i = 1; i <= n; i++) {
			int p = i + (i & -i);
			if (p <= n) {
				data[p - 1] += data[i - 1];
			}
		}
	}
}
0