Problem p02763 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <string>
using namespace std;
#define ll long long
#define ull unsigned long long
#define si(X) scanf("%d", &(X))
#define sll(X) scanf("%lld", &(X))
#define INFL 0x3f3f3f3f3f3f3f3fLL
const int mod = 1e9 + 7;
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll expo(ll base, ll pow) {
  ll ans = 1;
  while (pow != 0) {
    if (pow & 1 == 1) {
      ans = ans * base;
      ans = ans % mod;
    }
    base *= base;
    base %= mod;
    pow /= 2;
  }
  return ans;
}
ll inv(ll x) { return expo(x, mod - 2); }

const int M = 2e5 + 5;
int arr[M], n;

int segTree[4 * M][26];

void merge(int node) {
  for (int i = 0; i < 26; i++) {
    segTree[node][i] = segTree[2 * node][i] + segTree[2 * node + 1][i];
  }
}

void build(int node, int l, int r) {
  if (l > r)
    return;
  if (l == r) {
    segTree[node][arr[l]] = 1;
    return;
  }
  int mid = (l + r) / 2;
  build(2 * node, l, mid);
  build(2 * node + 1, mid + 1, r);
  merge(node);
}

void update(int node, int l, int r, int point, int val) {
  if (l > point || r < point)
    return;

  if (l == r) {
    for (int i = 0; i < 26; i++) {
      segTree[node][i] = 0;
    }
    segTree[node][val] = 1;
    return;
  }

  int mid = (l + r) / 2;
  update(2 * node, l, mid, point, val);
  update(2 * node + 1, mid + 1, r, point, val);

  merge(node);
}

vector<int> query(int node, int l, int r, int ql, int qr) {
  vector<int> vec;
  if (l > qr || r < ql)
    return vec;

  if (l >= ql && r <= qr) {
    for (int i = 0; i < 26; i++) {
      if (segTree[node][i] > 0)
        vec.push_back(i);
    }
    return vec;
  }

  int mid = (l + r) / 2;
  vector<int> v1 = query(2 * node, l, mid, ql, qr);
  vector<int> v2 = query(2 * node + 1, mid + 1, r, ql, qr);
  set<int> se;
  for (int i = 0; i < v1.size(); i++) {
    if (se.count(v1[i]))
      continue;
    se.insert(v1[i]);
    vec.push_back(v1[i]);
  }
  for (int i = 0; i < v2.size(); i++) {
    if (se.count(v2[i]))
      continue;
    se.insert(v2[i]);
    vec.push_back(v2[i]);
  }
  return vec;
}

int main() {

  si(n);

  string str;
  cin >> str;

  for (int i = 0; i < n; i++) {
    arr[i + 1] = str[i] - 'a';
  }

  build(1, 1, n);

  int q;
  si(q);

  while (q--) {
    int typ;
    si(typ);

    if (typ == 1) {
      int pos;
      char ex;
      si(pos);
      cin >> ex;

      update(1, 1, n, pos, ex - 'a');

    } else {
      int ql, qr;
      si(ql);
      si(qr);
      /* vector<int> v1= query(1 , 1 , n , ql , qr);
       for(int i = 0 ; i < v1.size() ; i++){
           cout<<v1[i]<<endl;
       }*/
      printf("%d\n", query(1, 1, n, ql, qr).size());
    }
  }
}


Fixed submission:
#include <bits/stdc++.h>
#include <string>
using namespace std;
#define ll long long
#define ull unsigned long long
#define si(X) scanf("%d", &(X))
#define sll(X) scanf("%lld", &(X))
#define INFL 0x3f3f3f3f3f3f3f3fLL
const int mod = 1e9 + 7;
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll expo(ll base, ll pow) {
  ll ans = 1;
  while (pow != 0) {
    if (pow & 1 == 1) {
      ans = ans * base;
      ans = ans % mod;
    }
    base *= base;
    base %= mod;
    pow /= 2;
  }
  return ans;
}
ll inv(ll x) { return expo(x, mod - 2); }

const int M = 5e5 + 5;
int arr[M], n;

int segTree[4 * M][26];

void merge(int node) {
  for (int i = 0; i < 26; i++) {
    segTree[node][i] = segTree[2 * node][i] + segTree[2 * node + 1][i];
  }
}

void build(int node, int l, int r) {
  if (l > r)
    return;
  if (l == r) {
    segTree[node][arr[l]] = 1;
    return;
  }
  int mid = (l + r) / 2;
  build(2 * node, l, mid);
  build(2 * node + 1, mid + 1, r);
  merge(node);
}

void update(int node, int l, int r, int point, int val) {
  if (l > point || r < point)
    return;

  if (l == r) {
    for (int i = 0; i < 26; i++) {
      segTree[node][i] = 0;
    }
    segTree[node][val] = 1;
    return;
  }

  int mid = (l + r) / 2;
  update(2 * node, l, mid, point, val);
  update(2 * node + 1, mid + 1, r, point, val);

  merge(node);
}

vector<int> query(int node, int l, int r, int ql, int qr) {
  vector<int> vec;
  if (l > qr || r < ql)
    return vec;

  if (l >= ql && r <= qr) {
    for (int i = 0; i < 26; i++) {
      if (segTree[node][i] > 0)
        vec.push_back(i);
    }
    return vec;
  }

  int mid = (l + r) / 2;
  vector<int> v1 = query(2 * node, l, mid, ql, qr);
  vector<int> v2 = query(2 * node + 1, mid + 1, r, ql, qr);
  set<int> se;
  for (int i = 0; i < v1.size(); i++) {
    if (se.count(v1[i]))
      continue;
    se.insert(v1[i]);
    vec.push_back(v1[i]);
  }
  for (int i = 0; i < v2.size(); i++) {
    if (se.count(v2[i]))
      continue;
    se.insert(v2[i]);
    vec.push_back(v2[i]);
  }
  return vec;
}

int main() {

  si(n);

  string str;
  cin >> str;

  for (int i = 0; i < n; i++) {
    arr[i + 1] = str[i] - 'a';
  }

  build(1, 1, n);

  int q;
  si(q);

  while (q--) {
    int typ;
    si(typ);

    if (typ == 1) {
      int pos;
      char ex;
      si(pos);
      cin >> ex;

      update(1, 1, n, pos, ex - 'a');

    } else {
      int ql, qr;
      si(ql);
      si(qr);
      /* vector<int> v1= query(1 , 1 , n , ql , qr);
       for(int i = 0 ; i < v1.size() ; i++){
           cout<<v1[i]<<endl;
       }*/
      printf("%d\n", query(1, 1, n, ql, qr).size());
    }
  }
}