Problem p02763 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

struct SegTree {
  int N;
  vector<vector<int>> seg;

  void init(int n) {
    N = 1;
    while (N < n)
      N <<= 1;
    seg = vector<vector<int>>(2 * N, vector<int>(26, 0));
  }

  void gather(int u) {
    for (int i = 0; i < 26; i++)
      seg[u][i] = seg[u * 2 + 1][i] + seg[u * 2 + 2][i];
  }

  void build(int u, int l, int r, vector<char> &a) {
    if (r - l == 1) {
      if (l >= (int)a.size())
        return;
      seg[u][a[l] - 'a'] = 1;
      return;
    }

    int m = (l + r) / 2;
    build(u * 2 + 1, l, m, a);
    build(u * 2 + 2, m, r, a);
    gather(u);
  }

  void update(int idx, int l, int r, int u, int x) {
    if (r - l == 1) {
      if (idx == l) {
        for (int i = 0; i < 26; i++)
          seg[u][i] = 0;
        seg[u][x] = 1;
      }
      return;
    }

    int m = (l + r) / 2;
    update(idx, l, m, u * 2 + 1, x);
    update(idx, m, r, u * 2 + 2, x);
    gather(u);
  }

  vector<int> query(int a, int b, int u, int l, int r) {
    if (r <= a || b <= l) {
      vector<int> res(26, 0);
      return res;
    }
    if (a <= l && r <= b)
      return seg[u];

    int m = (l + r) / 2;
    auto res1 = query(a, b, u * 2 + 1, l, m);
    auto res2 = query(a, b, u * 2 + 2, m, r);

    vector<int> res(26, 0);
    for (int i = 0; i < 26; i++)
      res[i] = res1[i] + res2[i];

    return res;
  }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n;
  cin >> n;

  vector<char> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];

  SegTree seg;
  seg.init(n);
  seg.build(0, 0, seg.N, a);

  int Q;
  cin >> Q;
  while (Q--) {
    int t;
    cin >> t;

    if (t == 1) {
      int idx;
      char c;
      cin >> idx >> c;
      seg.update(idx - 1, 0, seg.N, 0, c - 'a');
    } else {
      int l, r;
      cin >> l >> r;
      l--;

      auto res = seg.query(l, r, 0, 0, seg.N);
      int cnt = 0;
      for (auto i : res) {
        if (i >= 1)
          cnt += 1;
      }

      cout << cnt << '\n';
    }
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

struct SegTree {
  int N;
  vector<vector<int>> seg;

  void init(int n) {
    N = 1;
    while (N < n)
      N <<= 1;
    seg = vector<vector<int>>(2 * N, vector<int>(26, 0));
  }

  void gather(int u) {
    for (int i = 0; i < 26; i++)
      seg[u][i] = seg[u * 2 + 1][i] + seg[u * 2 + 2][i];
  }

  void build(int u, int l, int r, vector<char> &a) {
    if (r - l == 1) {
      if (l >= (int)a.size())
        return;
      seg[u][a[l] - 'a'] = 1;
      return;
    }

    int m = (l + r) / 2;
    build(u * 2 + 1, l, m, a);
    build(u * 2 + 2, m, r, a);
    gather(u);
  }

  void update(int idx, int l, int r, int u, int x) {
    if (idx < l || idx >= r)
      return;
    if (r - l == 1) {
      if (idx == l) {
        for (int i = 0; i < 26; i++)
          seg[u][i] = 0;
        seg[u][x] = 1;
      }
      return;
    }

    int m = (l + r) / 2;
    update(idx, l, m, u * 2 + 1, x);
    update(idx, m, r, u * 2 + 2, x);
    gather(u);
  }

  vector<int> query(int a, int b, int u, int l, int r) {
    if (r <= a || b <= l) {
      vector<int> res(26, 0);
      return res;
    }
    if (a <= l && r <= b)
      return seg[u];

    int m = (l + r) / 2;
    auto res1 = query(a, b, u * 2 + 1, l, m);
    auto res2 = query(a, b, u * 2 + 2, m, r);

    vector<int> res(26, 0);
    for (int i = 0; i < 26; i++)
      res[i] = res1[i] + res2[i];

    return res;
  }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n;
  cin >> n;

  vector<char> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];

  SegTree seg;
  seg.init(n);
  seg.build(0, 0, seg.N, a);

  int Q;
  cin >> Q;
  while (Q--) {
    int t;
    cin >> t;

    if (t == 1) {
      int idx;
      char c;
      cin >> idx >> c;
      seg.update(idx - 1, 0, seg.N, 0, c - 'a');
    } else {
      int l, r;
      cin >> l >> r;
      l--;

      auto res = seg.query(l, r, 0, 0, seg.N);
      int cnt = 0;
      for (auto i : res) {
        if (i >= 1)
          cnt += 1;
      }

      cout << cnt << '\n';
    }
  }

  return 0;
}