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

// created by mtnshh

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

#define ll long long
#define ld long double

#define rep(i, a, b) for (ll i = a; i < b; i++)
#define repb(i, a, b) for (ll i = a; i >= b; i--)

#define err() cout << "--------------------------" << endl;
#define errA(A)                                                                \\
  for (auto i : A)                                                             \\
    cout << i << " ";                                                          \\
  cout << endl;
#define err1(a) cout << #a << " " << a << endl
#define err2(a, b) cout << #a << " " << a << " " << #b << " " << b << endl
#define err3(a, b, c)                                                          \\
  cout << #a << " " << a << " " << #b << " " << b << " " << #c << " " << c     \\
       << endl
#define err4(a, b, c, d)                                                       \\
  cout << #a << " " << a << " " << #b << " " << b << " " << #c << " " << c     \\
       << " " << #d << " " << d << endl

#define pb push_back
#define all(A) A.begin(), A.end()
#define allr(A) A.rbegin(), A.rend()
#define ft first
#define sd second

#define pll pair<ll, ll>
#define V vector<ll>
#define S set<ll>
#define VV vector<V>
#define Vpll vector<pll>

#define endl "\\n"

const ll logN = 20;
const ll M = 1000000007;
const ll INF = 1e18;
#define PI 3.14159265

const ll N = 500005;

ll C[N];
ll ans[N];

int block_size = 1000;

struct Query {
  ll l, r, idx;
  bool operator<(Query other) const {
    return make_pair(l / block_size, r) <
           make_pair(other.l / block_size, other.r);
  }
};

vector<Query> Q;

ll cnt = 0;
ll A[N];

void add(ll idx) {
  if (A[C[idx]] == 0) {
    cnt++;
  }
  A[C[idx]]++;
}

void remove(ll idx) {
  A[C[idx]]--;
  if (A[C[idx]] == 0) {
    cnt--;
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll n, q;
  cin >> n >> q;
  rep(i, 0, n) cin >> C[i];
  Q.resize(q);

  rep(i, 0, q) {
    cin >> Q[i].l >> Q[i].r;
    Q[i].l--;
    Q[i].r--;
    Q[i].idx = i;
  }

  sort(all(Q));

  int cur_l = 0;
  int cur_r = -1;

  for (Query q : Q) {
    while (cur_l > q.l) {
      cur_l--;
      add(cur_l);
    }
    while (cur_r < q.r) {
      cur_r++;
      add(cur_r);
    }
    while (cur_l < q.l) {
      remove(cur_l);
      cur_l++;
    }
    while (cur_r > q.r) {
      remove(cur_r);
      cur_r--;
    }
    ans[q.idx] = cnt;
  }
  rep(i, 0, q) cout << ans[i] << endl;
  return 0;
}

Error: TLE

Fixed submission:

// created by mtnshh

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

#define ll long long
#define ld long double

#define rep(i, a, b) for (ll i = a; i < b; i++)
#define repb(i, a, b) for (ll i = a; i >= b; i--)

#define err() cout << "--------------------------" << endl;
#define errA(A)                                                                \\
  for (auto i : A)                                                             \\
    cout << i << " ";                                                          \\
  cout << endl;
#define err1(a) cout << #a << " " << a << endl
#define err2(a, b) cout << #a << " " << a << " " << #b << " " << b << endl
#define err3(a, b, c)                                                          \\
  cout << #a << " " << a << " " << #b << " " << b << " " << #c << " " << c     \\
       << endl
#define err4(a, b, c, d)                                                       \\
  cout << #a << " " << a << " " << #b << " " << b << " " << #c << " " << c     \\
       << " " << #d << " " << d << endl

#define pb push_back
#define all(A) A.begin(), A.end()
#define allr(A) A.rbegin(), A.rend()
#define ft first
#define sd second

#define pll pair<ll, ll>
#define V vector<ll>
#define S set<ll>
#define VV vector<V>
#define Vpll vector<pll>

#define endl "\\n"

const ll logN = 20;
const ll M = 1000000007;
const ll INF = 1e18;
#define PI 3.14159265

const ll N = 500005;

ll C[N];
ll ans[N];

int block_size = 500;

struct Query {
  ll l, r, idx;
  bool operator<(Query other) const {
    return make_pair(l / block_size, r) <
           make_pair(other.l / block_size, other.r);
  }
};

vector<Query> Q;

ll cnt = 0;
ll A[N];

void add(ll idx) {
  if (A[C[idx]] == 0) {
    cnt++;
  }
  A[C[idx]]++;
}

void remove(ll idx) {
  A[C[idx]]--;
  if (A[C[idx]] == 0) {
    cnt--;
  }
}

ll get_answer() { return cnt; }

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll n, q;
  cin >> n >> q;
  rep(i, 0, n) cin >> C[i];
  Q.resize(q);

  rep(i, 0, q) {
    cin >> Q[i].l >> Q[i].r;
    Q[i].l--;
    Q[i].r--;
    Q[i].idx = i;
  }

  sort(all(Q));

  int cur_l = 0;
  int cur_r = -1;

  for (Query q : Q) {
    while (cur_l > q.l) {
      cur_l--;
      add(cur_l);
    }
    while (cur_r < q.r) {
      cur_r++;
      add(cur_r);
    }
    while (cur_l < q.l) {
      remove(cur_l);
      cur_l++;
    }
    while (cur_r > q.r) {
      remove(cur_r);
      cur_r--;
    }
    ans[q.idx] = get_answer();
  }
  rep(i, 0, q) cout << ans[i] << endl;
  return 0;
}