Problem p02763 (C++)

Buggy submission (status: Runtime Error):
// #include <tourist>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> p;
const int INF = 1e9;
const ll LINF = ll(1e18) + 1;
const int MOD = 1000000007;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
const int Dx[8] = {0, 1, 1, 1, 0, -1, -1, -1},
          Dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
#define yes cout << "Yes" << endl
#define YES cout << "YES" << endl
#define no cout << "No" << endl
#define NO cout << "NO" << endl
#define rep(i, n) for (int i = 0; i < n; i++)
#define ALL(v) v.begin(), v.end()
#define debug(v)                                                               \
  cout << #v << ":";                                                           \
  for (auto x : v) {                                                           \
    cout << x << ' ';                                                          \
  }                                                                            \
  cout << endl;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
// cout<<fixed<<setprecision(15);有効数字15桁
//-std=c++14
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
int n, m;
vector<int> a;
// 区間和
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;

int N, Q;
struct LazySegmentTree {
private:
  int n;
  vector<ll> node, lazy;

public:
  LazySegmentTree(vector<ll> v) {
    int sz = (int)v.size();
    n = 1;
    while (n < sz)
      n *= 2;
    node.resize(2 * n - 1);
    lazy.resize(2 * n - 1, 0);

    for (int i = 0; i < sz; i++)
      node[i + n - 1] = v[i];
    for (int i = n - 2; i >= 0; i--)
      node[i] = node[i * 2 + 1] + node[i * 2 + 2];
  }

  void eval(int k, int l, int r) {
    if (lazy[k] != 0) {
      node[k] += lazy[k];
      if (r - l > 1) {
        lazy[2 * k + 1] += lazy[k] / 2;
        lazy[2 * k + 2] += lazy[k] / 2;
      }
      lazy[k] = 0;
    }
  }

  void add(int a, int b, ll x, int k = 0, int l = 0, int r = -1) {
    if (r < 0)
      r = n;
    eval(k, l, r);
    if (b <= l || r <= a)
      return;
    if (a <= l && r <= b) {
      lazy[k] += (r - l) * x;
      eval(k, l, r);
    } else {
      add(a, b, x, 2 * k + 1, l, (l + r) / 2);
      add(a, b, x, 2 * k + 2, (l + r) / 2, r);
      node[k] = node[2 * k + 1] + node[2 * k + 2];
    }
  }

  ll getsum(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r < 0)
      r = n;
    eval(k, l, r);
    if (b <= l || r <= a)
      return 0;
    if (a <= l && r <= b)
      return node[k];
    ll vl = getsum(a, b, 2 * k + 1, l, (l + r) / 2);
    ll vr = getsum(a, b, 2 * k + 2, (l + r) / 2, r);
    return vl + vr;
  }
};
string s;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n >> s;
  vector<LazySegmentTree> seg(26, LazySegmentTree(vector<ll>(n + 2, 0)));
  for (int i = 0; i < n; i++) {
    int j = s[i] - 'a';
    seg[j].add(i, n, 1);
  }

  cin >> m;
  vector<int> ans;
  for (int i = 0; i < m; i++) {
    int flag;
    cin >> flag;
    if (flag == 1) {
      int temp;
      char temp1;
      cin >> temp >> temp1;
      temp--;
      char temp2 = s[temp];
      int j = temp1 - 'a';
      int j1 = temp2 - 'a';
      s[i] = temp1;
      seg[j].add(temp, n, 1);
      seg[j1].add(temp, n, -1);
    } else {
      int temp;
      int temp1;
      cin >> temp >> temp1;
      temp--;
      temp1--;
      int count = 0;
      // cout<<s<<"\n";
      for (int j = 0; j < 26; j++) {
        int sum =
            seg[j].getsum(temp1, temp1 + 1) - seg[j].getsum(temp - 1, temp);
        // cout<<sum<<" ";
        if (sum > 0) {
          count++;
        }
      }
      // cout<<"\n";
      ans.push_back(count);
    }
  }
  rep(i, ans.size()) { cout << ans[i] << "\n"; }
}


