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

#include <bits/stdc++.h>
#include <iomanip>
#include <numeric>
#define ll long long
#define intm INT_MAX
#define llm LLONG_MAX

using namespace std;
typedef pair<int, int> P;
const int MOD = 998244353;
const ll mod = 1e9 + 7;
const int mxN = 5e5 + 9;

int n;
int bits[mxN];
int a[mxN];
int last[mxN];
pair<int, int> query[mxN];

void update(int i, int add) {
  while (i <= n) {
    bits[i] += add;
    i += i & (-i);
  }
}

int sum(int i) {
  int sm = 0;
  while (i > 0) {
    sm += bits[i];
    i -= i & (-i);
  }
  return sm;
}

int range_sum(int L, int R) {
  if (L == 1)
    return sum(R);
  return sum(R) - sum(L - 1);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int q;
  cin >> n >> q;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  memset(last, -1, sizeof(last));
  map<int, vector<pair<int, int>>> m1;
  map<pair<int, int>, int> ans;
  // sorting m1 by R( right end )
  for (int i = 1; i <= q; i++) {
    int L, R;
    cin >> L >> R;
    query[i] = make_pair(L, R);
    m1[R].push_back(make_pair(L, R));
  }

  int start = 1;
  for (auto &c : m1) {
    int right_end = c.first;
    for (int i = start; i <= right_end; i++) {
      if (last[a[i]] == -1) {
        last[a[i]] = i;
        update(i, 1);
      } else {
        update(last[a[i]], -1);
        last[a[i]] = i;
        update(last[a[i]], 1);
      }
    }
    start = right_end + 1;

    for (auto &p : m1[right_end]) {
      int L = p.first;
      int R = p.second;
      ans[make_pair(L, R)] = range_sum(L, R);
    }
  }

  for (int i = 1; i <= q; i++)
    cout << ans[query[i]] << "\\n";
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <iomanip>
#include <numeric>
#define ll long long
#define intm INT_MAX
#define llm LLONG_MAX

using namespace std;
typedef pair<int, int> P;
const int MOD = 998244353;
const ll mod = 1e9 + 7;
const int mxN = 1e6 + 9;

struct segtree {
  int sizee;
  vector<set<int>> s;

  void init(int n) {
    sizee = 1;
    while (sizee < n) {
      sizee *= 2;
    }
    // s.assign( 2*sizee, set<int>() );
    for (int i = 0; i < 2 * sizee; i++) {
      s.push_back(set<int>());
    }
  }

  void set1(int i, int v) { set1(i, v, 0, 0, sizee - 1); }

  void set1(int i, int v, int x, int lx, int rx) {
    if (rx - lx == 0) {
      s[x].insert(v);
      return;
    }
    int mid = lx + (rx - lx) / 2;
    if (i <= mid)
      set1(i, v, 2 * x + 1, lx, mid);
    else {
      set1(i, v, 2 * x + 2, mid + 1, rx);
    }
    s[x] = s[2 * x + 1];
    for (auto &c : s[2 * x + 2])
      s[x].insert(c);
  }

  set<int> query(int L, int R) { return query(L, R, 0, 0, sizee - 1); }

  set<int> query(int L, int R, int x, int lx, int rx) {
    // cout << L << " " << R << " " << x << " " << lx << " " << rx << "\\n";
    // completely outside
    if (lx > R || rx < L)
      return set<int>();
    // completely inside
    if (lx >= L && rx <= R)
      return s[x];
    // otherwise
    int mid = lx + (rx - lx) / 2;
    set<int> s1 = query(L, R, 2 * x + 1, lx, mid);
    set<int> s2 = query(L, R, 2 * x + 2, mid + 1, rx);
    for (auto &c : s2)
      s1.insert(c);
    return s1;
  }
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int N, Q;
  cin >> N >> Q;
  vector<int> arr(N, 0);
  for (int i = 0; i < N; i++) {
    cin >> arr[i];
  }
  segtree st;
  st.init(N);
  for (int i = 0; i < N; i++) {
    st.set1(i, arr[i]);
  }

  while (Q--) {
    int L, R;
    cin >> L >> R;
    cout << st.query(L - 1, R - 1).size() << "\\n";
  }
}