結果

問題 No.1778 括弧列クエリ / Bracketed Sequence Query
ユーザー eggkid6eggkid6
提出日時 2022-01-21 22:36:10
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 3,406 bytes
コンパイル時間 1,688 ms
コンパイル使用メモリ 177,100 KB
実行使用メモリ 28,544 KB
最終ジャッジ日時 2024-11-26 01:35:23
合計ジャッジ時間 10,347 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using V = vector<ll>;
using VV = vector<V>;
using VVV = vector<VV>;
using VS = vector<string>;
using VB = vector<bool>;
using VVB = vector<VB>;
using P = pair<ll,ll>;
using VP = vector<P>;
using M = map<ll,ll>;
using Q = queue<ll>;
using PQ = priority_queue<ll>;
using PQG = priority_queue<ll,V,greater<ll>>;
using S = set<ll>;
const ll MOD = 1000000007;
const ll mod = 998244353;
const ll INF = 1LL << 60;
#define rep(i,n) for(ll i = 0; i < n; i++)
#define rep2(i,s,n) for(ll i = s; i < n; i++)
#define per(i,n) for(ll i = n; i >= 0; i--)
#define per2(i,s,n) for(ll i = n; i >= s; i--)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define fi first
#define se second
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
template<class T>bool chmin(T&a, const T&b){if(a>b){a=b;return 1;}return 0;}
template<class T>bool chmax(T&a, const T&b){if(a<b){a=b;return 1;}return 0;}
template<class T>void Vin(vector<T>&a){rep(i,(ll)a.size())cin>>a[i];}
template<class T>void VVin(vector<vector<T>>&a){rep(i,(ll)a.size())rep(j,(ll)a[i].size())cin>>a[i][j];}
template<class T>void Vout(const vector<T>&a){rep(i,(int)a.size())cout<<a[i]<<endl;}
ll power(ll a,ll b,const ll&M){a%=M;ll res=1;while(b>0){if(b&1)res=res*a%M;a=a*a%M;b>>=1;}return res;}
const ll H[4] = {0,1,0,-1}, W[4] = {1,0,-1,0};

struct LCA {
    VV par;
    V dis;
    LCA() {}
    LCA(const VV &G, ll root = 0) {
        ll v = G.size(), k = 0;
        while(v) {
            v >>= 1;
            k++;
            par.assign(k,V(v,-1));
            dfs(G, root, -1, 0);
            rep(i,k-1) rep(j,v) {
                if(par[i][j] < 0) par[i+1][j] = -1;
                else par[i+1][j] = par[i][par[i][j]];
            }
        }
    }

    void dfs(const VV &G, ll v, ll p, ll d) {
        par[0][v] = p;
        dis[v] = d;
        for(ll nv : G[v]) if(nv != p) dfs(G, nv, v, d+1);
    }

    ll lca(ll u, ll v) {
        if(dis[u] < dis[v]) swap(u,v);
        ll k = par.size();
        rep(i,k) if(dis[u]-dis[v] & 1<<i) u = par[i][u];
        if(u == v) return u;
        per(i,k) if(par[i][u] != par[i][v]) {
            u = par[i][u];
            v = par[i][v];
        }
        return par[0][u];
    }
};

struct UnionFind {
    vector<long long> par, rank;
    UnionFind() {}
    UnionFind(long long n) : par(n), rank(n) {
        for(long long i = 0; i < n; i++) par[i] = i, rank[i] = 0;
    }
    long long root(long long v) {
        return par[v] == v ? v : par[v] = root(par[v]);
    }
    bool unite(long long sv, long long tv) {
        sv = root(sv);
        tv = root(tv);
        if(sv == tv) return 0;
        if(rank[sv] > rank[tv]) swap(sv,tv);
        par[sv] = tv;
        if(rank[sv] == rank[tv]) rank[tv]++;
        return 1;
    }
    bool same(long long sv, long long tv) {
        return root(sv) == root(tv);
    }
};

void f(VV &G, string &s, ll &cnt, ll &v, UnionFind &uf) {
    ll now = v;
    while(s[cnt++] == '(') {
        uf.unite(now,++v);
        G[now].pb(v);
        G[v].pb(now);
        f(G, s, cnt, v, uf);
    }
}

int main() {
    ll n, q;
    string s;
    cin >> n >> q >> s;
    UnionFind uf(n/2);
    VV G(n/2);
    ll cnt = 0, v = 0;
    while(cnt < n) f(G, s, ++cnt, v, uf);
    LCA l(G);
}
0