#include using namespace std; using ll = long long; using P = pair; #define rep(i, n) for(int i = 0; i < n; i++) #define all(x) (x).begin(),(x).end() struct Edge{ ll to; }; using G = vector>; struct LCA{ vector> par; vector dist; LCA(const G &g, int root = 0) { init(g, root); } void init(const G g, int root){ int n = g.size(), k = 1; while((1 << k) < n) k++; par.assign(k,vector(n,-1)); dist.assign(n,-1); dfs(g, root, -1, 0); rep(i,k-1){ rep(j,n){ if(par[i][j] < 0) par[i+1][j] = -1; else par[i+1][j] = par[i][par[i][j]]; } } } void dfs(const G &g, int now, int p, int d){ par[0][now] = p; dist[now] = d; for(auto x : g[now]){ if(x.to != p) dfs(g, x.to, now, d+1); } } int query(int u, int v){ if(dist[u] < dist[v]) swap(u, v); int k = par.size(); rep(i,k){ if((dist[u]-dist[v]) >> i & 1) u = par[i][u]; } if(u==v) return u; for(int i=k-1;i>=0;i--){ if(par[i][u] != par[i][v]){ u = par[i][u]; v = par[i][v]; } } return par[0][u]; } }; int main(){ int n,q; string s; cin >> n >> q >> s; stack

sta; sta.push({0,-1}); G g(n/2+1); vector num(n); vector

numinv(n/2+1); int now = 0; rep(i,n){ if(s[i] == '('){ now++; num[i] = now; Edge e1, e2; e1.to = sta.top().first; e2.to = now; g[e1.to].push_back(e2); g[e2.to].push_back(e1); sta.push({now,i}); }else{ num[i] = sta.top().first; numinv[sta.top().first] = {sta.top().second, i}; sta.pop(); } } LCA lca(g,0); rep(i,q){ int u,v; cin >> u >> v; auto x = lca.query(num[u-1],num[v-1]); if(!x) cout << -1 << endl; else cout << numinv[x].first+1 << " " << numinv[x].second+1 << endl; } return 0; }