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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define sz 500005
#define all(x) x.begin(), x.end()
#define l_b lower_bound
#define u_b upper_bound
#define pb push_back
#define MOD 1000000007
void fastio(void);

int arr[sz], ans[sz], BIT[sz], li[sz];
vector<pair<pair<int, int>, int>> Q;

bool cmp(pair<pair<int, int>, int> &a, pair<pair<int, int>, int> &b) {
  return a.first.second < b.first.second;
}

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

int query(int idx) {
  int ans = 0;
  while (idx > 0) {
    ans += BIT[idx];
    idx -= (idx & (-idx));
  }
  return ans;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  int n, q, la, l, r;
  scanf("%d%d", &n, &q);

  for (la = 1; la <= n; la++)
    scanf("%d", &arr[la]);

  for (la = 0; la < q; la++) {
    scanf("%d%d", &l, &r);
    Q.pb({{l, r}, la});
  }

  sort(all(Q), cmp);

  int cur = 0, curr = 0;

  for (la = 0; la < q; la++) {
    l = Q[la].first.first, r = Q[la].first.second;

    for (curr; curr < r; curr++) {
      if (li[arr[curr + 1]] != 0)
        update(li[arr[curr + 1]], -1, n);
      update(curr + 1, 1, n);
      li[arr[curr + 1]] = curr + 1;
    }

    cur = query(r) - query(l - 1);

    ans[Q[la].second + 1] = cur;
  }

  for (la = 1; la <= q; la++)
    printf("%d\\n", ans[la]);

  return 0;
}

void fastio(void) {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define sz 500005
#define all(x) x.begin(), x.end()
#define l_b lower_bound
#define u_b upper_bound
#define pb push_back
#define MOD 1000000007
void fastio(void);

int arr[sz], BLOCK = 700, cnt[sz], ans[sz];
vector<pair<pair<int, int>, int>> Q;

bool cmp(pair<pair<int, int>, int> &a, pair<pair<int, int>, int> &b) {
  if (a.second / BLOCK == b.second / BLOCK) {
    return a.first.second < b.first.second;
  }
  return a.second / BLOCK < b.second / BLOCK;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  int n, q, la, l, r;
  scanf("%d%d", &n, &q);

  for (la = 1; la <= n; la++)
    scanf("%d", &arr[la]);

  for (la = 0; la < q; la++) {
    scanf("%d%d", &l, &r);
    Q.pb({{l, r}, la});
  }

  sort(all(Q), cmp);
  int cur = 1, curl = 1, curr = 1;
  cnt[arr[1]]++;

  for (la = 0; la < q; la++) {
    l = Q[la].first.first, r = Q[la].first.second;

    if (curr < r) {
      while (curr < r) {
        curr++;
        cnt[arr[curr]]++;
        if (cnt[arr[curr]] == 1)
          cur++;
      }
    }

    if (curr > r) {
      while (curr > r) {
        cnt[arr[curr]]--;
        if (cnt[arr[curr]] == 0)
          cur--;
        curr--;
      }
    }

    if (curl < l) {
      while (curl < l) {
        cnt[arr[curl]]--;
        if (cnt[arr[curl]] == 0)
          cur--;
        curl++;
      }
    }

    if (curl > l) {
      while (curl > l) {
        curl--;
        cnt[arr[curl]]++;
        if (cnt[arr[curl]] == 1)
          cur++;
      }
    }

    ans[Q[la].second + 1] = cur;
  }

  for (la = 1; la <= q; la++)
    printf("%d\\n", ans[la]);

  return 0;
}

void fastio(void) {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}