Problem p02763 (C++)

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

#define INF 1000000007
#define ll long long
#define F first
#define S second
#define pii pair<ll int, ll int>
#define pb push_back
#define ppb pop_back
#define vi vector<ll int>

int seg[2000000][26];
int input[500001][26];

void construct_tree(int low, int high, int pos) {
  if (low == high) {
    for (int i = 0; i < 26; i++)
      seg[pos][i] = input[low][i];
    return;
  }
  int mid = (low + high) / 2;
  construct_tree(low, mid, 2 * pos + 1);
  construct_tree(mid + 1, high, 2 * pos + 2);
  for (int i = 0; i < 26; i++)
    seg[pos][i] = seg[2 * pos + 1][i] + seg[2 * pos + 2][i];
}

int range_query(int qlow, int qhigh, int low, int high, int pos, int c) {
  if (qhigh < low || high < qlow)
    return 0;
  if (qlow <= low && high <= qhigh)
    return seg[pos][c];

  int mid = (low + high) / 2;
  return range_query(qlow, qhigh, low, mid, 2 * pos + 1, c) +
         range_query(qlow, qhigh, mid + 1, high, 2 * pos + 2, c);
}

void range_update(int uindex, int low, int high, int pos) {
  if (uindex < low || high < uindex)
    return;
  if (low == uindex && high == uindex) {
    for (int i = 0; i < 26; i++) {
      seg[pos][i] = input[uindex][i];
    }
    // seg[pos][c] = input[uindex][c];
    // seg[pos][temp] = input[uindex][temp];
    return;
  }
  int mid = (low + high) / 2;
  range_update(uindex, low, mid, 2 * pos + 1);
  range_update(uindex, mid + 1, high, 2 * pos + 2);
  for (int i = 0; i < 26; i++) {
    seg[pos][i] = seg[2 * pos + 1][i] + seg[2 * pos + 2][i];
  }
  // seg[pos][c] =  seg[2*pos+1][c] + seg[2*pos+2][c];
  // seg[pos][temp] =  seg[2*pos+1][temp] + seg[2*pos+2][temp];
}

int main() {
  ifstream cin("01-handmade-00");
  ofstream cout("out");
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  // cin >> t;
  while (t--) {
    int n;
    cin >> n;
    string str;
    cin >> str;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 26; j++)
        input[i][j] = 0;
      input[i][str[i] - 'a'] = 1;
    }
    for (int i = 0; i < 2000000; i++)
      for (int j = 0; j < 26; j++) {
        seg[i][j] = 0;
      }
    construct_tree(0, n - 1, 0);
    // for(int i=0;i<15;i++){
    // 	cout << i << "---";
    // 	for(int j=0;j<26;j++) cout << seg[i][j] << " ";
    // 	cout << endl;
    // }
    int q;
    cin >> q;
    int op, l, r;
    while (q--) {
      cin >> op >> l;
      if (op == 1) {
        char c;
        cin >> c;
        for (int i = 0; i < 26; i++)
          input[l - 1][i] = 0;
        // int temp = str[l-1] - 'a';
        // str[l-1] = c;
        // input[l-1][temp] = 0;
        input[l - 1][c - 'a'] = 1;
        range_update(l - 1, 0, n - 1, 0);
        // for(int i=0;i<15;i++){
        // 	cout << i << "---";
        // 	for(int j=0;j<26;j++) cout << seg[i][j] << " ";
        // 	cout << endl;
        // }
      } else {
        cin >> r;
        int ans = 0;
        for (int i = 0; i < 26; i++) {
          if (range_query(l - 1, r - 1, 0, n - 1, 0, i) > 0)
            ans++;
        }
        cout << ans << endl;
      }
    }
  }
  return 0;
}


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

#define INF 1000000007
#define ll long long
#define F first
#define S second
#define pii pair<ll int, ll int>
#define pb push_back
#define ppb pop_back
#define vi vector<ll int>

int seg[2000000][26];
int input[500001][26];

void construct_tree(int low, int high, int pos) {
  if (low == high) {
    for (int i = 0; i < 26; i++)
      seg[pos][i] = input[low][i];
    return;
  }
  int mid = (low + high) / 2;
  construct_tree(low, mid, 2 * pos + 1);
  construct_tree(mid + 1, high, 2 * pos + 2);
  for (int i = 0; i < 26; i++)
    seg[pos][i] = seg[2 * pos + 1][i] + seg[2 * pos + 2][i];
}

int range_query(int qlow, int qhigh, int low, int high, int pos, int c) {
  if (qhigh < low || high < qlow)
    return 0;
  if (qlow <= low && high <= qhigh)
    return seg[pos][c];

  int mid = (low + high) / 2;
  return range_query(qlow, qhigh, low, mid, 2 * pos + 1, c) +
         range_query(qlow, qhigh, mid + 1, high, 2 * pos + 2, c);
}

void range_update(int uindex, int low, int high, int pos) {
  if (uindex < low || high < uindex)
    return;
  if (low == uindex && high == uindex) {
    for (int i = 0; i < 26; i++) {
      seg[pos][i] = input[uindex][i];
    }
    // seg[pos][c] = input[uindex][c];
    // seg[pos][temp] = input[uindex][temp];
    return;
  }
  int mid = (low + high) / 2;
  range_update(uindex, low, mid, 2 * pos + 1);
  range_update(uindex, mid + 1, high, 2 * pos + 2);
  for (int i = 0; i < 26; i++) {
    seg[pos][i] = seg[2 * pos + 1][i] + seg[2 * pos + 2][i];
  }
  // seg[pos][c] =  seg[2*pos+1][c] + seg[2*pos+2][c];
  // seg[pos][temp] =  seg[2*pos+1][temp] + seg[2*pos+2][temp];
}

int main() {
  // ifstream cin("01-handmade-00");
  // ofstream cout("out");
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  // cin >> t;
  while (t--) {
    int n;
    cin >> n;
    string str;
    cin >> str;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 26; j++)
        input[i][j] = 0;
      input[i][str[i] - 'a'] = 1;
    }
    for (int i = 0; i < 2000000; i++)
      for (int j = 0; j < 26; j++) {
        seg[i][j] = 0;
      }
    construct_tree(0, n - 1, 0);
    // for(int i=0;i<15;i++){
    // 	cout << i << "---";
    // 	for(int j=0;j<26;j++) cout << seg[i][j] << " ";
    // 	cout << endl;
    // }
    int q;
    cin >> q;
    int op, l, r;
    while (q--) {
      cin >> op >> l;
      if (op == 1) {
        char c;
        cin >> c;
        for (int i = 0; i < 26; i++)
          input[l - 1][i] = 0;
        // int temp = str[l-1] - 'a';
        // str[l-1] = c;
        // input[l-1][temp] = 0;
        input[l - 1][c - 'a'] = 1;
        range_update(l - 1, 0, n - 1, 0);
        // for(int i=0;i<15;i++){
        // 	cout << i << "---";
        // 	for(int j=0;j<26;j++) cout << seg[i][j] << " ";
        // 	cout << endl;
        // }
      } else {
        cin >> r;
        int ans = 0;
        for (int i = 0; i < 26; i++) {
          if (range_query(l - 1, r - 1, 0, n - 1, 0, i) > 0)
            ans++;
        }
        cout << ans << endl;
      }
    }
  }
  return 0;
}