#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<set>
#include<algorithm>
#include<map>
#include<vector>
#include<queue>
#include<iostream>
#include<string>
#include<cmath>
#define N 100010
#define FOR(i,a,b) for(i=(a);i<=(b);i++)
#define ROF(i,a,b) for(i=(a);i>=(b);i--)
typedef long long LL;
using namespace std;
int n,q;char s[N];int a[3*N];
void swap(int &a,int &b){int tmp=a;a=b;b=tmp;}
int min3(int t1,int t2,int t3)
{
  int t=t1<t2?t1:t2;
  return t<t3?t:t3;
}
int max3(int t1,int t2,int t3)
{
  int t=t1>t2?t1:t2;
  return t>t3?t:t3;
}
struct Splay
{
  //tp 0-lc 1-rc
  int lc[3*N],rc[3*N],tp[3*N],s[3*N],fa[3*N],key[3*N],ct[3*N],rev[3*N],lazy[3*N],Max[3*N][2],sum[3*N],flip[3*N],Min[3*N][2];
  int root,len,cnt,inf,sta[N],top;
  void init(){root=len=cnt=0;inf=2e9;}
  void upd(int x)
  {
    if (x==0) return;sum[0]=0;
    sum[x]=sum[lc[x]]+sum[rc[x]]+key[x];
    Min[x][0]=min3(Min[lc[x]][0],sum[lc[x]]+key[x],sum[lc[x]]+key[x]+Min[rc[x]][0]);
    Min[x][1]=min3(Min[rc[x]][1],sum[rc[x]]+key[x],sum[rc[x]]+key[x]+Min[lc[x]][1]);
    Max[x][0]=max3(Max[lc[x]][0],sum[lc[x]]+key[x],sum[lc[x]]+key[x]+Max[rc[x]][0]);
    Max[x][1]=max3(Max[rc[x]][1],sum[rc[x]]+key[x],sum[rc[x]]+key[x]+Max[lc[x]][1]);
  }
  void lt(int x)
  {
    int y=fa[x];tp[x]=tp[y];fa[x]=fa[y];tp[y]=1;fa[y]=x;
    s[x]=s[y];s[y]=s[rc[y]]+s[rc[x]]+ct[y];
    if (rc[x]!=0) {fa[rc[x]]=y;tp[rc[x]]=0;}
    lc[y]=rc[x];rc[x]=y;
    if (fa[x]==0) root=x;
    upd(y);upd(x);
  }
  void rt(int x)
  {
    int y=fa[x];tp[x]=tp[y];fa[x]=fa[y];tp[y]=0;fa[y]=x;
    s[x]=s[y];s[y]=s[lc[y]]+s[lc[x]]+ct[y];
    if (lc[x]!=0) {fa[lc[x]]=y;tp[lc[x]]=1;}
    rc[y]=lc[x];lc[x]=y;
    if (fa[x]==0) root=x;
    upd(y);upd(x);
  }
  void rotate(int x)
  {
    pushdown(fa[x]); 
    pushdown(x);  
    if (tp[x]==0) lt(x);else rt(x);
  }
  void srev(int x)
  {
    if (x==0) return;
    rev[x]^=1;
    swap(lc[x],rc[x]);
    tp[lc[x]]=0;tp[rc[x]]=1;
    swap(Min[x][0],Min[x][1]);
    swap(Max[x][0],Max[x][1]);
  }
  void sflip(int x)
  {
    if (x==0) return;
    flip[x]^=1;
    key[x]*=-1;sum[x]*=-1;
    for(int i=0;i<2;i++) {int t=Max[x][i];Max[x][i]=-Min[x][i];Min[x][i]=-t; }
  }
  void pushdown(int x)
  {
    if (rev[x])
    {                                                
      srev(lc[x]);srev(rc[x]);
      rev[x]=0;
    }
    if (flip[x])
    {
      sflip(lc[x]);sflip(rc[x]);
      flip[x]=0;
    }
  }
  void splay(int x)
  {
    int t=x;
    while (t!=root) rotate(t);
  }
  void build(int x,int L,int R)
  {
    int mid=(L+R)>>1;
    key[x]=a[mid];s[x]=ct[x]=1;rev[x]=0;
    if (mid>L) 
    {
      lc[x]=++len;fa[lc[x]]=x;tp[lc[x]]=0;
      build(lc[x],L,mid-1);s[x]+=s[lc[x]];
    }
    if (mid<R)
    {
      rc[x]=++len;fa[rc[x]]=x;tp[rc[x]]=1;
      build(rc[x],mid+1,R);s[x]+=s[rc[x]];
    }
    upd(x);
  }
  int select(int x,int ks)
  {
    pushdown(x);
    int ls=s[lc[x]];
    if (ks<=ls) return select(lc[x],ks);
    if (ks<=ls+ct[x]) return x;
    return select(rc[x],ks-ls-ct[x]);
  }
  int getid(int L,int R)
  {
    int pL=select(root,L-1+1);
    splay(pL);
    int troot=root;
    int pR=select(root,R+1+1);
    root=rc[root];
    fa[root]=0;
    splay(pR);
    fa[root]=troot;tp[root]=1;rc[troot]=root;
    root=troot;upd(root);
    return lc[rc[root]];
  }
  void frev(int L,int R)
  {
    if (L>R) return;
    int id=getid(L,R);
    srev(id);
  }
  void fflip(int L,int R)
  {
    if (L>R) return;
    int id=getid(L,R);
    sflip(id);
  }
  int getans(int L,int R)
  {
    int id=getid(L,R);
    return (abs(Min[id][0])+1)/2+(Max[id][1]+1)/2;
  }
}tree;
/*void prt(int x)
{
  tree.pushdown(x);
  if (tree.lc[x]) prt(tree.lc[x]);
  //printf("%d ",tree.key[x]);
  if (tree.rc[x]) prt(tree.rc[x]);
}*/
int main()
{
  //freopen("mk4.in","r",stdin);
  //freopen("mk4.out","w",stdout);
  scanf("%d%d\n",&n,&q);
  int i;char c;
  FOR(i,1,n)
  {
    scanf("%c",&c);
    if (c=='(') a[i]=1;else a[i]=-1;
  }
  tree.root=tree.len=1;
  tree.build(1,0,n+1);
  int op,x,y;
  FOR(i,1,q)
  {
    scanf("%d%d%d",&op,&x,&y);
    if (op==0) 
    {
      //prt(tree.root);
      printf("%d\n",tree.getans(x,y));
    }
    if (op==1) tree.fflip(x,y);
    if (op==2) tree.frev(x,y);
  }
}