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

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

typedef long long ll;
typedef pair<ll, ll> pi;
typedef vector<ll> vi;
typedef vector<pi> vpi;
#define f first
#define s second
#define FOR(i, s, e) for (ll i = s; i <= ll(e); ++i)
#define DEC(i, s, e) for (ll i = s; i >= ll(e); --i)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
#define aFOR(i, x) for (auto i : x)
#define mem(x, i) memset(x, i, sizeof x)
#define fast ios_base::sync_with_stdio(false), cin.tie(0)
#define maxn 500001
#define ll int
typedef pair<pi, int> pii;

int N, Q;
int C[maxn];
const int sqrtn = 700;
pii query[maxn];
int ans[maxn];
int coun[maxn];

int32_t main() {
  fast;

  cin >> N >> Q;

  FOR(i, 1, N) cin >> C[i];

  FOR(i, 1, Q) {
    cin >> query[i].f.f >> query[i].f.s;
    query[i].s = i;
  }

  sort(query + 1, query + Q + 1, [](pii i, pii j) {
    return pi(i.f.f / sqrtn, i.f.s) < pi(j.f.f / sqrtn, j.f.s);
  });

  int l = 1, r = 1;

  int res = 1;
  coun[C[1]]++;

  FOR(i, 1, Q) {
    pi cur = query[i].f;

    while (r < cur.s) {
      r++;
      if (coun[C[r]] == 0)
        res++;
      coun[C[r]]++;
    }

    while (l > cur.f) {
      l--;
      if (coun[C[l]] == 0)
        res++;
      coun[C[l]]++;
    }

    while (r > cur.s) {
      coun[C[r]]--;
      if (coun[C[r]] == 0)
        res--;
      r--;
    }

    while (l < cur.f) {
      coun[C[l]]--;
      if (coun[C[l]] == 0)
        res--;
      l++;
    }

    ans[query[i].s] = res;
  }

  FOR(i, 1, Q) cout << ans[i] << '\\n';
}


Error: TLE

Fixed submission:

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

typedef long long ll;
typedef pair<ll, ll> pi;
typedef vector<ll> vi;
typedef vector<pi> vpi;
#define f first
#define s second
#define FOR(i, s, e) for (ll i = s; i <= ll(e); ++i)
#define DEC(i, s, e) for (ll i = s; i >= ll(e); --i)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
#define aFOR(i, x) for (auto i : x)
#define mem(x, i) memset(x, i, sizeof x)
#define fast ios_base::sync_with_stdio(false), cin.tie(0)
#define maxn 500001
#define ll int
typedef pair<pi, int> pii;

int N, Q;
int C[maxn];
const int sqrtn = 700;
pii query[maxn];
int ans[maxn];

int32_t main() {
  fast;

  cin >> N >> Q;

  FOR(i, 1, N) cin >> C[i];

  FOR(i, 1, Q) {
    cin >> query[i].f.f >> query[i].f.s;
    query[i].s = i;
  }

  sort(query + 1, query + Q + 1, [](pii i, pii j) {
    return pi(i.f.f / sqrtn, i.f.s) < pi(j.f.f / sqrtn, j.f.s);
  });

  int l = 1, r = 1;
  set<int> st;
  multiset<int> mst;

  st.insert(C[1]);
  mst.insert(C[1]);

  FOR(i, 1, Q) {
    pi cur = query[i].f;

    while (r < cur.s) {
      r++;
      st.insert(C[r]);
      mst.insert(C[r]);
    }

    while (l > cur.f) {
      l--;
      st.insert(C[l]);
      mst.insert(C[l]);
    }

    while (r > cur.s) {
      mst.erase(mst.find(C[r]));
      if (mst.find(C[r]) == mst.end())
        st.erase(C[r]);
      r--;
    }

    while (l < cur.f) {
      mst.erase(mst.find(C[l]));
      if (mst.find(C[l]) == mst.end())
        st.erase(C[l]);
      l++;
    }

    ans[query[i].s] = st.size();
  }

  FOR(i, 1, Q) cout << ans[i] << '\\n';
}