結果

問題 No.1778 括弧列クエリ / Bracketed Sequence Query
ユーザー SSRSSSRS
提出日時 2021-12-07 00:10:27
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 473 ms / 2,000 ms
コード長 2,138 bytes
コンパイル時間 2,780 ms
コンパイル使用メモリ 181,196 KB
実行使用メモリ 17,240 KB
最終ジャッジ日時 2023-09-21 16:06:23
合計ジャッジ時間 14,925 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 365 ms
4,380 KB
testcase_01 AC 367 ms
4,376 KB
testcase_02 AC 358 ms
4,376 KB
testcase_03 AC 347 ms
4,380 KB
testcase_04 AC 367 ms
4,376 KB
testcase_05 AC 243 ms
6,196 KB
testcase_06 AC 170 ms
4,376 KB
testcase_07 AC 10 ms
4,772 KB
testcase_08 AC 464 ms
11,024 KB
testcase_09 AC 48 ms
5,728 KB
testcase_10 AC 200 ms
8,928 KB
testcase_11 AC 182 ms
7,508 KB
testcase_12 AC 261 ms
6,260 KB
testcase_13 AC 431 ms
10,740 KB
testcase_14 AC 168 ms
8,424 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 461 ms
11,020 KB
testcase_17 AC 461 ms
10,964 KB
testcase_18 AC 463 ms
11,120 KB
testcase_19 AC 468 ms
11,100 KB
testcase_20 AC 452 ms
11,028 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 431 ms
8,292 KB
testcase_24 AC 438 ms
9,752 KB
testcase_25 AC 463 ms
9,740 KB
testcase_26 AC 473 ms
17,228 KB
testcase_27 AC 404 ms
17,240 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
struct lowest_common_ancestor{
    vector<int> p, sz, in, next;
    void dfs1(vector<vector<int>> &c, int v = 0){
        sz[v] = 1;
        for (int &w : c[v]){
            dfs1(c, w);
            sz[v] += sz[w];
            if (sz[w] > sz[c[v][0]]){
                swap(w, c[v][0]);
            }
        }
    }
    void dfs2(vector<vector<int>> &c, int &t, int v = 0){
        in[v] = t;
        t++;
        for (int w : c[v]){
            if (w == c[v][0]){
                next[w] = next[v];
            } else {
                next[w] = w;
            }
            dfs2(c, t, w);
        }
    }
    lowest_common_ancestor(vector<int> &p, vector<vector<int>> &c): p(p){
        int N = p.size();
        sz = vector<int>(N);
        dfs1(c);
        in = vector<int>(N);
        next = vector<int>(N, 0);
        int t = 0;
        dfs2(c, t);
    }
    int lca(int u, int v){
        while (1){
            if (in[u] > in[v]){
                swap(u, v);
            }
            if (next[u] == next[v]){
                return u;
            }
            v = p[next[v]];
        }
    }
};
int main(){
   int N, Q;
  cin >> N >> Q;
  string S;
  cin >> S;
  vector<int> a;
  vector<int> b(N / 2);
  vector<vector<int>> c(N / 2 + 1);
  stack<pair<int, int>> st;
  st.push(make_pair(-1, -1));
  for (int i = 0; i < N; i++){
    if (S[i] == '('){
      if (!st.empty()){
        c[st.top().first + 1].push_back(a.size() + 1);
      }
      st.push(make_pair(a.size(), i));
      a.push_back(i);
    }
    if (S[i] == ')'){
      b[st.top().first] = i;
      st.pop();
    }
  }
  vector<int> pos(N);
  for (int i = 0; i < N / 2; i++){
    pos[a[i]] = i + 1;
    pos[b[i]] = i + 1;
  }
  vector<int> p(N / 2 + 1, -1);
  for (int i = 0; i <= N / 2; i++){
    for (int j : c[i]){
      p[j] = i;
    }
  }
  lowest_common_ancestor T(p, c);
  for (int i = 0; i < Q; i++){
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    int r = T.lca(pos[x], pos[y]);
    if (r == 0){
      cout << -1 << endl;
    } else {
      cout << a[r - 1] + 1 << ' ' << b[r - 1] + 1 << endl;
    }
  }
}
0