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

#line 2 "/Users/kaage/Desktop/ProgrammingWorkspace/library/other/template.hpp"
#define _CRT_SECURE_NO_WARNINGS
#pragma target("avx")
#pragma optimize("O3")
#pragma optimize("unroll-loops")
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = 1; i <= (n); i++)
#define all(V) V.begin(), V.end()
typedef long long lint;
typedef unsigned long long ulint;
typedef std::pair<int, int> P;
typedef std::pair<lint, lint> LP;
constexpr int INF = INT_MAX / 2;
constexpr lint LINF = LLONG_MAX / 2;
constexpr double eps = DBL_EPSILON;
constexpr double PI = 3.141592653589793238462643383279;
template <class T>
class prique : public std::priority_queue<T, std::vector<T>, std::greater<T>> {
};
template <class T, class U> inline bool chmax(T &lhs, const U &rhs) {
  if (lhs < rhs) {
    lhs = rhs;
    return 1;
  }
  return 0;
}
template <class T, class U> inline bool chmin(T &lhs, const U &rhs) {
  if (lhs > rhs) {
    lhs = rhs;
    return 1;
  }
  return 0;
}
inline lint gcd(lint a, lint b) {
  while (b) {
    lint c = a;
    a = b;
    b = c % b;
  }
  return a;
}
inline lint lcm(lint a, lint b) { return a / gcd(a, b) * b; }
bool isprime(lint n) {
  if (n == 1)
    return false;
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0)
      return false;
  }
  return true;
}
template <typename T> T mypow(T a, lint b) {
  if (!b)
    return T(1);
  if (b & 1)
    return mypow(a, b - 1) * a;
  T memo = mypow(a, b >> 1);
  return memo * memo;
}
lint modpow(lint a, lint b, lint m) {
  if (!b)
    return 1;
  if (b & 1)
    return modpow(a, b - 1, m) * a % m;
  lint memo = modpow(a, b >> 1, m);
  return memo * memo % m;
}
template <typename T> void printArray(std::vector<T> &vec) {
  rep(i, vec.size() - 1) std::cout << vec[i] << " ";
  std::cout << vec.back() << std::endl;
}
template <typename T> void printArray(T l, T r) {
  T rprev = r;
  rprev--;
  for (T i = l; i != rprev; i++) {
    std::cout << *i << " ";
  }
  std::cout << *rprev << std::endl;
}
#line 2 "main.cpp"
int n, q, c[500010];
int m;
int count[500010], ans[500010];
std::pair<P, int> p[500010];
int main() {
  scanf("%d%d", &n, &q);
  rep(i, n) scanf("%d", c + i);
  rep(i, q) {
    scanf("%d%d", &p[i].first.first, &p[i].first.second);
    p[i].second = i;
    p[i].first.first--;
  }
  m = sqrt(q);
  std::sort(p, p + q, [&](const auto &lhs, const auto &rhs) {
    if (lhs.first.first / m < rhs.first.first / m)
      return true;
    if (lhs.first.first / m == rhs.first.first / m &&
        lhs.first.second < rhs.first.second)
      return true;
    return false;
  });
  int l = 0, r = 0, cnt = 0;
  rep(i, q) {
    while (r < p[i].first.second) {
      if (count[c[r++]]++ == 0)
        cnt++;
    }
    while (p[i].first.second < r) {
      if ((--count[c[--r]]) == 0)
        cnt--;
    }
    while (l < p[i].first.first) {
      if ((--count[c[l++]]) == 0)
        cnt--;
    }
    while (p[i].first.first < l) {
      if (count[c[--l]]++ == 0)
        cnt++;
    }
    ans[p[i].second] = cnt;
  }
  rep(i, q) printf("%d\\n", ans[i]);
  return 0;
}

Error: TLE

Fixed submission:

#line 2 "/Users/kaage/Desktop/ProgrammingWorkspace/library/other/template.hpp"
#define _CRT_SECURE_NO_WARNINGS
#pragma target("avx")
#pragma optimize("O3")
#pragma optimize("unroll-loops")
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = 1; i <= (n); i++)
#define all(V) V.begin(), V.end()
typedef long long lint;
typedef unsigned long long ulint;
typedef std::pair<int, int> P;
typedef std::pair<lint, lint> LP;
constexpr int INF = INT_MAX / 2;
constexpr lint LINF = LLONG_MAX / 2;
constexpr double eps = DBL_EPSILON;
constexpr double PI = 3.141592653589793238462643383279;
template <class T>
class prique : public std::priority_queue<T, std::vector<T>, std::greater<T>> {
};
template <class T, class U> inline bool chmax(T &lhs, const U &rhs) {
  if (lhs < rhs) {
    lhs = rhs;
    return 1;
  }
  return 0;
}
template <class T, class U> inline bool chmin(T &lhs, const U &rhs) {
  if (lhs > rhs) {
    lhs = rhs;
    return 1;
  }
  return 0;
}
inline lint gcd(lint a, lint b) {
  while (b) {
    lint c = a;
    a = b;
    b = c % b;
  }
  return a;
}
inline lint lcm(lint a, lint b) { return a / gcd(a, b) * b; }
bool isprime(lint n) {
  if (n == 1)
    return false;
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0)
      return false;
  }
  return true;
}
template <typename T> T mypow(T a, lint b) {
  if (!b)
    return T(1);
  if (b & 1)
    return mypow(a, b - 1) * a;
  T memo = mypow(a, b >> 1);
  return memo * memo;
}
lint modpow(lint a, lint b, lint m) {
  if (!b)
    return 1;
  if (b & 1)
    return modpow(a, b - 1, m) * a % m;
  lint memo = modpow(a, b >> 1, m);
  return memo * memo % m;
}
template <typename T> void printArray(std::vector<T> &vec) {
  rep(i, vec.size() - 1) std::cout << vec[i] << " ";
  std::cout << vec.back() << std::endl;
}
template <typename T> void printArray(T l, T r) {
  T rprev = r;
  rprev--;
  for (T i = l; i != rprev; i++) {
    std::cout << *i << " ";
  }
  std::cout << *rprev << std::endl;
}
#line 2 "main.cpp"
int n, q, c[500010];
int m;
int ans[500010];
std::pair<P, int> p[500010];
int main() {
  scanf("%d%d", &n, &q);
  rep(i, n) scanf("%d", c + i);
  rep(i, q) {
    scanf("%d%d", &p[i].first.first, &p[i].first.second);
    p[i].second = i;
    p[i].first.first--;
  }
  m = sqrt(q);
  std::sort(p, p + q, [&](const auto &lhs, const auto &rhs) {
    if (lhs.first.first / m < rhs.first.first / m)
      return true;
    if (lhs.first.first / m == rhs.first.first / m &&
        lhs.first.second < rhs.first.second)
      return true;
    return false;
  });
  std::unordered_map<int, int> mp;
  int l = 0, r = 0;
  rep(i, q) {
    while (r < p[i].first.second) {
      mp[c[r++]]++;
    }
    while (p[i].first.second < r) {
      if ((--mp[c[--r]]) == 0) {
        mp.erase(mp.find(c[r]));
      }
    }
    while (l < p[i].first.first) {
      if ((--mp[c[l++]]) == 0) {
        mp.erase(mp.find(c[l - 1]));
      }
    }
    while (p[i].first.first < l) {
      mp[c[--l]]++;
    }
    ans[p[i].second] = mp.size();
  }
  rep(i, q) printf("%d\\n", ans[i]);
  return 0;
}