結果

問題 No.2308 [Cherry 5th Tune B] もしかして、真?
ユーザー tnakao0123tnakao0123
提出日時 2023-06-23 13:11:50
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,874 bytes
コンパイル時間 458 ms
コンパイル使用メモリ 55,084 KB
実行使用メモリ 7,440 KB
最終ジャッジ日時 2023-09-13 08:05:12
合計ジャッジ時間 11,164 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
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 AC 134 ms
4,896 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 135 ms
4,908 KB
testcase_20 WA -
testcase_21 AC 146 ms
7,372 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 146 ms
7,372 KB
testcase_25 AC 146 ms
7,292 KB
testcase_26 AC 146 ms
7,376 KB
testcase_27 AC 147 ms
7,376 KB
testcase_28 AC 146 ms
7,332 KB
testcase_29 AC 146 ms
7,300 KB
testcase_30 AC 146 ms
7,300 KB
testcase_31 AC 87 ms
7,332 KB
testcase_32 AC 87 ms
7,288 KB
testcase_33 WA -
testcase_34 AC 93 ms
7,292 KB
testcase_35 WA -
testcase_36 AC 94 ms
7,292 KB
testcase_37 WA -
testcase_38 WA -
権限があれば一括ダウンロードができます

ソースコード

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), 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