Problem p02763 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <forward_list>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>
#define rep(i, s, g) for ((i) = (s); (i) < (g); ++(i))
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll MOD = 1e9 + 7;
const ll INF = (1ll << 60);

int n;
vector<bitset<26>> seg(1000100);
string s;

void init(int n_) {
  n = 1;

  while (n < n_) {
    n *= 2;
  }
}

void update(int i, char x) {
  seg[i + n - 1].reset(s[i] - 'a');
  s[i] = x;
  seg[i + n - 1].set(s[i] - 'a');

  i += n - 1;

  while (i > 0) {
    i = (i - 1) / 2;
    seg[i] = seg[2 * i + 1] | seg[2 * i + 2];
  }
}

bitset<26> query(int a, int b, int k, int l, int r) {
  if (r <= a || b <= l) {
    bitset<26> bit(0ll);
    return bit;
  }

  if (a <= l && r <= b) {
    return seg[k];
  } else {
    bitset<26> bit1 = query(a, b, 2 * k + 1, l, (l + r) / 2);
    bitset<26> bit2 = query(a, b, 2 * k + 2, (l + r) / 2, r);

    return bit1 | bit2;
  }
}

int main(void) {
  int q;
  int i, j;
  int n_;
  vector<int> ans;

  cin >> n_ >> s >> q;

  init(n_);

  rep(i, 0, n_) { update(i, s[i]); }

  init(n_);

  rep(i, 0, q) {
    int x;

    cin >> x;

    if (x == 1) {
      int iq;
      char c;

      cin >> iq >> c;
      iq--;

      update(iq, c);
    } else {
      int l, r;
      int an = 0;

      cin >> l >> r;
      l--;

      bitset<26> bit = query(l, r, 0, 0, n);

      an = bit.count();

      ans.push_back(an);
    }
  }

  for (auto &z : ans) {
    cout << z << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <forward_list>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>
#define rep(i, s, g) for ((i) = (s); (i) < (g); ++(i))
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll MOD = 1e9 + 7;
const ll INF = (1ll << 60);

int n;
vector<bitset<26>> seg(2000100);
string s;

void init(int n_) {
  n = 1;

  while (n < n_) {
    n *= 2;
  }
}

void update(int i, char x) {
  seg[i + n - 1].reset(s[i] - 'a');
  s[i] = x;
  seg[i + n - 1].set(s[i] - 'a');

  i += n - 1;

  while (i > 0) {
    i = (i - 1) / 2;
    seg[i] = seg[2 * i + 1] | seg[2 * i + 2];
  }
}

bitset<26> query(int a, int b, int k, int l, int r) {
  if (r <= a || b <= l) {
    bitset<26> bit(0ll);
    return bit;
  }

  if (a <= l && r <= b) {
    return seg[k];
  } else {
    bitset<26> bit1 = query(a, b, 2 * k + 1, l, (l + r) / 2);
    bitset<26> bit2 = query(a, b, 2 * k + 2, (l + r) / 2, r);

    return bit1 | bit2;
  }
}

int main(void) {
  int q;
  int i, j;
  int n_;
  vector<int> ans;

  cin >> n_ >> s >> q;

  init(n_);

  rep(i, 0, n_) { update(i, s[i]); }

  init(n_);

  rep(i, 0, q) {
    int x;

    cin >> x;

    if (x == 1) {
      int iq;
      char c;

      cin >> iq >> c;
      iq--;

      update(iq, c);
    } else {
      int l, r;
      int an = 0;

      cin >> l >> r;
      l--;

      bitset<26> bit = query(l, r, 0, 0, n);

      an = bit.count();

      ans.push_back(an);
    }
  }

  for (auto &z : ans) {
    cout << z << endl;
  }
}