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

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define speedX                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
using namespace std;
// #define ONLINE_JUDGE
#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);
  __p(a.second);
  cout << "}";
}
template <typename T> void __f(const char *s, T t) {
  cout << s << " : ";
  __p(t);
}
template <typename T> void __t(const char *s, const T &x) {
  cout << s << " : { ";
  for (auto it : x)
    __p(it);
  cout << "} ";
}
template <typename T, typename... F> void __f(const char *s, T t, F... f) {
  int i = 0;
  for (;; ++i)
    if (s[i] == ',')
      break;
  cout.write(s, i) << " : ";
  __p(t);
  cout << " | ";
  __f(s + i + 1, f...);
}
#define trace(...)                                                             \\
  {                                                                            \\
    cout << "LINE: " << __LINE__ << " || ";                                    \\
    __f(#__VA_ARGS__, __VA_ARGS__);                                            \\
    cout << "\\n\\n";                                                            \\
  }
#define debug(...)                                                             \\
  {                                                                            \\
    cout << "LINE: " << __LINE__ << " || ";                                    \\
    __t(#__VA_ARGS__, __VA_ARGS__);                                            \\
    cout << "\\n\\n";                                                            \\
  }
int begtime = clock();
#define end_time()                                                             \\
  cout << "\\n\\nTime of execution: "                                            \\
       << (clock() - begtime) * 1000 / CLOCKS_PER_SEC << " ms\\n\\n";
#else
#define trace(...)
#define debug(...)
#define end_time()
#endif

#define int long long
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef vector<int> vi;
typedef vector<pii> vii;

inline bool equals(ld a, ld b) { return fabs(a - b) < 1e-9; }
inline int gcd(int a, int b) {
  a = abs(a);
  b = abs(b);
  while (a > 0 && b > 0)
    (a > b ? a %= b : b %= a);
  return (a == 0 ? b : a);
}
inline int power(int x, int n, int m = LLONG_MAX) {
  int res = 1;
  x = (x % m + m) % m;
  while (n) {
    if (n & 1)
      res = (res * x) % m;
    x = (x * x) % m;
    n >>= 1;
  }
  return res;
}
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define ff first
#define ss second
#define uset unordered_set
#define umap unordered_map
#define all(x) x.begin(), x.end()
#define mod 1000000007

const int MAX = 1000001;

// structure to store queries
struct Query {
  int l, r, idx;
};

// cmp function to sort queries according to r
bool cmp(Query x, Query y) { return x.r < y.r; }

// updating the bit array
void update(int idx, int val, int bit[], int n) {
  for (; idx <= n; idx += idx & -idx)
    bit[idx] += val;
}

// querying the bit array
int query(int idx, int bit[], int n) {
  int sum = 0;
  for (; idx > 0; idx -= idx & -idx)
    sum += bit[idx];
  return sum;
}

void answeringQueries(int arr[], int n, Query queries[], int q) {
  // initialising bit array
  int bit[n + 1];
  memset(bit, 0, sizeof(bit));

  // holds the rightmost index of any number
  // as numbers of a[i] are less than or equal to 10^6
  int last_visit[MAX];
  memset(last_visit, -1, sizeof(last_visit));

  // answer for each query
  int ans[q];
  int query_counter = 0;
  for (int i = 0; i < n; i++) {
    // If last visit is not -1 update -1 at the
    // idx equal to last_visit[arr[i]]
    if (last_visit[arr[i]] != -1)
      update(last_visit[arr[i]] + 1, -1, bit, n);

    // Setting last_visit[arr[i]] as i and updating
    // the bit array accordingly
    last_visit[arr[i]] = i;
    update(i + 1, 1, bit, n);

    // If i is equal to r of any query  store answer
    // for that query in ans[]
    while (query_counter < q && queries[query_counter].r == i) {
      ans[queries[query_counter].idx] =
          query(queries[query_counter].r + 1, bit, n) -
          query(queries[query_counter].l, bit, n);
      query_counter++;
    }
  }

  // print answer for each query
  for (int i = 0; i < q; i++)
    cout << ans[i] << endl;
}

void solve() {
  int n, q;
  cin >> n >> q;
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  Query queries[q];
  for (int i = 0; i < q; i++) {
    int x, y;
    cin >> x >> y;
    queries[i].l = x - 1;
    queries[i].r = y - 1;
    queries[i].idx = i;
  }
  sort(queries, queries + q, cmp);
  answeringQueries(a, n, queries, q);
  return;
}
signed main() {
  speedX;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int t = 1;
  // cin>>t;
  for (int i = 1; i <= t; ++i) {
    // cout<<"Case #"<<i<<": ";
    solve();
  }
  end_time();
  return 0;
}


Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define speedX                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
using namespace std;
// #define ONLINE_JUDGE
#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);
  __p(a.second);
  cout << "}";
}
template <typename T> void __f(const char *s, T t) {
  cout << s << " : ";
  __p(t);
}
template <typename T> void __t(const char *s, const T &x) {
  cout << s << " : { ";
  for (auto it : x)
    __p(it);
  cout << "} ";
}
template <typename T, typename... F> void __f(const char *s, T t, F... f) {
  int i = 0;
  for (;; ++i)
    if (s[i] == ',')
      break;
  cout.write(s, i) << " : ";
  __p(t);
  cout << " | ";
  __f(s + i + 1, f...);
}
#define trace(...)                                                             \\
  {                                                                            \\
    cout << "LINE: " << __LINE__ << " || ";                                    \\
    __f(#__VA_ARGS__, __VA_ARGS__);                                            \\
    cout << "\\n\\n";                                                            \\
  }
#define debug(...)                                                             \\
  {                                                                            \\
    cout << "LINE: " << __LINE__ << " || ";                                    \\
    __t(#__VA_ARGS__, __VA_ARGS__);                                            \\
    cout << "\\n\\n";                                                            \\
  }
int begtime = clock();
#define end_time()                                                             \\
  cout << "\\n\\nTime of execution: "                                            \\
       << (clock() - begtime) * 1000 / CLOCKS_PER_SEC << " ms\\n\\n";
#else
#define trace(...)
#define debug(...)
#define end_time()
#endif

#define int long long
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef vector<int> vi;
typedef vector<pii> vii;

inline bool equals(ld a, ld b) { return fabs(a - b) < 1e-9; }
inline int gcd(int a, int b) {
  a = abs(a);
  b = abs(b);
  while (a > 0 && b > 0)
    (a > b ? a %= b : b %= a);
  return (a == 0 ? b : a);
}
inline int power(int x, int n, int m = LLONG_MAX) {
  int res = 1;
  x = (x % m + m) % m;
  while (n) {
    if (n & 1)
      res = (res * x) % m;
    x = (x * x) % m;
    n >>= 1;
  }
  return res;
}
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define ff first
#define ss second
#define uset unordered_set
#define umap unordered_map
#define all(x) x.begin(), x.end()
#define mod 1000000007
uset<int> tree[4000022];
void build(int c[], int node, int l, int r) {
  if (l == r) {
    tree[node].insert(c[l]);
    return;
  }
  int m = (l + r) / 2;
  build(c, (2 * node), l, m);
  build(c, (2 * node) + 1, m + 1, r);
  tree[node].insert(all(tree[2 * node]));
  tree[node].insert(all(tree[(2 * node) + 1]));
  return;
}
uset<int> query(int node, int l, int r, int x, int y) {
  if (r < x || l > y || r < l) {
    uset<int> s;
    return s;
  }
  if (l >= x && r <= y) {
    return tree[node];
  }
  int m = (l + r) / 2;
  uset<int> a = query((2 * node), l, m, x, y);
  uset<int> b = query((2 * node) + 1, m + 1, r, x, y);
  a.insert(all(b));
  return a;
}
void solve() {
  int n, q;
  cin >> n >> q;
  int c[n];
  for (int i = 0; i < n; i++) {
    cin >> c[i];
  }
  build(c, 1, 0, n - 1);
  for (int i = 0; i < q; i++) {
    int x, y;
    cin >> x >> y;
    uset<int> s = query(1, 0, n - 1, x - 1, y - 1);
    cout << s.size() << "\\n";
  }
}
signed main() {
  speedX;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int t = 1;
  // cin>>t;
  for (int i = 1; i <= t; ++i) {
    // cout<<"Case #"<<i<<": ";
    solve();
  }
  end_time();
  return 0;
}