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

#include <bits/stdc++.h>
using namespace std;
#pragma region Macros
#define ios                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(nullptr);
#define VEC(type, name, size)                                                  \\
  V<type> name(size);                                                          \\
  IN(name)
#define VVEC(type, name, h, w)                                                 \\
  VV<type> name(h, V<type>(w));                                                \\
  IN(name)
#define INT(...)                                                               \\
  int __VA_ARGS__;                                                             \\
  IN(__VA_ARGS__)
#define LL(...)                                                                \\
  ll __VA_ARGS__;                                                              \\
  IN(__VA_ARGS__)
#define STR(...)                                                               \\
  string __VA_ARGS__;                                                          \\
  IN(__VA_ARGS__)
#define CHAR(...)                                                              \\
  char __VA_ARGS__;                                                            \\
  IN(__VA_ARGS__)
#define DOUBLE(...)                                                            \\
  DOUBLE __VA_ARGS__;                                                          \\
  IN(__VA_ARGS__)
#define LD(...)                                                                \\
  LD __VA_ARGS__;                                                              \\
  IN(__VA_ARGS__)
template <class T> void scan(T &a) { cin >> a; }
template <class T> void scan(vector<T> &a) {
  for (auto &i : a)
    scan(i);
}
template <class T, class L> void scan(pair<T, L> &p) {
  scan(p.first);
  scan(p.second);
}
void IN() {}
template <class Head, class... Tail> void IN(Head &head, Tail &...tail) {
  scan(head);
  IN(tail...);
}
template <class T> inline void print(T x) { cout << x << '\\n'; }
#define ll long long
#define ld long double
#define FOR(i, l, r) for (ll i = (l); i < (r); ++i)
#define REP(i, n) FOR(i, 0, n)
#define REPS(i, n) FOR(i, 1, n + 1)
#define RFOR(i, l, r) for (ll i = (l); i >= (r); --i)
#define RREP(i, n) RFOR(i, n - 1, 0)
#define RREPS(i, n) RFOR(i, n, 1)
#define pb push_back
#define eb emplace_back
#define SZ(x) ((ll)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
template <class T = ll> using V = vector<T>;
template <class T = ll> using VV = V<V<T>>;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
inline void Yes(bool b = true) { cout << (b ? "Yes" : "No") << '\\n'; }
inline void YES(bool b = true) { cout << (b ? "YES" : "NO") << '\\n'; }
inline void err(bool b = true) {
  if (b) {
    cout << -1 << '\\n';
    exit(0);
  }
}
template <class T> inline void fin(bool b = true, T e = 0) {
  if (b) {
    cout << e << '\\n';
    exit(0);
  }
}
template <class T> T Roundup_div(T x, T y) { return (x + (y - 1)) / y; }
template <typename T> T pow(T a, long long n, T e = 1) {
  T ret = e;
  while (n) {
    if (n & 1)
      ret *= a;
    a *= a;
    n >>= 1;
  }
  return ret;
}
const ll INF = 1e18;
#pragma endregion

// Binary Indexed Tree (Fenwick Tree)
template <typename T> struct BIT {
  int n, p;
  vector<T> d;
  BIT(int n = 0) : n(n), d(n + 1) {
    p = 1;
    while (p < n)
      p *= 2;
  }
  // 更新
  void add(int i, T x = 1) {
    for (i++; i <= n; i += i & -i)
      d[i] += x;
  }
  //[0,i)の和
  T sum(int i) {
    T res = 0;
    for (; i; i -= i & -i)
      res += d[i];
    return res;
  }
  //[l,r)の和
  T sum(int l, int r) { return sum(r) - sum(l); }
  // v0+v1+…vx>=mを満たすmin(x)を返す
  int lower_bound(T w) {
    if (w <= 0)
      return 0;
    T x = 0;
    for (int i = p; i; i /= 2) {
      if (i + x <= n && d[i + x] < w) {
        w -= d[i + x];
        x += i;
      }
    }
    return x;
  }
};

int main() {
  ios;
  LL(n, q);
  VEC(ll, c, n);
  REP(i, n) c[i]--;
  V<> last(n, -1);
  BIT<ll> BT(n);
  V<pair<pair<int, int>, int>> vec(q);
  REP(i, q) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;
    vec[i] = {{r, l}, i};
  }
  sort(all(vec));
  V<> ans(q);
  ll now = -1;
  REP(i, q) {
    ll r = vec[i].first.first;
    for (int i = now + 1; i <= r; i++) {
      if (last[c[i]] != -1)
        BT.add(last[c[i]], -1);
      last[c[i]] = i;
      BT.add(last[c[i]], 1);
      now = r;
    }
    ans[vec[i].second] = BT.sum(vec[i].first.second, vec[i].first.first + 1);
  }
  REP(i, q) print(ans[i]);
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#pragma region Macros
#define ios                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(nullptr);
#define VEC(type, name, size)                                                  \\
  V<type> name(size);                                                          \\
  IN(name)
