Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string.h>
#include <string>
#include <sys/time.h>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

#define endl '\\n'
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define UNIQ(v) (v).erase(unique((v).begin(), (v).end()), (v).end())

typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;
typedef complex<double> comp;
typedef vector<vector<ld>> matrix;
struct pairhash {
public:
  template <typename T, typename U>
  size_t operator()(const pair<T, U> &x) const {
    size_t seed = hash<T>()(x.first);
    return hash<U>()(x.second) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
  }
};
const ll inf = 1e18 + 9;
const ll mod = 998244353;
const double eps = 1e-8;
const double pi = acos(-1);

int n, q;
int c[500001];
int l[500001], r[500001];

int m[500001];
int k = 0;
void add(int v) {
  if (m[c[v]] == 0)
    k++;
  m[c[v]]++;
}

void del(int v) {
  m[c[v]]--;
  if (m[c[v]] == 0) {
    k--;
  }
}

void solve() {
  int sq = (int)sqrt(n);
  vector<T> qi(q);
  for (int i = 0; i < q; i++) {
    qi[i] = T(l[i] / sq, r[i], i);
  }
  sort(ALL(qi));

  int nl = 0, nr = 0;
  int res[q];
  for (T &t : qi) {
    int i = get<2>(t);
    while (nr < r[i]) {
      add(nr);
      nr++;
    }
    while (nl > l[i]) {
      nl--;
      add(nl);
    }
    while (nl < l[i]) {
      del(nl);
      nl++;
    }
    while (nr > r[i]) {
      nr--;
      del(nr);
    }
    res[i] = k;
  }

  for (int i = 0; i < q; i++) {
    printf("%d\\n", res[i]);
  }
}

void input() {
  scanf("%d%d", &n, &q);
  for (int i = 0; i < n; i++)
    scanf("%d", c + i);
  for (int i = 0; i < q; i++) {
    scanf("%d%d", l + i, r + i);
    l[i]--;
  }
}

int main() {
  input();
  solve();
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string.h>
#include <string>
#include <sys/time.h>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

#define endl '\\n'
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define UNIQ(v) (v).erase(unique((v).begin(), (v).end()), (v).end())

typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;
typedef complex<double> comp;
typedef vector<vector<ld>> matrix;
struct pairhash {
public:
  template <typename T, typename U>
  size_t operator()(const pair<T, U> &x) const {
    size_t seed = hash<T>()(x.first);
    return hash<U>()(x.second) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
  }
};
const ll inf = 1e18 + 9;
const ll mod = 998244353;
const double eps = 1e-8;
const double pi = acos(-1);

int n, q;
int c[500001];
int l[500001], r[500001];

int m[500001];
int k = 0;
void add(int v) {
  if (m[c[v]] == 0)
    k++;
  m[c[v]]++;
}

void del(int v) {
  m[c[v]]--;
  if (m[c[v]] == 0) {
    k--;
  }
}

void solve() {
  int sq = max(1, n / (int)sqrt(q));
  sq = max(1, sq - 10);
  vector<T> qi(q);
  for (int i = 0; i < q; i++) {
    qi[i] = T(l[i] / sq, r[i], i);
  }
  sort(ALL(qi));

  int nl = 0, nr = 0;
  int res[q];
  for (T &t : qi) {
    int i = get<2>(t);
    while (nr < r[i]) {
      add(nr);
      nr++;
    }
    while (nl > l[i]) {
      nl--;
      add(nl);
    }
    while (nl < l[i]) {
      del(nl);
      nl++;
    }
    while (nr > r[i]) {
      nr--;
      del(nr);
    }
    res[i] = k;
  }

  for (int i = 0; i < q; i++) {
    printf("%d\\n", res[i]);
  }
}

void input() {
  scanf("%d%d", &n, &q);
  for (int i = 0; i < n; i++)
    scanf("%d", c + i);
  for (int i = 0; i < q; i++) {
    scanf("%d%d", l + i, r + i);
    l[i]--;
  }
}

int main() {
  input();
  solve();
}