// #pragma GCC optimize(3) // 如果是acwing // 建议开一下03优化,卡常oj是这样的,牛子随便过 #include using namespace std; using ll = long long; using u32 = unsigned int; using u64 = unsigned long long; using i128 = __int128; template constexpr T infty = 0; template <> constexpr int infty = 1'000'000'000; template <> constexpr ll infty = ll(infty) * infty * 2; template <> constexpr u32 infty = infty; template <> constexpr u64 infty = infty; template <> constexpr i128 infty = i128(infty) * infty; template <> constexpr double infty = infty; template <> constexpr long double infty = infty; using pi = pair; using vi = vector; template using vc = vector; template using vvc = vector>; template using vvvc = vector>; template using vvvvc = vector>; template using vvvvvc = vector>; template using pq = priority_queue; template using pqg = priority_queue, greater>; #define vv(type, name, h, ...) \ vector> name(h, vector(__VA_ARGS__)) #define vvv(type, name, h, w, ...) \ vector>> name( \ h, vector>(w, vector(__VA_ARGS__))) #define vvvv(type, name, a, b, c, ...) \ vector>>> name( \ a, vector>>( \ b, vector>(c, vector(__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 name(size); \ IN(name) #define VECd(type, name, size) \ vector name(size); \ IN2(name) #define VEC2(type, name1, name2, size) \ vector name1(size), name2(size); \ for (int i = 0; i < size; i++) IN(name1[i], name2[i]) #define VEC2d(type, name1, name2, size) \ vector name1(size), name2(size); \ for (int i = 0; i < size; i++) IN2(name1[i], name2[i]) #define VEC3(type, name1, name2, name3, size) \ vector 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 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 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 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> name(h, vector(w)); \ IN(name) #define VVd(type, name, h, w) \ vector> name(h, vector(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 void scan(pair &p) { scan(p.first), scan(p.second); } template void scan(vector &); template void scan(vector &a) { for (auto &i : a) scan(i); } template void scan(T &a) { cin >> a; } void IN() {} void IN2() {} template void IN(Head &head, Tail &...tail) { scan(head); IN(tail...); } template void IN2(Head &head, Tail &...tail) { scan(head); --head; IN2(tail...); } template std::ostream &operator<<(std::ostream &os, const std::pair &pr) { return os << pr.first << ' ' << pr.second; } template std::istream &operator>>(std::istream &is, std::pair &pr) { return is >> pr.first >> pr.second; } template std::ostream &operator<<(std::ostream &os, const std::initializer_list &a) { for (auto it = a.begin(); it != a.end();) { os << *it; it++; if (it != a.end()) os << ' '; } return os; } template std::ostream &operator<<( std::ostream &os, const std::initializer_list> &a) { for (const auto &x : a) os << x << ' '; return os; } template std::ostream &operator<<(std::ostream &os, const std::vector &a) { FOR(i, a.size()) { if (i) os << ' '; os << a[i]; } return os; } template std::istream &operator>>(std::istream &is, std::vector &a) { FOR(i, a.size()) { is >> a[i]; } return is; } void OUT() { cout << '\n'; } template 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 T ceil(T x, U y) { return (x > 0 ? (x + y - 1) / y : x / y); } template T floor(T x, U y) { return (x > 0 ? x / y : (x - y + 1) / y); } template pair divmod(T x, U y) { T q = floor(x, y); return {q, x - q * y}; } template T SUM(const vector &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 T POP(deque &que) { T a = que.front(); que.pop_front(); return a; } template T POP(pq &que) { T a = que.top(); que.pop(); return a; } template T POP(pqg &que) { assert(!que.empty()); T a = que.top(); que.pop(); return a; } template T POP(vc &que) { assert(!que.empty()); T a = que.back(); que.pop_back(); return a; } template 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 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 inline bool chmax(T &a, const S &b) { return (a < b ? a = b, 1 : 0); } template inline bool chmin(T &a, const S &b) { return (a > b ? a = b, 1 : 0); } // ? 即 -1 vc s_to_vi(const string &S, char first_char) { vc A(S.size()); FOR(i, S.size()) { A[i] = (S[i] != '?' ? S[i] - first_char : -1); } return A; } // 前缀和 template vector cumsum(vector &A, int off = 1) { int N = A.size(); vector 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 vector argsort(const vector &A) { vector 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 vc rearrange(const vc &A, const vc &I) { vc B(len(I)); FOR(i, len(I)) B[i] = A[I[i]]; return B; } template struct Edge { int frm, to; T cost; int id; }; template struct Graph { int N, M; using cost_type = T; using edge_type = Edge; vector edges; vector indptr; vector csr_edges; vc 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 deg_array() { if (vc_deg.empty()) calc_deg(); return vc_deg; } pair, vc> 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 rearrange(vc V) { int n = len(V); map MP; FOR(i, n) MP[V[i]] = i; Graph 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, vc>> bracket_graph( std::string &S) { int N = len(S) / 2; Graph G(N + 1); vc> LR(N + 1); int now = 0; int nxt = 1; LR[0] = {0, len(S)}; vc 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 struct Tree { using Graph_type = GT; GT *G_ptr; using WT = typename GT::cost_type; int N; vector LID, RID, head, V, parent, VtoE; vc depth; vc 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 heavy_path_at(int v) { vc 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 collect_child(int v) { vc res; for (auto &&e : (*G_ptr)[v]) if (e.to != parent[v]) res.eb(e.to); return res; } // [始点, 终点] 的闭区间路径 vc> get_path_decomposition(int u, int v, bool edge) { vc> 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 restore_path(int u, int v) { vc 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 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); } }