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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

#define F first
#define S second
#define siz(v) ((int)v.size())
#define rs(n) resize(n)
#define ALL(v) v.begin(), v.end()
#define reset(v) memset((v), 0, sizeof(v))
#define EB emplace_back
#define MP make_pair
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define REP(i, a, b) for (int i = (a); i <= (b); i++)
#define debug(x) cout << " > " << #x << ':' << x << endl;
#define kiyohime                                                               \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);
#define endl '\\n'

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
template <typename T> using vec = vector<T>;
template <typename T> using Prior = priority_queue<T>;
template <typename T> using prior = priority_queue<T, vec<T>, greater<T>>;

const int INF = 1e9;
const ll LLINF = (ll)4 * 1e18;
const ll MOD = 1e9 + 7;
const double PI = 3.14159265358;
const double EPS = 1e-8;
const int xx[8] = {0, 1, 0, -1, 1, 1, -1, -1};
const int yy[8] = {1, 0, -1, 0, 1, -1, -1, 1};

void pmod(ll &a, ll b) { a = (a + b) % MOD; }
void mmod(ll &a, ll b) { a = (a - b + MOD) % MOD; }
void tmod(ll &a, ll b) { a = (a * b) % MOD; }
ll POW(ll a, ll b) {
  ll res = 1;
  do {
    if (b % 2)
      tmod(res, a);
    tmod(a, a);
  } while (b >>= 1);
  return res;
}
template <typename T> void amax(T &a, T b) {
  if (a < b)
    a = b;
}
template <typename T> void amin(T &a, T b) {
  if (a > b)
    a = b;
}

#define N 500010
#define sqN 1000

struct query {
  int l, r, idx;
};

bool cmp1(query a, query b) {
  return (a.l / sqN < b.l / sqN) || (a.l / sqN == b.l / sqN && a.r < b.r);
}

int cnt[N], tot;

void solve() {
  int n, m;

  cin >> n >> m;
  vec<int> v(n);
  for (auto &i : v)
    cin >> i;

  vec<query> q(m);
  rep(i, m) {
    cin >> q[i].l >> q[i].r;
    q[i].l--;
    q[i].r--;
    q[i].idx = i;
  }

  vec<int> ans(m);
  sort(ALL(q), cmp1);

  int l, r;
  l = r = 0;
  for (auto i : q) {
    while (r <= i.r) {
      if (cnt[v[r]] == 0)
        tot++;
      cnt[v[r++]]++;
    }
    while (r > i.r + 1) {
      cnt[v[--r]]--;
      if (cnt[v[r]] == 0)
        tot--;
    }
    while (l < i.l) {
      if (cnt[v[l]] == 1)
        tot--;
      cnt[v[l++]]--;
    }
    while (l > i.l) {
      cnt[v[--l]]++;
      if (cnt[v[l]] == 1)
        tot++;
    }

    ans[i.idx] = tot;
  }

  for (auto i : ans)
    cout << i << endl;
}

int main() {
  //    kiyohime
  int T = 1;

  //    cin >> T;
  while (T--) {
    solve();
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

#define F first
#define S second
#define siz(v) ((int)v.size())
#define rs(n) resize(n)
#define ALL(v) v.begin(), v.end()
#define reset(v) memset((v), 0, sizeof(v))
#define EB emplace_back
#define MP make_pair
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define REP(i, a, b) for (int i = (a); i <= (b); i++)
#define debug(x) cout << " > " << #x << ':' << x << endl;
#define kiyohime                                                               \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);
#define endl '\\n'

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
template <typename T> using vec = vector<T>;
template <typename T> using Prior = priority_queue<T>;
template <typename T> using prior = priority_queue<T, vec<T>, greater<T>>;

const int INF = 1e9;
const ll LLINF = (ll)4 * 1e18;
const ll MOD = 1e9 + 7;
const double PI = 3.14159265358;
const double EPS = 1e-8;
const int xx[8] = {0, 1, 0, -1, 1, 1, -1, -1};
const int yy[8] = {1, 0, -1, 0, 1, -1, -1, 1};

void pmod(ll &a, ll b) { a = (a + b) % MOD; }
void mmod(ll &a, ll b) { a = (a - b + MOD) % MOD; }
void tmod(ll &a, ll b) { a = (a * b) % MOD; }
ll POW(ll a, ll b) {
  ll res = 1;
  do {
    if (b % 2)
      tmod(res, a);
    tmod(a, a);
  } while (b >>= 1);
  return res;
}
template <typename T> void amax(T &a, T b) {
  if (a < b)
    a = b;
}
template <typename T> void amin(T &a, T b) {
  if (a > b)
    a = b;
}

#define N 500010
#define sqN 710

struct query {
  int l, r, idx, ans;
};

bool cmp1(query a, query b) {
  return (a.l / sqN < b.l / sqN) || (a.l / sqN == b.l / sqN && a.r < b.r);
}

bool cmp2(query a, query b) { return a.idx < b.idx; }

int cnt[N], tot;

void solve() {
  int n, m;

  cin >> n >> m;
  vec<int> v(n);
  for (auto &i : v)
    cin >> i;

  vec<query> q(m);
  rep(i, m) {
    cin >> q[i].l >> q[i].r;
    q[i].l--;
    q[i].r--;
    q[i].idx = i;
  }

  sort(ALL(q), cmp1);

  int l, r;
  l = r = 0;
  for (auto &i : q) {
    while (r <= i.r) {
      if (cnt[v[r]] == 0)
        tot++;
      cnt[v[r++]]++;
    }
    while (r > i.r + 1) {
      cnt[v[--r]]--;
      if (cnt[v[r]] == 0)
        tot--;
    }
    while (l < i.l) {
      if (cnt[v[l]] == 1)
        tot--;
      cnt[v[l++]]--;
    }
    while (l > i.l) {
      cnt[v[--l]]++;
      if (cnt[v[l]] == 1)
        tot++;
    }

    i.ans = tot;
  }

  sort(ALL(q), cmp2);

  for (auto i : q)
    cout << i.ans << endl;
}

int main() {
  //    kiyohime
  int T = 1;

  //    cin >> T;
  while (T--) {
    solve();
  }
}