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

#include <bits/stdc++.h>
using namespace std;

// TEMPLATE
#define pb push_back
#define mp make_pair
#define ll int
#define ld long double
#define pii pair<ll, ll>
#define piii pair<ll, pii>
#define F first
#define S second

#define newline printf("\\n")
#define minusone printf("-1\\n")
#define zeroo printf("0\\n")
#define scl1(a) scanf("%lld", &a)
#define scl2(a, b) scanf("%lld %lld", &a, &b)
#define scl3(a, b, c) scanf("%lld %lld %lld", &a, &b, &c)
#define prl1(a) printf("%lld\\n", a)
#define prl2(a, b) printf("%lld %lld\\n", a, b)
#define ssl1(s) scanf(" %[^\\n]", s)
#define scd1(a) scanf("%lf", &a)
#define scd2(a, b) scanf("%lf %lf", &a, &b)
#define prd(a) printf("%lf\\n", a)
#define prld(a) printf("%Lf\\n", a)
#define prcase(cs) printf("Case %lld: ", cs)
#define cin1(a) cin >> a
#define cin2(a, b) cin >> a >> b
#define cin3(a, b, c) cin >> a >> b >> c
#define cin4(a, b, c, d) cin >> a >> b >> c >> d
#define cot1(a) cout << a << "\\n"
#define cot2(a, b) cout << a << " " << b << "\\n"
#define cot3(a, b, c) cout << a << " " << b << " " << c << "\\n"
#define cot4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << "\\n"
#define cotline cout << "\\n"
#define cotminus cout << "-1\\n"
#define cot0 cout << "0\\n"
#define cotyes cout << "YES\\n"
#define cotno cout << "NO\\n"
#define cotcase(cs) cout << "Case " << cs << ": "

#define reversed(s) reverse(s.begin(), s.end())
#define asort(s) sort(s.begin(), s.end())
#define dsort(s) sort(s.rbegin(), s.rend())
#define all(s) s.begin(), s.end()
#define uniq(s) s.resize(distance(s.begin(), unique(s.begin(), s.end())))
#define found(s, x) (s.find(x) != s.end())

#define for0(i, n) for (i = 0; i < n; i++)
#define for1(i, n) for (i = 1; i <= n; i++)
#define fora(i, a, b) for (i = a; i <= b; i++)
#define forb(i, b, a) for (i = b; i >= a; i--)
#define fori(it, s) for (auto it = s.begin(); it != s.end(); it++)

#define FR                                                                     \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL)
#define ms(a, x) memset(a, x, sizeof a)
#define bitcount(n) __builtin_popcountll(n)

// #define pi              3.1415926536
#define pi acos(-1)

const ll INF = LLONG_MAX;
const ll SZ = 5e5 + 5;
const ll mod = 1e9 + 7;

struct query {
  ll l, r, id;
} q[SZ];
ll ans[SZ];
ll cnt[SZ];

ll n, total_q, block_size;
ll l = 0, r = -1, total = 0;
ll a[SZ];

bool cmp(query a, query b) {
  ll block_a = a.l / block_size;
  ll block_b = b.l / block_size;

  if (block_a == block_b)
    return a.r < b.r;
  return block_a < block_b;
}

void add(ll in) {
  cnt[a[in]]++;
  if (cnt[a[in]] == 1)
    total++;
}

void remove(ll in) {
  cnt[a[in]]--;
  if (cnt[a[in]] == 0)
    total--;
}

