結果
問題 | No.1778 括弧列クエリ / Bracketed Sequence Query |
ユーザー | swapfloor |
提出日時 | 2023-07-10 19:14:12 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 105 ms / 2,000 ms |
コード長 | 20,079 bytes |
コンパイル時間 | 2,912 ms |
コンパイル使用メモリ | 219,996 KB |
実行使用メモリ | 24,076 KB |
最終ジャッジ日時 | 2024-09-12 23:48:50 |
合計ジャッジ時間 | 9,467 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 53 ms
6,812 KB |
testcase_01 | AC | 53 ms
6,940 KB |
testcase_02 | AC | 53 ms
6,940 KB |
testcase_03 | AC | 46 ms
6,940 KB |
testcase_04 | AC | 56 ms
6,940 KB |
testcase_05 | AC | 49 ms
7,048 KB |
testcase_06 | AC | 30 ms
6,944 KB |
testcase_07 | AC | 6 ms
6,940 KB |
testcase_08 | AC | 97 ms
12,352 KB |
testcase_09 | AC | 14 ms
6,944 KB |
testcase_10 | AC | 47 ms
10,020 KB |
testcase_11 | AC | 39 ms
8,088 KB |
testcase_12 | AC | 52 ms
6,940 KB |
testcase_13 | AC | 91 ms
12,188 KB |
testcase_14 | AC | 40 ms
9,344 KB |
testcase_15 | AC | 2 ms
6,940 KB |
testcase_16 | AC | 98 ms
12,352 KB |
testcase_17 | AC | 98 ms
12,352 KB |
testcase_18 | AC | 98 ms
12,356 KB |
testcase_19 | AC | 97 ms
12,356 KB |
testcase_20 | AC | 98 ms
12,348 KB |
testcase_21 | AC | 2 ms
6,944 KB |
testcase_22 | AC | 2 ms
6,944 KB |
testcase_23 | AC | 70 ms
10,536 KB |
testcase_24 | AC | 75 ms
12,476 KB |
testcase_25 | AC | 86 ms
12,480 KB |
testcase_26 | AC | 105 ms
24,076 KB |
testcase_27 | AC | 79 ms
24,036 KB |
ソースコード
// #pragma GCC optimize(3) // 如果是acwing // 建议开一下03优化,卡常oj是这样的,牛子随便过 #include <bits/stdc++.h> using namespace std; using ll = long long; using u32 = unsigned int; using u64 = unsigned long long; using i128 = __int128; template <class T> constexpr T infty = 0; template <> constexpr int infty<int> = 1'000'000'000; template <> constexpr ll infty<ll> = ll(infty<int>) * infty<int> * 2; template <> constexpr u32 infty<u32> = infty<int>; template <> constexpr u64 infty<u64> = infty<ll>; template <> constexpr i128 infty<i128> = i128(infty<ll>) * infty<ll>; template <> constexpr double infty<double> = infty<ll>; template <> constexpr long double infty<long double> = infty<ll>; using pi = pair<ll, ll>; using vi = vector<ll>; template <class T> using vc = vector<T>; template <class T> using vvc = vector<vc<T>>; template <class T> using vvvc = vector<vvc<T>>; template <class T> using vvvvc = vector<vvvc<T>>; template <class T> using vvvvvc = vector<vvvvc<T>>; template <class T> using pq = priority_queue<T>; template <class T> using pqg = priority_queue<T, vector<T>, greater<T>>; #define vv(type, name, h, ...) \ vector<vector<type>> name(h, vector<type>(__VA_ARGS__)) #define vvv(type, name, h, w, ...) \ vector<vector<vector<type>>> name( \ h, vector<vector<type>>(w, vector<type>(__VA_ARGS__))) #define vvvv(type, name, a, b, c, ...) \ vector<vector<vector<vector<type>>>> name( \ a, vector<vector<vector<type>>>( \ b, vector<vector<type>>(c, vector<type>(__VA_ARGS__)))) // https://trap.jp/post/1224/ 类似 Python #define FOR1(a) for (ll _ = 0; _ < ll(a); ++_) #define FOR2(i, a) for (ll i = 0; i < ll(a); ++i) #define FOR3(i, a, b) for (ll i = a; i < ll(b); ++i) #define FOR4(i, a, b, c) for (ll i = a; i < ll(b); i += (c)) #define FOR1_R(a) for (ll i = (a)-1; i >= ll(0); --i) #define FOR2_R(i, a) for (ll i = (a)-1; i >= ll(0); --i) #define FOR3_R(i, a, b) for (ll i = (b)-1; i >= ll(a); --i) #define overload4(a, b, c, d, e, ...) e #define overload3(a, b, c, d, ...) d #define FOR(...) overload4(__VA_ARGS__, FOR4, FOR3, FOR2, FOR1)(__VA_ARGS__) #define FOR_R(...) overload3(__VA_ARGS__, FOR3_R, FOR2_R, FOR1_R)(__VA_ARGS__) #define FOR_subset(t, s) \ for (ll t = (s); t >= 0; t = (t == 0 ? -1 : (t - 1) & (s))) #define all(x) x.begin(), x.end() #define len(x) ll(x.size()) #define elif else if #define eb emplace_back #define mp make_pair #define mt make_tuple #define fi first #define se second #define INT(...) \ int __VA_ARGS__; \ IN(__VA_ARGS__) #define INTd(...) \ int __VA_ARGS__; \ IN2(__VA_ARGS__) #define LL(...) \ ll __VA_ARGS__; \ IN(__VA_ARGS__) #define LLd(...) \ ll __VA_ARGS__; \ IN2(__VA_ARGS__) #define STR(...) \ string __VA_ARGS__; \ IN(__VA_ARGS__) #define CHR(...) \ char __VA_ARGS__; \ IN(__VA_ARGS__) #define DBL(...) \ double __VA_ARGS__; \ IN(__VA_ARGS__) #define VEC(type, name, size) \ vector<type> name(size); \ IN(name) #define VECd(type, name, size) \ vector<type> name(size); \ IN2(name) #define VEC2(type, name1, name2, size) \ vector<type> name1(size), name2(size); \ for (int i = 0; i < size; i++) IN(name1[i], name2[i]) #define VEC2d(type, name1, name2, size) \ vector<type> name1(size), name2(size); \ for (int i = 0; i < size; i++) IN2(name1[i], name2[i]) #define VEC3(type, name1, name2, name3, size) \ vector<type> name1(size), name2(size), name3(size); \ for (int i = 0; i < size; i++) IN(name1[i], name2[i], name3[i]) #define VEC3d(type, name1, name2, name3, size) \ vector<type> name1(size), name2(size), name3(size); \ for (int i = 0; i < size; i++) IN2(name1[i], name2[i], name3[i]) #define VEC4(type, name1, name2, name3, name4, size) \ vector<type> name1(size), name2(size), name3(size), name4(size); \ for (int i = 0; i < size; i++) IN(name1[i], name2[i], name3[i], name4[i]); #define VEC4d(type, name1, name2, name3, name4, size) \ vector<type> name1(size), name2(size), name3(size), name4(size); \ for (int i = 0; i < size; i++) IN2(name1[i], name2[i], name3[i], name4[i]); #define VV(type, name, h, w) \ vector<vector<type>> name(h, vector<type>(w)); \ IN(name) #define VVd(type, name, h, w) \ vector<vector<type>> name(h, vector<type>(w)); \ IN2(name) int scan() { return getchar(); } void scan(int &a) { cin >> a; } void scan(long long &a) { cin >> a; } void scan(char &a) { cin >> a; } void scan(double &a) { cin >> a; } void scan(string &a) { cin >> a; } template <class T, class S> void scan(pair<T, S> &p) { scan(p.first), scan(p.second); } template <class T> void scan(vector<T> &); template <class T> void scan(vector<T> &a) { for (auto &i : a) scan(i); } template <class T> void scan(T &a) { cin >> a; } void IN() {} void IN2() {} template <class Head, class... Tail> void IN(Head &head, Tail &...tail) { scan(head); IN(tail...); } template <class Head, class... Tail> void IN2(Head &head, Tail &...tail) { scan(head); --head; IN2(tail...); } template <class U, class V> std::ostream &operator<<(std::ostream &os, const std::pair<U, V> &pr) { return os << pr.first << ' ' << pr.second; } template <class U, class V> std::istream &operator>>(std::istream &is, std::pair<U, V> &pr) { return is >> pr.first >> pr.second; } template <class T> std::ostream &operator<<(std::ostream &os, const std::initializer_list<T> &a) { for (auto it = a.begin(); it != a.end();) { os << *it; it++; if (it != a.end()) os << ' '; } return os; } template <class T> std::ostream &operator<<( std::ostream &os, const std::initializer_list<std::initializer_list<T>> &a) { for (const auto &x : a) os << x << ' '; return os; } template <class T> std::ostream &operator<<(std::ostream &os, const std::vector<T> &a) { FOR(i, a.size()) { if (i) os << ' '; os << a[i]; } return os; } template <class T> std::istream &operator>>(std::istream &is, std::vector<T> &a) { FOR(i, a.size()) { is >> a[i]; } return is; } void OUT() { cout << '\n'; } template <class Head, class... Tail> void OUT(const Head &head, const Tail &...tail) { cout << head; if (sizeof...(tail)) cout << ' '; OUT(tail...); } void close_sync() { ios::sync_with_stdio(false); cin.tie(nullptr); } int popcnt(int x) { return __builtin_popcount(x); } int popcnt(u32 x) { return __builtin_popcount(x); } int popcnt(ll x) { return __builtin_popcountll(x); } int popcnt(u64 x) { return __builtin_popcountll(x); } // (0, 1, 2, 3, 4) -> (-1, 0, 1, 1, 2) int topbit(int x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); } int topbit(u32 x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); } int topbit(ll x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); } int topbit(u64 x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); } // (0, 1, 2, 3, 4) -> (-1, 0, 1, 0, 2) int lowbit(int x) { return (x == 0 ? -1 : __builtin_ctz(x)); } int lowbit(u32 x) { return (x == 0 ? -1 : __builtin_ctz(x)); } int lowbit(ll x) { return (x == 0 ? -1 : __builtin_ctzll(x)); } int lowbit(u64 x) { return (x == 0 ? -1 : __builtin_ctzll(x)); } template <typename T, typename U> T ceil(T x, U y) { return (x > 0 ? (x + y - 1) / y : x / y); } template <typename T, typename U> T floor(T x, U y) { return (x > 0 ? x / y : (x - y + 1) / y); } template <typename T, typename U> pair<T, T> divmod(T x, U y) { T q = floor(x, y); return {q, x - q * y}; } template <typename T, typename U> T SUM(const vector<U> &A) { T sum = 0; for (auto &&a : A) sum += a; return sum; } #define MIN(v) *min_element(all(v)) #define MAX(v) *max_element(all(v)) #define LB(c, x) distance((c).begin(), lower_bound(all(c), (x))) #define UB(c, x) distance((c).begin(), upper_bound(all(c), (x))) #define UNIQUE(x) \ sort(all(x)), x.erase(unique(all(x)), x.end()), x.shrink_to_fit() template <typename T> T POP(deque<T> &que) { T a = que.front(); que.pop_front(); return a; } template <typename T> T POP(pq<T> &que) { T a = que.top(); que.pop(); return a; } template <typename T> T POP(pqg<T> &que) { assert(!que.empty()); T a = que.top(); que.pop(); return a; } template <typename T> T POP(vc<T> &que) { assert(!que.empty()); T a = que.back(); que.pop_back(); return a; } template <typename F> ll binary_search(F check, ll ok, ll ng, bool check_ok = true) { if (check_ok) assert(check(ok)); while (abs(ok - ng) > 1) { auto x = (ng + ok) / 2; tie(ok, ng) = (check(x) ? mp(x, ng) : mp(ok, x)); } return ok; } template <typename F> double binary_search_real(F check, double ok, double ng, int iter = 100) { FOR(iter) { double x = (ok + ng) / 2; tie(ok, ng) = (check(x) ? mp(x, ng) : mp(ok, x)); } return (ok + ng) / 2; } template <class T, class S> inline bool chmax(T &a, const S &b) { return (a < b ? a = b, 1 : 0); } template <class T, class S> inline bool chmin(T &a, const S &b) { return (a > b ? a = b, 1 : 0); } // ? 即 -1 vc<int> s_to_vi(const string &S, char first_char) { vc<int> A(S.size()); FOR(i, S.size()) { A[i] = (S[i] != '?' ? S[i] - first_char : -1); } return A; } // 前缀和 template <typename T, typename U> vector<T> cumsum(vector<U> &A, int off = 1) { int N = A.size(); vector<T> B(N + 1); FOR(i, N) { B[i + 1] = B[i] + A[i]; } if (off == 0) B.erase(B.begin()); return B; } // stable sort 稳定排序 即系数排序 template <typename T> vector<int> argsort(const vector<T> &A) { vector<int> ids(len(A)); iota(all(ids), 0); sort(all(ids), [&](int i, int j) { return (A[i] == A[j] ? i < j : A[i] < A[j]); }); return ids; } // A[I[0]], A[I[1]], ... template <typename T> vc<T> rearrange(const vc<T> &A, const vc<int> &I) { vc<T> B(len(I)); FOR(i, len(I)) B[i] = A[I[i]]; return B; } template <typename T> struct Edge { int frm, to; T cost; int id; }; template <typename T = int, bool directed = false> struct Graph { int N, M; using cost_type = T; using edge_type = Edge<T>; vector<edge_type> edges; vector<int> indptr; vector<edge_type> csr_edges; vc<int> vc_deg, vc_indeg, vc_outdeg; bool prepared; class OutgoingEdges { // 邻接表 public: OutgoingEdges(const Graph *G, int l, int r) : G(G), l(l), r(r) {} const edge_type *begin() const { if (l == r) { return 0; } return &G->csr_edges[l]; } const edge_type *end() const { if (l == r) { return 0; } return &G->csr_edges[r]; } private: const Graph *G; int l, r; }; bool is_prepared() { return prepared; } constexpr bool is_directed() { return directed; } Graph() : N(0), M(0), prepared(0) {} Graph(int N) : N(N), M(0), prepared(0) {} void build(int n) { N = n, M = 0; prepared = 0; edges.clear(); indptr.clear(); csr_edges.clear(); vc_deg.clear(); vc_indeg.clear(); vc_outdeg.clear(); } void add(int frm, int to, T cost = 1, int i = -1) { assert(!prepared); assert(0 <= frm && 0 <= to && to < N); if (i == -1) i = M; auto e = edge_type({frm, to, cost, i}); edges.eb(e); ++M; } // wt 是否有权,off 偏移默认为 1 void read_tree(bool wt = false, int off = 1) { read_graph(N - 1, wt, off); } // 读入,M 边数,wt 是否有权,off 偏移默认为 1 void read_graph(int M, bool wt = false, int off = 1) { for (int m = 0; m < M; ++m) { INT(a, b); a -= off, b -= off; if (!wt) { add(a, b); } else { T c; IN(c); add(a, b, c); } } build(); } void read_parent(int off = 1) { for (int v = 1; v < N; ++v) { INT(p); p -= off; add(p, v); } build(); } void build() { // 构建图 assert(!prepared); prepared = true; indptr.assign(N + 1, 0); for (auto &&e : edges) { indptr[e.frm + 1]++; if (!directed) indptr[e.to + 1]++; } for (int v = 0; v < N; ++v) { indptr[v + 1] += indptr[v]; } auto counter = indptr; csr_edges.resize(indptr.back() + 1); for (auto &&e : edges) { csr_edges[counter[e.frm]++] = e; if (!directed) csr_edges[counter[e.to]++] = edge_type({e.to, e.frm, e.cost, e.id}); } } OutgoingEdges operator[]( int v) const { // 获取v的所有出边的点,类似邻接表,需要 build 构建 assert(prepared); return {this, indptr[v], indptr[v + 1]}; } vc<int> deg_array() { if (vc_deg.empty()) calc_deg(); return vc_deg; } pair<vc<int>, vc<int>> deg_array_inout() { if (vc_indeg.empty()) calc_deg_inout(); return {vc_indeg, vc_outdeg}; } int deg(int v) { if (vc_deg.empty()) calc_deg(); return vc_deg[v]; } int in_deg(int v) { if (vc_indeg.empty()) calc_deg_inout(); return vc_indeg[v]; } int out_deg(int v) { if (vc_outdeg.empty()) calc_deg_inout(); return vc_outdeg[v]; } void debug() { OUT("Graph"); if (!prepared) { OUT("frm to cost id"); for (auto &&e : edges) OUT(e.frm, e.to, e.cost, e.id); } else { OUT("indptr", indptr); OUT("frm to cost id"); FOR(v, N) for (auto &&e : (*this)[v]) OUT(e.frm, e.to, e.cost, e.id); } } // G[V[i]] = G[i] 重新排列 ex s.t. {0, 1, 2, 3, 4} -> v{2, 4, 3, 1, 0} Graph<T, directed> rearrange(vc<int> V) { int n = len(V); map<int, int> MP; FOR(i, n) MP[V[i]] = i; Graph<T, directed> G(n); for (auto &&e : edges) { if (MP.count(e.frm) && MP.count(e.to)) { G.add(MP[e.frm], MP[e.to], e.cost); } } G.build(); return G; } private: void calc_deg() { assert(vc_deg.empty()); vc_deg.resize(N); for (auto &&e : edges) vc_deg[e.frm]++, vc_deg[e.to]++; } void calc_deg_inout() { assert(vc_indeg.empty()); vc_indeg.resize(N); vc_outdeg.resize(N); for (auto &&e : edges) { vc_indeg[e.to]++, vc_outdeg[e.frm]++; } } }; // {G, LR} // 应该是一个合法括号序列 // 以regular bracket sequence为图表。还创建表示每个顶点范围的阵列LR。 // 也制作表示整体的根节点,N/2+1顶点。 // ()() → [0,4), [0,2), [2,4) // G 可以从大括号向小括号连边 // regular bracket sequence以外来输入的时候,前后补充 () 就能使用。 std::pair<Graph<int, 1>, vc<std::pair<int, int>>> bracket_graph( std::string &S) { int N = len(S) / 2; Graph<int, 1> G(N + 1); vc<pair<int, int>> LR(N + 1); int now = 0; int nxt = 1; LR[0] = {0, len(S)}; vc<int> par(N + 1, -1); FOR(i, len(S)) { assert(S[i] == '(' || S[i] == ')'); if (S[i] == '(') { G.add(now, nxt); par[nxt] = now; LR[nxt].fi = i; now = nxt; nxt++; } if (S[i] == ')') { LR[now].se = i + 1; now = par[now]; } } assert(now == 0); G.build(); return {G, LR}; } // HLD euler tour // 非树也有 dfs 序和父亲 template <typename GT> struct Tree { using Graph_type = GT; GT *G_ptr; using WT = typename GT::cost_type; int N; vector<int> LID, RID, head, V, parent, VtoE; vc<int> depth; vc<WT> depth_weighted; Tree() {} Tree(GT &G, int r = 0, bool hld = 1) { build(G, r, hld); } void build(GT &G, int r = 0, bool hld = 1) { G_ptr = &G; N = G.N; LID.assign(N, -1), RID.assign(N, -1), head.assign(N, r); V.assign(N, -1), parent.assign(N, -1), VtoE.assign(N, -1); depth.assign(N, -1), depth_weighted.assign(N, 0); assert(G.is_prepared()); int t1 = 0; dfs_sz(r, -1, hld); dfs_hld(r, t1); } void dfs_sz(int v, int p, bool hld) { auto &sz = RID; parent[v] = p; depth[v] = (p == -1 ? 0 : depth[p] + 1); sz[v] = 1; int l = G_ptr->indptr[v], r = G_ptr->indptr[v + 1]; auto &csr = G_ptr->csr_edges; for (int i = r - 2; i >= l; --i) { if (hld && depth[csr[i + 1].to] == -1) swap(csr[i], csr[i + 1]); } int hld_sz = 0; for (int i = l; i < r; ++i) { auto e = csr[i]; if (depth[e.to] != -1) continue; depth_weighted[e.to] = depth_weighted[v] + e.cost; VtoE[e.to] = e.id; dfs_sz(e.to, v, hld); sz[v] += sz[e.to]; if (hld && chmax(hld_sz, sz[e.to]) && l < i) { swap(csr[l], csr[i]); } } } void dfs_hld(int v, int ×) { LID[v] = times++; RID[v] += LID[v]; V[LID[v]] = v; bool heavy = true; for (auto &&e : (*G_ptr)[v]) { if (depth[e.to] <= depth[v]) continue; head[e.to] = (heavy ? head[v] : e.to); heavy = false; dfs_hld(e.to, times); } } vc<int> heavy_path_at(int v) { vc<int> P = {v}; while (1) { int a = P.back(); for (auto &&e : (*G_ptr)[a]) { if (e.to != parent[a] && head[e.to] == v) { P.eb(e.to); break; } } if (P.back() == a) break; } return P; } int e_to_v(int eid) { auto e = (*G_ptr).edges[eid]; return (parent[e.frm] == e.to ? e.frm : e.to); } int v_to_e(int v) { return VtoE[v]; } int ELID(int v) { return 2 * LID[v] - depth[v]; } int ERID(int v) { return 2 * RID[v] - depth[v] - 1; } /* k: 0-indexed */ int LA(int v, int k) { // k 个祖先 assert(k <= depth[v]); while (1) { int u = head[v]; if (LID[v] - k >= LID[u]) return V[LID[v] - k]; k -= LID[v] - LID[u] + 1; v = parent[u]; } } int LCA(int u, int v) { for (;; v = parent[head[v]]) { if (LID[u] > LID[v]) swap(u, v); if (head[u] == head[v]) return u; } } int lca(int u, int v) { return LCA(u, v); } int la(int u, int v) { return LA(u, v); } int subtree_size(int v, int root = -1) { if (root == -1) return RID[v] - LID[v]; if (v == root) return N; int x = jump(v, root, 1); if (in_subtree(v, x)) return RID[v] - LID[v]; return N - RID[x] + LID[x]; } int dist(int a, int b) { int c = LCA(a, b); return depth[a] + depth[b] - 2 * depth[c]; } WT dist(int a, int b, bool weighted) { assert(weighted); int c = LCA(a, b); return depth_weighted[a] + depth_weighted[b] - WT(2) * depth_weighted[c]; } // a is in b bool in_subtree(int a, int b) { return LID[b] <= LID[a] && LID[a] < RID[b]; } int jump(int a, int b, ll k) { if (k == 1) { if (a == b) return -1; return (in_subtree(b, a) ? LA(b, depth[b] - depth[a] - 1) : parent[a]); } int c = LCA(a, b); int d_ac = depth[a] - depth[c]; int d_bc = depth[b] - depth[c]; if (k > d_ac + d_bc) return -1; if (k <= d_ac) return LA(a, k); return LA(b, d_ac + d_bc - k); } vc<int> collect_child(int v) { vc<int> res; for (auto &&e : (*G_ptr)[v]) if (e.to != parent[v]) res.eb(e.to); return res; } // [始点, 终点] 的闭区间路径 vc<pair<int, int>> get_path_decomposition(int u, int v, bool edge) { vc<pair<int, int>> up, down; while (1) { if (head[u] == head[v]) break; if (LID[u] < LID[v]) { down.eb(LID[head[v]], LID[v]); v = parent[head[v]]; } else { up.eb(LID[u], LID[head[u]]); u = parent[head[u]]; } } if (LID[u] < LID[v]) down.eb(LID[u] + edge, LID[v]); elif (LID[v] + edge <= LID[u]) up.eb(LID[u], LID[v] + edge); reverse(all(down)); up.insert(up.end(), all(down)); return up; } vc<int> restore_path(int u, int v) { vc<int> P; for (auto &&[a, b] : get_path_decomposition(u, v, 0)) { if (a <= b) { FOR(i, a, b + 1) P.eb(V[i]); } else { FOR_R(i, b, a + 1) P.eb(V[i]); } } return P; } }; int main() { // freopen("in.txt", "r", stdin); // freopen("out.txt", "w", stdout); close_sync(); INT(N, Q); STR(S); auto [G, LR] = bracket_graph(S); vc<int> idx(N); FOR(i, len(LR)) idx[LR[i].fi] = idx[LR[i].se - 1] = i; Tree tree(G); FOR(Q) { LL(a, b); a--; b--; int tl = idx[a], tr = idx[b]; auto p = tree.lca(tl, tr); if (!p) OUT(-1); else OUT(LR[p].fi + 1, LR[p].se); } }