Problem p00930 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (i = 0; i < n; i++)
#define repl(i, n) for (i = 1; i <= n; i++)

#define sz(x) (int)x.size()
#define pb push_back
#define all(x) x.begin(), x.end()
#define uu first
#define vv second
#define mem(x, y) memset(x, y, sizeof(x))
#define un(x) x.erase(unique(all(x)), x.end())

#define sdi(x) scanf("%d", &x)
#define sdii(x, y) scanf("%d %d", &x, &y)
#define sdiii(x, y, z) scanf("%d %d %d", &x, &y, &z)
#define sdl(x) scanf("%lld", &x)
#define sdll(x, y) scanf("%lld %lld", &x, &y)
#define sdlll(x, y, z) scanf("%lld %lld %lld", &x, &y, &z)
#define sds(x) scanf("%s", x)
#define pfi(x) printf("%d\\n", x)
#define pfii(x, y) printf("%d %d\\n", x, y)
#define pfiii(x, y, z) printf("%d %d %d\\n", x, y, z)
#define pfl(x) printf("%lld\\n", x)
#define pfll(x, y) printf("%lld %lld\\n", x, y)
#define pflll(x, y, z) printf("%lld %lld %lld\\n", x, y, z)

#define eps 1e-9
#define OK cerr << "ok\\n"
#define DB(x) cerr << #x << " = " << x << endl

typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
typedef pair<int, int> pii;

inline int setBit(int N, int pos) { return N = N | (1 << pos); }
inline int resetBit(int N, int pos) { return N = N & ~(1 << pos); }
inline bool checkBit(int N, int pos) { return (bool)(N & (1 << pos)); }

// int kx[] = {+2, +1, -1, -2, -2, -1, +1, +2};
// int ky[] = {+1, +2, +2, +1, -1, -2, -2, -1}; //Knight Direction
// int fx[] = {+0, +0, +1, -1, -1, +1, -1, +1};
// int fy[] = {-1, +1, +0, +0, +1, +1, -1, -1}; //Four & Eight Direction

const int MAX = 300005, INF = 100000000;
int n, q, cum[MAX];
char str[MAX];
set<int> closeList;
struct data {
  int mn, prop;
} tree[MAX * 4];

void init(int node, int beg, int endd) {
  if (beg == endd) {
    tree[node] = {cum[beg], 0};
    return;
  }

  int left = node << 1;
  int right = left + 1;
  int mid = (beg + endd) >> 1;

  init(left, beg, mid);
  init(right, mid + 1, endd);

  tree[node].mn = min(tree[left].mn, tree[right].mn);
}

inline void pushDown(int node, int left, int right) {
  tree[left].mn += tree[node].prop;
  tree[right].mn += tree[node].prop;
  tree[left].prop += tree[node].prop;
  tree[right].prop += tree[node].prop;
  tree[node].prop = 0;
}

void update(int node, int beg, int endd, int x, int y, int val) {
  if (x > y)
    return;
  if (beg == x && endd == y) {
    tree[node].mn += val;
    tree[node].prop += val;
    return;
  }

  int left = node << 1;
  int right = left + 1;
  int mid = (beg + endd) >> 1;

  if (tree[node].prop)
    pushDown(node, left, right);

  if (x <= min(y, mid))
    update(left, beg, mid, x, min(y, mid), val);
  if (max(x, mid + 1) <= y)
    update(right, mid + 1, endd, max(x, mid + 1), y, val);

  tree[node].mn = min(tree[left].mn, tree[right].mn);
}

int query(int node, int beg, int endd, int x, int y) {
  if (x > y)
    return INF;
  if (beg == x && endd == y)
    return tree[node].mn;

  int left = node << 1;
  int right = left + 1;
  int mid = (beg + endd) >> 1;

  if (tree[node].prop)
    pushDown(node, left, right);

  int l = (x <= min(y, mid)) ? query(left, beg, mid, x, min(y, mid)) : INF;
  int r = (max(x, mid + 1) <= y)
              ? query(right, mid + 1, endd, max(x, mid + 1), y)
              : INF;

  return min(l, r);
}

inline int bs(int high) {
  int low = 1, mid, ret = high, ub = high;
  while (low <= high) {
    mid = (low + high) >> 1;
    if (query(1, 1, n, mid, ub) >= 2) {
      ret = min(ret, mid);
      high = mid - 1;
    } else
      low = mid + 1;
  }
  return ret;
}

inline int query(int idx) {
  int ret = idx;
  if (str[idx] == '(') { /// open to close
    str[idx] = ')';
    closeList.insert(idx);
    update(1, 1, n, idx, n, -2);
    ret = *closeList.begin();
    str[ret] = '(';
    closeList.erase(ret);
    update(1, 1, n, ret, n, +2);
    return ret;
  } else { /// close to open
    str[idx] = '(';
    closeList.erase(idx);
    update(1, 1, n, idx, n, +2);
    ret = bs(idx);
    str[ret] = ')';
    closeList.insert(ret);
    update(1, 1, n, ret, n, -2);
    return ret;
  }
}

