Problem p02599 (C++)

Buggy submission (status: Time Limit Exceeded):
// HEADER FILES AND NAMESPACES

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

template <typename T>
using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag,
                              tree_order_statistics_node_update>;

// DEFINE STATEMENTS

#define num1 1000000007
#define num2 998244353
#define REP(i, a, n) for (ll i = a; i < n; i++)
#define REPd(i, a, n) for (ll i = a; i >= n; i--)
#define pb push_back
#define pob pop_back
#define f first
#define s second
#define fix(f, n) std::fixed << std::setprecision(n) << f
#define all(x) x.begin(), x.end()
#define M_PI 3.14159265358979323846
#define epsilon (double)(0.000000001)

typedef long long ll;
typedef vector<long long> vll;
typedef pair<long long, long long> pll;
typedef vector<pair<long long, long long>> vpll;
typedef vector<int> vii;

// DEBUG FUNCTIONS
#ifndef ONLINE_JUDGE

template <typename T> void __p(T a) { cout << a; }
template <typename T, typename F> void __p(pair<T, F> a) {
  cout << "{";
  __p(a.first);
  cout << ",";
  __p(a.second);
  cout << "}";
}
template <typename T> void __p(std::vector<T> a) {
  cout << "{";
  for (auto it = a.begin(); it < a.end(); it++)
    __p(*it), cout << ",}"[it + 1 == a.end()];
}
template <typename T> void __p(std::set<T> a) {
  cout << "{";
  for (auto it = a.begin(); it != a.end();) {
    __p(*it);
    cout << ",}"[++it == a.end()];
  }
}
template <typename T> void __p(std::multiset<T> a) {
  cout << "{";
  for (auto it = a.begin(); it != a.end();) {
    __p(*it);
    cout << ",}"[++it == a.end()];
  }
}
template <typename T, typename F> void __p(std::map<T, F> a) {
  cout << "{\n";
  for (auto it = a.begin(); it != a.end(); ++it) {
    __p(it->first);
    cout << ": ";
    __p(it->second);
    cout << "\n";
  }
  cout << "}\n";
}

template <typename T, typename... Arg> void __p(T a1, Arg... a) {
  __p(a1);
  __p(a...);
}
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cout << name << " : ";
  __p(arg1);
  cout << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  int bracket = 0, i = 0;
  for (;; i++)
    if (names[i] == ',' && bracket == 0)
      break;
    else if (names[i] == '(')
      bracket++;
    else if (names[i] == ')')
      bracket--;
  const char *comma = names + i;
  cout.write(names, comma - names) << " : ";
  __p(arg1);
  cout << " | ";
  __f(comma + 1, args...);
}
#define trace(...)                                                             \
  cout << "Line:" << __LINE__ << " ", __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...)
#define error(...)
#endif

// DEBUG FUNCTIONS END

// CUSTOM HASH TO SPEED UP UNORDERED MAP AND TO AVOID FORCED CLASHES
struct custom_hash {
  static uint64_t splitmix64(uint64_t x) {
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }

