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

#include <bits/stdc++.h>
using namespace std;

#define int long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, n) for (int i = 1; i <= (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define uniq(x) (x).erase(unique(all(x)), (x).end())
#define bit(n) (1LL << (n))
#define cdiv(a, b) (((a)-1) / (b) + 1)
#define dump(x) cerr << #x " = " << (x) << endl
using vint = vector<int>;
using vvint = vector<vint>;
using pint = pair<int, int>;
using vpint = vector<pint>;
template <typename T>
using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>;
constexpr double PI = 3.1415926535897932384626433832795028;
constexpr int DY[9] = {0, 1, 0, -1, 1, 1, -1, -1, 0};
constexpr int DX[9] = {1, 0, -1, 0, 1, -1, -1, 1, 0};
int gcd(int a, int b) {
  while (b) {
    swap(a %= b, b);
  }
  return a;
}
int lcm(int a, int b) { return a / gcd(a, b) * b; }
int sgn(int x) { return (x > 0) - (x < 0); }
template <typename T> void fin(T mes) {
  cout << mes << endl;
  exit(0);
}
template <typename T, typename U> bool chmax(T &a, const U &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T, typename U> bool chmin(T &a, const U &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &rhs) {
  os << "(" << rhs.first << ", " << rhs.second << ")";
  return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &rhs) {
  os << "{";
  for (auto itr = rhs.begin(); itr != rhs.end(); itr++) {
    os << *itr << (next(itr) != rhs.end() ? ", " : "");
  }
  os << "}";
  return os;
}
struct setup {
  static constexpr int PREC = 20;
  setup() {
    cout << fixed << setprecision(PREC);
    cerr << fixed << setprecision(PREC);
  };
} setup;

template <typename M> struct fenwick_tree {
  using T = typename M::T;
  int n;
  std::vector<T> data;
  fenwick_tree(int n) : n(n), data(n + 1, M::id()) {}
  void add(int i, const T &x) {
    for (i++; i <= n; i += i & -i) {
      data[i] = M::op(data[i], x);
    }
  }
  T get_sum(int i) const {
    T ret = M::id();
    for (; i > 0; i -= i & -i) {
      ret = M::op(ret, data[i]);
    }
    return ret;
  }
  T get_sum(int l, int r) const {
    return M::op(get_sum(r), M::inv(get_sum(l)));
  }
};

struct rsq {
  using T = int;
  static T id() { return 0; }
  static T op(const T &a, const T &b) { return a + b; }
  static T inv(const T &a) { return -a; }
};

int N, Q;
int c[550000];
int l[550000], r[550000];
pair<pint, int> rlq[550000];
int ans[550000];
signed main() {
  cin >> N >> Q;
  rep(i, N) { cin >> c[i]; }
  rep(i, Q) {
    cin >> l[i] >> r[i];
    l[i]--, r[i]--;
    rlq[i] = {{r[i], l[i]}, i};
  }
  sort(rlq, rlq + Q);
  fenwick_tree<rsq> ft(N);
  vint last(N + 1, -1);
  int cur = 0;
  rep(i, N) {
    if (last[c[i]] != -1) {
      ft.add(last[c[i]], -1);
    }
    last[c[i]] = i;
    ft.add(i, 1);
    while (cur < Q && rlq[cur].first.first == i) {
      ans[rlq[cur].second] =
          ft.get_sum(rlq[cur].first.second, rlq[cur].first.first + 1);
      cur++;
    }
  }
  rep(i, Q) { cout << ans[i] << endl; }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define int long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, n) for (int i = 1; i <= (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define uniq(x) (x).erase(unique(all(x)), (x).end())
#define bit(n) (1LL << (n))
#define cdiv(a, b) (((a)-1) / (b) + 1)
#define dump(x) cerr << #x " = " << (x) << endl
using vint = vector<int>;
using vvint = vector<vint>;
using pint = pair<int, int>;
using vpint = vector<pint>;
template <typename T>
using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>;
constexpr double PI = 3.1415926535897932384626433832795028;
constexpr int DY[9] = {0, 1, 0, -1, 1, 1, -1, -1, 0};
constexpr int DX[9] = {1, 0, -1, 0, 1, -1, -1, 1, 0};
int gcd(int a, int b) {
  while (b) {
    swap(a %= b, b);
  }
  return a;
}
int lcm(int a, int b) { return a / gcd(a, b) * b; }
int sgn(int x) { return (x > 0) - (x < 0); }
template <typename T> void fin(T mes) {
  cout << mes << endl;
  exit(0);
}
template <typename T, typename U> bool chmax(T &a, const U &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T, typename U> bool chmin(T &a, const U &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &rhs) {
  os << "(" << rhs.first << ", " << rhs.second << ")";
  return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &rhs) {
  os << "{";
  for (auto itr = rhs.begin(); itr != rhs.end(); itr++) {
    os << *itr << (next(itr) != rhs.end() ? ", " : "");
  }
  os << "}";
  return os;
}
struct setup {
  static constexpr int PREC = 20;
  setup() {
    cout << fixed << setprecision(PREC);
    cerr << fixed << setprecision(PREC);
  };
} setup;

template <typename M> struct segment_tree {
  using T = typename M::T;
  int n;
  std::vector<T> data;
  segment_tree(int n) : n(n), data(n << 1, M::id()) {}
  segment_tree(const std::vector<T> &src) : n(src.size()), data(n << 1) {
    std::copy(src.begin(), src.end(), data.begin() + n);
    for (int i = n - 1; i > 0; i--) {
      data[i] = M::op(data[i << 1 | 0], data[i << 1 | 1]);
    }
  }
  void set(int i, const T &x) {
    data[i += n] = x;
    while (i >>= 1) {
      data[i] = M::op(data[i << 1 | 0], data[i << 1 | 1]);
    }
  }
  const T &operator[](int i) const { return data[i + n]; }
  T get_sum(int l, int r) const {
    T a = M::id(), b = M::id();
    for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
      if (l & 1) {
        a = M::op(a, data[l++]);
      }
      if (r & 1) {
        b = M::op(data[--r], b);
      }
    }
    return M::op(a, b);
  }
};

struct rsq {
  using T = vint;
  static T op(T a, T b) {
    a.push_back(LLONG_MAX), b.push_back(LLONG_MAX);
    vint ret;
    int L = 0, R = 0;
    while (L < a.size() && R < b.size()) {
      if (a[L] < b[R]) {
        ret.push_back(a[L]), L++;
      } else if (a[L] > b[R]) {
        ret.push_back(b[R]), R++;
      } else {
        ret.push_back(a[L]), L++, R++;
      }
    }
    ret.pop_back();
    return ret;
  }
  static T id() { return {}; }
};

int N, Q;
int c[550000];
int l[550000], r[550000];
signed main() {
  cin >> N >> Q;
  vvint src(N);
  rep(i, N) {
    cin >> c[i];
    src[i].push_back(c[i]);
  }
  segment_tree<rsq> st(src);
  rep(i, Q) {
    cin >> l[i] >> r[i];
    cout << st.get_sum(l[i] - 1, r[i]).size() << '\\n';
  }
}