int main() {
  //    assert(freopen("in.txt","r",stdin));
  //    assert(freopen("out.txt","w",stdout));

  int i, idx;

  sdii(n, q);
  sds(str + 1);
  repl(i, n) {
    if (str[i] == '(')
      cum[i] = 1;
    else {
      cum[i] = -1;
      closeList.insert(i);
    }
    cum[i] += cum[i - 1];
  }
  init(1, 1, n);
  while (q--) {
    sdi(idx);
    pfi(query(idx));
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define For(i, n) for (int i = 1; i <= n; i++)
#define Fork(i, k, n) for (int i = k; i <= n; i++)
#define Rep(i, n) for (int i = 0; i < n; i++)
#define ForD(i, n) for (int i = n; i; i--)
#define ForkD(i, k, n) for (int i = n; i >= k; i--)
#define RepD(i, n) for (int i = n; i >= 0; i--)
#define Forp(x) for (int p = Pre[x]; p; p = Next[p])
#define Forpiter(x) for (int &p = iter[x]; p; p = Next[p])
#define Lson (o << 1)
#define Rson ((o << 1) + 1)
#define MEM(a) memset(a, 0, sizeof(a));
#define MEMI(a) memset(a, 127, sizeof(a));
#define MEMi(a) memset(a, 128, sizeof(a));
#define INF (2139062143)
#define F (1000000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int>
#define pi pair<int, int>
#define SI(a) ((a).size())
#define Pr(kcase, ans) printf("Case #%d: %I64d\\n", kcase, ans);
#define PRi(a, n)                                                              \\
  For(i, n - 1) cout << a[i] << ' ';                                           \\
  cout << a[n] << endl;
#define PRi2D(a, n, m)                                                         \\
  For(i, n) {                                                                  \\
    For(j, m - 1) cout << a[i][j] << ' ';                                      \\
    cout << a[i][m] << endl;                                                   \\
  }
#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a, ll b) { return (a * b) % F; }
ll add(ll a, ll b) { return (a + b) % F; }
ll sub(ll a, ll b) { return ((a - b) % F + F) % F; }
void upd(ll &a, ll b) { a = (a % F + b % F) % F; }
int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
#define MAXN (200000 + 10)
#define MEM2(a, x) memset(a, x, sizeof(a));
int A[MAXN];
class SegmentTree {
  ll a[MAXN * 4], minv[MAXN * 4], addv[MAXN * 4];
  int n;

public:
  void mem(int _n) {
    n = _n;
    MEMI(a) MEM(minv) MEM(addv)
  }
  void build(int o, int L, int R) {
    if (L == R) {
      minv[o] = a[o] = A[L];
      return;
    }
    int m = (L + R) / 2;
    build(Lson, L, m);
    build(Rson, m + 1, R);
    maintain(o, L, R);
  }
  void maintain(int o, int L, int R) {
    minv[o] = 0;
    if (L < R) {
      minv[o] = min(minv[Lson], minv[Rson]);
    }
    minv[o] += addv[o];
  }

  int y1, y2, v;
  void update(int o, int L, int R) {
    if (y1 <= L && R <= y2) {
      addv[o] += v;
      minv[o] += v;
      return;
    } else {
      pushdown(o, L, R);
      int M = (R + L) >> 1;
      if (y1 <= M)
        update(Lson, L, M);
      if (M < y2)
        update(Rson, M + 1, R);
    }
    maintain(o, L, R);
  }
  void add(int l, int r, int v) {
    y1 = l, y2 = r, this->v = v;
    update(1, 1, n);
  }
  void pushdown(int o, int L, int R) {
    if (L != R && addv[o]) {
      addv[Lson] += addv[o];
      addv[Rson] += addv[o];
      minv[Lson] += addv[o];
      minv[Rson] += addv[o];
      addv[o] = 0;
    }
  }
  int query(int o, int L, int R) {
    if (L == R)
      return L;
    pushdown(o, L, R);
    int m = (L + R) / 2;
    if (minv[Rson] < 2)
      return query(Rson, m + 1, R);
    else
      return query(Lson, L, m);
  }
} S;
set<int> S1;
set<int>::iterator it;
int n;
char s[MAXN];
int main() {
  //	freopen("G_data.in","r",stdin);
  //	freopen("G.out","w",stdout);
  int m;
  while (cin >> n >> m) {
    scanf("%s", s + 1);
    S.mem(n);
    S1.clear();
    int p = 0;
    For(i, n) {
      if (s[i] == '(')
        p++;
      else
        p--, S1.insert(i);
      A[i] = p;
    }
    S.build(1, 1, n);
    Rep(i, m) {
      int c = 0, p = read();
      //			printf("%s\\n",s+1);
      //			cout<<p<<endl;
      if (s[p] == '(') {
        s[p] = ')';
        S1.insert(p);
        c = *S1.begin();
        s[c] = '(';
        S1.erase(c);
        S.add(p, n, -2);
        S.add(c, n, 2);
      } else {
        S1.erase(p);
        S.add(p, n, 2);
        s[p] = '(';
        int l = 1, r = n, ans = r;
        ans = S.query(1, 1, n) + 1;
        c = ans;
        s[c] = ')';
        S1.insert(c);
        S.add(c, n, -2);
      }
      printf("%d\\n", c);
    }
  }
  return 0;
}