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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> VI;
typedef vector<vector<int>> VVI;
typedef vector<ll> VLL;
typedef vector<vector<ll>> VVLL;
typedef pair<ll, ll> Pair;
template <class T> T input() {
  T t;
  cin >> t;
  return t;
}
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define REP(i, b) FOR(i, 0, b)
#define RFOR(i, a, b) for (ll i = a - 1; i >= b; i--)
#define RREP(i, a) RFOR(i, a, 0)
#define REPALL(i, x) for (ll i = 0; i < x.size(); i++)
#define RREPALL(i, x) for (ll i = x.size() - 1; i >= 0; i--)
#define REPITR(itr, x) for (auto itr = (x).begin(); itr != (x).end(); itr++)
#define ALL(x) (x).begin(), (x).end()
#define SORT(x) sort(ALL(x))
#define MIN_ELEMENT(x) min_element(ALL(x))
#define MAX_ELEMENT(x) max_element(ALL(x))
#define COUNT(x, num) count(ALL(x), num)
#define MEMSET(x, val) memset(x, val, sizeof(x))
#define CHMAX(a, b) a = max(a, b)
#define CHMIN(a, b) a = min(a, b)
#define FIND(x, key) x.find(key) != x.end()
#define IN_RANGE_V2(v, k, x, y)                                                \\
  (0 <= x + dx[k] && x + dx[k] < v[y].size() && 0 <= y + dy[k] &&              \\
   y + dy[k] < v.size())
#define debug(x) cerr << __LINE__ << ": " << (#x) << " = " << (x) << endl;
void YES(bool flag) { cout << (flag ? "YES" : "NO") << endl; }
void Yes(bool flag) { cout << (flag ? "Yes" : "No") << endl; }
void yes(bool flag) { cout << (flag ? "yes" : "no") << endl; }
#define PRINT_ARRAY_H(a)                                                       \\
  REPALL(__i, a) { cout << a[__i] << (__i != a.size() - 1 ? " " : ""); }       \\
  newline;
#define PRINT_ARRAY_V(a)                                                       \\
  REPALL(__i, a) { cout << a[__i] << endl; }
#define e1 first
#define e2 second
#define newline putchar('\\n')
#define cspace ' '
#define sspace " "
const int INF = 1e7;
const ll MOD = 1e9 + 7;
const double pi = 3.141592653589793;
const VI dx = {1, 0, -1, 0};
const VI dy = {0, 1, 0, -1};
// const VI dx = {1, 1, 0, -1, -1, -1, 0, 1};
// const VI dy = {0, 1, 1, 1, 0, -1, -1, -1};

// 蟻本参考
// 整数配列の和に対応
class BIT {
public:
  VLL bit;
  int num;

  BIT(int n) {
    num = n;
    bit.resize(n + 1);
  }

  ll sum(int i) {
    ll r = 0ll;
    while (i > 0) {
      r += bit[i];
      i -= i & -i;
    }
    return r;
  }

  void add(int i, ll x) {
    while (i <= num) {
      bit[i] += x;
      i += i & -i;
    }
  }

  void sub(int i, ll x) { add(i, -x); }
};

void solve() {
  ll n, q;
  VLL c, ans, la; // last appear
  string s, t;
  cin >> n >> q;
  la.resize(n + 1, -1);
  REP(i, n) {
    ll tc = input<ll>();
    c.push_back(tc);
  }
  ans.resize(q);
  vector<tuple<int, int, int>> queries;
  REP(i, q) {
    int l, r;
    cin >> l >> r;
    // l--; r--;
    queries.emplace_back(r, l, i);
  }
  SORT(queries);
  BIT bit(n);
  int cp = 0; // current position
  REP(i, q) {
    int l = get<1>(queries[i]);
    int r = get<0>(queries[i]);
    FOR(j, cp, r) {
      if (la[c[j]] == -1) {
        bit.add(j + 1, 1);
      } else {
        bit.sub(la[c[j]], 1);
        bit.add(j + 1, 1);
      }
      la[c[j]] = j + 1;
    }
    cp = r;
    ll tans = bit.sum(r);
    if (l > 0)
      tans -= bit.sum(l - 1);

    ans[get<2>(queries[i])] = tans;
  }

  PRINT_ARRAY_V(ans);
}

