結果

問題 No.1778 括弧列クエリ / Bracketed Sequence Query
ユーザー たまごんたまごん
提出日時 2021-12-08 23:09:29
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 3,023 bytes
コンパイル時間 3,622 ms
コンパイル使用メモリ 179,036 KB
実行使用メモリ 66,224 KB
最終ジャッジ日時 2023-09-23 11:03:20
合計ジャッジ時間 12,312 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 355 ms
8,756 KB
testcase_01 AC 361 ms
4,380 KB
testcase_02 AC 352 ms
4,376 KB
testcase_03 AC 340 ms
4,376 KB
testcase_04 AC 360 ms
4,380 KB
testcase_05 AC 756 ms
19,816 KB
testcase_06 AC 182 ms
6,032 KB
testcase_07 AC 109 ms
9,320 KB
testcase_08 TLE -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
権限があれば一括ダウンロードができます

ソースコード

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-1; i >= 0; i--)
#define per2(i,s,n) for(ll i = n-1; 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(const VV &G) {
        ll v = G.size(), k = 1;
        while((1<<k) < v-1) k++;
        par.assign(k,V(v,-1));
        dis.resize(v);
        VB seen(v);
        rep(i,v) if(!seen[i]) dfs(G, i, -1, 0, seen);
        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, VB &seen) {
        seen[v] = true;
        par[0][v] = p;
        dis[v] = d;
        for(ll nv : G[v]) if(nv != p) dfs(G, nv, v, d+1, seen);
    }
    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];
    }
};

void f(VV &G, string s, ll &cnt, ll &v, V &p, VP &vp) {
  ll now = v++;
  p[cnt-1] = now;
  vp[now].fi = cnt;
  while(s[cnt++] == '(') {
    G[now].pb(v);
    G[v].pb(now);
    f(G, s, cnt, v, p, vp);
  }
  p[cnt-1] = now;
  vp[now].se = cnt;
}

int main() {
  ll n, q;
  string s;
  cin >> n >> q >> s;
  VV G(n/2);
  V p(n);
  VP vp(n/2);
  ll cnt = 0, v = 0;
  while(cnt++ < n) f(G, s, cnt, v, p, vp);
  LCA l(G);
  rep(i,q) {
    ll x, y;
    cin >> x >> y;
    x = p[x-1];
    y = p[y-1];
    ll ans = l.lca(x,y);
    if(ans == -1) cout << -1 << endl;
    else printf("%lld %lld\n",vp[ans].fi,vp[ans].se);
  }
}
0