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

// ######      ###      #######    #######    ##     #     #####        ###
// ##### // #     #    #   #          #        #       # #    #    #     # #   #
// ###  // #     #   #     #        #         #       #  #   #   #       #    #
// #    ###  //
// ######   #########      #          #       #   #  #   #           #########
// #   // #     #  #       #     #           #       #    # #   #    ####   # #
// #   // #     #  #       #    #            #       #     ##   #    #  #   # #
// //
// ######   #       #   #######    #######    #      #    #####  #   #       #
// #   //

// #pragma GCC optimize(2)
// #pragma GCC optimize(3)
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
// #pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double lld;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> ppl;
#define ain(a, n)                                                              \\
  for (ll i = 0; i < (n); ++i)                                                 \\
    cin >> (a)[i];
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define loop(i, n) for (ll i = 0; i < (n); ++i)
#define FOR(i, a, b) for (ll i = (a); i <= (b); ++i)
#define FORD(i, a, b) for (ll i = (a); i >= (b); --i)
#define cases                                                                  \\
  ll T = 0;                                                                    \\
  cin >> T;                                                                    \\
  while (T--)
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define END "\\n"
#define pb push_back
#define mp make_pair
#define go(c, itr) for (auto itr = (c).begin(); itr != (c).end(); ++itr)
// #define back(c,itr) for(auto itr=(c).rbegin(); itr!=(c).rend(); ++itr)
#define PI 3.14159265359
#define inf 9e18
#define MOD 1000000007
#define MODU 998244353
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define MAXN 500005
#define BLOCK 555
const string alpha = "abcdefghijklmnopqrstuvwxyz";
const ll N = 200005;
const long double epsilon = 1e-9;
ll binexp(ll a, ll b, ll m) {
  a %= m;
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a % m;
    a = a * a % m;
    b >>= 1;
  }
  return res;
}
ll modinvfermat(ll a, ll m) { return binexp(a, m - 2, m); }
void task(bool flag) {
  if (flag)
    cout << "YES\\n";
  else
    cout << "NO\\n";
}
ll lcm(ll a, ll b) { return ((1LL * a * b) / (__gcd(a, b))); }

struct Query {
  int l, r, idx;
};

bool cmp(Query x, Query y) { return x.r < y.r; }

void update(int idx, int val, int bit[], int n) {
  for (; idx <= n; idx += idx & -idx)
    bit[idx] += val;
}

int query(int idx, int bit[], int n) {
  int sum = 0;
  for (; idx > 0; idx -= idx & -idx)
    sum += bit[idx];
  return sum;
}

void answeringQueries(vl arr, int n, vector<Query> queries, int q) {
  int bit[n + 1];
  memset(bit, 0, sizeof(bit));

  int last_visit[MAXN];
  memset(last_visit, -1, sizeof(last_visit));

  int ans[q];
  int query_counter = 0;
  for (int i = 0; i < n; i++) {
    if (last_visit[arr[i]] != -1)
      update(last_visit[arr[i]] + 1, -1, bit, n);

    last_visit[arr[i]] = i;
    update(i + 1, 1, bit, n);

    while (query_counter < q && queries[query_counter].r == i) {
      ans[queries[query_counter].idx] =
          query(queries[query_counter].r + 1, bit, n) -
          query(queries[query_counter].l, bit, n);
      query_counter++;
    }
  }

  for (int i = 0; i < q; i++)
    cout << ans[i] << endl;
}

signed main() {
  // #ifndef ONLINE_JUDGE
  // 	freopen("input.txt", "r", stdin);
  // 	freopen("output.txt", "w", stdout);
  // #endif
  fast ll n, q;
  cin >> n >> q;
  vl a(n);
  ain(a, n);
  vector<Query> queries(q);
  loop(i, q) {
    cin >> queries[i].l >> queries[i].r;
    --queries[i].l, --queries[i].r;
    queries[i].idx = i;
  }

  sort(queries.begin(), queries.begin() + q, cmp);
  answeringQueries(a, n, queries, q);

  // #ifndef ONLINE_JUDGE
  // 	cout << "\\nTime Elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << "
  // sec\\n"; #endif
  return 0;
}


