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

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

template <class Abel> struct BIT {
  vector<Abel> dat[2];
  Abel UNITY_SUM = 0; // to be set

  /* [1, n] */
  BIT(int n) { init(n); }
  void init(int n) {
    for (int iter = 0; iter < 2; ++iter)
      dat[iter].assign(n + 1, UNITY_SUM);
  }

  /* a, b are 1-indexed, [a, b) */
  inline void sub_add(int p, int a, Abel x) {
    for (int i = a; i < (int)dat[p].size(); i += i & -i)
      dat[p][i] = dat[p][i] + x;
  }
  inline void add(int a, int b, Abel x) {
    sub_add(0, a, x * -(a - 1));
    sub_add(1, a, x);
    sub_add(0, b, x * (b - 1));
    sub_add(1, b, x * (-1));
  }

  /* a is 1-indexed, [a, b) */
  inline Abel sub_sum(int p, int a) {
    Abel res = UNITY_SUM;
    for (int i = a; i > 0; i -= i & -i)
      res = res + dat[p][i];
    return res;
  }
  inline Abel sum(int a, int b) {
    return sub_sum(0, b - 1) + sub_sum(1, b - 1) * (b - 1) - sub_sum(0, a - 1) -
           sub_sum(1, a - 1) * (a - 1);
  }

  /* debug */
  void print() {
    for (int i = 1; i < (int)dat[0].size(); ++i)
      cout << sum(i, i + 1) << ",";
    cout << endl;
  }
};

int main() {
  int N;
  int Q;
  cin >> N >> Q;
  vector<int> a(N);
  for (int i = 0; i < N; ++i)
    cin >> a[i];

  vector<int> lefts(Q), rights(Q), ids(Q);
  for (int i = 0; i < Q; ++i) {
    cin >> lefts[i] >> rights[i];
    --lefts[i];
    ids[i] = i;
  }
  sort(ids.begin(), ids.end(),
       [&](int i, int j) { return rights[i] < rights[j]; });

  BIT<int> bit(N + 5);
  vector<int> prev(1100000, -1);
  vector<int> res(Q, 0);
  int r = 0;
  for (auto i : ids) {
    for (; r < rights[i]; ++r) {
      bit.add(prev[a[r]] + 2, r + 2, 1);
      prev[a[r]] = r;
    }
    int tmp = bit.sum(lefts[i] + 1, lefts[i] + 2);
    res[i] = max(res[i], tmp);
  }
  for (int i = 0; i < Q; ++i)
    cout << res[i] << endl;
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"

#define MOD 1000000007
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; i--)
#define ALL(v) v.begin(), v.end()
#define rALL(v) v.rbegin(), v.rend()
#define FOR(i, j, k) for (ll i = j; i < k; i++)
#define debug_print(var) cerr << #var << "=" << var << endl;
#define DUMP(i, v)                                                             \\
  for (ll i = 0; i < v.size(); i++)                                            \\
  cerr << v[i] << " "

using namespace std;
typedef long long int ll;
typedef vector<ll> llvec;
typedef vector<double> dvec;
typedef pair<ll, ll> P;
typedef long double ld;
struct edge {
  ll x, c;
};

ll mod(ll a, ll mod) {
  ll res = a % mod;
  if (res < 0)
    res = res + mod;
  return res;
}

ll modpow(ll a, ll n, ll mod) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

ll modinv(ll a, ll mod) { return modpow(a, mod - 2, mod); }

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

bool is_prime(ll n) {
  ll i = 2;
  if (n == 1)
    return false;
  if (n == 2)
    return true;
  bool res = true;
  while (i * i < n) {
    if (n % i == 0) {
      res = false;
    }
    i = i + 1;
  }

  // if(i==1)res = false;
  if (n % i == 0)
    res = false;
  return res;
}

struct UnionFind {
  ll N;
  llvec p;
  llvec cnt;
  UnionFind(ll n) {
    N = n;
    p = llvec(N);
    cnt = llvec(N, 0);
    rep(i, N) {
      p[i] = i;
      cnt[i] = 1;
    }
  }

  void con(ll a, ll b) {
    P at = root(a);
    P bt = root(b);
    if (at.second != bt.second) {
      if (at.first > bt.first) {
        swap(at, bt);
        swap(a, b);
      }
      p[at.second] = bt.second;
      cnt[bt.second] += cnt[at.second];
      p[a] = bt.second;
    }
  }

  P root(ll a) {
    ll atmp = a;
    ll c = 0;
    while (atmp != p[atmp]) {
      atmp = p[atmp];
      c++;
    }
    p[a] = atmp;
    return {c, atmp};
  }

  bool is_con(ll a, ll b) {
    P at = root(a);
    P bt = root(b);
    return at.second == bt.second;
  }
};

struct dijkstra {
  ll N;
  llvec d;
  vector<vector<edge>> e;
  dijkstra(ll n) {
    N = n;
    // d = llvec(N, 1e18);
    e = vector<vector<edge>>(N);
  }

  void add_edge(ll from, ll to, ll cost) { e[from].push_back({to, cost}); }

  void run(ll start) {
    priority_queue<P, vector<P>, greater<P>> que;
    que.push({0, start});
    d = llvec(N, 1e18);
    d[start] = 0;
    while (!que.empty()) {
      P q = que.top();
      que.pop();
      ll dc = q.first;
      ll x = q.second;
      if (dc > d[x]) {
        continue;
      } else {
        for (auto ip : e[x]) {
          if (d[ip.x] <= d[x] + ip.c) {
            continue;
          } else {
            d[ip.x] = d[x] + ip.c;
            que.push({d[ip.x], ip.x});
          }
        }
      }
    }
  }
};

/**************************************
** A main function starts from here  **
***************************************/
int main() {
  ll N, Q;
  cin >> N >> Q;
  llvec c(N);
  llvec d(N + 1, 0);
  vector<map<ll, ll>> m(N + 1);
  rep(i, N) {
    m[i + 1] = m[i];
    cin >> c[i];
    m[i + 1][c[i]]++;
  }
  rep(i, Q) {
    ll l, r;
    cin >> l >> r;
    ll cnt = 0;
    for (auto im : m[l - 1]) {
      if ((m[r][im.first] - im.second) == 0)
        cnt -= 1;
    }
    cout << m[r].size() + cnt << endl;
  }
  return 0;
}