  size_t operator()(uint64_t x) const {
    static const uint64_t FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
};

mt19937_64 rng(chrono::steady_clock::now()
                   .time_since_epoch()
                   .count()); // FOR RANDOM NUMBER GENERATION

ll mod_exp(ll a, ll b, ll c) {
  ll res = 1;
  a = a % c;
  while (b > 0) {
    if (b % 2 == 1)
      res = (res * a) % c;
    b /= 2;
    a = (a * a) % c;
  }
  return res;
}
ll mymod(ll a, ll b) { return ((a % b) + b) % b; }
ll gcdExtended(ll, ll, ll *, ll *);
ll modInverse(ll a, ll m) {
  ll x, y;
  ll g = gcdExtended(a, m, &x, &y);
  g++; // this line was added just to remove compiler warning
  ll res = (x % m + m) % m;
  return res;
}
ll gcdExtended(ll a, ll b, ll *x, ll *y) {
  if (a == 0) {
    *x = 0, *y = 1;
    return b;
  }
  ll x1, y1;
  ll gcd = gcdExtended(b % a, a, &x1, &y1);
  *x = y1 - (b / a) * x1;
  *y = x1;
  return gcd;
}
struct Query {
  ll l, r, index;
};
const int xx = 300;
bool cmp(Query a, Query b) {
  if (a.l / xx < b.l / xx) {
    return true;
  } else if (a.l / xx == b.l / xx && a.r < b.r) {
    return true;
  }
  return false;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, q;
  cin >> n >> q;
  vll freq(n + 1, 0);
  ll init = 0;
  ll A[n];
  REP(i, 0, n) {
    cin >> A[i];
    freq[A[i]]++;
    if (freq[A[i]] == 1)
      init++;
  }
  Query Q[q];
  REP(i, 0, q) {
    cin >> Q[i].l >> Q[i].r;
    Q[i].l--;
    Q[i].r--;
    Q[i].index = i;
  }
  vll ans(q, -1);
  sort(Q, Q + q, cmp);
  ll left = 0, right = n - 1;
  ll cur_ans = init;
  REP(i, 0, q) {
    if (Q[i].l < left) {
      // cerr << "1" << endl;
      REP(j, Q[i].l, left) {
        freq[A[j]]++;
        if (freq[A[j]] == 1)
          cur_ans++;
      }
    } else if (Q[i].l > left) {
      // cerr << "2" << endl;
      REP(j, left, Q[i].l) {
        freq[A[j]]--;
        if (freq[A[j]] == 0)
          cur_ans--;
      }
    }
    if (Q[i].r > right) {
      // cerr << "3" << endl;
      REP(j, right + 1, Q[i].r + 1) {
        freq[A[j]]++;
        if (freq[A[j]] == 1)
          cur_ans++;
      }
    } else if (Q[i].r < right) {
      // cerr << "4" << endl;
      REP(j, Q[i].r + 1, right + 1) {
        freq[A[j]]--;
        if (freq[A[j]] == 0)
          cur_ans--;
      }
    }
    // trace("here");
    ans[Q[i].index] = cur_ans;
    left = Q[i].l;
    right = Q[i].r;
  }
  REP(i, 0, q) { cout << ans[i] << "\n"; }
  return 0;
}


Fixed submission:
// HEADER FILES AND NAMESPACES

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

template <typename T>
using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag,
                              tree_order_statistics_node_update>;

// DEFINE STATEMENTS

#define num1 1000000007
#define num2 998244353
#define REP(i, a, n) for (ll i = a; i < n; i++)
#define REPd(i, a, n) for (ll i = a; i >= n; i--)
#define pb push_back
#define pob pop_back
#define f first
#define s second
#define fix(f, n) std::fixed << std::setprecision(n) << f
#define all(x) x.begin(), x.end()
#define M_PI 3.14159265358979323846
#define epsilon (double)(0.000000001)

typedef long long ll;
typedef vector<long long> vll;
typedef pair<long long, long long> pll;
typedef vector<pair<long long, long long>> vpll;
typedef vector<int> vii;

// DEBUG FUNCTIONS
#ifndef ONLINE_JUDGE

template <typename T> void __p(T a) { cout << a; }
template <typename T, typename F> void __p(pair<T, F> a) {
  cout << "{";
  __p(a.first);
  cout << ",";
  __p(a.second);
  cout << "}";
}
template <typename T> void __p(std::vector<T> a) {
  cout << "{";
  for (auto it = a.begin(); it < a.end(); it++)
    __p(*it), cout << ",}"[it + 1 == a.end()];
}
template <typename T> void __p(std::set<T> a) {
  cout << "{";
  for (auto it = a.begin(); it != a.end();) {
    __p(*it);
    cout << ",}"[++it == a.end()];
  }
}
template <typename T> void __p(std::multiset<T> a) {
  cout << "{";
  for (auto it = a.begin(); it != a.end();) {
    __p(*it);
    cout << ",}"[++it == a.end()];
  }
}
template <typename T, typename F> void __p(std::map<T, F> a) {
  cout << "{\n";
  for (auto it = a.begin(); it != a.end(); ++it) {
    __p(it->first);
    cout << ": ";
    __p(it->second);
    cout << "\n";
  }
  cout << "}\n";
}

template <typename T, typename... Arg> void __p(T a1, Arg... a) {
  __p(a1);
  __p(a...);
}
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cout << name << " : ";
  __p(arg1);
  cout << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  int bracket = 0, i = 0;
  for (;; i++)
    if (names[i] == ',' && bracket == 0)
      break;
    else if (names[i] == '(')
      bracket++;
    else if (names[i] == ')')
      bracket--;
  const char *comma = names + i;
  cout.write(names, comma - names) << " : ";
  __p(arg1);
  cout << " | ";
  __f(comma + 1, args...);
}
#define trace(...)                                                             \
  cout << "Line:" << __LINE__ << " ", __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...)
