Problem p02763 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define mp make_pair
#define pb push_back
#define F first
#define S second
const int N = 500005;
#define M 1000000007
#define double long double
#define BINF 1000000000000001
#define init(arr, val) memset(arr, val, sizeof(arr))
#define MAXN 5000004
#define deb(x) cout << #x << " " << x << "\n";
const int LG = 22;

int n;
int t[26][2 * N];

void build() {
  for (int j = 0; j < 26; j++)
    for (int i = n - 1; i > 0; --i)
      t[j][i] = t[j][i << 1] + t[j][i << 1 | 1];
}

void modify(int x, int p, int value) {
  for (t[x][p += n] = value; p > 1; p >>= 1)
    t[x][p >> 1] = t[x][p] + t[x][p ^ 1];
}

int query(int x, int l, int r) {
  r++;
  int res = 0;
  for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
    if (l & 1)
      res += t[x][l++];
    if (r & 1)
      res += t[x][--r];
  }
  return res;
}

#undef int
int main() {
#define int long long int
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("optput.txt", "w", stdout);
#endif

  cin >> n;
  string s;
  cin >> s;
  for (int i = 0; i < n; i++)
    t[s[i] - 'a'][n + i] = 1;
  build();

  int q;
  cin >> q;
  while (q--) {
    int x;
    cin >> x;
    if (x == 1) {
      int pos;
      char c;
      cin >> pos >> c;
      pos--;
      modify(s[pos] - 'a', pos, 0);
      modify(c - 'a', pos, 1);
      s[pos] = c;

    } else {
      int l, r;
      cin >> l >> r;
      l--, r--;
      int ans = 0;
      for (int i = 0; i < 26; i++)
        ans += (query(i, l, r) > 0);
      cout << ans << endl;
    }
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define mp make_pair
#define pb push_back
#define F first
#define S second
const int N = 500005;
#define M 1000000007
#define double long double
#define BINF 1000000000000001
#define init(arr, val) memset(arr, val, sizeof(arr))
#define MAXN 5000004
#define deb(x) cout << #x << " " << x << "\n";
const int LG = 22;

int n;
int t[26][2 * N];

void build() {
  for (int j = 0; j < 26; j++)
    for (int i = n - 1; i > 0; --i)
      t[j][i] = t[j][i << 1] + t[j][i << 1 | 1];
}

void modify(int x, int p, int value) {
  for (t[x][p += n] = value; p > 1; p >>= 1)
    t[x][p >> 1] = t[x][p] + t[x][p ^ 1];
}

int query(int x, int l, int r) {
  r++;
  int res = 0;
  for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
    if (l & 1)
      res += t[x][l++];
    if (r & 1)
      res += t[x][--r];
  }
  return res;
}

#undef int
int main() {
#define int long long int
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  cin >> n;
  string s;
  cin >> s;
  for (int i = 0; i < n; i++)
    t[s[i] - 'a'][n + i] = 1;
  build();

  int q;
  cin >> q;
  while (q--) {
    int x;
    cin >> x;
    if (x == 1) {
      int pos;
      char c;
      cin >> pos >> c;
      pos--;
      modify(s[pos] - 'a', pos, 0);
      modify(c - 'a', pos, 1);
      s[pos] = c;

    } else {
      int l, r;
      cin >> l >> r;
      l--, r--;
      int ans = 0;
      for (int i = 0; i < 26; i++)
        ans += (query(i, l, r) > 0);
      cout << ans << endl;
    }
  }

  return 0;
}