結果
問題 | No.1778 括弧列クエリ / Bracketed Sequence Query |
ユーザー | eggkid6 |
提出日時 | 2022-01-21 22:40:54 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 726 ms / 2,000 ms |
コード長 | 2,996 bytes |
コンパイル時間 | 2,107 ms |
コンパイル使用メモリ | 183,860 KB |
実行使用メモリ | 35,788 KB |
最終ジャッジ日時 | 2024-11-26 01:44:43 |
合計ジャッジ時間 | 16,554 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 353 ms
5,248 KB |
testcase_01 | AC | 357 ms
5,248 KB |
testcase_02 | AC | 356 ms
5,248 KB |
testcase_03 | AC | 342 ms
5,248 KB |
testcase_04 | AC | 356 ms
5,248 KB |
testcase_05 | AC | 276 ms
12,624 KB |
testcase_06 | AC | 170 ms
5,504 KB |
testcase_07 | AC | 14 ms
7,936 KB |
testcase_08 | AC | 586 ms
26,840 KB |
testcase_09 | AC | 58 ms
11,152 KB |
testcase_10 | AC | 247 ms
21,052 KB |
testcase_11 | AC | 212 ms
15,108 KB |
testcase_12 | AC | 304 ms
12,700 KB |
testcase_13 | AC | 546 ms
26,260 KB |
testcase_14 | AC | 213 ms
19,080 KB |
testcase_15 | AC | 2 ms
5,248 KB |
testcase_16 | AC | 583 ms
26,844 KB |
testcase_17 | AC | 590 ms
26,828 KB |
testcase_18 | AC | 595 ms
26,824 KB |
testcase_19 | AC | 599 ms
26,956 KB |
testcase_20 | AC | 602 ms
26,840 KB |
testcase_21 | AC | 2 ms
5,248 KB |
testcase_22 | AC | 2 ms
5,248 KB |
testcase_23 | AC | 548 ms
18,944 KB |
testcase_24 | AC | 569 ms
23,296 KB |
testcase_25 | AC | 590 ms
27,208 KB |
testcase_26 | AC | 726 ms
35,788 KB |
testcase_27 | AC | 413 ms
35,788 KB |
ソースコード
#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); } }