結果

問題 No.3467 Bracket Warp
コンテスト
ユーザー GOTKAKO
提出日時 2026-03-03 01:42:40
言語 C++17
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 206 ms / 2,000 ms
コード長 2,479 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,508 ms
コンパイル使用メモリ 234,316 KB
実行使用メモリ 42,048 KB
最終ジャッジ日時 2026-03-03 01:42:48
合計ジャッジ時間 7,968 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 25
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    string s; cin >> s;
    int N = s.size();
    vector<int> P(N);
    stack<int> st; st.push(0);
    vector<vector<int>> Graph(N/2+1);
    for(int i=0,time=1; i<N; i++){
        if(s.at(i) == '(') st.push(i);
        else P.at(st.top()) = time,P.at(i) = time++,st.pop();
    }
    st.push(0);
    for(int i=0; i<N; i++){
        if(s.at(i) == '('){
            int u = st.top(),v = P.at(i);
            Graph.at(u).push_back(v);
            st.push(v);
        }
        else st.pop();
    }
    int n = N/2+1;
    vector<int> depth(N);
    vector<vector<int>> par(n,vector<int>(20,-1)),cost(n,vector<int>(20));
    {
        auto dfs = [&](auto dfs,int pos,int dep) -> void {
            depth.at(pos) = dep;
            int s = Graph.at(pos).size();
            for(int i=0; i<s; i++){
                int to = Graph.at(pos).at(i);
                par.at(to).at(0) = pos;
                cost.at(to).at(0) = min(i+1,s-i);
                dfs(dfs,to,dep+1);
            }
        };
        dfs(dfs,0,0);
        for(int d=1; d<20; d++) for(int i=0; i<n; i++){
            int p = par.at(i).at(d-1);
            if(p == -1) continue;
            par.at(i).at(d) = par.at(p).at(d-1);
            cost.at(i).at(d) = cost.at(i).at(d-1)+cost.at(p).at(d-1);
        }
    }
    vector<map<int,int>> M(n);
    for(int i=0; i<n; i++) for(int k=0; k<Graph.at(i).size(); k++) M.at(i)[Graph.at(i).at(k)] = k+1;
    int Q; cin >> Q;
    while(Q--){
        int u,v; cin >> u >> v,u--,v--;
        u = P.at(u),v = P.at(v);
        if(u == v){cout << "0\n"; continue;}
        if(depth.at(u) < depth.at(v)) swap(u,v);
        int answer = 0,diff = depth.at(u)-depth.at(v);
        for(int i=0; diff; i++){
            if(diff&(1<<i)){
                answer += cost.at(u).at(i);
                u = par.at(u).at(i);
                diff -= 1<<i;
            }
        }
        if(u == v){cout << answer << "\n"; continue;}
        for(int d=20; d--;){
            int nu = par.at(u).at(d),nv = par.at(v).at(d);
            if(nu == nv) continue;
            answer += cost.at(u).at(d)+cost.at(v).at(d);
            u = nu,v = nv;
        }
        int p = par.at(u).at(0),siz = Graph.at(p).size()+1;
        u = M.at(p)[u],v = M.at(p)[v];
        if(p == 0) answer += abs(v-u);
        else answer += min(abs(u-v),siz-abs(u-v));
        cout << answer << "\n";
    }
}
0