結果

問題 No.2308 [Cherry 5th Tune B] もしかして、真?
ユーザー tnakao0123tnakao0123
提出日時 2023-06-23 13:45:49
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 161 ms / 2,000 ms
コード長 2,865 bytes
コンパイル時間 476 ms
コンパイル使用メモリ 54,752 KB
実行使用メモリ 7,396 KB
最終ジャッジ日時 2023-09-13 08:40:12
合計ジャッジ時間 9,221 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 122 ms
4,376 KB
testcase_02 AC 138 ms
5,176 KB
testcase_03 AC 129 ms
5,324 KB
testcase_04 AC 130 ms
5,356 KB
testcase_05 AC 129 ms
5,812 KB
testcase_06 AC 135 ms
6,452 KB
testcase_07 AC 137 ms
5,964 KB
testcase_08 AC 141 ms
6,632 KB
testcase_09 AC 141 ms
6,572 KB
testcase_10 AC 131 ms
4,996 KB
testcase_11 AC 136 ms
5,036 KB
testcase_12 AC 137 ms
5,000 KB
testcase_13 AC 136 ms
4,996 KB
testcase_14 AC 138 ms
4,948 KB
testcase_15 AC 137 ms
5,012 KB
testcase_16 AC 138 ms
4,960 KB
testcase_17 AC 137 ms
4,956 KB
testcase_18 AC 137 ms
5,036 KB
testcase_19 AC 137 ms
5,044 KB
testcase_20 AC 137 ms
4,992 KB
testcase_21 AC 154 ms
7,372 KB
testcase_22 AC 160 ms
7,372 KB
testcase_23 AC 154 ms
7,328 KB
testcase_24 AC 153 ms
7,312 KB
testcase_25 AC 152 ms
7,396 KB
testcase_26 AC 153 ms
7,360 KB
testcase_27 AC 154 ms
7,368 KB
testcase_28 AC 155 ms
7,324 KB
testcase_29 AC 156 ms
7,312 KB
testcase_30 AC 161 ms
7,308 KB
testcase_31 AC 89 ms
7,388 KB
testcase_32 AC 88 ms
7,388 KB
testcase_33 AC 88 ms
7,328 KB
testcase_34 AC 95 ms
7,316 KB
testcase_35 AC 95 ms
7,392 KB
testcase_36 AC 95 ms
7,324 KB
testcase_37 AC 7 ms
4,376 KB
testcase_38 AC 138 ms
4,952 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 2308.cc:  No.2308 [Cherry 5th Tune B] もしかして、真? - yukicoder
 */

#include<cstdio>
#include<vector>
#include<algorithm>
 
using namespace std;

/* constant */

const int MAX_N = 200000;

enum { AND, OR, XOR, IMP };

/* typedef */

struct UFT {
  vector<int> links, ranks, sizes;
  UFT() {}

  void init(int n) {
    links.resize(n);
    for (int i = 0; i < n; i++) links[i] = i;
    ranks.assign(n, 1);
    sizes.assign(n, 1);
  }

  int root(int i) {
    int i0 = i;
    while (links[i0] != i0) i0 = links[i0];
    return (links[i] = i0);
  }

  int rank(int i) { return ranks[root(i)]; }
  int size(int i) { return sizes[root(i)]; }
  bool same(int i, int j) { return root(i) == root(j); }

  int merge(int i0, int i1) {
    int r0 = root(i0), r1 = root(i1), mr;
    if (r0 == r1) return r0;
    if (ranks[r0] == ranks[r1]) {
      links[r1] = r0;
      sizes[r0] += sizes[r1];
      ranks[r0]++;
      mr = r0;
    }
    else if (ranks[r0] > ranks[r1]) {
      links[r1] = r0;
      sizes[r0] += sizes[r1];
      mr = r0;
    }
    else {
      links[r0] = r1;
      sizes[r1] += sizes[r0];
      mr = r1;
    }
    return mr;
  }
};

template <typename T>
struct BIT {
  int n;
  vector<T> bits;
  
  BIT() {}
  BIT(int _n) { init(_n); }

  void init(int _n) {
    n = _n;
    bits.assign(n + 1, 0);
  }

  T sum(int x) {
    x = min(x, n);
    T s = 0;
    while (x > 0) {
      s += bits[x];
      x -= (x & -x);
    }
    return s;
  }

  void add(int x, T v) {
    if (x <= 0) return;
    while (x <= n) {
      bits[x] += v;
      x += (x & -x);
    }
  }
};

/* global variables */

bool fs[MAX_N];
int ops[MAX_N], ss[MAX_N];

/* subroutines */

bool func(int op, bool a, bool b) {
  switch (op) {
  case AND: return a && b;
  case OR:  return a || b;
  case XOR: return a ^ b;
  case IMP: return ! a || b;
  }
  return false;
}

/* main */

int main() {
  int tn;
  scanf("%d", &tn);

  while (tn--) {
    int n;
    scanf("%d", &n);

    for (int i = 0; i < n; i++) {
      char s[8];
      scanf("%s", s);
      fs[i] = (s[0] == 'T');
    }

    for (int i = 0; i < n - 1; i++) {
      char s[8];
      scanf("%s", s);
      ops[i] =
	(s[0] == 'a') ? AND : (s[0] == 'o') ? OR : (s[0] == 'x') ? XOR : IMP;
    }

    for (int i = 0; i < n - 1; i++) scanf("%d", ss + i);

    UFT uft; uft.init(n);
    BIT<int> bit; bit.init(n - 1);
    for (int i = 0; i < n - 1; i++) bit.add(i + 1, 1);

    for (int i = 0; i < n - 1; i++) {
      int x0 = -1, x1 = n - 1;
      while (x0 + 1 < x1) {
	int x = (x0 + x1) / 2;
	if (bit.sum(x + 1) >= ss[i]) x1 = x;
	else x0 = x;
      }

      int u = uft.root(x1), v = uft.root(x1 + 1);
      int r = uft.merge(u, v);
      fs[r] = func(ops[x1], fs[u], fs[v]);

      bit.add(x1 + 1, -1);
    }

    if (fs[uft.root(0)]) puts("True");
    else puts("False");
  }
  return 0;
}
0