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

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pii pair<int, int>
#define pb push_back
#define INF INT_MAX
#define mod 1000000007ll
#define fi first
#define se second
#define f(i, p, q) for (int i = p; i < q; i++)
#define si(a) scanf("%d", &a)
#define sii(a, b) scanf("%d %d", &a, &b)
#define siii(a, b, c) scanf("%d %d %d", &a, &b, &c)
#define pi(a) printf("%d\\n", a)
#define psi(a) printf(" %d", a)
#define poi(a) printf("%d", a)
#define sl(a) scanf("%lld", &a)
#define pl(a) printf("%lld\\n", a)
#define Nline() printf("\\n")
#define sstr(str) scanf("%s", str)
#define pstr(str) printf("%s\\n", str)
#define SET(a) memset(a, -1, sizeof(a))
#define CLR(a) memset(a, 0, sizeof(a))
#define vi vector<int>
#define vvi vector<vi>
#define all(v) (v).begin(), (v).end()
#define SORT(v) sort(all(v))
#define RSORT(v) sort(all(v), greater<int>())
#define ios                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(NULL);
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define ABS(X) ((X) > 0 ? (X) : (-(X)))
#define S(X) ((X) * (X))
const char nl = '\\n';
#define SZ(V) (int)V.size()
#define FORN(i, n) for (i = 0; i < n; i++)
#define FORAB(i, a, b) for (i = a; i <= b; i++)
#define ALL(V) V.begin(), V.end()
#define IN(A, B, C) (B) <= (A) && (A) <= (C)
#define AIN(A, B, C) assert(IN(A, B, C))
ll power(ll x, unsigned int y) {
  ll res = 1;
  while (y > 0) {
    if (y & 1)
      res = res * x % mod;
    y = y >> 1;
    x = x * x;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll ar[500005];
ll n, q, S;
class Query {
public:
  int l;
  int r;
  ll index;
  ll answer;
};
bool cmp(Query A, Query B) {
  if (A.l / S != B.l / S)
    return A.l / S < B.l / S;
  return A.r > B.r;
}
void solve() {
  cin >> n >> q;
  f(i, 0, n) cin >> ar[i];
  vector<Query> v;
  S = sqrt(n);
  //  map<pair<int,int>,int>mp;
  vector<pair<int, int>> p;
  for (int i = 0; i < q; i++) {
    int a, b;
    cin >> a >> b;
    // cout<<a<<" "<<b;
    // a--,b--;
    // mp[{a,b}]=0;
    p.pb({a, b});
    Query *q = new Query;
    q->l = a;
    q->r = b;
    q->index = i;
    v.pb(*q);
  }

  sort(v.begin(), v.end(), cmp);
  int fre[500005] = {0};
  int le = 0, ri = 0;
  int j = 0;
  int cur_dif = 0;
  while (j < q) {
    int L = v[j].l;
    int R = v[j].r;
    L--;
    R--;
    while (le < L) {
      fre[ar[le]]--;
      if (fre[ar[le]] == 0)
        cur_dif--;
      le++;
    }
    while (le > L) {
      fre[ar[le - 1]]++;
      if (fre[ar[le - 1]] == 1)
        cur_dif++;
      le--;
    }

    while (ri <= R) {
      fre[ar[ri]]++;
      if (fre[ar[ri]] == 1)
        cur_dif++;
      ri++;
    }

    while (ri > R + 1) {
      fre[ar[ri - 1]]--;
      if (fre[ar[ri - 1]] == 0)
        cur_dif--;
      ri--;
    }
    v[j].answer = cur_dif;
    // cout<<L+1 <<" "<<R+1<<" "<<cur_dif<<"\\n";
    j++;
  }
  // cout<<"\\n";
  ll vec[q + 1];
  for (int i = 0; i < q; i++) {
    vec[v[i].index] = v[i].answer;
  }
  f(i, 0, q) cout << vec[i] << "\\n";
}
int main() {
  ios;
  int t = 1;
  // cin>>t;
  while (t--)
    solve();
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pii pair<int, int>
#define pb push_back
#define INF INT_MAX
#define mod 1000000007ll
#define fi first
#define se second
#define f(i, p, q) for (int i = p; i < q; i++)
#define si(a) scanf("%d", &a)
#define sii(a, b) scanf("%d %d", &a, &b)
#define siii(a, b, c) scanf("%d %d %d", &a, &b, &c)
#define pi(a) printf("%d\\n", a)
#define psi(a) printf(" %d", a)
#define poi(a) printf("%d", a)
#define sl(a) scanf("%lld", &a)
#define pl(a) printf("%lld\\n", a)
#define Nline() printf("\\n")
#define sstr(str) scanf("%s", str)
#define pstr(str) printf("%s\\n", str)
#define SET(a) memset(a, -1, sizeof(a))
#define CLR(a) memset(a, 0, sizeof(a))
#define vi vector<int>
#define vvi vector<vi>
#define all(v) (v).begin(), (v).end()
#define SORT(v) sort(all(v))
#define RSORT(v) sort(all(v), greater<int>())
#define ios                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(NULL);
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define ABS(X) ((X) > 0 ? (X) : (-(X)))
#define S(X) ((X) * (X))
const char nl = '\\n';
#define SZ(V) (int)V.size()
#define FORN(i, n) for (i = 0; i < n; i++)
#define FORAB(i, a, b) for (i = a; i <= b; i++)
#define ALL(V) V.begin(), V.end()
#define IN(A, B, C) (B) <= (A) && (A) <= (C)
#define AIN(A, B, C) assert(IN(A, B, C))
ll power(ll x, unsigned int y) {
  ll res = 1;
  while (y > 0) {
    if (y & 1)
      res = res * x % mod;
    y = y >> 1;
    x = x * x;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll ar[500005];
ll n, q, S;
class Query {
public:
  int l;
  int r;
};
bool cmp(Query A, Query B) {
  if (A.l / S != B.l / S)
    return A.l / S < B.l / S;
  return A.r > B.r;
}
void solve() {
  cin >> n >> q;
  f(i, 0, n) cin >> ar[i];
  vector<Query> v;
  S = sqrt(n);
  map<pair<int, int>, int> mp;
  vector<pair<int, int>> p;
  for (int i = 0; i < q; i++) {
    int a, b;
    cin >> a >> b;
    // cout<<a<<" "<<b;
    // a--,b--;
    mp[{a, b}] = 0;
    p.pb({a, b});
    Query *q = new Query;
    q->l = a;
    q->r = b;
    v.pb(*q);
  }

  sort(v.begin(), v.end(), cmp);
  int fre[500005] = {0};
  int le = 0, ri = 0;
  int j = 0;
  int cur_dif = 0;
  while (j < q) {
    int L = v[j].l;
    int R = v[j].r;
    L--;
    R--;
    while (le < L) {
      fre[ar[le]]--;
      if (fre[ar[le]] == 0)
        cur_dif--;
      le++;
    }
    while (le > L) {
      fre[ar[le - 1]]++;
      if (fre[ar[le - 1]] == 1)
        cur_dif++;
      le--;
    }

    while (ri <= R) {
      fre[ar[ri]]++;
      if (fre[ar[ri]] == 1)
        cur_dif++;
      ri++;
    }

    while (ri > R + 1) {
      fre[ar[ri - 1]]--;
      if (fre[ar[ri - 1]] == 0)
        cur_dif--;
      ri--;
    }
    mp[{L + 1, R + 1}] = cur_dif;
    // cout<<L+1 <<" "<<R+1<<" "<<cur_dif<<"\\n";
    j++;
  }
  // cout<<"\\n";
  for (int i = 0; i < q; i++) {
    cout << mp[{p[i].fi, p[i].se}] << "\\n";
  }
}
int main() {
  ios;
  int t = 1;
  // cin>>t;
  while (t--)
    solve();
}