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

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

typedef long long ll;
typedef long double ld;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<vl> vvl;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef vector<pii> vpii;
typedef vector<vpll> vvpll;
typedef vector<vpii> vvpii;

#define pb push_back
#define eb emplace_back
#define pf push_front
#define all(c) (c).begin(), (c).end()
#define ff first
#define ss second
#define mp(x, y) make_pair((x), (y))
#define boost                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL)

const int N = 5e5 + 5;
const int BLK = 707;

int col[N], cnt[N], res[N];
int ans = 0;

class Query {
public:
  int L, R, id;

  Query(int L, int R, int id) : L(L), R(R), id(id) {}

  bool operator<(const Query &other) const {
    if (L / BLK == other.L / BLK)
      return (L / BLK % 2) ? R < other.R : R > other.R;
    return L / BLK < other.L / BLK;
  }
};

void add(int cur) {
  cnt[col[cur]]++;
  if (cnt[col[cur]] == 1)
    ans++;
}

void rem(int cur) {
  cnt[col[cur]]--;
  if (cnt[col[cur]] == 0)
    ans--;
}

void solve() {

  int n, q;
  cin >> n >> q;
  for (int i = 1; i <= n; i++) {
    cin >> col[i];
  }
  vector<Query> query;
  for (int i = 0; i < q; i++) {
    int L, R;
    cin >> L >> R;
    query.eb(L, R, i);
  }
  sort(all(query));
  memset(cnt, 0, sizeof(cnt));
  int curL = 1, curR = 0;
  for (Query q : query) {
    int L = q.L, R = q.R;
    while (curR < R) {
      curR++;
      add(curR);
    }
    while (curR > R) {
      rem(curR);
      curR--;
    }
    while (curL < L) {
      rem(curL);
      curL++;
    }
    while (curL > L) {
      curL--;
      add(curL);
    }
    res[q.id] = ans;
  }
  for (int i = 0; i < q; i++) {
    cout << res[i] << '\\n';
  }
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  boost;

  int t = 1;
  // cin >> t;

  for (int i = 1; i <= t; i++) {
    // cout << "Case #" << i << ": ";

    solve();
  }
  return 0;
}

Error: TLE

Fixed submission:

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

typedef long long ll;
typedef long double ld;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<vl> vvl;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef vector<pii> vpii;
typedef vector<vpll> vvpll;
typedef vector<vpii> vvpii;

#define pb push_back
#define eb emplace_back
#define pf push_front
#define all(c) (c).begin(), (c).end()
#define ff first
#define ss second
#define mp(x, y) make_pair((x), (y))
#define boost                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL)

const int N = 5e5 + 5;
const int BLK = 707;

int col[N], cnt[N], res[N];
int ans = 0;

class Query {
public:
  int L, R, id;

  Query(int L, int R, int id) : L(L), R(R), id(id) {}

  bool operator<(const Query &other) const {
    if (L / BLK == other.L / BLK)
      return (L / BLK % 2) ? R < other.R : R > other.R;
    return L / BLK < other.L / BLK;
  }
};

void add(int cur) {
  cnt[col[cur]]++;
  if (cnt[col[cur]] == 1)
    ans++;
}

void rem(int cur) {
  cnt[col[cur]]--;
  if (cnt[col[cur]] == 0)
    ans--;
}

void solve() {

  int n, q;
  cin >> n >> q;
  for (int i = 1; i <= n; i++) {
    cin >> col[i];
  }
  vector<Query> query;
  for (int i = 0; i < q; i++) {
    int L, R;
    cin >> L >> R;
    query.eb(L, R, i);
  }
  sort(all(query));
  memset(cnt, 0, sizeof(cnt));
  int curL = 1, curR = 0;
  int last = -1;
  for (Query q : query) {
    int L = q.L, R = q.R;
    if (L / BLK != last) {
      while (curL <= curR) {
        rem(curL);
        curL++;
      }
      ans = 0;
      last = L / BLK;
      curL = L;
      curR = L - 1;
    }
    while (curR < R) {
      curR++;
      add(curR);
    }
    while (curR > R) {
      rem(curR);
      curR--;
    }
    while (curL < L) {
      rem(curL);
      curL++;
    }
    while (curL > L) {
      curL--;
      add(curL);
    }
    res[q.id] = ans;
  }
  for (int i = 0; i < q; i++) {
    cout << res[i] << '\\n';
  }
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  boost;

  int t = 1;
  // cin >> t;

  for (int i = 1; i <= t; i++) {
    // cout << "Case #" << i << ": ";

    solve();
  }
  return 0;
}