#define VVEC(type, name, h, w)                                                 \\
  VV<type> name(h, V<type>(w));                                                \\
  IN(name)
#define INT(...)                                                               \\
  int __VA_ARGS__;                                                             \\
  IN(__VA_ARGS__)
#define LL(...)                                                                \\
  ll __VA_ARGS__;                                                              \\
  IN(__VA_ARGS__)
#define STR(...)                                                               \\
  string __VA_ARGS__;                                                          \\
  IN(__VA_ARGS__)
#define CHAR(...)                                                              \\
  char __VA_ARGS__;                                                            \\
  IN(__VA_ARGS__)
#define DOUBLE(...)                                                            \\
  DOUBLE __VA_ARGS__;                                                          \\
  IN(__VA_ARGS__)
#define LD(...)                                                                \\
  LD __VA_ARGS__;                                                              \\
  IN(__VA_ARGS__)
template <class T> void scan(T &a) { cin >> a; }
template <class T> void scan(vector<T> &a) {
  for (auto &i : a)
    scan(i);
}
template <class T, class L> void scan(pair<T, L> &p) {
  scan(p.first);
  scan(p.second);
}
void IN() {}
template <class Head, class... Tail> void IN(Head &head, Tail &...tail) {
  scan(head);
  IN(tail...);
}
template <class T> inline void print(T x) { cout << x << '\\n'; }
#define ll long long
#define ld long double
#define FOR(i, l, r) for (ll i = (l); i < (r); ++i)
#define REP(i, n) FOR(i, 0, n)
#define REPS(i, n) FOR(i, 1, n + 1)
#define RFOR(i, l, r) for (ll i = (l); i >= (r); --i)
#define RREP(i, n) RFOR(i, n - 1, 0)
#define RREPS(i, n) RFOR(i, n, 1)
#define pb push_back
#define eb emplace_back
#define SZ(x) ((ll)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
template <class T = ll> using V = vector<T>;
template <class T = ll> using VV = V<V<T>>;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
inline void Yes(bool b = true) { cout << (b ? "Yes" : "No") << '\\n'; }
inline void YES(bool b = true) { cout << (b ? "YES" : "NO") << '\\n'; }
inline void err(bool b = true) {
  if (b) {
    cout << -1 << '\\n';
    exit(0);
  }
}
template <class T> inline void fin(bool b = true, T e = 0) {
  if (b) {
    cout << e << '\\n';
    exit(0);
  }
}
template <class T> T Roundup_div(T x, T y) { return (x + (y - 1)) / y; }
template <typename T> T pow(T a, long long n, T e = 1) {
  T ret = e;
  while (n) {
    if (n & 1)
      ret *= a;
    a *= a;
    n >>= 1;
  }
  return ret;
}
const ll INF = 1e18;
#pragma endregion

// Binary Indexed Tree (Fenwick Tree)
template <typename T> struct BIT {
  int n, p;
  vector<T> d;
  BIT(int n = 0) : n(n), d(n + 1) {
    p = 1;
    while (p < n)
      p *= 2;
  }
  // 更新
  void add(int i, T x = 1) {
    for (i++; i <= n; i += i & -i)
      d[i] += x;
  }
  //[0,i)の和
  T sum(int i) {
    T res = 0;
    for (; i; i -= i & -i)
      res += d[i];
    return res;
  }
  //[l,r)の和
  T sum(int l, int r) { return sum(r) - sum(l); }
  // v0+v1+…vx>=mを満たすmin(x)を返す
  int lower_bound(T w) {
    if (w <= 0)
      return 0;
    T x = 0;
    for (int i = p; i; i /= 2) {
      if (i + x <= n && d[i + x] < w) {
        w -= d[i + x];
        x += i;
      }
    }
    return x;
  }
};

int main() {
  ios;
  LL(n, q);
  VEC(ll, c, n);
  REP(i, n) c[i]--;
  V<> last(n, -1);
  BIT<ll> BT(n);
  V<pair<pair<int, int>, int>> vec(q);
  REP(i, q) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;
    vec[i] = {{r, l}, i};
  }
  sort(all(vec));
  V<> ans(q);
  ll now = -1;
  REP(i, q) {
    ll r = vec[i].first.first;
    for (int i = now + 1; i <= r; i++) {
      if (last[c[i]] != -1)
        BT.add(last[c[i]], -1);
      last[c[i]] = i;
      BT.add(last[c[i]], 1);
    }
    ans[vec[i].second] = BT.sum(vec[i].first.second, vec[i].first.first + 1);
  }
  REP(i, q) print(ans[i]);
}