Problem p02763 (C++)

Buggy submission (status: Runtime Error):
// Md.Fahd
// HSTU CSE18

#include <bits/stdc++.h>
#define READ_IN freopen("Input.txt", "r", stdin);
using namespace std;
const int maxe = 100005;
struct can {
  int aa[26];
} tree[maxe * 3];

char ar[maxe];

void make(int node, int b, int e) {
  if (b == e) {
    tree[node].aa[ar[b] - 'a']++;

    return;
  }

  int left = node * 2;
  int right = node * 2 + 1;
  int mid = (b + e) / 2;

  make(left, b, mid);
  make(right, mid + 1, e);

  for (int i = 0; i < 26; i++) {
    tree[node].aa[i] = tree[left].aa[i] + tree[right].aa[i];
  }

  // tree[node].aa=tree[left].aa + tree[right].aa;
}

vector<int> ck(int node, int b, int e, int i, int j) {
  if (i > e || j < b) {
    vector<int> as(26, 0);
    return as;
  }

  if (b >= i && e <= j) {

    vector<int> as(26, 0);
    for (int i = 0; i < 26; i++) {
      as[i] = tree[node].aa[i];
    }
    return as;
  }

  int left = node * 2;
  int right = node * 2 + 1;
  int mid = (b + e) / 2;
  vector<int> p1(26, 0);
  p1 = ck(left, b, mid, i, j);
  vector<int> p2(26, 0);
  p2 = ck(right, mid + 1, e, i, j);

  vector<int> p3(26, 0);

  for (int i = 0; i < 26; i++) {
    p3[i] += p1[i];
    p3[i] += p2[i];
  }

  return p3;
}

void update(int node, int b, int e, int i, int newvalue) {
  if (i > e || i < b)
    return;

  if (b == i && e == i) {

    tree[node].aa[ar[b] - 'a'] = 0;
    for (int i = 0; i < 26; i++) {
      tree[node].aa[i] = 0;
    }
    ar[b] = newvalue;
    tree[node].aa[newvalue - 'a']++;

    return;
  }

  int left = node * 2;
  int right = node * 2 + 1;
  int mid = (b + e) / 2;

  update(left, b, mid, i, newvalue);
  update(right, mid + 1, e, i, newvalue);

  for (int i = 0; i < 26; i++) {
    tree[node].aa[i] = tree[left].aa[i] + tree[right].aa[i];
  }
}

int main(void) {
  // READ_IN

  int ff;
  cin >> ff;

  string ss;
  cin >> ss;

  for (int i = 1; i <= ss.size(); i++) {
    ar[i] = ss[i - 1];
  }

  int n = ss.size();

  make(1, 1, n);

  int q;
  cin >> q;

  while (q--) {
    int a;
    cin >> a;

    if (a == 1) {
      int index;
      cin >> index;
      char value;
      cin >> value;

      update(1, 1, n, index, value);
    } else {
      int l, r;
      cin >> l >> r;

      vector<int> aa(26, 0);

      aa = ck(1, 1, n, l, r);

      int cnt = 0;
      for (int i = 0; i < 26; i++) {
        if (aa[i] > 0)
          cnt++;
      }
      cout << cnt << endl;
    }
  }

  return 0;
}


Fixed submission:
// Md.Fahd
// HSTU CSE18

#include <bits/stdc++.h>
#define READ_IN freopen("Input.txt", "r", stdin);
using namespace std;
const int maxe = 500005;
struct can {
  int aa[26];
} tree[maxe * 3];

char ar[maxe];

void make(int node, int b, int e) {
  if (b == e) {
    tree[node].aa[ar[b] - 'a']++;

    return;
  }

  int left = node * 2;
  int right = node * 2 + 1;
  int mid = (b + e) / 2;

  make(left, b, mid);
  make(right, mid + 1, e);

  for (int i = 0; i < 26; i++) {
    tree[node].aa[i] = tree[left].aa[i] + tree[right].aa[i];
  }

  // tree[node].aa=tree[left].aa + tree[right].aa;
}

vector<int> ck(int node, int b, int e, int i, int j) {
  if (i > e || j < b) {
    vector<int> as(26, 0);
    return as;
  }

  if (b >= i && e <= j) {

    vector<int> as(26, 0);
    for (int i = 0; i < 26; i++) {
      as[i] = tree[node].aa[i];
    }
    return as;
  }

  int left = node * 2;
  int right = node * 2 + 1;
  int mid = (b + e) / 2;
  vector<int> p1(26, 0);
  p1 = ck(left, b, mid, i, j);
  vector<int> p2(26, 0);
  p2 = ck(right, mid + 1, e, i, j);

  vector<int> p3(26, 0);

  for (int i = 0; i < 26; i++) {
    p3[i] += p1[i];
    p3[i] += p2[i];
  }

  return p3;
}

void update(int node, int b, int e, int i, int newvalue) {
  if (i > e || i < b)
    return;

  if (b == i && e == i) {

    tree[node].aa[ar[b] - 'a'] = 0;
    for (int i = 0; i < 26; i++) {
      tree[node].aa[i] = 0;
    }
    ar[b] = newvalue;
    tree[node].aa[newvalue - 'a']++;

    return;
  }

  int left = node * 2;
  int right = node * 2 + 1;
  int mid = (b + e) / 2;

  update(left, b, mid, i, newvalue);
  update(right, mid + 1, e, i, newvalue);

  for (int i = 0; i < 26; i++) {
    tree[node].aa[i] = tree[left].aa[i] + tree[right].aa[i];
  }
}

int main(void) {
  // READ_IN

  int ff;
  cin >> ff;

  string ss;
  cin >> ss;

  for (int i = 1; i <= ss.size(); i++) {
    ar[i] = ss[i - 1];
  }

  int n = ss.size();

  make(1, 1, n);

  int q;
  cin >> q;

  while (q--) {
    int a;
    cin >> a;

    if (a == 1) {
      int index;
      cin >> index;
      char value;
      cin >> value;

      update(1, 1, n, index, value);
    } else {
      int l, r;
      cin >> l >> r;

      vector<int> aa(26, 0);

      aa = ck(1, 1, n, l, r);

      int cnt = 0;
      for (int i = 0; i < 26; i++) {
        if (aa[i] > 0)
          cnt++;
      }
      cout << cnt << endl;
    }
  }

  return 0;
}