Problem p02763 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define pp pair<pair<ll, ll>, pair<ll, ll>>
#define pll pair<ll, ll>
#define pdd pair<double, double>
#define vii vector<int>
#define vll vector<ll>
#define mat vector<vector<ll>>
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define fi first
#define sc second
#define rep(i, n) for (ll i = 0; i < n; i++)
#define rep2(i, a, b) for (ll i = a; i < b; i++)
#define repr(i, n) for (ll i = n - 1; i >= 0; i--)
#define all(x) x.begin(), x.end()
#define sz(x) (ll)(x).size()
#define pq priority_queue<ll>
#define pqg priority_queue<ll, vector<ll>, greater<ll>>
#define LB(v, x) (lower_bound(v.begin(), v.end(), x) - v.begin())
#define UB(v, x) (upper_bound(v.begin(), v.end(), x) - v.begin())
#define ERASE(v)                                                               \
  sort(v.begin(), v.end());                                                    \
  v.erase(unique(v.begin(), v.end()), v.end())
#define int ll
using namespace std;
const ll INF = (1 << 30) - 1;
const ll LLINF = (1LL << 60LL);
const ll MOD = 1000000007;
const ll mod = 998244353;
const ll MAX = 1100000;
const double pi = acos(-1);
const double eps = 1e-10;
ll dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

struct Timer {
  chrono::system_clock::time_point start, end;
  Timer() { start = chrono::system_clock::now(); }
  ~Timer() {
    end = chrono::system_clock::now();
    auto msec =
        chrono::duration_cast<chrono::milliseconds>(end - start).count();
    cerr << "time : " << msec << " ms" << endl;
  }
};

struct SegmentTree {
  static const int MAX_N = 1 << 18;
  int n, dat[2 * MAX_N - 1];
  SegmentTree(int n_) {
    n = 1;
    while (n < n_)
      n *= 2;
    for (int i = 0; i < 2 * n - 1; i++)
      dat[i] = 0;
  }
  void update(int k, int a) {
    k += n - 1;
    dat[k] = (1 << a);
    while (k > 0) {
      k = (k - 1) / 2;
      // dat[k]=min(dat[k*2+1],dat[k*2+2]);
      dat[k] = (dat[k * 2 + 1] | dat[k * 2 + 2]);
    }
  }
  void print() {
    rep(i, n * 2) {
      // if(i==n-1) cout<<"//";
      // cout<<dat[i]<<" ";
    }
    // cout<<endl;
  }
  int query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return 0;
    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 (vr | vl);
    }
  }
  int query(int a, int b) { return query(a, b, 0, 0, n); }
};

void solve() {
  ll n, q;
  cin >> n;
  string s;
  cin >> s >> q;
  SegmentTree sg(n);
  rep(i, n) { sg.update(i, s[i] - 'a'); }
  // sg.print();
  // cout<<"------"<<endl;
  rep(i, q) {
    // sg.print();
    ll c;
    cin >> c;
    if (c == 1) {
      ll a;
      char b;
      cin >> a >> b;
      sg.update(a - 1, b - 'a');
    } else {
      ll a, b;
      cin >> a >> b;
      ll t = sg.query(a - 1, b);
      ll sum = 0;
      rep(j, 26) {
        if (t & (1 << j))
          sum++;
      }
      cout << sum << endl;
    }
  }
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  // Timer TM;

  solve();

  return 0;
}


Fixed submission:
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define pp pair<pair<ll, ll>, pair<ll, ll>>
#define pll pair<ll, ll>
#define pdd pair<double, double>
#define vii vector<int>
#define vll vector<ll>
#define mat vector<vector<ll>>
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define fi first
#define sc second
#define rep(i, n) for (ll i = 0; i < n; i++)
#define rep2(i, a, b) for (ll i = a; i < b; i++)
#define repr(i, n) for (ll i = n - 1; i >= 0; i--)
#define all(x) x.begin(), x.end()
#define sz(x) (ll)(x).size()
#define pq priority_queue<ll>
#define pqg priority_queue<ll, vector<ll>, greater<ll>>
#define LB(v, x) (lower_bound(v.begin(), v.end(), x) - v.begin())
#define UB(v, x) (upper_bound(v.begin(), v.end(), x) - v.begin())
#define ERASE(v)                                                               \
  sort(v.begin(), v.end());                                                    \
  v.erase(unique(v.begin(), v.end()), v.end())
#define int ll
using namespace std;
const ll INF = (1 << 30) - 1;
const ll LLINF = (1LL << 60LL);
const ll MOD = 1000000007;
const ll mod = 998244353;
const ll MAX = 1100000;
const double pi = acos(-1);
const double eps = 1e-10;
ll dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

struct Timer {
  chrono::system_clock::time_point start, end;
  Timer() { start = chrono::system_clock::now(); }
  ~Timer() {
    end = chrono::system_clock::now();
    auto msec =
        chrono::duration_cast<chrono::milliseconds>(end - start).count();
    cerr << "time : " << msec << " ms" << endl;
  }
};

struct SegmentTree {
  static const int MAX_N = 1 << 19;
  int n, dat[2 * MAX_N - 1];
  SegmentTree(int n_) {
    n = 1;
    while (n < n_)
      n *= 2;
    for (int i = 0; i < 2 * n - 1; i++)
      dat[i] = 0;
  }
  void update(int k, int a) {
    k += n - 1;
    dat[k] = (1 << a);
    while (k > 0) {
      k = (k - 1) / 2;
      // dat[k]=min(dat[k*2+1],dat[k*2+2]);
      dat[k] = (dat[k * 2 + 1] | dat[k * 2 + 2]);
    }
  }
  void print() {
    rep(i, n * 2) {
      // if(i==n-1) cout<<"//";
      // cout<<dat[i]<<" ";
    }
    // cout<<endl;
  }
  int query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return 0;
    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 (vr | vl);
    }
  }
  int query(int a, int b) { return query(a, b, 0, 0, n); }
};

void solve() {
  ll n, q;
  cin >> n;
  string s;
  cin >> s >> q;
  SegmentTree sg(n);
  rep(i, n) { sg.update(i, s[i] - 'a'); }
  // sg.print();
  // cout<<"------"<<endl;
  rep(i, q) {
    // sg.print();
    ll c;
    cin >> c;
    if (c == 1) {
      ll a;
      char b;
      cin >> a >> b;
      sg.update(a - 1, b - 'a');
    } else {
      ll a, b;
      cin >> a >> b;
      ll t = sg.query(a - 1, b);
      ll sum = 0;
      rep(j, 26) {
        if (t & (1 << j))
          sum++;
      }
      cout << sum << endl;
    }
  }
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  // Timer TM;

  solve();

  return 0;
}