#include <bits/stdc++.h>

using namespace std;

const int MAXN = 200200;

struct node {
    node *lv = nullptr, *rv = nullptr;
    int sum = 0;
    bool prom = false;
};

int n;
string s;
node mem[MAXN << 1];
node *mpos = mem, *root = nullptr;

void build(node* &v, int l, int r) {
    if (v == nullptr) v = mpos++;
    if (r - l == 1) {
        v->sum = (s[l] == '(' ? 1 : -1);
        return;
    }
    int mid = (l + r) >> 1;
    build(v->lv, l, mid);
    build(v->rv, mid, r);
    v->sum = v->lv->sum + v->rv->sum;
}

void push(node *v, int l, int r) {
    if (!v->prom) return;
    v->sum *= -1;
    if (r - l > 1) {
        v->lv->prom ^= 1;
        v->rv->prom ^= 1;
    }
    v->prom = false;
}

void inv_range(node *v, int l, int r, int ql, int qr) {
    push(v, l, r);
    if (qr <= l || r <= ql) return;
    if (ql <= l && r <= qr) {
        v->prom = true;
        push(v, l, r);
        return;
    }
    int mid = (l + r) >> 1;
    inv_range(v->lv, l, mid, ql, qr);
    inv_range(v->rv, mid, r, ql, qr);
    v->sum = v->lv->sum + v->rv->sum;
}

void build() {
    build(root, 0, n);
}

int main() {
    int m; cin >> n >> m >> s;
    build();
    while (m--) {
        int l, r; cin >> l >> r, --l;
        inv_range(root, 0, n, l, r);
        push(root, 0, n);
        cout << abs(root->sum) << '\n';
    }
}