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

#include "algorithm"
#include "bitset"
#include "cassert"
#include "climits"
#include "cmath"
#include "cstdio"
#include "ctime"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "numeric"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "unordered_set"

using namespace std;

constexpr long long int MOD = 1000000007;
// constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
// constexpr long long int MOD = 998244353;
constexpr double EPS = 1e-12;

int N, M, K, H, W, L, R;
// long long int N, M, K, H, W, L, R;

constexpr int di = 700;

struct Query {
  int l, r, index;
  bool operator<(const Query &q) const {
    int a = l / di;
    int b = q.l / di;
    if (a < b)
      return true;
    else if (a > b)
      return false;
    if (a & 1)
      return -r < -q.r;
    else
      return r < q.r;
  }
};

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

  cin >> N >> K;
  vector<int> v(N);
  for (auto &i : v)
    cin >> i;
  vector<Query> query(K);
  int cnt = 0;
  for (auto &i : query) {
    i.index = cnt++;
    cin >> i.l >> i.r;
    i.l--;
  }
  sort(query.begin(), query.end());
  vector<int> num(N + 1);
  int ans = 0;
  L = 0, R = 0;
  vector<int> ret(K);
  for (auto q : query) {
    while (q.l > L) {
      num[v[L]]--;
      if (num[v[L]] == 0)
        ans--;
      L++;
    }
    while (q.l < L) {
      L--;
      num[v[L]]++;
      if (num[v[L]] == 1)
        ans++;
    }
    while (q.r > R) {
      num[v[R]]++;
      if (num[v[R]] == 1)
        ans++;
      R++;
    }
    while (q.r < R) {
      R--;
      num[v[R]]--;
      if (num[v[R]] == 0)
        ans--;
    }
    ret[q.index] = ans;
  }
  for (auto i : ret)
    cout << i << endl;
}

Error: TLE

Fixed submission:

#include "algorithm"
#include "bitset"
#include "cassert"
#include "climits"
#include "cmath"
#include "cstdio"
#include "ctime"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "numeric"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "unordered_set"

using namespace std;

constexpr long long int MOD = 1000000007;
// constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
// constexpr long long int MOD = 998244353;
constexpr double EPS = 1e-12;

int N, M, K, H, W, L, R;
// long long int N, M, K, H, W, L, R;

struct Query {
  int l, r, index;
  bool operator<(const Query &q) const {
    return make_pair(l / M, r) < make_pair(q.l / M, q.r);
  }
};

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

  cin >> N >> K;
  vector<int> v(N);
  M = sqrt(N) + 1;
  for (auto &i : v)
    cin >> i;
  // for (auto &i : v)i = 2;
  vector<Query> query(K);
  int cnt = 0;
  for (auto &i : query) {
    i.index = cnt++;
    cin >> i.l >> i.r;
    // i.l = cnt;
    // if (cnt & 1)i.r = cnt;
    // else i.r = N;
    i.l--;
  }
  sort(query.begin(), query.end());
  vector<int> num(N + 1);
  int ans = 0;
  L = 0, R = 0;
  vector<int> ret(K);
  for (auto q : query) {
    while (q.l > L) {
      num[v[L]]--;
      if (num[v[L]] == 0)
        ans--;
      L++;
    }
    while (q.l < L) {
      L--;
      num[v[L]]++;
      if (num[v[L]] == 1)
        ans++;
    }
    while (q.r > R) {
      num[v[R]]++;
      if (num[v[R]] == 1)
        ans++;
      R++;
    }
    while (q.r < R) {
      R--;
      num[v[R]]--;
      if (num[v[R]] == 0)
        ans--;
    }
    ret[q.index] = ans;
  }
  for (auto i : ret)
    cout << i << "\\n";
}