int main() {
  FR;
  ll cs = 0, tc = 1, x, y, z, i, j, k, g, p, sum = 0, c = 0, t = 0;
  //    ll a, b, d;
  string s, s1, s2;

  cin2(n, total_q);
  for0(i, n) cin1(a[i]);

  block_size = 1000;

  for0(i, total_q) {
    cin2(x, y);
    x--, y--;
    q[i].l = x, q[i].r = y;
    q[i].id = i;
  }
  sort(q, q + total_q, cmp);

  for0(i, total_q) {
    while (l > q[i].l)
      add(--l);
    while (r < q[i].r)
      add(++r);
    while (l < q[i].l)
      remove(l++);
    while (r > q[i].r)
      remove(r--);
    ans[q[i].id] = total;
  }

  for0(i, total_q) cot1(ans[i]);

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

// TEMPLATE
#define pb push_back
#define mp make_pair
#define ll int
#define ld long double
#define pii pair<ll, ll>
#define piii pair<ll, pii>
#define F first
#define S second

#define newline printf("\\n")
#define minusone printf("-1\\n")
#define zeroo printf("0\\n")
#define scl1(a) scanf("%lld", &a)
#define scl2(a, b) scanf("%lld %lld", &a, &b)
#define scl3(a, b, c) scanf("%lld %lld %lld", &a, &b, &c)
#define prl1(a) printf("%lld\\n", a)
#define prl2(a, b) printf("%lld %lld\\n", a, b)
#define ssl1(s) scanf(" %[^\\n]", s)
#define scd1(a) scanf("%lf", &a)
#define scd2(a, b) scanf("%lf %lf", &a, &b)
#define prd(a) printf("%lf\\n", a)
#define prld(a) printf("%Lf\\n", a)
#define prcase(cs) printf("Case %lld: ", cs)
#define cin1(a) cin >> a
#define cin2(a, b) cin >> a >> b
#define cin3(a, b, c) cin >> a >> b >> c
#define cin4(a, b, c, d) cin >> a >> b >> c >> d
#define cot1(a) cout << a << "\\n"
#define cot2(a, b) cout << a << " " << b << "\\n"
#define cot3(a, b, c) cout << a << " " << b << " " << c << "\\n"
#define cot4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << "\\n"
#define cotline cout << "\\n"
#define cotminus cout << "-1\\n"
#define cot0 cout << "0\\n"
#define cotyes cout << "YES\\n"
#define cotno cout << "NO\\n"
#define cotcase(cs) cout << "Case " << cs << ": "

#define reversed(s) reverse(s.begin(), s.end())
#define asort(s) sort(s.begin(), s.end())
#define dsort(s) sort(s.rbegin(), s.rend())
#define all(s) s.begin(), s.end()
#define uniq(s) s.resize(distance(s.begin(), unique(s.begin(), s.end())))
#define found(s, x) (s.find(x) != s.end())

#define for0(i, n) for (i = 0; i < n; i++)
#define for1(i, n) for (i = 1; i <= n; i++)
#define fora(i, a, b) for (i = a; i <= b; i++)
#define forb(i, b, a) for (i = b; i >= a; i--)
#define fori(it, s) for (auto it = s.begin(); it != s.end(); it++)

#define FR                                                                     \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL)
#define ms(a, x) memset(a, x, sizeof a)
#define bitcount(n) __builtin_popcountll(n)

// #define pi              3.1415926536
#define pi acos(-1)

const ll INF = LLONG_MAX;
const ll SZ = 5e5 + 5;
const ll mod = 1e9 + 7;

struct query {
  ll l, r, id;
} q[SZ];
ll ans[SZ];
ll cnt[SZ];

ll n, total_q, block_size;
ll l = 0, r = -1, total = 0;
ll a[SZ];

bool cmp(query a, query b) {
  ll block_a = a.l / block_size;
  ll block_b = b.l / block_size;

  if (block_a == block_b)
    return a.r < b.r;
  return block_a < block_b;
}

void add(ll in) {
  cnt[a[in]]++;
  if (cnt[a[in]] == 1)
    total++;
}

void remove(ll in) {
  cnt[a[in]]--;
  if (cnt[a[in]] == 0)
    total--;
}

int main() {
  FR;
  ll cs = 0, tc = 1, x, y, z, i, j, k, g, p, sum = 0, c = 0, t = 0;
  //    ll a, b, d;
  string s, s1, s2;

  cin2(n, total_q);
  for0(i, n) cin1(a[i]);

  block_size = 800;

  for0(i, total_q) {
    cin2(x, y);
    x--, y--;
    q[i].l = x, q[i].r = y;
    q[i].id = i;
  }
  sort(q, q + total_q, cmp);

  for0(i, total_q) {
    while (l > q[i].l)
      add(--l);
    while (r < q[i].r)
      add(++r);
    while (l < q[i].l)
      remove(l++);
    while (r > q[i].r)
      remove(r--);
    ans[q[i].id] = total;
  }

  for0(i, total_q) cot1(ans[i]);

  return 0;
}