結果
問題 | No.1778 括弧列クエリ / Bracketed Sequence Query |
ユーザー | 👑 emthrm |
提出日時 | 2021-12-07 00:14:18 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 182 ms / 2,000 ms |
コード長 | 3,344 bytes |
コンパイル時間 | 2,685 ms |
コンパイル使用メモリ | 221,692 KB |
実行使用メモリ | 26,948 KB |
最終ジャッジ日時 | 2024-07-07 09:55:46 |
合計ジャッジ時間 | 7,601 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 56 ms
5,248 KB |
testcase_01 | AC | 60 ms
5,376 KB |
testcase_02 | AC | 56 ms
5,376 KB |
testcase_03 | AC | 45 ms
5,376 KB |
testcase_04 | AC | 56 ms
5,376 KB |
testcase_05 | AC | 64 ms
10,324 KB |
testcase_06 | AC | 37 ms
5,376 KB |
testcase_07 | AC | 8 ms
6,892 KB |
testcase_08 | AC | 142 ms
21,124 KB |
testcase_09 | AC | 20 ms
9,116 KB |
testcase_10 | AC | 72 ms
16,660 KB |
testcase_11 | AC | 57 ms
12,356 KB |
testcase_12 | AC | 75 ms
10,332 KB |
testcase_13 | AC | 142 ms
20,516 KB |
testcase_14 | AC | 61 ms
15,300 KB |
testcase_15 | AC | 2 ms
5,376 KB |
testcase_16 | AC | 145 ms
21,252 KB |
testcase_17 | AC | 145 ms
21,224 KB |
testcase_18 | AC | 146 ms
21,256 KB |
testcase_19 | AC | 145 ms
21,252 KB |
testcase_20 | AC | 147 ms
21,256 KB |
testcase_21 | AC | 2 ms
5,376 KB |
testcase_22 | AC | 1 ms
5,376 KB |
testcase_23 | AC | 85 ms
15,936 KB |
testcase_24 | AC | 97 ms
18,956 KB |
testcase_25 | AC | 119 ms
18,820 KB |
testcase_26 | AC | 182 ms
26,944 KB |
testcase_27 | AC | 84 ms
26,948 KB |
ソースコード
#define _USE_MATH_DEFINES #include <bits/stdc++.h> using namespace std; #define FOR(i,m,n) for(int i=(m);i<(n);++i) #define REP(i,n) FOR(i,0,n) #define ALL(v) (v).begin(),(v).end() using ll = long long; constexpr int INF = 0x3f3f3f3f; constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL; constexpr double EPS = 1e-8; constexpr int MOD = 1000000007; // constexpr int MOD = 998244353; constexpr int DY[]{1, 0, -1, 0}, DX[]{0, -1, 0, 1}; constexpr int DY8[]{1, 1, 0, -1, -1, -1, 0, 1}, DX8[]{0, -1, -1, -1, 0, 1, 1, 1}; template <typename T, typename U> inline bool chmax(T& a, U b) { return a < b ? (a = b, true) : false; } template <typename T, typename U> inline bool chmin(T& a, U b) { return a > b ? (a = b, true) : false; } struct IOSetup { IOSetup() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); std::cout << fixed << setprecision(20); } } iosetup; struct LowestCommonAncestorByDoubling { std::vector<int> depth, dist; LowestCommonAncestorByDoubling(const std::vector<std::vector<int>> &graph) : graph(graph) { n = graph.size(); depth.resize(n); dist.resize(n); while ((1 << table_h) <= n) ++table_h; parent.resize(table_h, std::vector<int>(n)); } void build(int root = 0) { is_built = true; dfs(-1, root, 0, 0); for (int i = 0; i + 1 < table_h; ++i) for (int ver = 0; ver < n; ++ver) { parent[i + 1][ver] = parent[i][ver] == -1 ? -1 : parent[i][parent[i][ver]]; } } int query(int u, int v) const { assert(is_built); if (depth[u] > depth[v]) std::swap(u, v); for (int i = 0; i < table_h; ++i) { if ((depth[v] - depth[u]) >> i & 1) v = parent[i][v]; } if (u == v) return u; for (int i = table_h - 1; i >= 0; --i) { if (parent[i][u] != parent[i][v]) { u = parent[i][u]; v = parent[i][v]; } } return parent[0][u]; } int distance(int u, int v) const { assert(is_built); return dist[u] + dist[v] - dist[query(u, v)] * 2; } private: bool is_built = false; int n, table_h = 1; std::vector<std::vector<int>> graph, parent; void dfs(int par, int ver, int now_depth, int now_dist) { depth[ver] = now_depth; dist[ver] = now_dist; parent[0][ver] = par; for (int e : graph[ver]) { if (e != par) dfs(ver, e, now_depth + 1, now_dist + 1); } } }; int main() { int n, q; string s; cin >> n >> q >> s; n += 2; s = '(' + s + ')'; vector<vector<int>> graph; vector<pair<int, int>> node; vector<int> rev(n, -1), l; REP(i, n) { if (s[i] == '(') { l.emplace_back(i); } else if (s[i] == ')') { const int idx = graph.size(); graph.emplace_back(); rev[i] = idx; while (l.back() < 0) { const int child = -l.back() - 1; graph.back().emplace_back(child); // graph[child].emplace_back(idx); l.pop_back(); } node.emplace_back(l.back(), i); rev[l.back()] = idx; l.pop_back(); l.emplace_back(-(idx + 1)); } } const int m = graph.size(); LowestCommonAncestorByDoubling lca(graph); lca.build(m - 1); while (q--) { int x, y; cin >> x >> y; const int ans = lca.query(rev[x], rev[y]); if (ans == m - 1) { cout << "-1\n"; } else { cout << node[ans].first << ' ' << node[ans].second << '\n'; } } return 0; }