Problem p02763 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("O3", "unroll-loops")
// #pragma GCC target("avx2")
#include <bits/stdc++.h>
#include <stdio.h>

using namespace std;
#define ull unsigned long long
#define si short int
#define ll long long
#define ld long double
#define pb push_back
#define pii pair<int, int>
#define pil pair<int, ll>
#define pli pair<ll, int>
#define pll pair<ll, ll>
#define piii pair<pair<int, int>, int>
#define all(vec) vec.begin(), vec.end()
#define piiii pair<pair<int, int>, pair<int, int>>
#define fr(a, from, c) for (int a = (from); (a) < (c); (a)++)

/*-----------------------------------------------------SegTree---------------------------------------------*/
int mxn11 = (int)(2e5 + 1);
vector<vector<int>> t(26, vector<int>(4 * mxn11));
string s;
void build(int v, int vl, int vr, int i) {
  if (vl == vr) {
    if (s[vl] - 'a' == i) {
      t[i][v] = 1;
    }
  } else {
    int tmp = (vl + vr) >> 1;
    build(2 * v, vl, tmp, i);
    build(2 * v + 1, tmp + 1, vr, i);
    t[i][v] = t[i][2 * v] + t[i][2 * v + 1];
  }
}

int get(int v, int vl, int vr, int l, int r, int i) {
  if (l > r)
    return 0;
  if (vl == l && vr == r) {
    return t[i][v];
  }
  int tmp = (vl + vr) >> 1;
  ll a = get(2 * v, vl, tmp, l, min(tmp, r), i);
  ll b = get(2 * v + 1, tmp + 1, vr, max(tmp + 1, l), r, i);
  return a + b;
}

void update(int v, int vl, int vr, int pos, int what) {
  if (vl == vr) {
    for (int i = 0; i < 26; i++) {
      t[i][v] = (what == i) ? 1 : 0;
    }
    return;
  }
  int tmp = (vl + vr) >> 1;
  if (pos <= tmp)
    update(2 * v, vl, tmp, pos, what);
  else
    update(2 * v + 1, tmp + 1, vr, pos, what);
  for (int i = 0; i < 26; i++) {
    t[i][v] = t[i][2 * v + 1] + t[i][2 * v];
  }
}
/*-----------------------------------------------------SegTree---------------------------------------------*/

/*-----------------------------------------------------MATH------------------------------------------------*/
inline ll gcd(ll a, ll b) {
  while (b) {
    a %= b;
    swap(a, b);
  }
  return a;
}

inline ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

inline ll pwm(ll xx, ll pow, ll MD) {
  if (pow < 0) {
    pow = pow % (MD - 1) + MD - 1;
  }
  ll mlt = 1;
  while (pow) {
    if (pow & 1) {
      mlt *= xx;
      mlt %= MD;
    }
    xx *= xx;
    pow >>= 1;
    xx %= MD;
  }
  return mlt;
}
inline ll gcdex(ll a, ll b, ll &x, ll &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  ll xx, yy;
  ll gc = gcdex(b, a % b, yy, xx);
  x = xx;
  y = yy - (a / b) * xx;
  return gc;
}

inline int inv(ll r, int _mod) { return pwm(r % _mod, _mod - 2, _mod); }

inline int64_t hilbertOrder(int x, int y, int pow, int rotate) {
  if (pow == 0) {
    return 0;
  }
  int hpow = 1 << (pow - 1);
  int seg = (x < hpow) ? ((y < hpow) ? 0 : 3) : ((y < hpow) ? 1 : 2);
  seg = (seg + rotate) & 3;
  const int rotateDelta[4] = {3, 0, 0, 1};
  int nx = x & (x ^ hpow), ny = y & (y ^ hpow);
  int nrot = (rotate + rotateDelta[seg]) & 3;
  int64_t subSquareSize = int64_t(1) << (2 * pow - 2);
  int64_t ans = seg * subSquareSize;
  int64_t add = hilbertOrder(nx, ny, pow - 1, nrot);
  ans += (seg == 1 || seg == 2) ? add : (subSquareSize - add - 1);
  return ans;
}

/*-----------------------------------------------------MATH------------------------------------------------*/
void solve() {
  int n;
  cin >> n;
  cin >> s;
  for (int i = 0; i < 26; i++) {
    build(1, 0, n - 1, i);
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int kek;
    cin >> kek;
    if (kek == 1) {
      int pos;
      cin >> pos;
      char t;
      cin >> t;
      update(1, 0, n - 1, pos - 1, t - 'a');
    } else {
      int r;
      int l;
      cin >> r >> l;
      int ans = 0;
      for (int j = 0; j < 26; j++) {
        ans += min(get(1, 0, n - 1, r - 1, l - 1, j), 1);
      }
      cout << ans << endl;
    }
  }
}

int main() {
  /*ios::sync_with_stdio(false);
  cin.tie(nullptr);*/
  // freopen("C:\\Users\\Maximus\\CLionProjects\\Codeforces\\BestTeam\\output.txt",
  // "r", stdin);
  // freopen("C:\\Users\\Maximus\\CLionProjects\\Codeforces\\BestTeam\\output.txt",
  // "w", stdout); unsigned int beg_time = clock();
  int n = 1;
  for (int i = 0; i < n; i++)
    solve();

  // unsigned int end_time = clock();
  // cout<<endl<<endl<<end_time-beg_time;

  return 0;
}

