Problem p02763 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("O3")
#pragma GCC target("avx")
// #include<bits/stdc++.h>
#include <cstdio>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please

const int AS = 500000;
char cn[AS], *ci = cn, ct;
inline int getint() {
  int A = 0;
  while ((ct = *ci++) >= '0')
    A = A * 10 + ct - '0';
  return A;
}
char *di = cn;
int dat[2 * AS];

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

  int N = 0;
  while ((ct = getchar_unlocked()) >= '0')
    N = N * 10 + ct - '0';
  fread(cn, 1, N + 1, stdin);
  auto da = dat + AS;
  rep(i, N) *da++ = (1 << *ci++ - 'a');
  da = dat + AS - 1;
  for (auto db = dat + AS * 2 - 1; da > dat; da--)
    *da = *db-- | *db--;

  ci = cn;
  fread(cn, 1, AS, stdin);
  int Q = getint();

  rep(j, Q) {
    char q = *ci++;
    ci++;
    if (q == '1') {
      int i = getint() - 1 + AS;
      for (dat[i] = 1 << *ci++ - 'a'; i > 1; i >>= 1)
        dat[i >> 1] = dat[i] | dat[i ^ 1];
      ci++;
    } else {
      int ret = 0;
      for (int l = getint() - 1 + AS, r = getint() + AS; l < r;
           l >>= 1, r >>= 1) {
        if (l & 1)
          ret |= dat[l++];
        if (r & 1)
          ret |= dat[--r];
      }
      int kotae = __builtin_popcount(ret);
      int ue = kotae / 10;
      if (ue)
        *di++ = ue + '0';
      *di++ = kotae - ue * 10 + '0';
      *di++ = '\n';
    }
  }
  fwrite(cn, 1, di - cn, stdout);

  Would you please return 0;
}

Fixed submission:
#pragma GCC optimize("O3")
#pragma GCC target("avx")
// #include<bits/stdc++.h>
#include <cstdio>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please

const int AS = 500001;
char cn[AS], *ci = cn, ct;
inline int getint() {
  int A = 0;
  while ((ct = *ci++) >= '0')
    A = A * 10 + ct - '0';
  return A;
}
char *di = cn;
int dat[2 * AS];

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

  int N = 0;
  while ((ct = getchar_unlocked()) >= '0')
    N = N * 10 + ct - '0';
  fread(cn, 1, N + 1, stdin);
  auto da = dat + AS;
  rep(i, N) *da++ = (1 << *ci++ - 'a');
  da = dat + AS - 1;
  for (auto db = dat + AS * 2 - 1; da > dat; da--)
    *da = *db-- | *db--;

  ci = cn;
  fread(cn, 1, AS, stdin);
  int Q = getint();

  rep(j, Q) {
    char q = *ci++;
    ci++;
    if (q == '1') {
      int i = getint() - 1 + AS;
      for (dat[i] = 1 << *ci++ - 'a'; i > 1; i >>= 1)
        dat[i >> 1] = dat[i] | dat[i ^ 1];
      ci++;
    } else {
      int ret = 0;
      for (int l = getint() - 1 + AS, r = getint() + AS; l < r;
           l >>= 1, r >>= 1) {
        if (l & 1)
          ret |= dat[l++];
        if (r & 1)
          ret |= dat[--r];
      }
      int kotae = __builtin_popcount(ret);
      int ue = kotae / 10;
      if (ue)
        *di++ = ue + '0';
      *di++ = kotae - ue * 10 + '0';
      *di++ = '\n';
    }
  }
  fwrite(cn, 1, di - cn, stdout);

  Would you please return 0;
}