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

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;

#define sz(a) int((a).size())
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define ini(a, i) memset(a, i, sizeof(a))
#define inin(a, n, i) memset(a, i, sizeof(a[0]) * (n))
#define contains(c, i) ((c).find(i) != (c).end())
#define present(i, c) (find(all(c), i) != (c).end())

#define trav(x, c) for (auto &x : c)
#define rep(i, n) for (int i = 0; i < n; i++)
#define repa(i, b, e)                                                          \\
  for (int i = (b) - ((b) > (e)); i != (e) - ((b) > (e));                      \\
       i += 1 - 2 * ((b) > (e)))

template <class T> bool chkmax(T &x, T y) {
  return x < y ? x = y, true : false;
}
template <class T> bool chkmin(T &x, T y) {
  return x > y ? x = y, true : false;
}
template <class A, class B> A cvt(B x) {
  stringstream s;
  s << x;
  A r;
  s >> r;
  return r;
}

void read() {}
void print() {}
template <class T, class... Args> void read(T &a, Args &...args) {
  cin >> a;
  read(args...);
}
template <class T, class... Args> void print(T a, Args... args) {
  cout << a << ' ';
  print(args...);
}
template <class... Args> void println(Args... args) {
  print(args...);
  cout << endl;
}

#define debug(args...)                                                         \\
  {                                                                            \\
    string s_(#args);                                                          \\
    replace(all(s_), ',', ' ');                                                \\
    stringstream ss_(s_);                                                      \\
    istream_iterator<string> it_(ss_);                                         \\
    cerr_(it_, args);                                                          \\
  }
void cerr_(istream_iterator<string> it) { (void)it; }
template <class T, class... Args>
void cerr_(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << endl;
  cerr_(++it, args...);
}

template <class T, class S>
ostream &operator<<(ostream &os, const pair<T, S> &p) {
  os << "(" << p.first << ", " << p.second << ")";
  return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << '{';
  string sep;
  for (const auto &x : v)
    os << sep << x, sep = ", ";
  return os << '}';
}
template <class T> ostream &operator<<(ostream &os, const set<T> &s) {
  os << '{';
  string sep;
  for (const auto &x : s)
    os << sep << x, sep = ", ";
  return os << '}';
}
template <class T, class S>
ostream &operator<<(ostream &os, const map<T, S> &m) {
  os << '{';
  string sep;
  for (const auto &x : m)
    os << sep << x, sep = ", ";
  return os << '}';
}
template <class T, size_t size>
ostream &operator<<(ostream &os, const array<T, size> &arr) {
  os << '{';
  string sep;
  for (const auto &x : arr)
    os << sep << x, sep = ", ";
  return os << '}';
}

//=========================

const int MAXN = (int)5e5 + 1;

// Binary Indexed Tree (1-based indexation)
int BIT[MAXN + 1];

// update/set an element 'x' at index 'i'
void update(int i, int x) {
  while (i <= MAXN)
    BIT[i] += x, i += (i & -i);
}

// sum of all elements in the range [1, i]
int sum(int i) {
  int sum = 0;
  while (i > 0)
    sum += BIT[i], i -= (i & -i);
  return sum;
}

// sum of all elements in the range [l, r]
int sum(int l, int r) { return sum(r) - sum(l - 1); }

void run_test() {
  int n, q;
  read(n, q);
  vi c(n + 1);
  repa(i, 1, n + 1) read(c[i]);
  vector<vector<pii>> qrys(n + 1);
  rep(i, q) {
    int l, r;
    read(l, r);
    qrys[l].eb(r, i);
  }
  ini(BIT, 0);
  vi last_seen(n + 1, -1);
  vi ans(q);
  repa(i, n + 1, 1) {
    if (last_seen[c[i]] != -1)
      update(last_seen[c[i]], -1);
    last_seen[c[i]] = i;
    update(i, 1);
    for (auto [r, iq] : qrys[i])
      ans[iq] = sum(i, r);
  }
  rep(i, q) println(ans[i]);
}

//=========================

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  // int t_ = 1, t__; cin >> t__; while(t_ <= t__) { cout << "Case #" << t_++ <<
  // ": "; run_test(); }
  //  int t_; cin >> t_; while(t_--) run_test();
  run_test();
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;

#define sz(a) int((a).size())
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define ini(a, i) memset(a, i, sizeof(a))
#define inin(a, n, i) memset(a, i, sizeof(a[0]) * (n))
#define contains(c, i) ((c).find(i) != (c).end())
#define present(i, c) (find(all(c), i) != (c).end())

#define trav(x, c) for (auto &x : c)
#define rep(i, n) for (int i = 0; i < n; i++)
#define repa(i, b, e)                                                          \\
  for (int i = (b) - ((b) > (e)); i != (e) - ((b) > (e));                      \\
       i += 1 - 2 * ((b) > (e)))

template <class T> bool chkmax(T &x, T y) {
  return x < y ? x = y, true : false;
}
template <class T> bool chkmin(T &x, T y) {
  return x > y ? x = y, true : false;
}
template <class A, class B> A cvt(B x) {
  stringstream s;
  s << x;
  A r;
  s >> r;
  return r;
}

void read() {}
void print() {}
template <class T, class... Args> void read(T &a, Args &...args) {
  cin >> a;
  read(args...);
}
template <class T, class... Args> void print(T a, Args... args) {
  cout << a << ' ';
  print(args...);
}
template <class... Args> void println(Args... args) {
  print(args...);
  cout << endl;
}

#define debug(args...)                                                         \\
  {                                                                            \\
    string s_(#args);                                                          \\
    replace(all(s_), ',', ' ');                                                \\
    stringstream ss_(s_);                                                      \\
    istream_iterator<string> it_(ss_);                                         \\
    cerr_(it_, args);                                                          \\
  }
void cerr_(istream_iterator<string> it) { (void)it; }
template <class T, class... Args>
void cerr_(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << endl;
  cerr_(++it, args...);
}

template <class T, class S>
ostream &operator<<(ostream &os, const pair<T, S> &p) {
  os << "(" << p.first << ", " << p.second << ")";
  return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << '{';
  string sep;
  for (const auto &x : v)
    os << sep << x, sep = ", ";
  return os << '}';
}
template <class T> ostream &operator<<(ostream &os, const set<T> &s) {
  os << '{';
  string sep;
  for (const auto &x : s)
    os << sep << x, sep = ", ";
  return os << '}';
}
template <class T, class S>
ostream &operator<<(ostream &os, const map<T, S> &m) {
  os << '{';
  string sep;
  for (const auto &x : m)
    os << sep << x, sep = ", ";
  return os << '}';
}
template <class T, size_t size>
ostream &operator<<(ostream &os, const array<T, size> &arr) {
  os << '{';
  string sep;
  for (const auto &x : arr)
    os << sep << x, sep = ", ";
  return os << '}';
}

//=========================

typedef unordered_set<int> item;

struct segtree {
  int size;
  vector<item> values;

  //---
  const item NEUTRAL_ELEMENT = item();

  item single(int v) {
    item it;
    it.insert(v);
    return it;
  }

  item merge(item &m1, item &m2) {
    item it;
    trav(e, m1) it.insert(e);
    trav(e, m2) it.insert(e);
    return it;
  }
  //---

  void init(int n) {
    size = 1;
    while (size < n)
      size *= 2;             // set the size to the closest power of two
    values.resize(2 * size); // a full binary tree has #nodes == 2 * #leafs - 1
  }

  void build(vector<int> &a, int x, int lx, int rx) {
    if (rx - lx == 1) {
      if (lx < (int)a.size())
        values[x] = single(a[lx]); // since the array size may be greater than
                                   // the input by construction
      return;
    }
    int mid = lx + (rx - lx) / 2;
    build(a, 2 * x + 1, lx, mid);
    build(a, 2 * x + 2, mid, rx);
    values[x] = merge(values[2 * x + 1], values[2 * x + 2]);
  }

  void build(vector<int> &a) {
    init(a.size());
    build(a, 0, 0, size);
  }

  void set(int i, int v, int x, int lx, int rx) {
    if (rx - lx == 1) {
      values[x] = single(v);
      return;
    }
    int mid = lx + (rx - lx) / 2;
    if (i < mid)
      set(i, v, 2 * x + 1, lx, mid);
    else
      set(i, v, 2 * x + 2, mid, rx);
    values[x] = merge(values[2 * x + 1], values[2 * x + 2]);
  }

  void set(int i, int v) { set(i, v, 0, 0, size); }

  item query(int l, int r, int x, int lx, int rx) {
    if (rx <= l || lx >= r)
      return NEUTRAL_ELEMENT;
    if (lx >= l && rx <= r)
      return values[x];
    int mid = lx + (rx - lx) / 2;
    item left = query(l, r, 2 * x + 1, lx, mid);
    item right = query(l, r, 2 * x + 2, mid, rx);
    return merge(left, right);
  }

  item query(int l, int r) { return query(l, r, 0, 0, size); }
};

void run_test() {
  int n, q;
  read(n, q);
  vi c(n);
  rep(i, n) read(c[i]);
  segtree st;
  st.build(c);
  rep(i, q) {
    int l, r;
    read(l, r);
    println(st.query(l - 1, r).size());
  }
}

//=========================

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  // int t_ = 1, t__; cin >> t__; while(t_ <= t__) { cout << "Case #" << t_++ <<
  // ": "; run_test(); }
  //  int t_; cin >> t_; while(t_--) run_test();
  run_test();
  return 0;
}