Fixed submission:
#pragma GCC optimize("O3", "unroll-loops")
// #pragma GCC target("avx2")
#include <bits/stdc++.h>
#include <stdio.h>

using namespace std;
#define ull unsigned long long
#define si short int
#define ll long long
#define ld long double
#define pb push_back
#define pii pair<int, int>
#define pil pair<int, ll>
#define pli pair<ll, int>
#define pll pair<ll, ll>
#define piii pair<pair<int, int>, int>
#define all(vec) vec.begin(), vec.end()
#define piiii pair<pair<int, int>, pair<int, int>>
#define fr(a, from, c) for (int a = (from); (a) < (c); (a)++)

/*-----------------------------------------------------SegTree---------------------------------------------*/
int mxn11 = (int)(5e5 + 1);
vector<vector<int>> t(26, vector<int>(4 * mxn11));
string s;
void build(int v, int vl, int vr, int i) {
  if (vl == vr) {
    if (s[vl] - 'a' == i) {
      t[i][v] = 1;
    }
  } else {
    int tmp = (vl + vr) >> 1;
    build(2 * v, vl, tmp, i);
    build(2 * v + 1, tmp + 1, vr, i);
    t[i][v] = t[i][2 * v] + t[i][2 * v + 1];
  }
}

int get(int v, int vl, int vr, int l, int r, int i) {
  if (l > r)
    return 0;
  if (vl == l && vr == r) {
    return t[i][v];
  }
  int tmp = (vl + vr) >> 1;
  ll a = get(2 * v, vl, tmp, l, min(tmp, r), i);
  ll b = get(2 * v + 1, tmp + 1, vr, max(tmp + 1, l), r, i);
  return a + b;
}

void update(int v, int vl, int vr, int pos, int what) {
  if (vl == vr) {
    for (int i = 0; i < 26; i++) {
      t[i][v] = (what == i) ? 1 : 0;
    }
    return;
  }
  int tmp = (vl + vr) >> 1;
  if (pos <= tmp)
    update(2 * v, vl, tmp, pos, what);
  else
    update(2 * v + 1, tmp + 1, vr, pos, what);
  for (int i = 0; i < 26; i++) {
    t[i][v] = t[i][2 * v + 1] + t[i][2 * v];
  }
}
/*-----------------------------------------------------SegTree---------------------------------------------*/

/*-----------------------------------------------------MATH------------------------------------------------*/
inline ll gcd(ll a, ll b) {
  while (b) {
    a %= b;
    swap(a, b);
  }
  return a;
}

inline ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

inline ll pwm(ll xx, ll pow, ll MD) {
  if (pow < 0) {
    pow = pow % (MD - 1) + MD - 1;
  }
  ll mlt = 1;
  while (pow) {
    if (pow & 1) {
      mlt *= xx;
      mlt %= MD;
    }
    xx *= xx;
    pow >>= 1;
    xx %= MD;
  }
  return mlt;
}
inline ll gcdex(ll a, ll b, ll &x, ll &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  ll xx, yy;
  ll gc = gcdex(b, a % b, yy, xx);
  x = xx;
  y = yy - (a / b) * xx;
  return gc;
}

inline int inv(ll r, int _mod) { return pwm(r % _mod, _mod - 2, _mod); }

inline int64_t hilbertOrder(int x, int y, int pow, int rotate) {
  if (pow == 0) {
    return 0;
  }
  int hpow = 1 << (pow - 1);
  int seg = (x < hpow) ? ((y < hpow) ? 0 : 3) : ((y < hpow) ? 1 : 2);
  seg = (seg + rotate) & 3;
  const int rotateDelta[4] = {3, 0, 0, 1};
  int nx = x & (x ^ hpow), ny = y & (y ^ hpow);
  int nrot = (rotate + rotateDelta[seg]) & 3;
  int64_t subSquareSize = int64_t(1) << (2 * pow - 2);
  int64_t ans = seg * subSquareSize;
  int64_t add = hilbertOrder(nx, ny, pow - 1, nrot);
  ans += (seg == 1 || seg == 2) ? add : (subSquareSize - add - 1);
  return ans;
}

/*-----------------------------------------------------MATH------------------------------------------------*/
void solve() {
  int n;
  cin >> n;
  cin >> s;
  for (int i = 0; i < 26; i++) {
    build(1, 0, n - 1, i);
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int kek;
    cin >> kek;
    if (kek == 1) {
      int pos;
      cin >> pos;
      char t;
      cin >> t;
      update(1, 0, n - 1, pos - 1, t - 'a');
    } else {
      int r;
      int l;
      cin >> r >> l;
      int ans = 0;
      for (int j = 0; j < 26; j++) {
        ans += min(get(1, 0, n - 1, r - 1, l - 1, j), 1);
      }
      cout << ans << endl;
    }
  }
}

int main() {
  /*ios::sync_with_stdio(false);
  cin.tie(nullptr);*/
  // freopen("C:\\Users\\Maximus\\CLionProjects\\Codeforces\\BestTeam\\output.txt",
  // "r", stdin);
  // freopen("C:\\Users\\Maximus\\CLionProjects\\Codeforces\\BestTeam\\output.txt",
  // "w", stdout); unsigned int beg_time = clock();
  int n = 1;
  for (int i = 0; i < n; i++)
    solve();

  // unsigned int end_time = clock();
  // cout<<endl<<endl<<end_time-beg_time;

  return 0;
}