結果
問題 | No.1778 括弧列クエリ / Bracketed Sequence Query |
ユーザー | miscalc |
提出日時 | 2021-12-07 00:37:36 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 659 ms / 2,000 ms |
コード長 | 3,365 bytes |
コンパイル時間 | 2,135 ms |
コンパイル使用メモリ | 216,124 KB |
実行使用メモリ | 37,316 KB |
最終ジャッジ日時 | 2024-07-07 10:00:57 |
合計ジャッジ時間 | 14,671 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 324 ms
5,248 KB |
testcase_01 | AC | 324 ms
5,376 KB |
testcase_02 | AC | 327 ms
5,376 KB |
testcase_03 | AC | 308 ms
5,376 KB |
testcase_04 | AC | 337 ms
5,376 KB |
testcase_05 | AC | 270 ms
13,520 KB |
testcase_06 | AC | 163 ms
5,500 KB |
testcase_07 | AC | 14 ms
8,448 KB |
testcase_08 | AC | 543 ms
29,228 KB |
testcase_09 | AC | 56 ms
12,008 KB |
testcase_10 | AC | 238 ms
23,040 KB |
testcase_11 | AC | 197 ms
16,224 KB |
testcase_12 | AC | 285 ms
13,652 KB |
testcase_13 | AC | 516 ms
28,540 KB |
testcase_14 | AC | 206 ms
20,972 KB |
testcase_15 | AC | 2 ms
6,940 KB |
testcase_16 | AC | 547 ms
29,356 KB |
testcase_17 | AC | 545 ms
29,356 KB |
testcase_18 | AC | 550 ms
29,236 KB |
testcase_19 | AC | 557 ms
29,360 KB |
testcase_20 | AC | 566 ms
29,232 KB |
testcase_21 | AC | 2 ms
5,376 KB |
testcase_22 | AC | 1 ms
5,376 KB |
testcase_23 | AC | 585 ms
24,764 KB |
testcase_24 | AC | 625 ms
30,256 KB |
testcase_25 | AC | 659 ms
30,224 KB |
testcase_26 | AC | 517 ms
37,316 KB |
testcase_27 | AC | 383 ms
37,312 KB |
ソースコード
#include <bits/stdc++.h> #include <atcoder/modint> using namespace std; using namespace atcoder; //using mint = modint998244353; using mint = modint1000000007; using ll = long long; using ld = long double; using pll = pair<ll, ll>; using tlll = tuple<ll, ll, ll>; constexpr ll INF = 1LL << 60; template<class T> bool chmin(T& a, T b) {if (a > b) {a = b; return true;} return false;} template<class T> bool chmax(T& a, T b) {if (a < b) {a = b; return true;} return false;} ll safemod(ll A, ll M) {return (A % M + M) % M;} ll divfloor(ll A, ll B) {if (B < 0) {return divfloor(-A, -B);} return (A - safemod(A, B)) / B;} ll divceil(ll A, ll B) {if (B < 0) {return divceil(-A, -B);} return divfloor(A + B - 1, B);} #define FINALANS(A) do {cout << (A) << '\n'; exit(0);} while (false) class lca { public: ll N, K; vector<vector<ll>> par; vector<ll> dist; vector<ll> v_dfs; vector<ll> i_dfs; void dfs(vector<vector<ll>> &G, ll v, ll pv) { par.at(0).at(v) = pv; dist.at(v) = (pv == -1 ? 0 : dist.at(pv) + 1); v_dfs.push_back(v); for (auto nv : G.at(v)) { if (nv != pv) dfs(G, nv, v); } } lca(vector<vector<ll>> &G, ll root) { N = G.size(); K = 1; ll tmp = 1; while (tmp < N) { tmp *= 2; K++; } par = vector<vector<ll>>(K, vector<ll>(N, -1)); dist = vector<ll>(N, -1); dfs(G, root, -1); for (ll k = 0; k < K - 1; k++) { for (ll v = 0; v < N; v++) { if (par.at(k).at(v) == -1) par.at(k + 1).at(v) = -1; else par.at(k + 1).at(v) = par.at(k).at(par.at(k).at(v)); } } i_dfs.resize(N); for (ll i = 0; i < N; i++) { i_dfs.at(v_dfs.at(i)) = i; } } ll query_ancestor(ll v, ll n) { for (ll k = K - 1; k >= 0; k--) { if (n & (1 << k)) v = par.at(k).at(v); if (v == -1) return -1; } return v; } ll query_lca(ll u, ll v) { if (dist.at(u) < dist.at(v)) swap(u, v); u = query_ancestor(u, dist.at(u) - dist.at(v)); if (u == v) return u; for (ll k = K - 1; k >= 0; k--) { if (par.at(k).at(u) != par.at(k).at(v)) u = par.at(k).at(u), v = par.at(k).at(v); } return par.at(0).at(u); } ll query_dist(ll u, ll v) { return dist.at(u) + dist.at(v) - 2 * dist.at(query_lca(u, v)); } }; int main() { ll N, Q; cin >> N >> Q; string S; cin >> S; vector<pll> A; vector<ll> st; vector<ll> mpA(N); for (ll i = 0; i < N; i++) { if (S.at(i) == '(') st.push_back(i); else { mpA.at(st.back()) = A.size(); mpA.at(i) = A.size(); A.push_back(make_pair(st.back(), i)); st.pop_back(); } } A.push_back(make_pair(-1, -1)); vector<vector<ll>> G(N / 2 + 1); vector<ll> st2; for (ll i = 0; i < N / 2 + 1; i++) { while (!st2.empty() && A.at(st2.back()).first > A.at(i).first) { G.at(st2.back()).push_back(i); G.at(i).push_back(st2.back()); st2.pop_back(); } st2.push_back(i); } lca lc(G, N / 2); for (ll q = 0; q < Q; q++) { ll x, y; cin >> x >> y; x--, y--; ll v = lc.query_lca(mpA.at(x), mpA.at(y)); auto [ans1, ans2] = A.at(v); if (ans1 == -1) cout << -1 << '\n'; else cout << ans1 + 1 << " " << ans2 + 1 << '\n'; } }