Problem p02763 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
const int iinf = 1 << 28;
const long long llinf = 1ll << 60;
const double PI = 3.14159265;

// int st[1050000][26];
// const int mid = 524288;
int st[16][26];
const int mid = 8;

void upd(int idx, char c) {
  int v = c - 'a';
  for (int i = 0; i < 26; ++i) {
    if (st[mid + idx][i])
      st[mid + idx][i]--;
  }
  st[mid + idx][v] = 1;
  for (int i = (mid + idx) >> 1; i > 0; i >>= 1) {
    for (int j = 0; j < 26; ++j) {
      st[i][j] = st[i << 1][j] + st[(i << 1) + 1][j];
    }
  }
}

void getv(int idx, int l, int r, int ll, int rr, int nelements, int c[26]) {
  if (l > rr || r < ll)
    return;
  if (l == ll && r == rr) {
    for (int i = 0; i < 26; ++i)
      c[i] = st[idx][i];
    return;
  }
  int c1[26];
  for (int i = 0; i < 26; ++i)
    c1[i] = 0;
  getv(idx << 1, l, min(r, ll + (nelements >> 1) - 1), ll,
       ll + (nelements >> 1) - 1, nelements >> 1, c1);
  int c2[26];
  for (int i = 0; i < 26; ++i)
    c2[i] = 0;
  getv((idx << 1) + 1, max(l, ll + (nelements >> 1)), r, ll + (nelements >> 1),
       rr, nelements >> 1, c2);
  for (int i = 0; i < 26; ++i) {
    c[i] = c1[i] + c2[i];
  }
}

void pr() {
  for (int i = 0; i < 26; ++i) {
    char c = 'a' + i;
    cout << c << ": ";
    for (int j = 1; j < 16; ++j) {
      cout << st[j][i];
    }
    cout << "\n";
  }
}

void work() {
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    char c;
    cin >> c;
    int v = c - 'a';
    st[i + mid][v]++;
  }
  for (int i = mid - 1; i > 0; --i) {
    for (int j = 0; j < 26; ++j) {
      st[i][j] = st[i << 1][j] + st[(i << 1) + 1][j];
    }
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int t;
    cin >> t;
    if (t == 1) {
      int a;
      char b;
      cin >> a >> b;
      upd(a - 1, b);
    } else {
      int a, b;
      cin >> a >> b;
      int c[26];
      for (int i = 0; i < 26; ++i)
        c[i] = 0;
      getv(1, a - 1, b - 1, 0, mid - 1, mid, c);
      int ans = 0;
      for (int i = 0; i < 26; ++i) {
        if (c[i])
          ++ans;
      }
      cout << ans << endl;
      // pr();
    }
  }
}

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  work();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
const int iinf = 1 << 28;
const long long llinf = 1ll << 60;
const double PI = 3.14159265;

int st[1050000][26];
const int mid = 524288;
// int st[16][26];
// const int mid = 8;

void upd(int idx, char c) {
  int v = c - 'a';
  for (int i = 0; i < 26; ++i) {
    if (st[mid + idx][i])
      st[mid + idx][i]--;
  }
  st[mid + idx][v] = 1;
  for (int i = (mid + idx) >> 1; i > 0; i >>= 1) {
    for (int j = 0; j < 26; ++j) {
      st[i][j] = st[i << 1][j] + st[(i << 1) + 1][j];
    }
  }
}

void getv(int idx, int l, int r, int ll, int rr, int nelements, int c[26]) {
  if (l > rr || r < ll)
    return;
  if (l == ll && r == rr) {
    for (int i = 0; i < 26; ++i)
      c[i] = st[idx][i];
    return;
  }
  int c1[26];
  for (int i = 0; i < 26; ++i)
    c1[i] = 0;
  getv(idx << 1, l, min(r, ll + (nelements >> 1) - 1), ll,
       ll + (nelements >> 1) - 1, nelements >> 1, c1);
  int c2[26];
  for (int i = 0; i < 26; ++i)
    c2[i] = 0;
  getv((idx << 1) + 1, max(l, ll + (nelements >> 1)), r, ll + (nelements >> 1),
       rr, nelements >> 1, c2);
  for (int i = 0; i < 26; ++i) {
    c[i] = c1[i] + c2[i];
  }
}

void pr() {
  for (int i = 0; i < 26; ++i) {
    char c = 'a' + i;
    cout << c << ": ";
    for (int j = 1; j < 16; ++j) {
      cout << st[j][i];
    }
    cout << "\n";
  }
}

void work() {
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    char c;
    cin >> c;
    int v = c - 'a';
    st[i + mid][v]++;
  }
  for (int i = mid - 1; i > 0; --i) {
    for (int j = 0; j < 26; ++j) {
      st[i][j] = st[i << 1][j] + st[(i << 1) + 1][j];
    }
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int t;
    cin >> t;
    if (t == 1) {
      int a;
      char b;
      cin >> a >> b;
      upd(a - 1, b);
    } else {
      int a, b;
      cin >> a >> b;
      int c[26];
      for (int i = 0; i < 26; ++i)
        c[i] = 0;
      getv(1, a - 1, b - 1, 0, mid - 1, mid, c);
      int ans = 0;
      for (int i = 0; i < 26; ++i) {
        if (c[i])
          ++ans;
      }
      cout << ans << endl;
      // pr();
    }
  }
}

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  work();
  return 0;
}