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

/*
--------------              |   /
      |                     |  /
      |                     | /
      |             *       |/          |    |         ------            *
      |                     |           |    |        /      \\
      |             |       |\\          |    |       |       |\\          |
   \\  |             |       | \\         |    |       |       | \\         |
    \\ |             |       |  \\        |    |        \\     /   \\        |
     V              |       |   \\        \\__/|         -----     \\       |
*/
#include <bits/stdc++.h>
using namespace std;

#define EmiliaMyWife                                                           \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(NULL);
#define mem(i, j) memset(i, j, sizeof(i));
#define F first
#define S second
#define lowbit(x) ((x) & (-(x)))
#define siz(v) (int)(v).size()
typedef int64_t ll;
typedef uint64_t ull;
typedef long double ld;
const double EPS = 1e-8;
const int INF = 0x3F3F3F3F;
const ll LINF = 4611686018427387903;
const int MOD = 1e9 + 7;
/*-----------------------------------------------------------------------------------------------------*/

const int N = 5e5 + 5;
int arr[N], cnt[N], vans[N];
int ans;
struct query {
  int i, l, r, b;
};
query que[N];

void add(int p) {
  cnt[arr[p]]++;
  if (cnt[arr[p]] == 1)
    ans++;
}
void rem(int p) {
  cnt[arr[p]]--;
  if (!cnt[arr[p]])
    ans--;
}

signed main() {
  EmiliaMyWife

      int n,
      q;
  cin >> n >> q;
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  int w = sqrt(n) + 1;
  for (int i = 0; i < q; i++)
    cin >> que[i].l >> que[i].r, que[i].l--, que[i].i = i,
                                             que[i].b = que[i].l / w;
  sort(que, que + q, [](query a, query b) {
    return (a.b == b.b ? (a.r < b.r) ^ (a.b & 1) : a.b < b.b);
  });
  for (int i = 0, l = 0, r = 0; i < q; i++) {
    while (r < que[i].r)
      add(r++);
    while (l > que[i].l)
      add(--l);
    while (r > que[i].r)
      rem(--r);
    while (l < que[i].l)
      rem(l++);
    vans[que[i].i] = ans;
  }
  for (int i = 0; i < q; i++)
    cout << vans[i] << '\\n';

  return 0;
}


Error: TLE

Fixed submission:

/*
--------------              |   /
      |                     |  /
      |                     | /
      |             *       |/          |    |         ------            *
      |                     |           |    |        /      \\
      |             |       |\\          |    |       |       |\\          |
   \\  |             |       | \\         |    |       |       | \\         |
    \\ |             |       |  \\        |    |        \\     /   \\        |
     V              |       |   \\        \\__/|         -----     \\       |
*/
#include <bits/stdc++.h>
using namespace std;

#define EmiliaMyWife                                                           \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(NULL);
#define mem(i, j) memset(i, j, sizeof(i));
#define F first
#define S second
#define lowbit(x) ((x) & (-(x)))
#define siz(v) (int)(v).size()
typedef int64_t ll;
typedef uint64_t ull;
typedef long double ld;
const double EPS = 1e-8;
const int INF = 0x3F3F3F3F;
const ll LINF = 4611686018427387903;
const int MOD = 1e9 + 7;
/*-----------------------------------------------------------------------------------------------------*/

const int N = 5e5 + 5;
int arr[N], cnt[N], vans[N];
int ans;
struct query {
  int i, l, r, b;
};
query que[N];

void add(int p) {
  cnt[arr[p]]++;
  if (cnt[arr[p]] == 1)
    ans++;
}
void rem(int p) {
  cnt[arr[p]]--;
  if (!cnt[arr[p]])
    ans--;
}

signed main() {
  EmiliaMyWife

      int n,
      q;
  cin >> n >> q;
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  int w = sqrt(n) + 1;
  for (int i = 0; i < q; i++)
    cin >> que[i].l >> que[i].r, que[i].l--, que[i].i = i,
                                             que[i].b = que[i].l / w;
  sort(que, que + q,
       [](query a, query b) { return (a.b == b.b ? a.r < b.r : a.b < b.b); });
  for (int i = 0, l = 0, r = 0; i < q; i++) {
    while (r < que[i].r)
      add(r++);
    while (l > que[i].l)
      add(--l);
    while (r > que[i].r)
      rem(--r);
    while (l < que[i].l)
      rem(l++);
    // cerr << l << ' ' <<r << '\\n';
    vans[que[i].i] = ans;
  }
  for (int i = 0; i < q; i++)
    cout << vans[i] << '\\n';

  return 0;
}