Problem p02763 (C++)

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

#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define int long long int
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<vector<int>> vvi;
typedef unordered_map<int, int> uoii;
typedef unordered_map<string, int> uosi;

const int MOD = 1e9 + 7;

const int INF = 1e10;
int n;
const int MAXT = 5e5 + 10;
int tr[26][MAXT];
string s;

void add(int *tr, int x, int v) {
  //  cout<<"AAA "<<x<<" "<<n<<'\n';
  for (int i = x; i <= n; i += i & -i) {
    tr[i] += v;
    //    cout<<"TR "<<tr[i]<<" ";
  }
  //  cout<<'\n';
}

int f(int *tr, int x) {
  int ans = 0;
  for (int i = x; i > 0; i -= i & -i) {
    // cout<<"IN";
    ans += tr[i];
  }
  return ans;
}
int get(int *tr, int l, int r) {
  // cout<<"GET "<<n<<'\n';
  // cout<<l<<" "<<r<<'\n';
  if (l > r)
    return 0;
  int x = f(tr, r);
  int y = f(tr, l - 1);
  // cout<<x<<" "<<y<<'\n';
  /*
      for(int i=0;i<8;i++){
          cout<<tr[i]<<" "<<' ';
      }
      cout<<'\n';*/
  return (x - y) >= 1;
}

int32_t main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  IOS

          cin >>
      n;
  cin >> s;
  for (int i = 1; i <= n; i++) {
    int x = s[i - 1] - 'a';
    add(tr[x], i, 1);
  }
  int q;
  cin >> q;
  while (q--) {
    int a;
    cin >> a;
    if (a == 1) {
      int l;
      char t;
      cin >> l >> t;
      int x = s[l - 1] - 'a';
      int y = t - 'a';
      add(tr[x], l, -1);
      add(tr[y], l, 1);
      s[l - 1] = t;
    } else {
      int l, r;
      cin >> l >> r;
      int ans = 0;
      for (int i = 0; i < 26; i++) {
        //   cout<<"IND "<<i<<'\n';
        ans += get(tr[i], l, r);
      }
      cout << ans << '\n';
    }
  }

  return 0;
}


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

#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define int long long int
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<vector<int>> vvi;
typedef unordered_map<int, int> uoii;
typedef unordered_map<string, int> uosi;

const int MOD = 1e9 + 7;

const int INF = 1e10;
int n;
const int MAXT = 5e5 + 10;
int tr[26][MAXT];
string s;

void add(int *tr, int x, int v) {
  //  cout<<"AAA "<<x<<" "<<n<<'\n';
  for (int i = x; i <= n; i += i & -i) {
    tr[i] += v;
    //    cout<<"TR "<<tr[i]<<" ";
  }
  //  cout<<'\n';
}

int f(int *tr, int x) {
  int ans = 0;
  for (int i = x; i > 0; i -= i & -i) {
    // cout<<"IN";
    ans += tr[i];
  }
  return ans;
}
int get(int *tr, int l, int r) {
  // cout<<"GET "<<n<<'\n';
  // cout<<l<<" "<<r<<'\n';
  if (l > r)
    return 0;
  int x = f(tr, r);
  int y = f(tr, l - 1);
  // cout<<x<<" "<<y<<'\n';
  /*
      for(int i=0;i<8;i++){
          cout<<tr[i]<<" "<<' ';
      }
      cout<<'\n';*/
  return (x - y) >= 1;
}

int32_t main() {

  IOS

          cin >>
      n;
  cin >> s;
  for (int i = 1; i <= n; i++) {
    int x = s[i - 1] - 'a';
    add(tr[x], i, 1);
  }
  int q;
  cin >> q;
  while (q--) {
    int a;
    cin >> a;
    if (a == 1) {
      int l;
      char t;
      cin >> l >> t;
      int x = s[l - 1] - 'a';
      int y = t - 'a';
      add(tr[x], l, -1);
      add(tr[y], l, 1);
      s[l - 1] = t;
    } else {
      int l, r;
      cin >> l >> r;
      int ans = 0;
      for (int i = 0; i < 26; i++) {
        //   cout<<"IND "<<i<<'\n';
        ans += get(tr[i], l, r);
      }
      cout << ans << '\n';
    }
  }

  return 0;
}