#define _USE_MATH_DEFINES #include 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 inline bool chmax(T& a, U b) { return a < b ? (a = b, true) : false; } template 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 depth, dist; LowestCommonAncestorByDoubling(const std::vector> &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(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> 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> graph; vector> node; vector 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; }