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

#include <bits/stdc++.h>

using namespace std;

#define ll long long
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<string> vs;
typedef vector<vs> vvs;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<pii> vpii;
typedef vector<vpii> vvpii;
typedef vector<pll> vpll;
typedef vector<vpll> vvpll;
typedef set<int> si;
typedef set<vi> svi;
typedef set<ll> sll;
typedef set<vll> svll;
typedef set<pii> spii;
typedef set<vpii> svpii;
typedef set<pll> spll;
typedef set<vpll> svpll;
typedef vector<si> vsi;
typedef vector<sll> vsll;
typedef vector<spii> vspii;
typedef vector<spll> vspll;
#define ft first
#define sc second
#define pb push_back
#define ins insert
#define io                                                                     \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define all(v) v.begin(), v.end()
#define Sort(v) sort(all(v))
#define sortarr(a, n) sort(a, a + n)
#define tt                                                                     \\
  ll tc;                                                                       \\
  cin >> tc;                                                                   \\
  while (tc--)
#define rep(var, start, end, interval)                                         \\
  for (ll var = start;                                                         \\
       (interval > 0 and var < end) or (interval < 0 and var > end);           \\
       var += interval)
#define each(x, v) for (auto x : v)
#define takearr0(a, n) rep(z, 0, n, 1) cin >> a[z];
#define takearr1(a, n) rep(z, 1, n + 1, 1) cin >> a[z];

const ll max_n = 5e5 + 5;

class Q {
public:
  ll l, r, b, i;
};

ll n, q, sz, ans = 0;
ll cnt[max_n + 1] = {0}, c[max_n], a[max_n];
Q qry[max_n];

void add(ll pos) {
  if (!cnt[c[pos]])
    ++ans;
  ++cnt[c[pos]];
}

void remove(ll pos) {
  --cnt[c[pos]];
  if (!cnt[c[pos]])
    --ans;
}

bool cmp(Q &a, Q &b) {
  if (a.b != b.b)
    return a.b < b.b;
  return (a.r < b.r) ^ (a.b & 1);
}

int main() {
  io;
  cin >> n >> q;
  takearr0(c, n);
  sz = sqrt(n);
  ll x, y;
  rep(i, 0, q, 1) {
    cin >> x >> y;
    qry[i].l = x - 1;
    qry[i].r = y - 1;
    qry[i].i = i;
    qry[i].b = (x - 1) / sz;
  }
  sort(qry, qry + q, cmp);
  ll cr = -1, cl = 0;
  // printf("here\\n");
  rep(i, 0, q, 1) {
    ll l = qry[i].l, r = qry[i].r;
    while (cr < r)
      add(++cr);
    while (cl > l)
      add(--cl);
    while (cl < l)
      remove(cl++);
    while (cr > r)
      remove(cr--);
    a[qry[i].i] = ans;
  }
  rep(x, 0, q, 1) cout << a[x] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define ll long long
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<string> vs;
typedef vector<vs> vvs;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<pii> vpii;
typedef vector<vpii> vvpii;
typedef vector<pll> vpll;
typedef vector<vpll> vvpll;
typedef set<int> si;
typedef set<vi> svi;
typedef set<ll> sll;
typedef set<vll> svll;
typedef set<pii> spii;
typedef set<vpii> svpii;
typedef set<pll> spll;
typedef set<vpll> svpll;
typedef vector<si> vsi;
typedef vector<sll> vsll;
typedef vector<spii> vspii;
typedef vector<spll> vspll;
#define ft first
#define sc second
#define pb push_back
#define ins insert
#define io                                                                     \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define all(v) v.begin(), v.end()
#define Sort(v) sort(all(v))
#define sortarr(a, n) sort(a, a + n)
#define tt                                                                     \\
  ll tc;                                                                       \\
  cin >> tc;                                                                   \\
  while (tc--)
#define rep(var, start, end, interval)                                         \\
  for (ll var = start;                                                         \\
       (interval > 0 and var < end) or (interval < 0 and var > end);           \\
       var += interval)
#define each(x, v) for (auto x : v)
#define takearr0(a, n) rep(z, 0, n, 1) cin >> a[z];
#define takearr1(a, n) rep(z, 1, n + 1, 1) cin >> a[z];

const ll max_n = 5e5 + 5;

ll n, q, sz, ans = 1;
vll cnt, c;

void add(ll pos) {
  ++cnt[c[pos]];
  if (cnt[c[pos]] == 1)
    ++ans;
}

void remove(ll pos) {
  --cnt[c[pos]];
  if (cnt[c[pos]] == 0)
    --ans;
}

bool cmp(pair<pll, ll> a, pair<pll, ll> b) {
  if (a.ft.ft / sz != b.ft.ft / sz)
    return a < b;
  return (a.ft.sc < b.ft.sc) ^ ((a.ft.ft / sz) & 1);
}

int main() {
  io;
  cin >> n >> q;
  c.resize(n);
  cnt.resize(n + 1);
  cnt.assign(n + 1, 0);
  takearr0(c, n);
  sz = 1e3;
  // while((sz*sz) < n)
  // 	++sz;
  vector<pair<pll, ll>> qry(q);
  vll a(q, 0);
  rep(i, 0, q, 1) {
    cin >> qry[i].ft.ft >> qry[i].ft.sc;
    qry[i].sc = i;
  }
  sort(all(qry), cmp);
  ll cr = 0, cl = 0;
  cnt[c[0]] = 1;
  rep(i, 0, q, 1) {
    ll l = qry[i].ft.ft - 1, r = qry[i].ft.sc - 1;
    while (cr < r)
      add(++cr);
    while (cl > l)
      add(--cl);
    while (cl < l)
      remove(cl++);
    while (cr > r)
      remove(cr--);
    a[qry[i].sc] = ans;
  }
  each(x, a) cout << x << endl;
  return 0;
}