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

#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define llu unsigned long long int
#define sa(a, n, t)                                                            \\
  for (int ii = 0; ii < n; ii++)                                               \\
  scanf("%" #t "", &(a)[ii])
#define rep(i, n) for (int i = 0; i < n; i++)
#define reps(i, a, n) for (int i = a; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define rreps(i, a, n) for (int i = n - 1; i >= a; i--)
#define validl(i, a, b) ((i >= a) && (i <= b)) ? 1 : 0
#define validmat(x, y, a, b, c, d) (validl(x, a, b) && validl(y, a, b)) ? 1 : 0
#define validmn(x, y, m, n) validmat(x, y, 0, m, 0, n)
#define valid(i, n) validl(i, 0, n)
#define v(t) vector<t>
#define vv(t) vector<vector<t>>
#define mk make_pair
#define pb push_back
#define pa(a, n, t)                                                            \\
  for (int i = 0; i < n; i++)                                                  \\
  printf("%" #t "", a[i])
#define p2 pair<int, int>
#define p3 pair<p2, int>
#define fi first
#define se second
#define sd(a) scanf("%d", &a)
#define sl(a) scanf("%lld", &a)
#define pl(a) printf("%lld", a)
#define pd(a) printf("%d", a)
#define sf(a) scanf("%lf", &a)
#define pf(a) printf("%lf", a)
#define nline printf("\\n")
#define ss(a) scanf("%s", a)
#define ps(a) printf("%s", a)
#define sc(a) scanf("%c", &a)
#define pc(a) printf("%c", &a)
#define mf(m, f) m.find(f) != m.end()
#define pp3(m) cout << m.fi.fi << " " << m.fi.se << " " << m.se << " "
#define pp2(m) cout << m.fi << " " << m.se << " "
#define debug 0
/*v(int) adj[200001];
void dfs(int i,int p){
    rep(j,adj[i].size()){
        if(p!=adj[i][j])
            dfs(adj[i][j],i);
    }
}*/
int rootn;
bool compare(p3 a, p3 b) {
  if ((a.fi.fi / rootn) != (b.fi.fi / rootn))
    return (a.fi.fi / rootn) < (b.fi.fi / rootn);
  return a.fi.se < b.fi.se;
}
int brr[500001] = {0};
int dif = 0;
void add(int x) {
  if (brr[x] == 0)
    dif++;
  brr[x]++;
}
void rem(int x) {
  if (brr[x] == 1)
    dif--;
  brr[x]--;
}
int arr[500001];
int main() {
  int n;

  string s;
  cin >> n;
  rootn = static_cast<int>(sqrt(n));
  rootn = max(rootn, 1000);
  // cout<<rootn<<endl;
  int k;
  cin >> k;

  sa(arr, n, d);
  int m = 0;
  v(p3) vv;
  rep(i, k) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    vv.pb(mk(mk(a, b), i));
  }
  sort(vv.begin(), vv.end(), compare);
  int ll = 0, rr = -1;
  v(int) ans(k);
  rep(i, k) {
    // cout<<vv[i].fi.fi<<" "<<vv[i].fi.se<<endl;
    int l = vv[i].fi.fi;
    int r = vv[i].fi.se;
    while (rr < r) {
      rr++;
      add(arr[rr]);
    }
    while (r < rr) {
      rem(arr[rr]);
      rr--;
    }
    while (ll < l) {
      rem(arr[ll]);
      ll++;
    }
    while (l < ll) {
      ll--;
      add(arr[ll]);
    }
    ans[vv[i].se] = dif;
  }
  rep(i, k) cout << ans[i] << "\\n";
  // cout<<endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define llu unsigned long long int
#define sa(a, n, t)                                                            \\
  for (int ii = 0; ii < n; ii++)                                               \\
  scanf("%" #t "", &(a)[ii])
#define rep(i, n) for (int i = 0; i < n; i++)
#define reps(i, a, n) for (int i = a; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define rreps(i, a, n) for (int i = n - 1; i >= a; i--)
#define validl(i, a, b) ((i >= a) && (i <= b)) ? 1 : 0
#define validmat(x, y, a, b, c, d) (validl(x, a, b) && validl(y, a, b)) ? 1 : 0
#define validmn(x, y, m, n) validmat(x, y, 0, m, 0, n)
#define valid(i, n) validl(i, 0, n)
#define v(t) vector<t>
#define vv(t) vector<vector<t>>
#define mk make_pair
#define pb push_back
#define pa(a, n, t)                                                            \\
  for (int i = 0; i < n; i++)                                                  \\
  printf("%" #t "", a[i])
#define p2 pair<int, int>
#define p3 pair<p2, int>
#define fi first
#define se second
#define sd(a) scanf("%d", &a)
#define sl(a) scanf("%lld", &a)
#define pl(a) printf("%lld", a)
#define pd(a) printf("%d", a)
#define sf(a) scanf("%lf", &a)
#define pf(a) printf("%lf", a)
#define nline printf("\\n")
#define ss(a) scanf("%s", a)
#define ps(a) printf("%s", a)
#define sc(a) scanf("%c", &a)
#define pc(a) printf("%c", &a)
#define mf(m, f) m.find(f) != m.end()
#define pp3(m) cout << m.fi.fi << " " << m.fi.se << " " << m.se << " "
#define pp2(m) cout << m.fi << " " << m.se << " "
#define debug 0
/*v(int) adj[200001];
void dfs(int i,int p){
    rep(j,adj[i].size()){
        if(p!=adj[i][j])
            dfs(adj[i][j],i);
    }
}*/
int rootn;
bool compare(p3 a, p3 b) {
  if ((a.fi.fi / rootn) != (b.fi.fi / rootn))
    return (a.fi.fi / rootn) < (b.fi.fi / rootn);
  return a.fi.se < b.fi.se;
}
int brr[500001] = {0};
int dif = 0;
void add(int x) {
  if (brr[x] == 0)
    dif++;
  brr[x]++;
}
void rem(int x) {
  if (brr[x] == 1)
    dif--;
  brr[x]--;
}
int arr[500001];
int main() {
  int n;

  string s;
  cin >> n;
  rootn = static_cast<int>(sqrt(n));
  rootn = min(rootn, 500);
  // cout<<rootn<<endl;
  int k;
  cin >> k;

  sa(arr, n, d);
  int m = 0;
  v(p3) vv;
  rep(i, k) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    vv.pb(mk(mk(a, b), i));
  }
  sort(vv.begin(), vv.end(), compare);
  int ll = 0, rr = -1;
  v(int) ans(k);
  rep(i, k) {
    // cout<<vv[i].fi.fi<<" "<<vv[i].fi.se<<endl;
    int l = vv[i].fi.fi;
    int r = vv[i].fi.se;
    while (rr < r) {
      rr++;
      add(arr[rr]);
    }
    while (r < rr) {
      rem(arr[rr]);
      rr--;
    }
    while (ll < l) {
      rem(arr[ll]);
      ll++;
    }
    while (l < ll) {
      ll--;
      add(arr[ll]);
    }
    ans[vv[i].se] = dif;
  }
  rep(i, k) cout << ans[i] << "\\n";
  // cout<<endl;
}