結果

問題 No.1778 括弧列クエリ / Bracketed Sequence Query
ユーザー eggkid6eggkid6
提出日時 2022-01-21 22:40:54
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 641 ms / 2,000 ms
コード長 2,996 bytes
コンパイル時間 1,968 ms
コンパイル使用メモリ 181,804 KB
実行使用メモリ 32,336 KB
最終ジャッジ日時 2023-08-17 07:00:24
合計ジャッジ時間 14,777 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 354 ms
4,380 KB
testcase_01 AC 370 ms
4,380 KB
testcase_02 AC 361 ms
4,380 KB
testcase_03 AC 348 ms
4,380 KB
testcase_04 AC 370 ms
4,376 KB
testcase_05 AC 270 ms
12,356 KB
testcase_06 AC 173 ms
5,288 KB
testcase_07 AC 12 ms
7,756 KB
testcase_08 AC 525 ms
26,488 KB
testcase_09 AC 55 ms
11,064 KB
testcase_10 AC 235 ms
20,752 KB
testcase_11 AC 199 ms
14,700 KB
testcase_12 AC 286 ms
12,640 KB
testcase_13 AC 504 ms
25,852 KB
testcase_14 AC 199 ms
18,892 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 531 ms
26,676 KB
testcase_17 AC 540 ms
26,936 KB
testcase_18 AC 539 ms
26,608 KB
testcase_19 AC 538 ms
26,664 KB
testcase_20 AC 545 ms
26,688 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 505 ms
18,728 KB
testcase_24 AC 523 ms
23,276 KB
testcase_25 AC 559 ms
27,192 KB
testcase_26 AC 641 ms
32,336 KB
testcase_27 AC 414 ms
32,320 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