Problem p02599 (C++)

Buggy submission (status: Time Limit Exceeded):
#define taskname "test"

#include <bits/stdc++.h>

using namespace std;

#define sz(x) (int)x.size()
#define fi first
#define se second

typedef long long lli;
typedef pair<int, int> pii;

const int maxn = 5e5 + 5;
const int K = 710;

int n, Q;
int a[maxn];
pair<pii, int> query[maxn];

int cnt[maxn], ans[maxn];

template <typename T> void Read(T &x) {
  bool Neg = false;
  char c;
  for (c = getchar(); c < '0' || c > '9'; c = getchar())
    if (c == '-')
      Neg = !Neg;
  x = c - '0';
  for (c = getchar(); c >= '0' && c <= '9'; c = getchar())
    x = x * 10 + c - '0';
  if (Neg && x > 0)
    x = -x;
}

template <typename T> void WriteU(T x) {
  if (x > 9)
    WriteU(x / 10);
  putchar(x % 10 + '0');
}

void read_input() {
  // cin >> n >> Q;
  Read(n);
  Read(Q);
  for (int i = 1; i <= n; ++i)
    // cin >> a[i];
    Read(a[i]);
  for (int i = 1; i <= Q; ++i) {
    int l, r;
    // cin >> l >> r;
    Read(l);
    Read(r);
    query[i] = make_pair(pii(l, r), i);
  }
}

bool cmp(const pair<pii, int> &x, const pair<pii, int> &y) {
  if (x.fi.fi / K == y.fi.fi / K)
    return x.fi.se < y.fi.se;
  return x.fi.fi < y.fi.fi;
}

void update(int l, int r, int &cur) {
  static int curl = 1, curr = 0;
  while (curr < r) {
    ++curr;
    ++cnt[a[curr]];
    if (cnt[a[curr]] == 1)
      ++cur;
  }
  while (curr > r) {
    --cnt[a[curr]];
    if (cnt[a[curr]] == 0)
      --cur;
    --curr;
  }
  while (curl < l) {
    --cnt[a[curl]];
    if (cnt[a[curl]] == 0)
      --cur;
    ++curl;
  }
  while (curl > l) {
    --curl;
    ++cnt[a[curl]];
    if (cnt[a[curl]] == 1)
      ++cur;
  }
}

void solve() {
  sort(query + 1, query + Q + 1, cmp);
  fill(begin(cnt), end(cnt), 0);
  int cur = 0;
  for (int i = 1; i <= Q; ++i) {
    int l = query[i].fi.fi, r = query[i].fi.se;
    int id = query[i].se;
    update(l, r, cur);
    ans[id] = cur;
  }
  for (int i = 1; i <= Q; ++i) {
    WriteU(ans[i]);
    putchar('\n');
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  read_input();
  solve();
}


Fixed submission:
#define taskname "test"

#include <bits/stdc++.h>

using namespace std;

#define sz(x) (int)x.size()
#define fi first
#define se second

typedef long long lli;
typedef pair<int, int> pii;

const int maxn = 5e5 + 5;
const int K = 710;

int n, Q;
int a[maxn];
pair<pii, int> query[maxn];

int cnt[maxn], ans[maxn];

template <typename T> void Read(T &x) {
  bool Neg = false;
  char c;
  for (c = getchar(); c < '0' || c > '9'; c = getchar())
    if (c == '-')
      Neg = !Neg;
  x = c - '0';
  for (c = getchar(); c >= '0' && c <= '9'; c = getchar())
    x = x * 10 + c - '0';
  if (Neg && x > 0)
    x = -x;
}

template <typename T> void WriteU(T x) {
  if (x > 9)
    WriteU(x / 10);
  putchar(x % 10 + '0');
}

void read_input() {
  // cin >> n >> Q;
  Read(n);
  Read(Q);
  for (int i = 1; i <= n; ++i)
    // cin >> a[i];
    Read(a[i]);
  for (int i = 1; i <= Q; ++i) {
    int l, r;
    // cin >> l >> r;
    Read(l);
    Read(r);
    query[i] = make_pair(pii(l, r), i);
  }
}

bool cmp(const pair<pii, int> &x, const pair<pii, int> &y) {
  if (x.fi.fi / K == y.fi.fi / K)
    return x.fi.se < y.fi.se;
  return x.fi.fi < y.fi.fi;
}

inline void update(const int &l, const int &r, int &cur) {
  static int curl = 1, curr = 0;
  while (curr < r) {
    ++curr;
    ++cnt[a[curr]];
    if (cnt[a[curr]] == 1)
      ++cur;
  }
  while (curr > r) {
    --cnt[a[curr]];
    if (cnt[a[curr]] == 0)
      --cur;
    --curr;
  }
  while (curl < l) {
    --cnt[a[curl]];
    if (cnt[a[curl]] == 0)
      --cur;
    ++curl;
  }
  while (curl > l) {
    --curl;
    ++cnt[a[curl]];
    if (cnt[a[curl]] == 1)
      ++cur;
  }
}

void solve() {
  sort(query + 1, query + Q + 1, cmp);
  fill(begin(cnt), end(cnt), 0);
  int cur = 0;
  for (int i = 1; i <= Q; ++i) {
    int l = query[i].fi.fi, r = query[i].fi.se;
    int id = query[i].se;
    update(l, r, cur);
    ans[id] = cur;
  }
  for (int i = 1; i <= Q; ++i) {
    WriteU(ans[i]);
    putchar('\n');
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  read_input();
  solve();
}