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

#include <bits/stdc++.h>
#define FI first
#define SE second
#define EPS 1e-9
#define ALL(a) a.begin(), a.end()
#define SZ(a) int((a).size())
#define MS(s, n) memset(s, n, sizeof(s))
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORE(i, a, b) for (int i = (a); i >= (b); i--)
#define FORALL(it, a)                                                          \\
  for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)
//__builtin_ffs(x) return 1 + index of least significant 1-bit of x
//__builtin_clz(x) return number of leading zeros of x
//__builtin_ctz(x) return number of trailing zeros of x

using namespace std;
using ll = long long;
using ld = double;
typedef pair<int, int> II;
typedef pair<II, int> III;
typedef complex<ld> cd;
typedef vector<cd> vcd;

const ll MODBASE = 1000000007LL;
const int MAXN = 500010;
const int MAXM = 1000;
const int MAXK = 16;
const int MAXQ = 200010;

struct Data {
  int l, r, cs, block;
  Data(int l = 0, int r = 0, int cs = 0, int block = 0)
      : l(l), r(r), cs(cs), block(block) {}
};

bool operator<(Data a, Data b) {
  if (a.block == b.block)
    return a.r < b.r;
  return a.block < b.block;
}

vector<Data> b;
int n, q, a[MAXN], kq, cnt[MAXN], res[MAXN];

void add(int u) { kq += (cnt[a[u]]++ == 0); }
void del(int u) { kq -= (--cnt[a[u]] == 0); }

int main() {
  ios::sync_with_stdio(0);
  cin.tie(nullptr);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  cin >> n >> q;
  FOR(i, 1, n) cin >> a[i];

  // length of one block
  int k = int(sqrt(n));
  // num of blocks
  int part = n / k + (n % k > 0);

  FOR(i, 1, q) {
    int l, r;
    cin >> l >> r;
    b.push_back(Data(l, r, i, l / part - (l % part == 0)));
  }

  sort(ALL(b));
  int currL = 1, currR = 1;
  cnt[a[1]]++;
  kq = 1;
  FOR(i, 0, q - 1) {
    while (currR > b[i].r)
      del(currR--);
    while (currR < b[i].r)
      add(++currR);
    while (currL < b[i].l)
      del(currL++);
    while (currL > b[i].l)
      add(--currL);
    res[b[i].cs] = kq;
  }

  FOR(i, 1, q) cout << res[i] << "\\n";
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <tr1/unordered_map>
#include <tr1/unordered_set>
#include <vector>
// Useful constants
#define INF (int)1e9
#define EPS 1e-9
#define MOD 1000000007
#define Pi 3.14159
// for input-output
#define fio                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
// iteration macros: have advantage of auto-casting and not recomputing
// arguments
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define setBit(S, j) (S |= (1 << j))
#define clearBit(S, j) (S &= ~(1 << j))
#define toggleBit(S, j) (S ^= (1 << j))
// input macro
#define IN(x, n)                                                               \\
  for (int e = 0; e < n; e++) {                                                \\
    ll y;                                                                      \\
    cin >> y;                                                                  \\
    x.pb(y);                                                                   \\
  }
// output macro
#define print(x)                                                               \\
  for (auto it : x)                                                            \\
    cout << it << ' ';                                                         \\
  cout << endl;
#define printPI(x)                                                             \\
  for (auto it : x)                                                            \\
    cout << it.F << ' ' << it.S << '\\t';                                       \\
  cout << endl;
// shortcut for data type
#define ll long long
#define PI pair<int, int>
#define PL pair<ll, ll>
#define vi vector<int>
#define vl vector<ll>
#define vvi vector<VI>
#define vvl vector<VL>
#define vvvi vector<VVI>
#define vvvl vector<VVL>
#define vpi vector<PI>
#define vpl vector<PL>
#define pb push_back
#define ppb pop_back
#define eb emplace_back
#define mp make_pair
#define F first
#define S second
#define uset unordered_set
#define umap unordered_map
#define dbg(x, y) cout << x << " = " << y << endl

// namespace
using namespace std;

// const
const int Ntest = 1e5 + 5;

// useful function
ll gcd(ll a, ll b) {
  ll temp;
  while (b > 0) {
    temp = a % b;
    a = b;
    b = temp;
  }
  return a;
}
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
ll fpow(ll b, ll exp, ll mod) {
  if (exp == 0)
    return 1;
  ll t = fpow(b, exp / 2, mod);
  if (exp & 1)
    return t * t % mod * b % mod;
  return t * t % mod;
}
ll divmod(ll i, ll j, ll mod) {
  i %= mod, j %= mod;
  return i * fpow(j, mod - 2, mod) % mod;
}
// --clock function
clock_t time_p = clock();
void TimeTaken() {
  time_p = clock() - time_p;
  cerr << "Completion time : " << (float)(time_p) / CLOCKS_PER_SEC << "\\n";
}

void sol() {

  int n, q;
  cin >> n >> q;

  vector<int> c(n);
  for (int i = 0; i < n; ++i) {
    cin >> c[i];
  }
  for (int i = 0; i < q; ++i) {
    int l, r;
    set<int> s;
    cin >> l >> r;
    for (int j = l; j <= r; ++j) {
      s.insert(c[j - 1]);
    }
    cout << s.size() << '\\n';
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  int tc = 1; // cin >> tc;
  while (tc--) {
    sol();
  }
}