Problem p02763 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
ll N, S, Q;
ll q[500001];
map<char, int>::iterator ite;
ll A[500001][20][26];
int B[500001];
int ans[26];

int henkan(char s) {
  if (s == 'a')
    return 0;
  if (s == 'b')
    return 1;
  if (s == 'c')
    return 2;
  if (s == 'd')
    return 3;
  if (s == 'e')
    return 4;
  if (s == 'f')
    return 5;
  if (s == 'g')
    return 6;
  if (s == 'h')
    return 7;
  if (s == 'i')
    return 8;
  if (s == 'j')
    return 9;
  if (s == 'k')
    return 10;
  if (s == 'l')
    return 11;
  if (s == 'm')
    return 12;
  if (s == 'n')
    return 13;
  if (s == 'o')
    return 14;
  if (s == 'p')
    return 15;
  if (s == 'q')
    return 16;
  if (s == 'r')
    return 17;
  if (s == 's')
    return 18;
  if (s == 't')
    return 19;
  if (s == 'u')
    return 20;
  if (s == 'v')
    return 21;
  if (s == 'w')
    return 22;
  if (s == 'x')
    return 23;
  if (s == 'y')
    return 24;
  if (s == 'z')
    return 25;
  else
    return 0;
}
int main() {
  cin >> N;
  int NN = N;
  for (int i = 0; i < N; i++) {
    char s;
    cin >> s;
    int a = henkan(s);
    A[i][0][a] = 1;
    B[i] = a;
  }
  int h = 1;
  N /= 2;
  while (N > 0) {
    for (int i = 0; i < N; i++) {
      for (int k = 0; k < 26; k++) {
        A[i][h][k] = A[2 * i][h - 1][k] + A[2 * i + 1][h - 1][k];
      }
    }
    N /= 2;
    h++;
  }

  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int a;
    cin >> a;
    if (a == 1) {
      int b;
      char c;
      cin >> b >> c;
      int d = henkan(c);
      int e = B[b - 1];
      B[b - 1] = d;
      int bb = b;
      int j = 0;
      int NNN = NN;
      while (bb <= NNN) {
        A[bb - 1][j][e] -= 1;
        A[bb - 1][j][d] += 1;
        bb = (bb + 1) / 2;
        NNN /= 2;
        j++;
      }
    } else {
      int b, c;
      cin >> b >> c;
      int k = 0;
      while (c > 0) {
        if (c % 2 == 1) {
          for (int j = 0; j < 26; j++) {
            ans[j] += A[c - 1][k][j];
          }
        }
        k++;
        c /= 2;
      }
      k = 0;
      b = b - 1;
      while (b > 0) {
        if (b % 2 == 1) {
          for (int j = 0; j < 26; j++) {
            ans[j] -= A[b - 1][k][j];
          }
        }
        k++;
        b /= 2;
      }

      int kai = 0;
      for (int j = 0; j < 26; j++) {
        if (ans[j] != 0) {
          kai++;
          ans[j] = 0;
        }
      }
      cout << kai << endl;
    }
  }
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
int N, S, Q;
int A[500001][20][26];
int B[500001];
int ans[26];

int henkan(char s) {
  if (s == 'a')
    return 0;
  if (s == 'b')
    return 1;
  if (s == 'c')
    return 2;
  if (s == 'd')
    return 3;
  if (s == 'e')
    return 4;
  if (s == 'f')
    return 5;
  if (s == 'g')
    return 6;
  if (s == 'h')
    return 7;
  if (s == 'i')
    return 8;
  if (s == 'j')
    return 9;
  if (s == 'k')
    return 10;
  if (s == 'l')
    return 11;
  if (s == 'm')
    return 12;
  if (s == 'n')
    return 13;
  if (s == 'o')
    return 14;
  if (s == 'p')
    return 15;
  if (s == 'q')
    return 16;
  if (s == 'r')
    return 17;
  if (s == 's')
    return 18;
  if (s == 't')
    return 19;
  if (s == 'u')
    return 20;
  if (s == 'v')
    return 21;
  if (s == 'w')
    return 22;
  if (s == 'x')
    return 23;
  if (s == 'y')
    return 24;
  if (s == 'z')
    return 25;
  else
    return 0;
}
int main() {
  cin >> N;
  int NN = N;
  for (int i = 0; i < N; i++) {
    char s;
    cin >> s;
    int a = henkan(s);
    A[i][0][a] = 1;
    B[i] = a;
  }
  int h = 1;
  N /= 2;
  while (N > 0) {
    for (int i = 0; i < N; i++) {
      for (int k = 0; k < 26; k++) {
        A[i][h][k] = A[2 * i][h - 1][k] + A[2 * i + 1][h - 1][k];
      }
    }
    N /= 2;
    h++;
  }

  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int a;
    cin >> a;
    if (a == 1) {
      int b;
      char c;
      cin >> b >> c;
      int d = henkan(c);
      int e = B[b - 1];
      B[b - 1] = d;
      int bb = b;
      int j = 0;
      int NNN = NN;
      while (bb <= NNN) {
        A[bb - 1][j][e] -= 1;
        A[bb - 1][j][d] += 1;
        bb = (bb + 1) / 2;
        NNN /= 2;
        j++;
      }
    } else {
      int b, c;
      cin >> b >> c;
      int k = 0;
      while (c > 0) {
        if (c % 2 == 1) {
          for (int j = 0; j < 26; j++) {
            ans[j] += A[c - 1][k][j];
          }
        }
        k++;
        c /= 2;
      }
      k = 0;
      b = b - 1;
      while (b > 0) {
        if (b % 2 == 1) {
          for (int j = 0; j < 26; j++) {
            ans[j] -= A[b - 1][k][j];
          }
        }
        k++;
        b /= 2;
      }

      int kai = 0;
      for (int j = 0; j < 26; j++) {
        if (ans[j] != 0) {
          kai++;
          ans[j] = 0;
        }
      }
      cout << kai << endl;
    }
  }
}