int main() {
  cout << fixed << setprecision(20);
  int __t = 1;
  // cin>>__t;
  REP(i, __t) { solve(); }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> VI;
typedef vector<vector<int>> VVI;
typedef vector<ll> VLL;
typedef vector<vector<ll>> VVLL;
typedef pair<ll, ll> Pair;
template <class T> T input() {
  T t;
  cin >> t;
  return t;
}
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define REP(i, b) FOR(i, 0, b)
#define RFOR(i, a, b) for (ll i = a - 1; i >= b; i--)
#define RREP(i, a) RFOR(i, a, 0)
#define REPALL(i, x) for (ll i = 0; i < x.size(); i++)
#define RREPALL(i, x) for (ll i = x.size() - 1; i >= 0; i--)
#define REPITR(itr, x) for (auto itr = (x).begin(); itr != (x).end(); itr++)
#define ALL(x) (x).begin(), (x).end()
#define SORT(x) sort(ALL(x))
#define MIN_ELEMENT(x) min_element(ALL(x))
#define MAX_ELEMENT(x) max_element(ALL(x))
#define COUNT(x, num) count(ALL(x), num)
#define MEMSET(x, val) memset(x, val, sizeof(x))
#define CHMAX(a, b) a = max(a, b)
#define CHMIN(a, b) a = min(a, b)
#define FIND(x, key) x.find(key) != x.end()
#define IN_RANGE_V2(v, k, x, y)                                                \\
  (0 <= x + dx[k] && x + dx[k] < v[y].size() && 0 <= y + dy[k] &&              \\
   y + dy[k] < v.size())
#define debug(x) cerr << __LINE__ << ": " << (#x) << " = " << (x) << endl;
void YES(bool flag) { cout << (flag ? "YES" : "NO") << endl; }
void Yes(bool flag) { cout << (flag ? "Yes" : "No") << endl; }
void yes(bool flag) { cout << (flag ? "yes" : "no") << endl; }
#define PRINT_ARRAY_H(a)                                                       \\
  REPALL(__i, a) { cout << a[__i] << (__i != a.size() - 1 ? " " : ""); }       \\
  newline;
#define PRINT_ARRAY_V(a)                                                       \\
  REPALL(__i, a) { cout << a[__i] << endl; }
#define e1 first
#define e2 second
#define newline putchar('\\n')
#define cspace ' '
#define sspace " "
const int INF = 1e7;
const ll MOD = 1e9 + 7;
const double pi = 3.141592653589793;
const VI dx = {1, 0, -1, 0};
const VI dy = {0, 1, 0, -1};
// const VI dx = {1, 1, 0, -1, -1, -1, 0, 1};
// const VI dy = {0, 1, 1, 1, 0, -1, -1, -1};

void solve() {
  ll a, b, c, d, n, m, l, r, k, x, y, z, ans = 0ll, Q;
  string s, t;
  cin >> n >> Q;
  vector<map<int, int>> vmp(1);
  REP(i, n) {
    cin >> c;
    vmp.push_back(vmp[i]);
    vmp[i + 1][c]++;
  }
  REP(q, Q) {
    ans = 0;
    cin >> l >> r;
    REPITR(itr, vmp[r]) {
      if (FIND(vmp[l - 1], itr->e1)) {
        ans += (vmp[r][itr->e1] > vmp[l - 1][itr->e1]);
      } else {
        ans++;
      }
    }
    cout << ans << endl;
  }
}

int main() {
  cout << fixed << setprecision(20);
  int __t = 1;
  // cin>>__t;
  REP(i, __t) { solve(); }
  return 0;
}