#line 2 "cpplib/util/template.hpp" #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC target("avx2") #include using namespace std; struct __INIT__{__INIT__(){cin.tie(0);ios::sync_with_stdio(false);cout< vec; typedef vector> mat; typedef vector>> mat3; typedef vector svec; typedef vector> smat; templateusing V=vector; templateusing VV=V>; templateinline void output(T t){bool f=0;for(auto i:t){cout<<(f?" ":"")<inline void output2(T t){for(auto i:t)output(i);} templateinline void debug(T t){bool f=0;for(auto i:t){cerr<<(f?" ":"")<inline void debug2(T t){for(auto i:t)debug(i);} #define loop(n) for(long long _=0;_<(long long)(n);++_) #define _overload4(_1,_2,_3,_4,name,...) name #define __rep(i,a) repi(i,0,a,1) #define _rep(i,a,b) repi(i,a,b,1) #define repi(i,a,b,c) for(long long i=(long long)(a);i<(long long)(b);i+=c) #define rep(...) _overload4(__VA_ARGS__,repi,_rep,__rep)(__VA_ARGS__) #define _overload3_rev(_1,_2,_3,name,...) name #define _rep_rev(i,a) repi_rev(i,0,a) #define repi_rev(i,a,b) for(long long i=(long long)(b)-1;i>=(long long)(a);--i) #define rrep(...) _overload3_rev(__VA_ARGS__,repi_rev,_rep_rev)(__VA_ARGS__) // #define rep(i,...) for(auto i:range(__VA_ARGS__)) // #define rrep(i,...) for(auto i:reversed(range(__VA_ARGS__))) // #define repi(i,a,b) for(lint i=lint(a);i<(lint)(b);++i) // #define rrepi(i,a,b) for(lint i=lint(b)-1;i>=lint(a);--i) // #define irep(i) for(lint i=0;;++i) // inline vector range(long long n){if(n<=0)return vector();vectorv(n);iota(v.begin(),v.end(),0LL);return v;} // inline vector range(long long a,long long b){if(b<=a)return vector();vectorv(b-a);iota(v.begin(),v.end(),a);return v;} // inline vector range(long long a,long long b,long long c){if((b-a+c-1)/c<=0)return vector();vectorv((b-a+c-1)/c);for(int i=0;i<(int)v.size();++i)v[i]=i?v[i-1]+c:a;return v;} // templateinline T reversed(T v){reverse(v.begin(),v.end());return v;} #define all(n) begin(n),end(n) templatebool chmin(T& s,const E& t){bool res=s>t;s=min(s,t);return res;} templatebool chmax(T& s,const E& t){bool res=s(s,t);return res;} const string ds="DRUL"; const vector dx={1,0,-1,0,1,1,-1,-1}; const vector dy={0,1,0,-1,1,-1,1,-1}; #define SUM(v) accumulate(all(v),0LL) #if __cplusplus>=201703L templateauto make_vector(T x,int arg,Args ...args){if constexpr(sizeof...(args)==0)return vector(arg,x);else return vector(arg,make_vector(x,args...));} #endif #define extrep(v,...) for(auto v:__MAKE_MAT__({__VA_ARGS__})) #define bit(n,a) ((n>>a)&1) vector> __MAKE_MAT__(vector v){if(v.empty())return vector>(1,vector());long long n=v.back();v.pop_back();vector> ret;vector> tmp=__MAKE_MAT__(v);for(auto e:tmp)for(long long i=0;i>; templateusing graph_w=vector>>; templateostream& operator<<(ostream& out,pairv){out<<"("<=201703L constexpr inline long long powll(long long a,long long b){long long res=1;while(b--)res*=a;return res;} #endif templatepair& operator+=(pair&s,const pair&t){s.first+=t.first;s.second+=t.second;return s;} templatepair& operator-=(pair&s,const pair&t){s.first-=t.first;s.second-=t.second;return s;} templatepair operator+(const pair&s,const pair&t){auto res=s;return res+=t;} templatepair operator-(const pair&s,const pair&t){auto res=s;return res-=t;} #define BEGIN_STACK_EXTEND(size) void * stack_extend_memory_ = malloc(size);void * stack_extend_origin_memory_;char * stack_extend_dummy_memory_ = (char*)alloca((1+(int)(((long long)stack_extend_memory_)&127))*16);*stack_extend_dummy_memory_ = 0;asm volatile("mov %%rsp, %%rbx\nmov %%rax, %%rsp":"=b"(stack_extend_origin_memory_):"a"((char*)stack_extend_memory_+(size)-1024)); #define END_STACK_EXTEND asm volatile("mov %%rax, %%rsp"::"a"(stack_extend_origin_memory_));free(stack_extend_memory_); #line 5 "cpplib/graph_tree/graph_template.hpp" /** * @brief グラフテンプレート */ using graph=std::vector>; template using graph_w=std::vector>>; graph load_graph(int n,int m){graph g(n);for(int i=0;i>s>>t;--s;--t;g[s].push_back(t);g[t].push_back(s);}return g;} graph load_digraph(int n,int m){graph g(n);for(int i=0;i>s>>t;--s;--t;g[s].push_back(t);}return g;} graph load_graph0(int n,int m){graph g(n);for(int i=0;i>s>>t;g[s].push_back(t);g[t].push_back(s);}return g;} graph load_digraph0(int n,int m){graph g(n);for(int i=0;i>s>>t;g[s].push_back(t);}return g;} graph load_tree(int n){graph g(n);for(int i=0;i>s>>t;--s;--t;g[s].push_back(t);g[t].push_back(s);}return g;} graph load_tree0(int n){graph g(n);for(int i=0;i>s>>t;g[s].push_back(t);g[t].push_back(s);}return g;} graph load_treep(int n){graph g(n);for(int i=0;i>t;g[i+1].push_back(t);g[t].push_back(i+1);}return g;} templategraph_w load_graph_weight(int n,int m){graph_w g(n);for(int i=0;i>s>>t>>u;--s;--t;g[s].emplace_back(t,u);g[t].emplace_back(s,u);}return g;} templategraph_w load_digraph_weight(int n,int m){graph_w g(n);for(int i=0;i>s>>t>>u;--s;--t;g[s].emplace_back(t,u);}return g;} templategraph_w load_graph0_weight(int n,int m){graph_w g(n);for(int i=0;i>s>>t>>u;g[s].emplace_back(t,u);g[t].emplace_back(s,u);}return g;} templategraph_w load_digraph0_weight(int n,int m){graph_w g(n);for(int i=0;i>s>>t>>u;g[s].emplace_back(t,u);}return g;} templategraph_w load_tree_weight(int n){graph_w g(n);for(int i=0;i>s>>t>>u;--s;--t;g[s].emplace_back(t,u);g[t].emplace_back(s,u);}return g;} templategraph_w load_tree0_weight(int n){graph_w g(n);for(int i=0;i>s>>t>>u;g[s].emplace_back(t,u);g[t].emplace_back(s,u);}return g;} templategraph_w load_treep_weight(int n){graph_w g(n);for(int i=0;i>t>>u;g[i+1].emplace_back(t,u);g[t].emplace_back(i+1,u);}return g;} #line 5 "cpplib/graph_tree/depth.hpp" /** * @brief 根からの深さ */ std::vector depth(const graph& g,int start){ std::vectormemo(g.size()); auto f=[&](auto f,int v,int p)->int{ int mx=0; for(auto t:g[v]){ if(t==p)continue; mx=std::max(mx,f(f,t,v)); } return memo[v]=mx+1; }; f(f,start,-1); return memo; } #line 1 "cpplib/graph_tree/distance.hpp" std::vector distance(const graph& G,int start){ std::vectormemo(G.size()); auto f=[&](auto f,int v,int p,int i)->void{ for(auto t:G[v]){ if(t==p)continue; f(f,t,v,i+1); } memo[v]=i; }; f(f,start,-1,0); return memo; } #line 3 "cpplib/data_structure/small_rmq.hpp" /** * RMQ(small) &lt;O(N),O(1)&gt;(N<=64) */ template class small_rmq{ using u64=unsigned long long; std::vectortable; std::vector v; public: small_rmq(std::vector v):v(v){ assert(v.size()<=64); std::vectortmp(v.size()); table.resize(v.size(),0); std::stackstk; for(int i=0;i<(int)v.size();++i){ tmp.resize(v.size()); while(!stk.empty()&&v[stk.top()]>=v[i]){ stk.pop(); } tmp[i]=stk.empty()?-1:stk.top(); stk.emplace(i); } for(int i=0;i<(int)v.size();++i){ if(tmp[i]!=-1)table[i]=table[tmp[i]]|(1ULL<<(tmp[i])); } } T query(int l,int r){ assert(l!=r); const u64 tmp=table[r-1]&~((1ULL< struct maybe{ bool _is_none; T val; maybe():_is_none(true){} maybe(T val):_is_none(false),val(val){} T unwrap()const{ assert(!_is_none); return val; } T unwrap_or(T e)const{ return _is_none?e:val; } bool is_none()const{return _is_none;} bool is_some()const{return !_is_none;} }; template auto expand(F op){ return [&op](const maybe& __s,const maybe& __t)->maybe{ if(__s.is_none())return __t; if(__t.is_none())return __s; return maybe(op(__s.unwrap(),__t.unwrap())); }; } #line 7 "cpplib/data_structure/sparse_table.hpp" /** * @brief SparseTable */ template class sparse_table{ F f; std::vector>data; public: sparse_table(std::vector v,F f=F()):f(f){ int n=v.size(),log=log2(n)+1; data.resize(n,std::vector(log)); for(int i=0;i get(int l,int r){ if(l==r)return maybe(); if(r(f(data[l][k],data[r-(1< struct argmin{ std::pair operator()(const std::pair& s,const std::pair& t){ return s.second<=t.second?s:t; } }; #line 11 "cpplib/data_structure/arg_rmq.hpp" /** * @brief RangeArgminQuery &lt;O(N),O(1)&gt; * @see https://noshi91.hatenablog.com/entry/2018/08/16/125415 */ template class arg_rmq{ constexpr static int b=64; std::vectorv; std::vector*>backet; sparse_table,argmin>* st=0; public: arg_rmq(std::vectorv):v(v){ std::vector>tmp2; for(int i=0;i<(int)v.size();i+=b){ std::vectortmp; T mn=std::numeric_limits::max(); int idx=-1; for(int j=0;i+j<(int)v.size()&&jv[i+j]){ mn=v[i+j]; idx=i+j; } } tmp2.emplace_back(idx,mn); backet.push_back(new small_rmq(tmp)); } st=new sparse_table,argmin>(tmp2); } maybe> query(int s,int t){ if(s==t)return maybe>(); if(s/b==t/b){ int idx=s/b*b+backet[s/b]->query(s%b,t%b); return maybe>(std::make_pair(idx,v[idx])); } std::pair res=std::make_pair(-1,std::numeric_limits::max()); { int idx=s/b*b+backet[s/b]->query(s%b,b); res=argmin()(res,std::make_pair(idx,v[idx])); } if(s/b+1!=t/b)res=argmin()(res,st->get(s/b+1,t/b).unwrap()); if(t%b!=0){ int idx=t/b*b+backet[t/b]->query(0,t%b); res=argmin()(res,std::make_pair(idx,v[idx])); } return maybe>(res); } }; #line 9 "cpplib/graph_tree/lca.hpp" /** * @brief LCA &lt;O(N),O(1)&gt;(HL分解と同等の速さ) */ class lca{ std::vectordata; std::vectorcomp_data; std::vectorstart; arg_rmq*st; std::vector __dist; public: lca(){} lca(std::vector>v,int s){ data.resize(v.size()*2-1); comp_data.resize(v.size()*2-1); start.resize(v.size()); int i=0; __dist=distance(v,s); auto f=[&](auto f,int n,int p)->void{ start[n]=i; data[i]=n; comp_data[i++]=__dist[n]; for(int t:v[n]){ if(t==p)continue; f(f,t,n); data[i]=n; comp_data[i++]=__dist[n]; } }; f(f,s,-1); st=new arg_rmq(comp_data); } int query(int p,int q){ return data[st->query(std::min(start[p],start[q]),std::max(start[p],start[q])+1).unwrap().first]; } int dist(int p,int q){ return __dist[p]+__dist[q]-2*__dist[query(p,q)]; } }; #line 3 "main.cpp" int main(){ lint n,m; cin>>n>>m; string s; cin>>s; graph g(n+3); stackstk; lint cnt=1; stk.emplace(0); vec a(n+3); mat b(n+3); s="("+s+')'; rep(i,n){ if(s[i]=='('){ a[i]=cnt; b[cnt].emplace_back(i); g[stk.top()].emplace_back(cnt); g[cnt].emplace_back(stk.top()); stk.emplace(cnt); cnt++; }else{ lint now=stk.top(); stk.pop(); a[i]=now; b[now].emplace_back(i); } } lca l(g,0); rep(i,m){ lint s,t; cin>>s>>t; if(l.query(a[s],a[t])==0){ cout<<-1<