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

#define _USE_MATH_DEFINES
#include <algorithm>
#include <array>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
using ll = long long;
using ull = unsigned long long;
using namespace std;
#define int long long
#define CONTAINS_VEC(v, n) (find((v).begin(), (v).end(), (n)) != (v).end())
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
#define ARY_SORT(a, size) sort((a), (a) + (size))
#define REMOVE(v, a) (v.erase(remove((v).begin(), (v).end(), (a)), (v).end()))
#define REVERSE(v) (reverse((v).begin(), (v).end()))
#define ARY_REVERSE(v, a) (reverse((v), (v) + (a)))
#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define CONTAINS_MAP(m, a) (m).find((a)) != m.end()
#define CONTAINS_SET(m, a) (m).find((a)) != m.end()
void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; }
void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; }

//----------BIT 0-index----------
int BIT_N = 1000010;
int bit_tree[1000010];
//[index]にnを追加
void bit_add(int index, int n) {
  for (int x = index; x < BIT_N; x |= x + 1) {
    bit_tree[x] += n;
  }
}
//[0, index)の合計
int bit_sum(int index) {
  int ret = 0;
  for (int x = index - 1; x >= 0; x = (x & (x + 1)) - 1) {
    ret += bit_tree[x];
  }
  return ret;
}

struct St {
  int index;
  int l;
  int r;
  bool operator<(const St &st) const {
    return (r < st.r); //(昇順)
  }
};

St sts[1000000];
int N, Q;
int ary[1000000];
int C[1000000];
int ans[1000000];

int cur_r = 0;
void update_r(int r) {
  for (int i = cur_r + 1; i <= r; i++) {
    if (ary[C[i]] != 0) {
      int index = ary[C[i]];
      bit_add(index, -1);
    }
    ary[C[i]] = i;
    bit_add(i, 1);
  }
  cur_r = r;
}

signed main() {
  cin >> N >> Q;
  REP(i, N) cin >> C[i + 1];
  REP(i, Q) {
    cin >> sts[i].l >> sts[i].r;
    sts[i].index = i;
  }

  ARY_SORT(sts, Q);
  for (int i = 0; i < Q; i++) {
    update_r(sts[i].r);
    int a = bit_sum(sts[i].r + 1);
    int b = bit_sum(sts[i].l);
    ans[sts[i].index] = a - b;
  }

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


Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <array>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
using ll = long long;
using ull = unsigned long long;
using namespace std;
#define int long long
#define CONTAINS_VEC(v, n) (find((v).begin(), (v).end(), (n)) != (v).end())
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
#define ARY_SORT(a, size) sort((a), (a) + (size))
#define REMOVE(v, a) (v.erase(remove((v).begin(), (v).end(), (a)), (v).end()))
#define REVERSE(v) (reverse((v).begin(), (v).end()))
#define ARY_REVERSE(v, a) (reverse((v), (v) + (a)))
#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define CONTAINS_MAP(m, a) (m).find((a)) != m.end()
#define CONTAINS_SET(m, a) (m).find((a)) != m.end()
void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; }
void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; }

int N, Q;
int C[500001];

int cnt = 0;
int work[500001];
int ans[500001];

struct St {
  int index;
  int l;
  int r;
  int cnt;
  bool operator<(const St &st) const {
    return (val() < st.val()); //(昇順)
  }

  int val() const { return (r + l) * 1000 + (r - l); }
};

vector<St> sts;

signed main() {
  cin >> N >> Q;
  REP(i, N) cin >> C[i + 1];
  REP(i, Q) {
    St st;
    cin >> st.l >> st.r;
    st.index = i;
    sts.push_back(st);
  }
  SORT(sts);

  for (int i = sts[0].l; i <= sts[0].r; i++) {
    if (work[C[i]] == 0) {
      cnt++;
    }
    work[C[i]]++;
    ans[sts[0].index] = cnt;
  }

  int cur_l = sts[0].l;
  int cur_r = sts[0].r;

  for (int i = 1; i < sts.size(); i++) {
    St &st = sts[i];

    if (st.l < cur_l) {
      for (int i = st.l; i < cur_l; i++) {
        if (work[C[i]] == 0) {
          cnt++;
        }
        work[C[i]]++;
      }
    }

    if (cur_r < st.r) {
      for (int i = cur_r + 1; i <= st.r; i++) {
        if (work[C[i]] == 0) {
          cnt++;
        }
        work[C[i]]++;
      }
    }

    if (cur_l < st.l) {
      for (int i = cur_l; i < st.l; i++) {
        if (work[C[i]] == 1) {
          cnt--;
        }
        work[C[i]]--;
      }
    }

    if (st.r < cur_r) {
      for (int i = st.r + 1; i <= cur_r; i++) {
        if (work[C[i]] == 1) {
          cnt--;
        }
        work[C[i]]--;
      }
    }

    cur_l = st.l;
    cur_r = st.r;
    ans[st.index] = cnt;
  }

  REP(i, Q) { cout << ans[i] << endl; }
}