結果

問題 No.1778 括弧列クエリ / Bracketed Sequence Query
ユーザー たまごんたまごん
提出日時 2021-12-08 23:26:47
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 610 ms / 2,000 ms
コード長 2,996 bytes
コンパイル時間 2,303 ms
コンパイル使用メモリ 183,732 KB
実行使用メモリ 35,788 KB
最終ジャッジ日時 2024-07-16 10:44:26
合計ジャッジ時間 13,945 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 344 ms
6,812 KB
testcase_01 AC 344 ms
6,940 KB
testcase_02 AC 345 ms
6,940 KB
testcase_03 AC 334 ms
6,940 KB
testcase_04 AC 339 ms
6,944 KB
testcase_05 AC 255 ms
12,624 KB
testcase_06 AC 163 ms
6,944 KB
testcase_07 AC 12 ms
8,064 KB
testcase_08 AC 497 ms
26,840 KB
testcase_09 AC 49 ms
11,284 KB
testcase_10 AC 216 ms
20,992 KB
testcase_11 AC 190 ms
15,188 KB
testcase_12 AC 273 ms
12,700 KB
testcase_13 AC 483 ms
26,132 KB
testcase_14 AC 205 ms
19,204 KB
testcase_15 AC 2 ms
6,940 KB
testcase_16 AC 514 ms
26,908 KB
testcase_17 AC 512 ms
26,956 KB
testcase_18 AC 508 ms
26,768 KB
testcase_19 AC 503 ms
26,952 KB
testcase_20 AC 528 ms
26,840 KB
testcase_21 AC 2 ms
6,944 KB
testcase_22 AC 2 ms
6,940 KB
testcase_23 AC 501 ms
19,072 KB
testcase_24 AC 530 ms
23,292 KB
testcase_25 AC 519 ms
27,084 KB
testcase_26 AC 610 ms
35,788 KB
testcase_27 AC 396 ms
35,788 KB
権限があれば一括ダウンロードができます

ソースコード

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};

VV G;
string s;
V p;
VP vp;

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(ll &cnt, ll &v) {
  ll now = v++;
  p[cnt-1] = now;
  vp[now].fi = cnt;
  while(s[cnt++] == '(') {
    G[now].pb(v);
    G[v].pb(now);
    f(cnt, v);
  }
  p[cnt-1] = now;
  vp[now].se = cnt;
}

int main() {
  ll n, q;
  cin >> n >> q >> s;
  G.resize(n/2);
  p.resize(n);
  vp.resize(n/2);
  ll cnt = 0, v = 0;
  while(cnt++ < n) f(cnt, v);
  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