#define error(...)
#endif

// DEBUG FUNCTIONS END

// CUSTOM HASH TO SPEED UP UNORDERED MAP AND TO AVOID FORCED CLASHES
struct custom_hash {
  static uint64_t splitmix64(uint64_t x) {
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }

  size_t operator()(uint64_t x) const {
    static const uint64_t FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
};

mt19937_64 rng(chrono::steady_clock::now()
                   .time_since_epoch()
                   .count()); // FOR RANDOM NUMBER GENERATION

ll mod_exp(ll a, ll b, ll c) {
  ll res = 1;
  a = a % c;
  while (b > 0) {
    if (b % 2 == 1)
      res = (res * a) % c;
    b /= 2;
    a = (a * a) % c;
  }
  return res;
}
ll mymod(ll a, ll b) { return ((a % b) + b) % b; }
ll gcdExtended(ll, ll, ll *, ll *);
ll modInverse(ll a, ll m) {
  ll x, y;
  ll g = gcdExtended(a, m, &x, &y);
  g++; // this line was added just to remove compiler warning
  ll res = (x % m + m) % m;
  return res;
}
ll gcdExtended(ll a, ll b, ll *x, ll *y) {
  if (a == 0) {
    *x = 0, *y = 1;
    return b;
  }
  ll x1, y1;
  ll gcd = gcdExtended(b % a, a, &x1, &y1);
  *x = y1 - (b / a) * x1;
  *y = x1;
  return gcd;
}
struct Query {
  ll l, r, index;
};
const int xx = 750;
bool cmp(Query a, Query b) {
  if (a.l / xx < b.l / xx) {
    return true;
  } else if (a.l / xx == b.l / xx && a.r < b.r) {
    return true;
  }
  return false;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, q;
  cin >> n >> q;
  vll freq(n + 1, 0);
  ll init = 0;
  ll A[n];
  REP(i, 0, n) {
    cin >> A[i];
    freq[A[i]]++;
    if (freq[A[i]] == 1)
      init++;
  }
  Query Q[q];
  REP(i, 0, q) {
    cin >> Q[i].l >> Q[i].r;
    Q[i].l--;
    Q[i].r--;
    Q[i].index = i;
  }
  vll ans(q, -1);
  sort(Q, Q + q, cmp);
  ll left = 0, right = n - 1;
  ll cur_ans = init;
  REP(i, 0, q) {
    if (Q[i].l < left) {
      // cerr << "1" << endl;
      REP(j, Q[i].l, left) {
        freq[A[j]]++;
        if (freq[A[j]] == 1)
          cur_ans++;
      }
    } else if (Q[i].l > left) {
      // cerr << "2" << endl;
      REP(j, left, Q[i].l) {
        freq[A[j]]--;
        if (freq[A[j]] == 0)
          cur_ans--;
      }
    }
    if (Q[i].r > right) {
      // cerr << "3" << endl;
      REP(j, right + 1, Q[i].r + 1) {
        freq[A[j]]++;
        if (freq[A[j]] == 1)
          cur_ans++;
      }
    } else if (Q[i].r < right) {
      // cerr << "4" << endl;
      REP(j, Q[i].r + 1, right + 1) {
        freq[A[j]]--;
        if (freq[A[j]] == 0)
          cur_ans--;
      }
    }
    // trace("here");
    ans[Q[i].index] = cur_ans;
    left = Q[i].l;
    right = Q[i].r;
  }
  REP(i, 0, q) { cout << ans[i] << "\n"; }
  return 0;
}