結果

問題 No.1778 括弧列クエリ / Bracketed Sequence Query
ユーザー KudeKude
提出日時 2021-12-07 06:08:45
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 115 ms / 2,000 ms
コード長 3,116 bytes
コンパイル時間 4,081 ms
コンパイル使用メモリ 227,476 KB
実行使用メモリ 18,444 KB
最終ジャッジ日時 2023-09-21 16:35:35
合計ジャッジ時間 8,952 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 51 ms
4,376 KB
testcase_01 AC 52 ms
4,380 KB
testcase_02 AC 51 ms
4,376 KB
testcase_03 AC 45 ms
4,376 KB
testcase_04 AC 58 ms
4,376 KB
testcase_05 AC 53 ms
8,488 KB
testcase_06 AC 30 ms
4,376 KB
testcase_07 AC 5 ms
5,696 KB
testcase_08 AC 112 ms
17,208 KB
testcase_09 AC 14 ms
7,848 KB
testcase_10 AC 49 ms
13,852 KB
testcase_11 AC 42 ms
10,040 KB
testcase_12 AC 58 ms
8,768 KB
testcase_13 AC 103 ms
16,900 KB
testcase_14 AC 46 ms
12,676 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 110 ms
17,276 KB
testcase_17 AC 107 ms
17,188 KB
testcase_18 AC 114 ms
17,204 KB
testcase_19 AC 109 ms
17,156 KB
testcase_20 AC 115 ms
17,216 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 87 ms
14,468 KB
testcase_24 AC 102 ms
17,376 KB
testcase_25 AC 113 ms
17,180 KB
testcase_26 AC 78 ms
18,444 KB
testcase_27 AC 66 ms
18,256 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
namespace {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-function"
#include<atcoder/all>
#pragma GCC diagnostic pop
using namespace std;
using namespace atcoder;
#define rep(i,n)for (int i = 0; i < int(n); ++i)
#define rrep(i,n)for (int i = int(n)-1; i >= 0; --i)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
template<class T> void chmax(T& a, const T& b) { a = max(a, b); }
template<class T> void chmin(T& a, const T& b) { a = min(a, b); }
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;

struct doubling {
  const int n;
  int k;
  vector<vector<int>> to;
  doubling(int n) : n(n), k(-1), to(1, vector<int>(n + 1, n)) {}
  void set_dest(int u, int v) {
    // u -> v
    to[0][u] = v;
  }
  void build() { build(n); }
  template<class T>
  void build(T d) {
    // move by d will be accepted later
    // k := floor_lg(d)
    k = 1;
    while ((T(1) << (k + 1)) <= d) k++;
    // prepare data area
    to.reserve(k);
    for (int i = to.size(); i < k; i++) {
      to.emplace_back(n + 1);
    }
    // construct doubling table
    for (int i = 1; i < k; i++) {
      for (int u = 0; u <= n; u++) {
        to[i][u] = to[i-1][to[i-1][u]];
      }
    }
  }
  template<class T>
  int move(int v, T d) {
    assert(k > 0);
    // returns -1 if move from v reaches endpoint before d step
    assert(d >= 0);
    for (int l = 0; l < k && d > 0; l++, d >>= 1) {
      if (d & 1) v = to[l][v];
    }
    assert(d == 0);
    if (v == n) v = -1;
    return v;
  }
  template <class F>
  int find_first_true(int v, F f) {
    assert(k > 0);
    // returns -1 if not found
    if (f(v)) return v;
    // f(v) keeps false below
    int res = -1;
    for (int i = k - 1; i >= 0; i--) {
      int w = to[i][v];
      if (w == n) continue;
      if (f(w)) res = w;
      else v = w;
    }
    return res;
  }
  template <class F>
  int find_last_true(int v, F f) {
    assert(k > 0);
    // returns -1 if f is already false at v
    if (!f(v)) return -1;
    // f(v) keeps true below
    for (int i = k - 1; i >= 0; i--) {
      int w = to[i][v];
      if (w == n) continue;
      if (f(w)) v = w;
    }
    return v;
  }
};

} int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, q;
  cin >> n >> q;
  string s;
  cin >> s;
  VI paired_pos(n, -1);
  VI st;
  doubling d(n);
  rep(i, n) {
    if (s[i] == '(') {
      if (!st.empty()) {
        d.set_dest(i, st.back());
      }
      st.push_back(i);
    } else {
      int j = st.back(); st.pop_back();
      paired_pos[j] = i;
      paired_pos[i] = j;
    }
  }
  d.build();
  while(q--) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    chmin(x, paired_pos[x]);
    chmin(y, paired_pos[y]);
    int rmx = max(paired_pos[x], paired_pos[y]);
    int lmn = min(x, y);
    int i = d.find_first_true(lmn, [&](int i) {
      return paired_pos[i] >= rmx;
    });
    if (i == -1) {
      cout << -1 << '\n';
    } else {
      cout << i + 1 << ' ' << paired_pos[i] + 1 << '\n';
    }
  }
}
0