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

#include <bits/stdc++.h>

#define int long long
#define uint unsigned int
#define ld long double
#define showoff                                                                \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define pb push_back
#define pii pair<int, int>
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define RFOR(i, a, b) for (int i = a; i > b; --i)
#define f first
#define se second
#define maxn 500005
#define all(v) v.begin(), v.end()
#define sz(x) (int)x.size()
#define mod 1000000007
#define pqueue priority_queue<int>
#define pdqueue priority_queue<int, vector<int>, greater<int>>

using namespace std;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int power(int a, int n) {
  a %= mod;
  if (n == 1)
    return a;
  if (n == 0)
    return 1;
  if (n % 2)
    return (a * (power((a * a) % mod, n / 2) % mod)) % mod;
  return power((a * a) % mod, n / 2) % mod;
}
const int inf = (int)1e18;

int inverse(int x) {
  return power(x, mod - 2) % mod; // little fermat....
}
int bno[maxn], ans[maxn], cnt[maxn];

void solve() {
  int n, q;
  cin >> n >> q;
  int bsiz = sqrt(n);
  vector<int> v(n + 5);
  FOR(i, 1, n + 1) cin >> v[i];
  FOR(i, 1, n + 1) { bno[i] = (i - 1) / bsiz + 1; }
  vector<pair<pii, int>> u;
  FOR(i, 1, q + 1) {
    int l, r;
    cin >> l >> r;
    u.pb({{l, r}, i});
  }
  sort(u.begin(), u.end());
  int ci = 0, cl = 1, cr = 1, tot = 1;
  cnt[v[1]] = 1;
  FOR(i, 1, n + 1) {
    if (ci == sz(u))
      break;
    vector<pair<pii, int>> a;
    while (ci < sz(u) && bno[u[ci].f.f] == i) {
      a.pb({{u[ci].f.se, u[ci].f.f}, u[ci].se});
      ci++;
    }
    sort(a.begin(), a.end());
    for (auto &x : a) {
      int l = x.f.se, r = x.f.f, ind = x.se;
      while (cr < r) {
        cr++;
        if (cnt[v[cr]] == 0)
          tot++;
        cnt[v[cr]]++;
      }
      while (cl < l) {
        cnt[v[cl]]--;
        if (cnt[v[cl]] == 0)
          tot--;
        cl++;
      }
      while (cl > l) {
        cl--;
        if (cnt[v[cl]] == 0)
          tot++;
        cnt[v[cl]]++;
      }
      while (cr > r) {
        cnt[v[cr]]--;
        if (cnt[v[cr]] == 0)
          tot--;
        cr--;
      }
      ans[ind] = tot;
    }
  }
  FOR(i, 1, q + 1) cout << ans[i] << "\\n";
}
signed main() {
  showoff;
  int T = 1;
  // cin >> T;
  FOR(t, 1, T + 1) {
    solve();
    // cout << "\\n";
  }
  return 0;
}
//*->for large size of matrix take int not long long if possible......
//*->always take maximum as inf for safer side ...


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define int long long
#define uint unsigned int
#define ld long double
#define showoff                                                                \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define pb push_back
#define pii pair<int, int>
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define RFOR(i, a, b) for (int i = a; i > b; --i)
#define f first
#define se second
#define maxn 500005
#define all(v) v.begin(), v.end()
#define sz(x) (int)x.size()
#define mod 1000000007
#define pqueue priority_queue<int>
#define pdqueue priority_queue<int, vector<int>, greater<int>>

using namespace std;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int power(int a, int n) {
  a %= mod;
  if (n == 1)
    return a;
  if (n == 0)
    return 1;
  if (n % 2)
    return (a * (power((a * a) % mod, n / 2) % mod)) % mod;
  return power((a * a) % mod, n / 2) % mod;
}
const int inf = (int)1e18;

int inverse(int x) {
  return power(x, mod - 2) % mod; // little fermat....
}
int bno[maxn], ans[maxn], cnt[maxn];

void solve() {
  int n, q;
  cin >> n >> q;
  int bsiz = sqrt(n);
  vector<int> v(n + 5);
  FOR(i, 1, n + 1) cin >> v[i];
  FOR(i, 1, n + 1) { bno[i] = (i - 1) / bsiz + 1; }
  vector<pair<pii, int>> u;
  FOR(i, 1, q + 1) {
    int l, r;
    cin >> l >> r;
    u.pb({{l, r}, i});
  }
  sort(u.begin(), u.end());
  int ci = 0, cl = 1, cr = 1;
  set<int> cols;
  cnt[v[1]] = 1;
  cols.insert(v[1]);
  FOR(i, 1, n + 1) {
    if (ci == sz(u))
      break;
    vector<pair<pii, int>> a;
    while (ci < sz(u) && bno[u[ci].f.f] == i) {
      a.pb({{u[ci].f.se, u[ci].f.f}, u[ci].se});
      ci++;
    }
    sort(a.begin(), a.end());
    for (auto &x : a) {
      int l = x.f.se, r = x.f.f, ind = x.se;
      while (cr < r) {
        cr++;
        cols.insert(v[cr]);
        cnt[v[cr]]++;
      }
      while (cl < l) {
        cnt[v[cl]]--;
        if (cnt[v[cl]] == 0)
          cols.erase(v[cl]);
        cl++;
      }
      while (cl > l) {
        cl--;
        cols.insert(v[cl]);
        cnt[v[cl]]++;
      }
      while (cr > r) {
        cnt[v[cr]]--;
        if (cnt[v[cr]] == 0)
          cols.erase(v[cr]);
        cr--;
      }
      ans[ind] = sz(cols);
    }
  }
  FOR(i, 1, q + 1) cout << ans[i] << "\\n";
}
signed main() {
  showoff;
  int T = 1;
  // cin >> T;
  FOR(t, 1, T + 1) {
    solve();
    // cout << "\\n";
  }
  return 0;
}
//*->for large size of matrix take int not long long if possible......
//*->always take maximum as inf for safer side ...