Problem p02599 (C++)

Buggy submission (status: Time Limit Exceeded):

#include <bits/stdc++.h>
using namespace std;
/**
******************* Author:Bisnu sarkar ****************************
**/
#define ull unsigned long long
#define ll long long
#define pii pair<int, int>
#define sit set<int>::iterator
#define vrit vector<int>::reverse iterator
#define ff first
#define ss second
#define endl '\n';
#define sz(s) (int)s.size()
#define all(s) s.begin(), s.end()
#define IO ios_base::sync_with_stdio(false), cin.tie(NULL);
#define what_is(x) cerr << #x << " is " << x << endl;
///*....Debugger....*///
#define error(args...)                                                         \
  {                                                                            \
    string _s = #args;                                                         \
    replace(_s.begin(), _s.end(), ',', ' ');                                   \
    stringstream _ss(_s);                                                      \
    istream_iterator<string> _it(_ss);                                         \
    err(_it, args);                                                            \
  }
void err(istream_iterator<string> it) { cout << endl; }
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << ", ";
  err(++it, args...);
}
///*....Input....*///
template <typename T> inline void Int(T &n) {
  n = 0;
  int f = 1;
  register int ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      f = -1;
  for (; isdigit(ch); ch = getchar())
    n = (n << 3) + (n << 1) + ch - '0';
  n = n * f;
}

template <typename T, typename TT> inline void Int(T &n, TT &m) {
  Int(n);
  Int(m);
}
template <typename T, typename TT, typename TTT>
inline void Int(T &n, TT &m, TTT &l) {
  Int(n, m);
  Int(l);
}
double ind() {
  double x;
  scanf("%lf", &x);
  return x;
}

///*....Bitmask....*///
int set_1(int n, int pos) { return n = (n | (1 << pos)); }
int reset_0(int n, int pos) { return n = n & ~(1 << pos); }
bool check_bit(int n, int pos) { return n = n & (1 << pos); }

int dx[8] = {1, 0, -1, 0, -1, -1, 1, 1};
int dy[8] = {0, 1, 0, -1, -1, 1, -1, 1};

///*....Constraints....*///
const int N = (int)5e5 + 5;
const int M = (int)1e9 + 7;
const double pi = 2 * acos(0.0);
const double eps = 1e-9;

struct query {
  int l, r, id;
} q[N];
int a[N], k = 750, l = 1, r = 0;

bool compare(query &a, query &b) {
  int b_a = a.l / k;
  int b_b = b.l / k;
  if (b_a == b_b)
    return a.r < b.r;

  return b_a < b_b;
}
int cnt[N], ans[N];
int res = 0;
void add(int x) {
  cnt[a[x]]++;
  if (cnt[a[x]] == 1)
    ++res;
}
void remove(int x) {
  cnt[a[x]]--;
  if (cnt[a[x]] == 0)
    --res;
}

int main() {
  int t = 1, te = 0;
  // Int(t);
  while (t--) {
    int n;
    Int(n);
    int Q;
    Int(Q);
    for (int i = 1; i <= n; ++i)
      Int(a[i]);

    for (int i = 1; i <= Q; ++i) {
      Int(q[i].l, q[i].r);
      q[i].id = i;
    }
    sort(q + 1, q + Q + 1, compare);

    for (int i = 1; i <= Q; ++i) {
      while (l > q[i].l)
        add(--l);
      while (r < q[i].r)
        add(++r);
      while (l < q[i].l)
        remove(l++);
      while (r > q[i].r)
        remove(r--);
      ans[q[i].id] = res;
    }

    for (int i = 1; i <= Q; ++i) {
      printf("%d\n", ans[i]);
    }

    // printf("Case %d: ",++te);
  }
  return 0;
}


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
/**
******************* Author:Bisnu sarkar ****************************
**/
#define ull unsigned long long
#define ll long long
#define pii pair<int, int>
#define sit set<int>::iterator
#define vrit vector<int>::reverse iterator
#define ff first
#define ss second
#define endl '\n';
#define sz(s) (int)s.size()
#define all(s) s.begin(), s.end()
#define IO ios_base::sync_with_stdio(false), cin.tie(NULL);
#define what_is(x) cerr << #x << " is " << x << endl;
///*....Debugger....*///
#define error(args...)                                                         \
  {                                                                            \
    string _s = #args;                                                         \
    replace(_s.begin(), _s.end(), ',', ' ');                                   \
    stringstream _ss(_s);                                                      \
    istream_iterator<string> _it(_ss);                                         \
    err(_it, args);                                                            \
  }
void err(istream_iterator<string> it) { cout << endl; }
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << ", ";
  err(++it, args...);
}
///*....Input....*///
template <typename T> inline void Int(T &n) {
  n = 0;
  int f = 1;
  register int ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      f = -1;
  for (; isdigit(ch); ch = getchar())
    n = (n << 3) + (n << 1) + ch - '0';
  n = n * f;
}

template <typename T, typename TT> inline void Int(T &n, TT &m) {
  Int(n);
  Int(m);
}
template <typename T, typename TT, typename TTT>
inline void Int(T &n, TT &m, TTT &l) {
  Int(n, m);
  Int(l);
}
double ind() {
  double x;
  scanf("%lf", &x);
  return x;
}

///*....Bitmask....*///
int set_1(int n, int pos) { return n = (n | (1 << pos)); }
int reset_0(int n, int pos) { return n = n & ~(1 << pos); }
bool check_bit(int n, int pos) { return n = n & (1 << pos); }

int dx[8] = {1, 0, -1, 0, -1, -1, 1, 1};
int dy[8] = {0, 1, 0, -1, -1, 1, -1, 1};

///*....Constraints....*///
const int N = (int)5e5 + 5;
const int M = (int)1e9 + 7;
const double pi = 2 * acos(0.0);
const double eps = 1e-9;

struct query {
  int l, r, id;
} q[N];
int a[N], k = 1000, l = 1, r = 0;

bool compare(query &a, query &b) {
  int b_a = a.l / k;
  int b_b = b.l / k;
  if (b_a == b_b)
    return a.r < b.r;

  return b_a < b_b;
}
int cnt[N], ans[N];
int res = 0;
void add(int x) {
  cnt[a[x]]++;
  if (cnt[a[x]] == 1)
    ++res;
}
void remove(int x) {
  cnt[a[x]]--;
  if (cnt[a[x]] == 0)
    --res;
}

int main() {
  int t = 1, te = 0;
  // Int(t);
  while (t--) {
    int n;
    Int(n);
    int Q;
    Int(Q);
    for (int i = 1; i <= n; ++i)
      Int(a[i]);

    for (int i = 1; i <= Q; ++i) {
      Int(q[i].l, q[i].r);
      q[i].id = i;
    }
    sort(q + 1, q + Q + 1, compare);

    for (int i = 1; i <= Q; ++i) {
      while (l > q[i].l)
        add(--l);
      while (r < q[i].r)
        add(++r);
      while (l < q[i].l)
        remove(l++);
      while (r > q[i].r)
        remove(r--);
      ans[q[i].id] = res;
    }

    for (int i = 1; i <= Q; ++i) {
      printf("%d\n", ans[i]);
    }

    // printf("Case %d: ",++te);
  }
  return 0;
}