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

#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)
#define ALL(c) (c).begin(), (c).end()

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<double> VD;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<VD> VVD;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;

template <typename T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}
template <typename T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}

int in() {
  int x;
  scanf("%d", &x);
  return x;
}
ll lin() {
  ll x;
  scanf("%lld", &x);
  return x;
}

int main(void) {
  int n, q;
  cin >> n >> q;
  VI a(n);
  REP(i, n) a[i] = in();
  VI left(q), right(q);
  REP(i, q) {
    scanf("%d %d", &left[i], &right[i]);
    left[i]--;
    right[i];
  }

  int width = sqrt(n);
  VI ord(q);
  REP(i, q) ord[i] = i;
  sort(begin(ord), end(ord), [&](int a, int b) {
    if (left[a] / width != left[b] / width)
      return left[a] < left[b];
    return right[a] < right[b];
  });

  VI ans(q);
  int cnt = 0;
  VI mp(n + 1);
  int x = 0, y = 0;
  REP(j, q) {
    int i = ord[j];
    while (x > left[i]) {
      x--;
      mp[a[x]]++;
      if (mp[a[x]] == 1)
        cnt++;
    }
    while (y < right[i]) {
      mp[a[y]]++;
      if (mp[a[y]] == 1)
        cnt++;
      y++;
    }
    while (x < left[i]) {
      mp[a[x]]--;
      if (mp[a[x]] == 0)
        cnt--;
      x++;
    }
    while (y > right[i]) {
      y--;
      mp[a[y]]--;
      if (mp[a[y]] == 0)
        cnt--;
    }
    ans[i] = cnt;
  }

  REP(i, q) printf("%d\\n", ans[i]);
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)
#define ALL(c) (c).begin(), (c).end()

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<double> VD;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<VD> VVD;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;

template <typename T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}
template <typename T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}

int in() {
  int x;
  scanf("%d", &x);
  return x;
}
ll lin() {
  ll x;
  scanf("%lld", &x);
  return x;
}

int main(void) {
  int n, q;
  cin >> n >> q;
  VI a(n);
  REP(i, n) a[i] = in();
  VI left(q), right(q);
  REP(i, q) {
    scanf("%d %d", &left[i], &right[i]);
    left[i]--;
    right[i];
  }

  int width = sqrt(n);
  VI ord(q);
  REP(i, q) ord[i] = i;
  sort(begin(ord), end(ord), [&](int a, int b) {
    if (left[a] / width != left[b] / width)
      return left[a] < left[b];
    return right[a] < right[b];
  });

  VI ans(q);
  int cnt = 0;
  map<int, int> mp;
  int x = 0, y = 0;
  REP(j, q) {
    int i = ord[j];
    while (x > left[i]) {
      x--;
      mp[a[x]]++;
      if (mp[a[x]] == 1)
        cnt++;
    }
    while (y < right[i]) {
      mp[a[y]]++;
      if (mp[a[y]] == 1)
        cnt++;
      y++;
    }
    while (x < left[i]) {
      mp[a[x]]--;
      if (mp[a[x]] == 0)
        cnt--;
      x++;
    }
    while (y > right[i]) {
      y--;
      mp[a[y]]--;
      if (mp[a[y]] == 0)
        cnt--;
    }
    ans[i] = cnt;
  }

  REP(i, q) printf("%d\\n", ans[i]);
  return 0;
}