Problem p02763 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;

const ll mod = 1e9 + 7;
const ll MAXN = 5e5 + 5;

vector<vector<ll>> t(MAXN, vector<ll>(26, 0));
ll n;
string s;

void build(ll v, ll tl, ll tr) {
  if (tl == tr) {
    t[v][s[tl] - 'a']++;
  } else {
    ll tm = (tr - tl) / 2 + tl;
    build(2 * v, tl, tm);
    build(2 * v + 1, tm + 1, tr);
    for (int i = 0; i < 26; i++)
      t[v][i] = t[2 * v][i] + t[2 * v + 1][i];
  }
}

void update(ll v, ll tl, ll tr, ll pos, char ch) {
  if (tl == tr) {
    t[v][s[tl] - 'a']--;
    t[v][ch - 'a']++;
  } else {
    ll tm = (tr - tl) / 2 + tl;
    if (pos <= tm)
      update(2 * v, tl, tm, pos, ch);
    else
      update(2 * v + 1, tm + 1, tr, pos, ch);
    for (int i = 0; i < 26; i++)
      t[v][i] = t[2 * v][i] + t[2 * v + 1][i];
  }
}

vector<ll> query(ll v, ll tl, ll tr, ll l, ll r) {
  if (l > r) {
    vector<ll> tmp(26, 0);
    return tmp;
  }
  if (tl == l && tr == r) {
    return t[v];
  } else {
    ll tm = (tr - tl) / 2 + tl;
    vector<ll> a = query(2 * v, tl, tm, l, min(r, tm));
    vector<ll> b = query(2 * v + 1, tm + 1, tr, max(l, tm + 1), r);
    for (int i = 0; i < 26; i++)
      a[i] += b[i];
    return a;
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  cin >> n;
  cin >> s;

  build(1, 0, n - 1);

  ll q;
  cin >> q;
  while (q--) {
    int a;
    cin >> a;
    if (a == 1) {
      ll pos;
      cin >> pos;
      char ch;
      cin >> ch;
      update(1, 0, n - 1, pos - 1, ch);
      s[pos - 1] = ch;
    } else {
      ll l, r;
      cin >> l >> r;
      vector<ll> ans = query(1, 0, n - 1, l - 1, r - 1);
      ll cnt = 0;
      for (int i = 0; i < 26; i++) {
        if (ans[i])
          cnt++;
      }
      cout << cnt << '\n';
    }
  }
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;

const ll mod = 1e9 + 7;
const ll MAXN = 5e5 + 5;

vector<vector<ll>> t(4 * MAXN, vector<ll>(26, 0));
ll n;
string s;

void build(ll v, ll tl, ll tr) {
  if (tl == tr) {
    t[v][s[tl] - 'a']++;
  } else {
    ll tm = (tr - tl) / 2 + tl;
    build(2 * v, tl, tm);
    build(2 * v + 1, tm + 1, tr);
    for (int i = 0; i < 26; i++)
      t[v][i] = t[2 * v][i] + t[2 * v + 1][i];
  }
}

void update(ll v, ll tl, ll tr, ll pos, char ch) {
  if (tl == tr) {
    t[v][s[tl] - 'a']--;
    t[v][ch - 'a']++;
  } else {
    ll tm = (tr - tl) / 2 + tl;
    if (pos <= tm)
      update(2 * v, tl, tm, pos, ch);
    else
      update(2 * v + 1, tm + 1, tr, pos, ch);
    for (int i = 0; i < 26; i++)
      t[v][i] = t[2 * v][i] + t[2 * v + 1][i];
  }
}

vector<ll> query(ll v, ll tl, ll tr, ll l, ll r) {
  if (l > r) {
    vector<ll> tmp(26, 0);
    return tmp;
  }
  if (tl == l && tr == r) {
    return t[v];
  } else {
    ll tm = (tr - tl) / 2 + tl;
    vector<ll> a = query(2 * v, tl, tm, l, min(r, tm));
    vector<ll> b = query(2 * v + 1, tm + 1, tr, max(l, tm + 1), r);
    for (int i = 0; i < 26; i++)
      a[i] += b[i];
    return a;
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  cin >> n;
  cin >> s;

  build(1, 0, n - 1);

  ll q;
  cin >> q;
  while (q--) {
    int a;
    cin >> a;
    if (a == 1) {
      ll pos;
      cin >> pos;
      char ch;
      cin >> ch;
      update(1, 0, n - 1, pos - 1, ch);
      s[pos - 1] = ch;
    } else {
      ll l, r;
      cin >> l >> r;
      vector<ll> ans = query(1, 0, n - 1, l - 1, r - 1);
      ll cnt = 0;
      for (int i = 0; i < 26; i++) {
        if (ans[i])
          cnt++;
      }
      cout << cnt << '\n';
    }
  }
}