Fixed submission:
// #include <tourist>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> p;
const int INF = 1e9;
const ll LINF = ll(1e18) + 1;
const int MOD = 1000000007;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
const int Dx[8] = {0, 1, 1, 1, 0, -1, -1, -1},
          Dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
#define yes cout << "Yes" << endl
#define YES cout << "YES" << endl
#define no cout << "No" << endl
#define NO cout << "NO" << endl
#define rep(i, n) for (int i = 0; i < n; i++)
#define ALL(v) v.begin(), v.end()
#define debug(v)                                                               \
  cout << #v << ":";                                                           \
  for (auto x : v) {                                                           \
    cout << x << ' ';                                                          \
  }                                                                            \
  cout << endl;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
// cout<<fixed<<setprecision(15);有効数字15桁
//-std=c++14
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
int n, m;
vector<int> a;
// 区間和
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;

int N, Q;
struct LazySegmentTree {
private:
  int n;
  vector<ll> node, lazy;

public:
  LazySegmentTree(vector<ll> v) {
    int sz = (int)v.size();
    n = 1;
    while (n < sz)
      n *= 2;
    node.resize(2 * n - 1);
    lazy.resize(2 * n - 1, 0);

    for (int i = 0; i < sz; i++)
      node[i + n - 1] = v[i];
    for (int i = n - 2; i >= 0; i--)
      node[i] = node[i * 2 + 1] + node[i * 2 + 2];
  }

  void eval(int k, int l, int r) {
    if (lazy[k] != 0) {
      node[k] += lazy[k];
      if (r - l > 1) {
        lazy[2 * k + 1] += lazy[k] / 2;
        lazy[2 * k + 2] += lazy[k] / 2;
      }
      lazy[k] = 0;
    }
  }

  void add(int a, int b, ll x, int k = 0, int l = 0, int r = -1) {
    if (r < 0)
      r = n;
    eval(k, l, r);
    if (b <= l || r <= a)
      return;
    if (a <= l && r <= b) {
      lazy[k] += (r - l) * x;
      eval(k, l, r);
    } else {
      add(a, b, x, 2 * k + 1, l, (l + r) / 2);
      add(a, b, x, 2 * k + 2, (l + r) / 2, r);
      node[k] = node[2 * k + 1] + node[2 * k + 2];
    }
  }

  ll getsum(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r < 0)
      r = n;
    eval(k, l, r);
    if (b <= l || r <= a)
      return 0;
    if (a <= l && r <= b)
      return node[k];
    ll vl = getsum(a, b, 2 * k + 1, l, (l + r) / 2);
    ll vr = getsum(a, b, 2 * k + 2, (l + r) / 2, r);
    return vl + vr;
  }
};
string s;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n >> s;
  vector<LazySegmentTree> seg(26, LazySegmentTree(vector<ll>(n + 2, 0)));
  for (int i = 0; i < n; i++) {
    int j = s[i] - 'a';
    seg[j].add(i, n, 1);
  }

  cin >> m;
  vector<int> ans;
  for (int i = 0; i < m; i++) {
    int flag;
    cin >> flag;
    if (flag == 1) {
      int temp;
      char temp1;
      cin >> temp >> temp1;
      temp--;
      char temp2 = s[temp];
      int j = temp1 - 'a';
      int j1 = temp2 - 'a';
      s[temp] = temp1;
      seg[j].add(temp, n, 1);
      seg[j1].add(temp, n, -1);
    } else {
      int temp;
      int temp1;
      cin >> temp >> temp1;
      temp--;
      temp1--;
      int count = 0;
      // cout<<s<<"\n";
      for (int j = 0; j < 26; j++) {
        int sum =
            seg[j].getsum(temp1, temp1 + 1) - seg[j].getsum(temp - 1, temp);
        // cout<<sum<<" ";
        if (sum > 0) {
          count++;
        }
      }
      // cout<<"\n";
      ans.push_back(count);
    }
  }
  rep(i, ans.size()) { cout << ans[i] << "\n"; }
}