結果

問題 No.2398 ヒドラ崩し
ユーザー SSRSSSRS
提出日時 2023-07-28 22:34:22
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 17 ms / 2,000 ms
コード長 1,784 bytes
コンパイル時間 2,032 ms
コンパイル使用メモリ 170,344 KB
実行使用メモリ 10,320 KB
最終ジャッジ日時 2024-04-16 06:31:35
合計ジャッジ時間 2,980 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 2 ms
5,376 KB
testcase_25 AC 3 ms
5,376 KB
testcase_26 AC 11 ms
10,320 KB
testcase_27 AC 12 ms
5,376 KB
testcase_28 AC 17 ms
9,364 KB
testcase_29 AC 17 ms
5,504 KB
testcase_30 AC 12 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000;
struct segment_tree{
  int N;
  vector<int> ST;
  segment_tree(vector<int> A){
    int N2 = A.size();
    N = 1;
    while (N < N2){
      N *= 2;
    }
    ST = vector<int>(N * 2 - 1, INF);
    for (int i = 0; i < N2; i++){
      ST[N - 1 + i] = A[i];
    }
    for (int i = N - 2; i >= 0; i--){
      ST[i] = min(ST[i * 2 + 1], ST[i * 2 + 2]);
    }
  }
  int range_min(int L, int R, int i, int l, int r){
    if (r <= L || R <= l){
      return INF;
    } else if (L <= l && r <= R){
      return ST[i];
    } else {
      int m = (l + r) / 2;
      return min(range_min(L, R, i * 2 + 1, l, m), range_min(L, R, i * 2 + 2, m, r));
    }
  }
  int range_min(int L, int R){
    return range_min(L, R, 0, 0, N);
  }
};
int main(){
  string H;
  cin >> H;
  int N = H.size();
  auto get_finite = [&](int n){
    string ans;
    for (int i = 0; i < n; i++){
      ans += "()";
    }
    return ans;
  };
  if (H == get_finite(N / 2)){
    if (N / 2 % 2 == 0){
      cout << 1 << endl;
    } else {
      cout << 0 << endl;
    }
  } else {
    vector<int> S(N + 1);
    S[0] = 0;
    for (int i = 0; i < N; i++){
      S[i + 1] = S[i];
      if (H[i] == '('){
        S[i + 1]++;
      }
      if (H[i] == ')'){
        S[i + 1]--;
      }
    }
    segment_tree ST(S);
    auto dfs = [&](auto dfs, int L, int R) -> int {
      if (L == R){
        return 1;
      }
      if (H.substr(R - 2, 2) == "()"){
        return dfs(dfs, L, R - 2) ^ 1;
      }
      if (ST.range_min(L + 1, R) == S[L]){
        for (int i = L + 1; i < R; i++){
          if (S[i] == S[L]){
            return dfs(dfs, i, R);
          }
        }
      }
      return dfs(dfs, L + 1, R - 1);
    };
    cout << dfs(dfs, 0, N) << endl;
  }
}
0