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

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef double db;

#define dbg(x) cout << #x << " is " << x << endl

const int maxn = 5e5 + 9;

int ql[maxn], qr[maxn];
int ord[maxn];
int aaa[maxn];
int c[maxn];
int arr[maxn];
int n, q;

inline int cmp(int a, int b) {
  if (qr[a] != qr[b])
    return qr[a] < qr[b];
  return ql[a] < ql[b];
}

inline int lowbit(int x) { return (-x) & x; }

inline void add(int p, int a) {
  for (int i = p; i <= n; i += lowbit(i)) {
    arr[i] += a;
  }
}

inline int sum(int p) {
  int ans = 0;
  for (int i = p; i > 0; i -= lowbit(i)) {
    ans += arr[i];
  }
  return ans;
}

inline int read() {
  int x = 0, f = 1;
  char ch;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return f * x;
}

inline void init() {
  //    scanf("%d%d", &n, &q);
  n = read();
  q = read();
  for (int i = 1; i <= n; i++) {
    //        scanf("%d", c + i);
    c[i] = read();
  }
  for (int i = 0; i < q; i++) {
    ord[i] = i;
    ql[i] = read();
    qr[i] = read();
    //        scanf("%d%d", ql + i, qr + i);
  }
  sort(ord, ord + q, cmp);
}

int pre[maxn];

int main() {
  init();
  //    pt2();
  int nr = 0;
  memset(pre, -1, sizeof(pre));
  for (int i = 0; i < q; i++) {
    int t = ord[i];
    for (int j = nr + 1; j <= qr[t]; j++) {
      if (pre[c[j]] != -1) {
        add(pre[c[j]], -1);
      }
      pre[c[j]] = j;
      add(j, 1);
    }
    nr = qr[t];
    //        dbg(i);
    //        dbg(t);
    //        pt();
    int tmp = sum(qr[t]);
    if (ql[t] != 1) {
      tmp -= sum(ql[t] - 1);
    }
    aaa[t] = tmp;
  }
  for (int i = 0; i < q; i++) {
    printf("%d\\n", aaa[i]);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef double db;

#define dbg(x) cout << #x << " is " << x << endl

const int maxn = 5e5 + 9;

int ql[maxn], qr[maxn];
int ord[maxn];
int aaa[maxn];
int c[maxn];
int arr[maxn];
int n, q;

int cmp(int a, int b) {
  if (qr[a] != qr[b])
    return qr[a] < qr[b];
  return ql[a] < ql[b];
}

int lowbit(int x) { return (-x) & x; }

void add(int p, int a) {
  for (int i = p; i <= n; i += lowbit(i)) {
    arr[i] += a;
  }
}

int sum(int p) {
  int ans = 0;
  for (int i = p; i > 0; i -= lowbit(i)) {
    ans += arr[i];
  }
  return ans;
}

int read() {
  int x = 0, f = 1;
  char ch;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return f * x;
}

void init() {
  //    scanf("%d%d", &n, &q);
  n = read();
  q = read();
  for (int i = 1; i <= n; i++) {
    //        scanf("%d", c + i);
    c[i] = read();
  }
  for (int i = 0; i < q; i++) {
    ord[i] = i;
    ql[i] = read();
    qr[i] = read();
    //        scanf("%d%d", ql + i, qr + i);
  }
  sort(ord, ord + q, cmp);
}

int pre[maxn];

void pt() {
  for (int i = 1; i <= n; i++) {
    printf("%d ", arr[i]);
  }
  printf("\\n");
}

void pt2() {
  for (int i = 0; i < n; i++) {
    printf("%d ", ord[i]);
  }
  printf("\\n");
}

int main() {
  init();
  //    pt2();
  int nr = 0;
  memset(pre, -1, sizeof(pre));
  for (int i = 0; i < q; i++) {
    int t = ord[i];
    for (int j = nr + 1; j <= qr[t]; j++) {
      if (pre[c[j]] != -1) {
        add(pre[c[j]], -1);
      }
      pre[c[j]] = j;
      add(j, 1);
    }
    //        dbg(i);
    //        dbg(t);
    //        pt();
    int tmp = sum(qr[t]);
    if (ql[t] != 0) {
      tmp -= sum(ql[t] - 1);
    }
    aaa[t] = tmp;
  }
  for (int i = 0; i < q; i++) {
    printf("%d\\n", aaa[i]);
  }
  return 0;
}