Problem p02763 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

typedef long long ll;
typedef uint64_t ull;
typedef pair<int, int> P;
typedef pair<int, double> Pid;
typedef pair<double, int> Pdi;
typedef pair<ll, int> Pl;
typedef pair<ll, ll> Pll;
typedef pair<int, pair<int, int>> PP;
typedef pair<P, int> PPi;
constexpr double PI = 3.1415926535897932; // acos(-1)
constexpr double EPS = 1e-9;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define chadd(x, y) x = (x + y) % mod

template <typename Monoid> struct SegmentTree {
  using F = function<Monoid(Monoid, Monoid)>;

  int sz;
  vector<Monoid> seg;

  const F f; // モノイドに対して二項演算を行う関数オブジェクト
  const Monoid M1;

  SegmentTree(int n, const F f, const Monoid &M1) : f(f), M1(M1) {
    sz = 1;
    while (sz < n)
      sz <<= 1;
    seg.assign(2 * sz, M1);
  }

  void set(int k, const Monoid &x) { seg[k + sz] = x; }

  void build() {
    for (int k = sz - 1; k > 0; --k) {
      seg[k] = f(seg[k << 1], seg[k << 1 | 1]);
    }
  }

  void update(int k, const Monoid &x) {
    k += sz;
    seg[k] = x;
    while (k >>= 1) {
      seg[k] = f(seg[k << 1], seg[k << 1 | 1]);
    }
  }

  Monoid query(int a, int b) {
    Monoid L = M1, R = M1;
    for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
      if (a & 1)
        L = f(L, seg[a++]);
      if (b & 1)
        R = f(seg[--b], R);
    }
    return f(L, R);
  }

  Monoid operator[](const int &k) const { return seg[k + sz]; }
};

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

  int n, q;
  string s;
  cin >> n >> s >> q;
  vector<SegmentTree<int>> seg(26,
                               SegmentTree<int>(
                                   n, [](int a, int b) { return a + b; }, 0));
  for (int i = 0; i < n; ++i) {
    int foo = s[i] - 'a';
    seg[foo].update(i, 1);
  }
  for (int i = 0; i < q; ++i) {
    int query;
    cin >> query;
    if (query == 1) {
      int a;
      char c;
      cin >> a >> c;
      --a;
      int foo = s[a] - 'a';
      int bar = c - 'a';
      seg[foo].update(a, 0);
      seg[bar].update(a, 1);
      s[i] = c;
    } else {
      int a, b;
      cin >> a >> b;
      --a;
      int ans = 0;
      for (int j = 0; j < 26; ++j) {
        if (seg[j].query(a, b) > 0)
          ++ans;
      }
      cout << ans << "\n";
    }
  }
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

typedef long long ll;
typedef uint64_t ull;
typedef pair<int, int> P;
typedef pair<int, double> Pid;
typedef pair<double, int> Pdi;
typedef pair<ll, int> Pl;
typedef pair<ll, ll> Pll;
typedef pair<int, pair<int, int>> PP;
typedef pair<P, int> PPi;
constexpr double PI = 3.1415926535897932; // acos(-1)
constexpr double EPS = 1e-9;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define chadd(x, y) x = (x + y) % mod

template <typename Monoid> struct SegmentTree {
  using F = function<Monoid(Monoid, Monoid)>;

  int sz;
  vector<Monoid> seg;

  const F f; // モノイドに対して二項演算を行う関数オブジェクト
  const Monoid M1;

  SegmentTree(int n, const F f, const Monoid &M1) : f(f), M1(M1) {
    sz = 1;
    while (sz < n)
      sz <<= 1;
    seg.assign(2 * sz, M1);
  }

  void set(int k, const Monoid &x) { seg[k + sz] = x; }

  void build() {
    for (int k = sz - 1; k > 0; --k) {
      seg[k] = f(seg[k << 1], seg[k << 1 | 1]);
    }
  }

  void update(int k, const Monoid &x) {
    k += sz;
    seg[k] = x;
    while (k >>= 1) {
      seg[k] = f(seg[k << 1], seg[k << 1 | 1]);
    }
  }

  Monoid query(int a, int b) {
    Monoid L = M1, R = M1;
    for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
      if (a & 1)
        L = f(L, seg[a++]);
      if (b & 1)
        R = f(seg[--b], R);
    }
    return f(L, R);
  }

  Monoid operator[](const int &k) const { return seg[k + sz]; }
};

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

  int n, q;
  string s;
  cin >> n >> s >> q;
  vector<SegmentTree<int>> seg(26,
                               SegmentTree<int>(
                                   n, [](int a, int b) { return a + b; }, 0));
  for (int i = 0; i < n; ++i) {
    int foo = s[i] - 'a';
    seg[foo].update(i, 1);
  }
  for (int i = 0; i < q; ++i) {
    int query;
    cin >> query;
    if (query == 1) {
      int a;
      char c;
      cin >> a >> c;
      --a;
      int foo = s[a] - 'a';
      int bar = c - 'a';
      seg[foo].update(a, 0);
      seg[bar].update(a, 1);
      s[a] = c;
    } else {
      int a, b;
      cin >> a >> b;
      --a;
      int ans = 0;
      for (int j = 0; j < 26; ++j) {
        if (seg[j].query(a, b) > 0)
          ++ans;
      }
      cout << ans << "\n";
    }
  }
}