Problem p00930 (C++) — buggy submission, verdict: Time Limit Exceeded

#ifndef ___Class_BIT
#define ___Class_BIT

// ------ Includes ------ //
#include <vector>
#pragma warning(disable : 4146)

// ------ Class ------ //
template <typename Type> class BIT {
private:
  unsigned size_;
  std::vector<Type> v;
  Type sum(unsigned r) {
    Type ret = 0;
    while (r > 0) {
      ret += v[r];
      r -= r & (-r);
    }
    return ret;
  }

public:
  BIT(){};
  BIT(unsigned size__) {
    for (size_ = 1; size_ < size__;)
      size_ <<= 1;
    v.resize(size_ + 1, 0);
  }
  inline void add(unsigned i, Type x) {
    i++;
    while (i <= size_) {
      v[i] += x;
      i += i & -i;
    }
  }
  inline Type sum(unsigned l, unsigned r) { return sum(r) - sum(l); }
  bool operator[](unsigned x) { return sum(x, x + 1); }
};

#endif

#ifndef ___Class_StarrySkyTree
#define ___Class_StarrySkyTree

// ------ Includes ------ //
#include <algorithm>
#include <limits>
#include <vector>

// ------ Starry Sky Tree Class ------ //
template <typename Type> class StarrySkyTree {
private:
  unsigned size_;
  std::vector<Type> dat1, dat2;
  inline void update_(unsigned a, unsigned b, Type x, unsigned k, unsigned l,
                      unsigned r) {
    if (r <= a || b <= l)
      return;
    if (a <= l && r <= b) {
      dat2[k] += x;
      return;
    }
    update_(a, b, x, (k << 1), l, (l + r) >> 1);
    update_(a, b, x, (k << 1) + 1, (l + r) >> 1, r);
    dat1[k] = std::min(dat1[k << 1] + dat2[k << 1],
                       dat1[(k << 1) + 1] + dat2[(k << 1) + 1]);
  }
  inline Type query_(unsigned a, unsigned b, unsigned k, unsigned l,
                     unsigned r) {
    if (r <= a || b <= l)
      return std::numeric_limits<Type>::max();
    if (a <= l && r <= b)
      return dat1[k] + dat2[k];
    Type lc = query_(a, b, (k << 1), l, (l + r) >> 1);
    Type rc = query_(a, b, (k << 1) + 1, (l + r) >> 1, r);
    return std::min(lc, rc) + dat2[k];
  }

public:
  StarrySkyTree()
      : size_(0), dat1(std::vector<Type>()), dat2(std::vector<Type>()){};
  StarrySkyTree(int size__) {
    for (size_ = 1; size_ < size__;)
      size_ <<= 1;
    dat1.resize(size_ << 1, 0);
    dat2.resize(size_ << 1, 0);
  }
  inline unsigned size() { return size_; }
  inline void add(unsigned i, Type x) { update_(i, i + 1, x, 1, 0, size_); }
  inline void add(unsigned l, unsigned r, Type x) {
    update_(l, r, x, 1, 0, size_);
  }
  inline Type query(unsigned l, unsigned r) {
    return query_(l, r, 1, 0, size_);
  }
};

#endif

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int N, Q, x;
string s;
int main() {
  ios::sync_with_stdio(false);
  cin >> N >> Q >> s;
  BIT<int> r1(N);
  StarrySkyTree<int> r2(N);
  for (int i = 0; i < N; i++) {
    r1.add(i, s[i] == '(' ? 1 : -1);
    r2.add(i, N, s[i] == '(' ? 1 : -1);
  }
  for (int i = 0; i < Q; i++) {
    cin >> x;
    x--;
    if (s[x] == '(') {
      r1.add(x, -2);
      s[x] = ')';
      r2.add(x, N, -2);
      int l = 0, r = N + 1;
      while (r - l > 1) {
        int m = (l + r) / 2;
        int res = r1.sum(0, m);
        if (res != m)
          r = m;
        else
          l = m;
      }
      r1.add(l, 2);
      s[l] = '(';
      r2.add(l, N, 2);
      cout << l + 1 << endl;
    } else {
      r1.add(x, 2);
      s[x] = '(';
      r2.add(x, N, 2);
      int l = 0, r = N;
      while (r - l > 1) {
        int m = (l + r) / 2;
        if (r2.query(m, N) >= 2)
          r = m;
        else
          l = m;
      }
      r1.add(r, -2);
      s[r] = ')';
      r2.add(r, N, -2);
      cout << r + 1 << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
#include <vector>
#pragma warning(disable : 4996)
using namespace std;
int n, q, r, d[300000], dat[1048576], size_;
string s;
inline void update(int i, int x) {
  i += size_;
  dat[i] = x;
  while (i > 1)
    i >>= 1, dat[i] = dat[2 * i] + dat[2 * i + 1];
}
inline int query(int a, int b, int k, int l, int r) {
  if (r <= a || b <= l)
    return 0;
  if (a <= l && r <= b)
    return dat[k];
  int lc = query(a, b, 2 * k, l, (l + r) / 2);
  int rc = query(a, b, 2 * k + 1, (l + r) / 2, r);
  return lc + rc;
}
int main() {
  scanf("%d%d", &n, &q);
  cin >> s;
  for (size_ = 1; size_ < n;)
    size_ <<= 1;
  for (int i = 0; i < n; i++)
    d[i] = (s[i] == '(' ? 1 : -1);
  for (int i = 0; i < n; i++)
    update(i, d[i] == -1 ? 1 : 0);
  for (int i = 0; i < q; i++) {
    cin >> r;
    r--;
    if (d[r] == -1) {
      int sum = r - query(0, r, 1, 0, size_) * 2, pl = 0, pr = r, l = r;
      for (l = r - 1; l >= 0; l--) {
        if (sum < 2) {
          l++;
          break;
        }
        sum -= d[l];
      }
      printf("%d\\n", l + 1);
      d[r] = 1;
      update(r, 0);
      d[l] = -1;
      update(l, 1);
    } else {
      int pl = 0, pr = r;
      while (pr - pl > 1) {
        int pm = (pl + pr) / 2, pc = query(0, pm + 1, 1, 0, size_);
        if (query(0, pm + 1, 1, 0, size_))
          pr = pm;
        else
          pl = pm;
      }
      printf("%d\\n", pr + 1);
      d[r] = -1;
      update(r, 1);
      d[pr] = 1;
      update(pr, 0);
    }
  }
  return 0;
}