Problem p02763 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define F first
#define S second
#define MP make_pair
#define pb push_back
#define all(a) a.begin(), a.end()
#define lcm(a, b) (a) / __gcd((a), (b)) * (b)
#define endl '\n'

using namespace std;

typedef long long LL;
typedef pair<int, int> P;
typedef pair<LL, LL> LP;

static const int INF = INT_MAX;
static const LL LINF = LLONG_MAX;
static const int MIN = INT_MIN;
static const LL LMIN = LLONG_MIN;
static const int MOD = 1000000007;
static const int SIZE = 200005;

const int dx[] = {0, -1, 1, 0};
const int dy[] = {-1, 0, 0, 1};

LL fac[SIZE], finv[SIZE], inv[SIZE];
void combInit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < SIZE; ++i) {
    fac[i] = (fac[i - 1] * i) % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = (finv[i - 1] * inv[i]) % MOD;
  }
}

LL comb(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return (fac[n] * (finv[k] * finv[n - k] % MOD)) % MOD;
}

vector<int> Div(int n) {
  vector<int> ret;
  for (int i = 1; i * i <= n; ++i) {
    if (n % i == 0) {
      ret.pb(i);

      if (i * i != n)
        ret.pb(n / i);
    }
  }
  sort(all(ret));
  return ret;
}

int chcnt[1000005][26];
string s;
int ch[26];
struct SegmentTree {
private:
  int n;
  vector<int> v;

public:
  SegmentTree(string str) {
    int sz = str.size();
    n = 1;
    while (n < sz)
      n <<= 1;
    v.resize(2 * n - 1, INF);

    for (int i = 0; i < sz; ++i) {
      chcnt[i + n - 1][str[i] - 'a'] = 1;
    }
    for (int i = n - 2; i >= 0; --i) {
      for (int j = 0; j < 26; ++j) {
        chcnt[i][j] = chcnt[i * 2 + 1][j] + chcnt[i * 2 + 2][j];
      }
    }
  }

  void Update(int x, int v) {
    int t = s[x];
    s[x] = v;
    x += n - 1;
    chcnt[x][t - 'a'] = 0;
    chcnt[x][v - 'a'] = 1;
    while (x > 0) {
      x = (x - 1) / 2;
      chcnt[x][t - 'a']--;
      chcnt[x][v - 'a']++;
    }
  }

  void query(int a, int b, int k, int l, int r = -1) {
    if (r < 0) {
      r = n;
    }
    if (r <= a || b <= l)
      return;
    if (a <= l && r <= b) {
      for (int i = 0; i < 26; ++i) {
        ch[i] += chcnt[k][i];
      }
      return;
    }

    query(a, b, 2 * k + 1, l, (l + r) / 2);
    query(a, b, 2 * k + 2, (l + r) / 2, r);
    return;
  }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n;
  cin >> n;
  cin >> s;

  SegmentTree seg(s);
  int q;
  cin >> q;
  while (q--) {
    int a, b;
    cin >> a >> b;
    if (a == 1) {
      char c;
      cin >> c;
      seg.Update(b - 1, c);

    } else {
      int c;
      cin >> c;
      seg.query(b - 1, c, 0, 0, -1);
      int cnt = 0;
      for (int i = 0; i < 26; ++i) {
        if (ch[i] != 0) {
          ++cnt;
          ch[i] = 0;
        }
      }
      cout << cnt << endl;
    }
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#define F first
#define S second
#define MP make_pair
#define pb push_back
#define all(a) a.begin(), a.end()
#define lcm(a, b) (a) / __gcd((a), (b)) * (b)
#define endl '\n'

using namespace std;

typedef long long LL;
typedef pair<int, int> P;
typedef pair<LL, LL> LP;

static const int INF = INT_MAX;
static const LL LINF = LLONG_MAX;
static const int MIN = INT_MIN;
static const LL LMIN = LLONG_MIN;
static const int MOD = 1000000007;
static const int SIZE = 200005;

const int dx[] = {0, -1, 1, 0};
const int dy[] = {-1, 0, 0, 1};

LL fac[SIZE], finv[SIZE], inv[SIZE];
void combInit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < SIZE; ++i) {
    fac[i] = (fac[i - 1] * i) % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = (finv[i - 1] * inv[i]) % MOD;
  }
}

LL comb(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return (fac[n] * (finv[k] * finv[n - k] % MOD)) % MOD;
}

vector<int> Div(int n) {
  vector<int> ret;
  for (int i = 1; i * i <= n; ++i) {
    if (n % i == 0) {
      ret.pb(i);

      if (i * i != n)
        ret.pb(n / i);
    }
  }
  sort(all(ret));
  return ret;
}

int chcnt[1 << 20][26];
string s;
int ch[26];
struct SegmentTree {
private:
  int n;
  vector<int> v;

public:
  SegmentTree(string str) {
    int sz = str.size();
    n = 1;
    while (n < sz)
      n <<= 1;
    v.resize(2 * n - 1, INF);

    for (int i = 0; i < sz; ++i) {
      chcnt[i + n - 1][str[i] - 'a'] = 1;
    }
    for (int i = n - 2; i >= 0; --i) {
      for (int j = 0; j < 26; ++j) {
        chcnt[i][j] = chcnt[i * 2 + 1][j] + chcnt[i * 2 + 2][j];
      }
    }
  }

  void Update(int x, int v) {
    int t = s[x];
    s[x] = v;
    x += n - 1;
    chcnt[x][t - 'a'] = 0;
    chcnt[x][v - 'a'] = 1;
    while (x > 0) {
      x = (x - 1) / 2;
      chcnt[x][t - 'a']--;
      chcnt[x][v - 'a']++;
    }
  }

  void query(int a, int b, int k, int l, int r = -1) {
    if (r < 0) {
      r = n;
    }
    if (r <= a || b <= l)
      return;
    if (a <= l && r <= b) {
      for (int i = 0; i < 26; ++i) {
        ch[i] += chcnt[k][i];
      }
      return;
    }

    query(a, b, 2 * k + 1, l, (l + r) / 2);
    query(a, b, 2 * k + 2, (l + r) / 2, r);
    return;
  }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n;
  cin >> n;
  cin >> s;

  SegmentTree seg(s);
  int q;
  cin >> q;
  while (q--) {
    int a, b;
    cin >> a >> b;
    if (a == 1) {
      char c;
      cin >> c;
      seg.Update(b - 1, c);

    } else {
      int c;
      cin >> c;
      seg.query(b - 1, c, 0, 0, -1);
      int cnt = 0;
      for (int i = 0; i < 26; ++i) {
        if (ch[i] != 0) {
          ++cnt;
          ch[i] = 0;
        }
      }
      cout << cnt << endl;
    }
  }
  return 0;
}