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


#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()

using ll = long long;

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

template <typename T> class BinaryIndexedTree {
private:
  int S;
  std::vector<T> v;

public:
  BinaryIndexedTree<T>(int _S) {
    S = _S;
    v.resize(S + 1);
  }
  void update(int i, T k) {
    for (i++; i <= S; i += i & -i)
      v[i] = v[i] + k;
  }
  T read(int i) {
    T sum = 0;
    for (i++; i; i -= i & -i)
      sum = sum + v[i];
    return sum;
  }
  T read(int l, int r) { return read(r) - read(l - 1); }
};

int main(void) {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  int n, q;
  std::cin >> n >> q;
  std::vector<int> c(n);
  rep(i, n) std::cin >> c[i];

  std::vector<std::vector<int>> ps(n);
  std::unordered_map<int, int> idx;
  for (int i = n - 1; i >= 0; i--) {
    if (idx.count(c[i]) != 0) {
      ps[i].push_back(idx[c[i]]);
    }
    idx[c[i]] = i;
  }

  std::vector<std::vector<std::pair<int, int>>> Query(n);
  rep(i, q) {
    int l, r;
    std::cin >> l >> r;
    l--, r--;
    Query[l].emplace_back(r, i);
  }

  BinaryIndexedTree<int> bit(n + 1);
  std::vector<int> ans(q);
  for (int x = n - 1; x >= 0; x--) {
    for (auto &&p : ps[x])
      bit.update(p, 1);
    for (auto &&q : Query[x]) {
      int r = q.first;
      int i = q.second;
      ans[i] = r - x + 1 - bit.read(x, r);
    }
  }
  for (auto &&a : ans)
    std::cout << a << std::endl;

  return 0;
}


Error: TLE

Fixed submission:


#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()

using ll = long long;

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

template <typename T> class BinaryIndexedTree {
private:
  int S;
  std::vector<T> v;

public:
  BinaryIndexedTree<T>(int _S) {
    S = _S;
    v.resize(S + 1);
  }
  void update(int i, T k) {
    for (i++; i <= S; i += i & -i)
      v[i] = v[i] + k;
  }
  T read(int i) {
    T sum = 0;
    for (i++; i; i -= i & -i)
      sum = sum + v[i];
    return sum;
  }
  T read(int l, int r) { return read(r) - read(l - 1); }
};

int main(void) {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  int n, q;
  std::cin >> n >> q;
  std::vector<int> c(n);
  rep(i, n) std::cin >> c[i];

  std::vector<int> x, y;
  x.reserve(n);
  y.reserve(n);
  std::unordered_map<int, int> idx;
  for (int i = n - 1; i >= 0; i--) {
    if (idx.count(c[i]) != 0) {
      x.push_back(i);
      y.push_back(idx[c[i]]);
    }
    idx[c[i]] = i;
  }

  std::vector<std::pair<int, int>> Query(q);
  rep(i, q) std::cin >> Query[i].first >> Query[i].second;
  rep(i, q) Query[i].first--, Query[i].second--;
  std::vector<int> indx(q);
  rep(i, q) indx[i] = i;
  std::sort(all(indx), [Query](int &a, int &b) {
    return Query[a].first > Query[b].first;
  });

  BinaryIndexedTree<int> bit(n + 1);
  int left = 0;
  std::vector<int> ans(q);
  rep(i, q) {
    int l = Query[indx[i]].first;
    int r = Query[indx[i]].second;
    for (; left < sz(x); left++) {
      if (x[left] >= l)
        bit.update(y[left], 1);
      else
        break;
    }
    ans[indx[i]] = r - l + 1 - bit.read(l, r);
  }
  for (auto &&a : ans)
    std::cout << a << std::endl;

  return 0;
}