Problem p02763 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update

/*
 *    coder :: ATUL_PANDEY_2608
 *        >>> INDIA <<<
 */
using namespace std;
using namespace __gnu_pbds;

// #define  part ..
#define pb(a) push_back(a)
#define all(a) a.begin(), a.end()
#define mod 1000000007
// #define maxx 200006
#define ll long long
#define quick ios_base::sync_with_stdio(NULL), cin.tie(0);
#define listll vector<long long>
#define listi vector<int>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define minheap priority_queue<long long, vector<long long>, greater<long long>>
#define rep(i, a, b) for (int i = a; i < b; i++)
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

template <class T>
using oset =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

// end of #define

// define globals ...
const int maxx = 2e5 + 26;

// write function from here ...

int ft[maxx][26];

int n;

void update(int i, int val, int j) {

  for (++i; i <= n; i += (i & -i)) {
    ft[i][j] += val;
  }
}

vector<int> qry(int i) {

  vector<int> alp(26, 0);

  for (; i; i -= (i & -i)) {

    for (int j = 0; j < 26; j++) {

      if (ft[i][j] > 0) {
        alp[j] += ft[i][j];
      }
    }
  }

  return alp;
}

int Main() {

  cin >> n;
  string s;
  cin >> s;

  for (int i = 0; i < n; i++) {

    update(i, 1, s[i] - 'a');
  }

  int q;
  cin >> q;
  while (q--) {

    int a;
    cin >> a;

    if (a == 1) {
      int i;
      char c;
      cin >> i;
      i--;
      cin >> c;

      update(i, -1, s[i] - 'a');
      update(i, 1, c - 'a');
      s[i] = c;

    } else {

      int l, r;
      cin >> l >> r;

      vector<int> ans1 = qry(r), ans2 = qry(l - 1);
      int ans = 0;
      ////int alp [26];
      // memset(alp,0,sizeof(alp));
      for (int i = 0; i < 26; i++) {

        if (ans1[i] > ans2[i])
          ans++;
      }
      cout << ans << endl;
    }
  }

  return 0;
}

int main() {
  quick;
  int t = 1;
  // cin>>t;
  while (t--)
    Main();

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update

/*
 *    coder :: ATUL_PANDEY_2608
 *        >>> INDIA <<<
 */
using namespace std;
using namespace __gnu_pbds;

// #define  part ..
#define pb(a) push_back(a)
#define all(a) a.begin(), a.end()
#define mod 1000000007
// #define maxx 200006
#define ll long long
#define quick ios_base::sync_with_stdio(NULL), cin.tie(0);
#define listll vector<long long>
#define listi vector<int>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define minheap priority_queue<long long, vector<long long>, greater<long long>>
#define rep(i, a, b) for (int i = a; i < b; i++)
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

template <class T>
using oset =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

// end of #define

// define globals ...
const int maxx = 2e6 + 26;

// write function from here ...

int ft[maxx][26];

int n;

void update(int i, int val, int j) {

  for (++i; i <= n; i += (i & -i)) {
    ft[i][j] += val;
  }
}

vector<int> qry(int i) {

  vector<int> alp(26, 0);

  for (; i; i -= (i & -i)) {

    for (int j = 0; j < 26; j++) {

      if (ft[i][j] > 0) {
        alp[j] += ft[i][j];
      }
    }
  }

  return alp;
}

int Main() {

  cin >> n;
  string s;
  cin >> s;

  for (int i = 0; i < n; i++) {

    update(i, 1, s[i] - 'a');
  }

  int q;
  cin >> q;
  while (q--) {

    int a;
    cin >> a;

    if (a == 1) {
      int i;
      char c;
      cin >> i;
      i--;
      cin >> c;

      update(i, -1, s[i] - 'a');
      update(i, 1, c - 'a');
      s[i] = c;

    } else {

      int l, r;
      cin >> l >> r;

      vector<int> ans1 = qry(r), ans2 = qry(l - 1);
      int ans = 0;
      ////int alp [26];
      // memset(alp,0,sizeof(alp));
      for (int i = 0; i < 26; i++) {

        if (ans1[i] > ans2[i])
          ans++;
      }
      cout << ans << endl;
    }
  }

  return 0;
}

int main() {
  quick;
  int t = 1;
  // cin>>t;
  while (t--)
    Main();

  return 0;
}