Problem p02763 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <vector>
#define int long long
#define REP(i, n) for (int i = 0; i < n; i++)
#define pb push_back
#define MOD 1000000007
#define IDE 0

using namespace std;

const int MAX_N = 1 << 17;
int segtr_size, dat[2 * MAX_N - 1];

void init(int n_) {
  segtr_size = 1;
  while (segtr_size < n_)
    segtr_size *= 2;
  REP(i, 2 * segtr_size - 1) dat[i] = IDE;
}

int segfunc(int l, int m) { return l | m; }

// k番目の値(0-indexed)をaに変更
void update(int k, int a) {
  k += segtr_size - 1;
  dat[k] = a;
  while (k > 0) {
    k = (k - 1) / 2;
    dat[k] = segfunc(dat[k * 2 + 1], dat[k * 2 + 2]);
  }
}

int find(int k) { return dat[k + segtr_size - 1]; }

int query(int a, int b, int k, int l, int r) {
  // cout << "query: " << a << b << k << l << r << "\n";
  if (r <= a || b <= l)
    return IDE;
  if (a <= l && r <= b)
    return dat[k];
  else {
    int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
    int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
    return segfunc(vl, vr);
  }
}

int alp_to_int(char c) {
  // cout << c << " " << (int)(c - 'a') << "\n";
  return (int)(c - 'a');
}

int count(int segtrval) {
  int retval = 0;
  REP(i, 26) { retval += (segtrval >> i) % 2; }
  // cout << segtrval << " " << retval << "\n";
  return retval;
}

signed main() {
  // cin.tie(0);
  // ios::sync_with_stdio(false);
  int n, q;
  cin >> n;
  init(n);
  vector<char> s(n);
  REP(i, n) {
    char st;
    cin >> st;
    s[i] = st;
    update(i, (1LL << alp_to_int(st)));
  }
  cin >> q;
  REP(_, q) {
    int qtype;
    cin >> qtype;
    if (qtype == 1) {
      int i;
      char c;
      cin >> i >> c;
      int c_int = alp_to_int(c);
      update(i - 1, 1LL << c_int);
    } else {
      int l, r;
      cin >> l >> r;
      cout << count(query(l - 1, r, 0, 0, segtr_size)) << "\n";
    }
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <vector>
#define int long long
#define REP(i, n) for (int i = 0; i < n; i++)
#define pb push_back
#define MOD 1000000007
#define IDE 0

using namespace std;

const int MAX_N = 1 << 20;
int segtr_size, dat[2 * MAX_N - 1];

void init(int n_) {
  segtr_size = 1;
  while (segtr_size < n_)
    segtr_size *= 2;
  REP(i, 2 * segtr_size - 1) dat[i] = IDE;
}

int segfunc(int l, int m) { return l | m; }

// k番目の値(0-indexed)をaに変更
void update(int k, int a) {
  k += segtr_size - 1;
  dat[k] = a;
  while (k > 0) {
    k = (k - 1) / 2;
    dat[k] = segfunc(dat[k * 2 + 1], dat[k * 2 + 2]);
  }
}

int find(int k) { return dat[k + segtr_size - 1]; }

int query(int a, int b, int k, int l, int r) {
  // cout << "query: " << a << b << k << l << r << "\n";
  if (r <= a || b <= l)
    return IDE;
  if (a <= l && r <= b)
    return dat[k];
  else {
    int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
    int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
    return segfunc(vl, vr);
  }
}

int alp_to_int(char c) {
  // cout << c << " " << (int)(c - 'a') << "\n";
  return (int)(c - 'a');
}

int count(int segtrval) {
  int retval = 0;
  REP(i, 26) { retval += (segtrval >> i) % 2; }
  // cout << segtrval << " " << retval << "\n";
  return retval;
}

signed main() {
  // cin.tie(0);
  // ios::sync_with_stdio(false);
  int n, q;
  cin >> n;
  init(n);
  vector<char> s(n);
  REP(i, n) {
    char st;
    cin >> st;
    s[i] = st;
    update(i, (1LL << alp_to_int(st)));
  }
  cin >> q;
  REP(_, q) {
    int qtype;
    cin >> qtype;
    if (qtype == 1) {
      int i;
      char c;
      cin >> i >> c;
      int c_int = alp_to_int(c);
      update(i - 1, 1LL << c_int);
    } else {
      int l, r;
      cin >> l >> r;
      cout << count(query(l - 1, r, 0, 0, segtr_size)) << "\n";
    }
  }

  return 0;
}