Problem p02763 (C++)

Buggy submission (status: Runtime Error):
// 失敗するからこそ
// そこから立ち向かって行く強さがあってそんな強さが本当の強さだと私は思うから
#ifdef DAIJOBU
#include "/home/v-o_o-v/deb.h"
#define deb(x...)                                                              \
  cerr << "[" << #x << "] = [";                                                \
  _print(x)
#else
#include <bits/stdc++.h>
#define deb(x...)
#endif

using namespace std;

#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef long long lint;
typedef double ld;

const int maxn = 1e5 + 100;
vector<vector<int>> tree(maxn, vector<int>(26, 0));

void add(int in, int id, int del) {
  while (in < maxn) {
    // deb("inf loop");
    tree[in][id] += del;
    in += (in & -in);
  }
}

vi sum(int in) {
  vi ret(26, 0);
  while (in) {
    forn(i, 26) ret[i] += tree[in][i];
    in -= (in & -in);
  }
  return ret;
}

int rsum(int l, int r) {
  vi R = sum(r);
  vi L = sum(l - 1);
  forn(i, 26) R[i] -= L[i];
  int ans = 0;
  forn(i, 26) if (R[i]) ans++;
  return ans;
}

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

  int n;
  cin >> n;
  string s;
  cin >> s;
  for1(i, s.size()) { add(i, s[i - 1] - 'a', 1); }
  int q;
  cin >> q;
  while (q--) {
    int op;
    cin >> op;
    if (op == 1) {
      int pos;
      char c;
      cin >> pos >> c;
      // pos--;
      add(pos, s[pos - 1] - 'a', -1);
      add(pos, c - 'a', 1);
      s[pos - 1] = c;
    } else {
      int l, r;
      cin >> l >> r;
      cout << rsum(l, r) << '\n';
    }
  }
  return 0;
}
// Write Here


Fixed submission:
// 失敗するからこそ
// そこから立ち向かって行く強さがあってそんな強さが本当の強さだと私は思うから
#ifdef DAIJOBU
#include "/home/v-o_o-v/deb.h"
#define deb(x...)                                                              \
  cerr << "[" << #x << "] = [";                                                \
  _print(x)
#else
#include <bits/stdc++.h>
#define deb(x...)
#endif

using namespace std;

#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef long long lint;
typedef double ld;

const int maxn = 5e5 + 100;
vector<vector<int>> tree(maxn, vector<int>(26, 0));

void add(int in, int id, int del) {
  while (in < maxn) {
    // deb("inf loop");
    tree[in][id] += del;
    in += (in & -in);
  }
}

vi sum(int in) {
  vi ret(26, 0);
  while (in) {
    forn(i, 26) ret[i] += tree[in][i];
    in -= (in & -in);
  }
  return ret;
}

int rsum(int l, int r) {
  vi R = sum(r);
  vi L = sum(l - 1);
  forn(i, 26) R[i] -= L[i];
  int ans = 0;
  forn(i, 26) if (R[i]) ans++;
  return ans;
}

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

  int n;
  cin >> n;
  string s;
  cin >> s;
  for1(i, s.size()) { add(i, s[i - 1] - 'a', 1); }
  int q;
  cin >> q;
  while (q--) {
    int op;
    cin >> op;
    if (op == 1) {
      int pos;
      char c;
      cin >> pos >> c;
      // pos--;
      add(pos, s[pos - 1] - 'a', -1);
      add(pos, c - 'a', 1);
      s[pos - 1] = c;
    } else {
      int l, r;
      cin >> l >> r;
      cout << rsum(l, r) << '\n';
    }
  }
  return 0;
}
// Write Here