Problem p02599 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;

#define ll long long
#define MAX 500000
#define MAX1 1148576
#define MAX2 100000
#define inf 1000000000
#define eps 1e-12
#define MOD 1000000007

int c[MAX + 5], tree[MAX1 + 5], last[MAX + 5];
struct node {
  int l, r, id;
} query[MAX + 5];
int ans[MAX + 5];

bool compare(node a, node b) { return a.r <= b.r; }

void update(int id, int l, int r, int pos, int val) {
  if (l == r) {
    tree[id] += val;
    return;
  }
  int lt = id << 1, rt = lt | 1, mid = (l + r) >> 1;
  if (pos <= mid) {
    update(lt, l, mid, pos, val);
  } else {
    update(rt, mid + 1, r, pos, val);
  }
  tree[id] = tree[lt] + tree[rt];
}

int get(int id, int l, int r, int rl, int rr) {
  if (l > r or r < rl or l > rr) {
    return 0;
  }
  if (l >= rl and r <= rr) {
    return tree[id];
  }
  int lt = id << 1, rt = lt | 1, mid = (l + r) >> 1;
  return get(lt, l, mid, rl, rr) + get(rt, mid + 1, r, rl, rr);
}

int main() {

  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int n, q, i, j, l, r;
  cin >> n >> q;
  for (i = 1; i <= n; ++i) {
    cin >> c[i];
  }
  for (i = 0; i < q; ++i) {
    cin >> l >> r;
    query[i] = {l, r, i};
  }
  sort(query, query + q, compare);
  for (j = 0, i = 1; i <= n; ++i) {
    if (last[c[i]]) {
      update(1, 1, n, last[c[i]], -1);
    }
    last[c[i]] = i;
    update(1, 1, n, last[c[i]], 1);
    while (j < q and query[j].r == i) {
      ans[query[j].id] = get(1, 1, n, query[j].l, query[j].r);
      ++j;
    }
  }
  for (i = 0; i < q; ++i) {
    cout << ans[i] << "\n";
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;

#define ll long long
#define MAX 500000
#define MAX1 1148576
#define MAX2 100000
#define inf 1000000000
#define eps 1e-12
#define MOD 1000000007

int c[MAX + 5], tree[MAX1 + 5], last[MAX + 5];
struct node {
  int l, r, id;
} query[MAX + 5];
int ans[MAX + 5];

bool compare(node a, node b) { return a.r < b.r; }

void update(int id, int l, int r, int pos, int val) {
  if (l == r) {
    tree[id] += val;
    return;
  }
  int lt = id << 1, rt = lt | 1, mid = (l + r) >> 1;
  if (pos <= mid) {
    update(lt, l, mid, pos, val);
  } else {
    update(rt, mid + 1, r, pos, val);
  }
  tree[id] = tree[lt] + tree[rt];
}

int get(int id, int l, int r, int rl, int rr) {
  if (l > r or r < rl or l > rr) {
    return 0;
  }
  if (l >= rl and r <= rr) {
    return tree[id];
  }
  int lt = id << 1, rt = lt | 1, mid = (l + r) >> 1;
  return get(lt, l, mid, rl, rr) + get(rt, mid + 1, r, rl, rr);
}

int main() {

  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int n, q, i, j, l, r;
  cin >> n >> q;
  for (i = 1; i <= n; ++i) {
    cin >> c[i];
  }
  for (i = 0; i < q; ++i) {
    cin >> l >> r;
    query[i] = {l, r, i};
  }
  sort(query, query + q, compare);
  for (j = 0, i = 1; i <= n; ++i) {
    if (last[c[i]]) {
      update(1, 1, n, last[c[i]], -1);
    }
    last[c[i]] = i;
    update(1, 1, n, last[c[i]], 1);
    while (j < q and query[j].r == i) {
      ans[query[j].id] = get(1, 1, n, query[j].l, query[j].r);
      ++j;
    }
  }
  for (i = 0; i < q; ++i) {
    cout << ans[i] << "\n";
  }

  return 0;
}