Problem p02763 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <ctype.h>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iostream>
#include <istream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define REP(i, a, b) for (int i = a; i < (b); ++i)
#define all(x) (x).begin(), (x).end()
#define pint pair<int, int>
#define SEG_LEN (1 << 23)
const int INF = 1000000007;
const long double EPS = 1e-15;
const long double PI = acos(-1);
typedef long long ll;
using namespace std;
using Graph = vector<vector<int>>;
using Graph_weighted = vector<vector<pair<int, int>>>;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }
ll ceil(const ll a, const ll b) { return (a + b - 1) / b; }
template <class T> class segment_tree_Max {
private:
  vector<long long> seg;
  int seg_size = SEG_LEN * 2;

public:
  segment_tree_Max() {
    seg.resize(seg_size);
    for (int i = 0; i < seg_size; i++)
      seg[i] = -INF;
  }
  void update(int ind, int n) {
    ind += SEG_LEN;
    seg[ind] = n;
    while (true) {
      ind /= 2;
      if (ind == 0)
        break;
      seg[ind] = max(seg[ind * 2], seg[ind * 2 + 1]);
    }
  }
  long long Max(int l, int r) {
    l += SEG_LEN;
    r += SEG_LEN + 1;
    long long ans = -INF;
    while (l < r) {
      if (l % 2 == 1) {
        ans = max(seg[l], ans);
        l++;
      }
      if (l >= r)
        break;
      l /= 2;
      if (r % 2 == 1) {
        ans = max(seg[r - 1], ans);
        r--;
      }
      r /= 2;
    }
    if (ans == -INF)
      return 0;
    return ans;
  }
};
int main() {
  int n, q;
  string s;
  cin >> n >> s >> q;
  vector<segment_tree_Max<ll>> seg(26);
  rep(i, n) seg[s[i] - 'a'].update(i, 1);
  rep(i, q) {
    int type;
    cin >> type;
    if (type == 1) {
      int ind;
      char c;
      cin >> ind >> c;
      ind--;
      seg[s[ind] - 'a'].update(ind, 0);
      seg[c - 'a'].update(ind, 1);
      s[ind] = c;
    }
    if (type == 2) {
      ll tmp = 0;
      int l, r;
      cin >> l >> r;
      l--, r--;
      rep(j, 26) tmp += seg[j].Max(l, r);
      cout << tmp << endl;
    }
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <ctype.h>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iostream>
#include <istream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define REP(i, a, b) for (int i = a; i < (b); ++i)
#define all(x) (x).begin(), (x).end()
#define pint pair<int, int>
#define SEG_LEN (1 << 20)
const int INF = 1000000007;
const long double EPS = 1e-15;
const long double PI = acos(-1);
typedef long long ll;
using namespace std;
using Graph = vector<vector<int>>;
using Graph_weighted = vector<vector<pair<int, int>>>;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }
ll ceil(const ll a, const ll b) { return (a + b - 1) / b; }
template <class T> class segment_tree_Max {
private:
  vector<long long> seg;
  int seg_size = SEG_LEN * 2;

public:
  segment_tree_Max() {
    seg.resize(seg_size);
    for (int i = 0; i < seg_size; i++)
      seg[i] = -INF;
  }
  void update(int ind, int n) {
    ind += SEG_LEN;
    seg[ind] = n;
    while (true) {
      ind /= 2;
      if (ind == 0)
        break;
      seg[ind] = max(seg[ind * 2], seg[ind * 2 + 1]);
    }
  }
  long long Max(int l, int r) {
    l += SEG_LEN;
    r += SEG_LEN + 1;
    long long ans = -INF;
    while (l < r) {
      if (l % 2 == 1) {
        ans = max(seg[l], ans);
        l++;
      }
      if (l >= r)
        break;
      l /= 2;
      if (r % 2 == 1) {
        ans = max(seg[r - 1], ans);
        r--;
      }
      r /= 2;
    }
    if (ans == -INF)
      return 0;
    return ans;
  }
};
int main() {
  int n, q;
  string s;
  cin >> n >> s >> q;
  vector<segment_tree_Max<ll>> seg(26);
  rep(i, n) seg[s[i] - 'a'].update(i, 1);
  rep(i, q) {
    int type;
    cin >> type;
    if (type == 1) {
      int ind;
      char c;
      cin >> ind >> c;
      ind--;
      seg[s[ind] - 'a'].update(ind, 0);
      seg[c - 'a'].update(ind, 1);
      s[ind] = c;
    }
    if (type == 2) {
      ll tmp = 0;
      int l, r;
      cin >> l >> r;
      l--, r--;
      rep(j, 26) tmp += seg[j].Max(l, r);
      cout << tmp << endl;
    }
  }
  return 0;
}