Error: TLE

Fixed submission:

// ######      ###      #######    #######    ##     #     #####        ###
// ##### // #     #    #   #          #        #       # #    #    #     # #   #
// ###  // #     #   #     #        #         #       #  #   #   #       #    #
// #    ###  //
// ######   #########      #          #       #   #  #   #           #########
// #   // #     #  #       #     #           #       #    # #   #    ####   # #
// #   // #     #  #       #    #            #       #     ##   #    #  #   # #
// //
// ######   #       #   #######    #######    #      #    #####  #   #       #
// #   //

// #pragma GCC optimize(2)
// #pragma GCC optimize(3)
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
// #pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double lld;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> ppl;
#define ain(a, n)                                                              \\
  for (ll i = 0; i < (n); ++i)                                                 \\
    cin >> (a)[i];
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define loop(i, n) for (ll i = 0; i < (n); ++i)
#define FOR(i, a, b) for (ll i = (a); i <= (b); ++i)
#define FORD(i, a, b) for (ll i = (a); i >= (b); --i)
#define cases                                                                  \\
  ll T = 0;                                                                    \\
  cin >> T;                                                                    \\
  while (T--)
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define END "\\n"
#define pb push_back
#define mp make_pair
#define go(c, itr) for (auto itr = (c).begin(); itr != (c).end(); ++itr)
// #define back(c,itr) for(auto itr=(c).rbegin(); itr!=(c).rend(); ++itr)
#define PI 3.14159265359
#define inf 9e18
#define MOD 1000000007
#define MODU 998244353
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define MAXN 500005
#define BLOCK 555
const string alpha = "abcdefghijklmnopqrstuvwxyz";
const ll N = 200005;
const long double epsilon = 1e-9;
ll binexp(ll a, ll b, ll m) {
  a %= m;
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a % m;
    a = a * a % m;
    b >>= 1;
  }
  return res;
}
ll modinvfermat(ll a, ll m) { return binexp(a, m - 2, m); }
void task(bool flag) {
  if (flag)
    cout << "YES\\n";
  else
    cout << "NO\\n";
}
ll lcm(ll a, ll b) { return ((1LL * a * b) / (__gcd(a, b))); }

struct query {
  int l;
  int r;
  int i;
};
vector<query> Q(MAXN);
vl ar(MAXN), ans(MAXN), fre(MAXN);
ll cnt = 0;

bool comp(query a, query b) {
  if (a.l / BLOCK != b.l / BLOCK)
    return a.l / BLOCK < b.l / BLOCK;
  return a.r < b.r;
}

void add(int pos) {
  fre[ar[pos]]++;
  if (fre[ar[pos]] == 1)
    cnt++;
}

void remove(int pos) {
  fre[ar[pos]]--;
  if (fre[ar[pos]] == 0)
    cnt--;
}

signed main() {
  // #ifndef ONLINE_JUDGE
  // 	freopen("input.txt", "r", stdin);
  // 	freopen("output.txt", "w", stdout);
  // #endif
  fast ll n, q;
  cin >> n >> q;
  ain(ar, n);
  loop(i, q) {
    cin >> Q[i].l >> Q[i].r;
    Q[i].i = i, Q[i].l--, Q[i].r--;
  }

  sort(Q.begin(), Q.begin() + q, comp);
  ll ML = 0, MR = -1;
  loop(i, q) {
    int L = Q[i].l;
    int R = Q[i].r;

    while (ML > L)
      ML--, add(ML);

    while (MR < R)
      MR++, add(MR);

    while (ML < L)
      remove(ML), ML++;

    while (MR > R)
      remove(MR), MR--;

    ans[Q[i].i] = cnt;
  }

  loop(i, q) cout << ans[i] << END;

  // #ifndef ONLINE_JUDGE
  // 	cout << "\\nTime Elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << "
  // sec\\n"; #endif
  return 0;
}