結果

問題 No.1778 括弧列クエリ / Bracketed Sequence Query
ユーザー milanis48663220milanis48663220
提出日時 2021-12-29 21:02:48
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 694 ms / 2,000 ms
コード長 4,112 bytes
コンパイル時間 1,673 ms
コンパイル使用メモリ 134,452 KB
実行使用メモリ 32,168 KB
最終ジャッジ日時 2024-04-15 02:53:38
合計ジャッジ時間 15,111 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 320 ms
6,816 KB
testcase_01 AC 317 ms
6,940 KB
testcase_02 AC 316 ms
6,940 KB
testcase_03 AC 312 ms
6,944 KB
testcase_04 AC 320 ms
6,944 KB
testcase_05 AC 244 ms
14,464 KB
testcase_06 AC 145 ms
6,944 KB
testcase_07 AC 16 ms
8,960 KB
testcase_08 AC 508 ms
30,848 KB
testcase_09 AC 54 ms
12,672 KB
testcase_10 AC 226 ms
24,064 KB
testcase_11 AC 185 ms
17,408 KB
testcase_12 AC 265 ms
14,464 KB
testcase_13 AC 504 ms
29,952 KB
testcase_14 AC 187 ms
21,840 KB
testcase_15 AC 2 ms
6,940 KB
testcase_16 AC 529 ms
30,920 KB
testcase_17 AC 506 ms
30,736 KB
testcase_18 AC 511 ms
30,792 KB
testcase_19 AC 518 ms
30,920 KB
testcase_20 AC 523 ms
30,920 KB
testcase_21 AC 2 ms
6,940 KB
testcase_22 AC 2 ms
6,944 KB
testcase_23 AC 493 ms
26,048 KB
testcase_24 AC 537 ms
31,916 KB
testcase_25 AC 560 ms
32,168 KB
testcase_26 AC 694 ms
32,012 KB
testcase_27 AC 362 ms
32,140 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>
#include <stack>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

template<typename T>
vector<vector<T>> vec2d(int n, int m, T v){
    return vector<vector<T>>(n, vector<T>(m, v));
}

template<typename T>
vector<vector<vector<T>>> vec3d(int n, int m, int k, T v){
    return vector<vector<vector<T>>>(n, vector<vector<T>>(m, vector<T>(k, v)));
}

template<typename T>
void print_vector(vector<T> v, char delimiter=' '){
    if(v.empty()) {
        cout << endl;
        return;
    }
    for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter;
    cout << v.back() << endl;
}

class Lca{
    public:
        vector<int> depth;
        vector<vector<int>> parent;
        vector<vector<int>> G;
        int root;
        int n;
        const int N_LOG_MAX = 25;
        bool initialized = false;

        Lca(vector<vector<int>> g, int _root){
            n = g.size();
            G = g;
            root = _root;
            parent = vector<vector<int>>(N_LOG_MAX, vector<int>(n));
            depth = vector<int>(n, 0);
        }

        void init(){
            initialized = true;
            dfs(root, -1, 0);
            for(int i = 0; i < N_LOG_MAX-1; i++){
                for(int v = 0; v < n; v++){
                    if(parent[i][v] < 0) parent[i+1][v] = -1;
                    else parent[i+1][v] = parent[i][parent[i][v]];
                }
            }
        }

        int lca(int u, int v){
            assert(initialized);
            if(depth[u] > depth[v]) swap(u, v);
            for(int i = 0; i < N_LOG_MAX; i++){
                if((depth[v] - depth[u]) >> i & 1){
                    v = parent[i][v];
                }
            }
            if(u == v) return u;
            for(int i = N_LOG_MAX-1; i >= 0; i--){
                if(parent[i][u] != parent[i][v]) {
                    u = parent[i][u];
                    v = parent[i][v];
                }
            }
            return parent[0][u];
        }

        int dist(int u, int v){
            int l = lca(u, v);
            return depth[u]+depth[v]-2*depth[l];
        }

    private:
        void dfs(int v, int p, int d){
            parent[0][v] = p;
            depth[v] = d;
            for(int i = 0; i < G[v].size(); i++){
                if(G[v][i] != p) dfs(G[v][i], v, d+1); 
            }
        }
};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int n, q; cin >> n >> q;
    int m = n/2;
    string s; cin >> s;
    vector<int> l(m+1), r(m+1);
    vector<vector<int>> g(m+1);
    stack<int> st;
    st.push(0);
    int idx = 1;
    for(int i = 0; i < n; i++){
        if(s[i] == '('){
            int par = st.top();
            st.push(idx);
            l[idx] = i;
            g[par].push_back(idx);
            g[idx].push_back(par);
            idx++;
        }else{
            r[st.top()] = i;
            st.pop();
        }
    }
    assert(st.size() == 1);
    auto lca = Lca(g, 0);
    lca.init();
    vector<int> l_inv(n, -1);
    vector<int> r_inv(n, -1);
    for(int i = 1; i <= m; i++) {
        l_inv[l[i]] = i;
        r_inv[r[i]] = i;
    }
    while(q--){
        int x, y; cin >> x >> y; x--; y--;
        int lx = max(l_inv[x], r_inv[x]);
        int ly = max(l_inv[y], r_inv[y]);
        int la = lca.lca(lx, ly);
        // cout << lx << ':' << ly << endl;
        if(la == 0) {
            cout << -1 << endl;
        }else{
            cout << l[la]+1 << ' ' << r[la]+1 << endl;
        }
    }
}
0