結果

問題 No.1778 括弧列クエリ / Bracketed Sequence Query
ユーザー hourenhouren
提出日時 2021-12-08 09:40:34
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 668 ms / 2,000 ms
コード長 2,180 bytes
コンパイル時間 2,526 ms
コンパイル使用メモリ 185,056 KB
実行使用メモリ 26,680 KB
最終ジャッジ日時 2023-09-23 01:16:48
合計ジャッジ時間 18,823 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 368 ms
4,376 KB
testcase_01 AC 368 ms
4,376 KB
testcase_02 AC 370 ms
4,376 KB
testcase_03 AC 348 ms
4,384 KB
testcase_04 AC 373 ms
4,380 KB
testcase_05 AC 267 ms
11,332 KB
testcase_06 AC 171 ms
4,964 KB
testcase_07 AC 13 ms
7,016 KB
testcase_08 AC 545 ms
23,516 KB
testcase_09 AC 56 ms
10,032 KB
testcase_10 AC 235 ms
18,660 KB
testcase_11 AC 208 ms
13,396 KB
testcase_12 AC 295 ms
11,264 KB
testcase_13 AC 508 ms
23,004 KB
testcase_14 AC 207 ms
16,932 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 558 ms
23,524 KB
testcase_17 AC 569 ms
23,536 KB
testcase_18 AC 565 ms
23,620 KB
testcase_19 AC 558 ms
23,544 KB
testcase_20 AC 560 ms
23,480 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 513 ms
19,844 KB
testcase_24 AC 525 ms
24,500 KB
testcase_25 AC 550 ms
24,284 KB
testcase_26 AC 668 ms
26,520 KB
testcase_27 AC 433 ms
26,680 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int,int>;
#define rep(i, n) for(int i = 0; i < n; i++)
#define all(x) (x).begin(),(x).end()
struct Edge{ ll to; };
using G = vector<vector<Edge>>;
struct LCA{
    vector<vector<int>> par;
    vector<int> dist;
    LCA(const G &g, int root = 0) { init(g, root); }

    void init(const G g, int root){
        int n = g.size(), k = 1;
        while((1 << k) < n) k++;
        par.assign(k,vector<int>(n,-1));
        dist.assign(n,-1);
        dfs(g, root, -1, 0);
        rep(i,k-1){
            rep(j,n){
                if(par[i][j] < 0) par[i+1][j] = -1;
                else par[i+1][j] = par[i][par[i][j]];
            }
        }
    }
    void dfs(const G &g, int now, int p, int d){
        par[0][now] = p;
        dist[now] = d;
        for(auto x : g[now]){
            if(x.to != p) dfs(g, x.to, now, d+1);
        }
    }
    int query(int u, int v){
        if(dist[u] < dist[v]) swap(u, v);
        int k = par.size();
        rep(i,k){
            if((dist[u]-dist[v]) >> i & 1) u = par[i][u];
        }
        if(u==v) return u;
        for(int i=k-1;i>=0;i--){
            if(par[i][u] != par[i][v]){
                u = par[i][u];
                v = par[i][v];
            }
        }
        return par[0][u];
    }
};
int main(){
    int n,q;
    string s;
    cin >> n >> q >> s;
    stack<P> sta;
    sta.push({0,-1});
    G g(n/2+1);
    vector<int> num(n);
    vector<P> numinv(n/2+1);
    int now = 0;
    rep(i,n){
        if(s[i] == '('){
            now++;
            num[i] = now;
            Edge e1, e2;
            e1.to = sta.top().first; e2.to = now;
            g[e1.to].push_back(e2);
            g[e2.to].push_back(e1);
            sta.push({now,i});
        }else{
            num[i] = sta.top().first;
            numinv[sta.top().first] = {sta.top().second, i};
            sta.pop();
        }
    }
    LCA lca(g,0);
    rep(i,q){
        int u,v;
        cin >> u >> v;
        auto x = lca.query(num[u-1],num[v-1]);
        if(!x) cout << -1 << endl;
        else cout << numinv[x].first+1 << " " << numinv[x].second+1 << endl;
    }
    return 0;
}
0