#pragma GCC optimize("O3,unroll-loops")
 
#include <bits/stdc++.h>
 
#define endl '\n'
 
using namespace std;
 
int n, m;
string s;
 
int main() {
    ios_base::sync_with_stdio(false), cin.tie(0);
    cin >> n >> m >> s;
    int bal = 0;
    for (char c: s)
        bal += (c == '(' ? 1 : -1);
    while (m--) {
        int l, r; cin >> l >> r, --l;
        for (int i = l; i < r; ++i) {
            s[i] ^= 1;
            bal += (s[i] == '(' ? 2 : -2);
        }
        cout << abs(bal) << endl;
    }
}
