alexjercan/bugnet huggingface dataset:
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define N 510000
struct ques {
  int l, r, id;
  int ans;
} q[N];
int pos[N], c[N], s[N], ans, n, m;
bool cmp(ques x, ques y) {
  if (pos[x.l] == pos[y.l])
    return x.r < y.r;
  return x.l < y.l;
}
bool __id(ques x, ques y) { return x.id < y.id; }
void add(int color) {
  s[color]++;
  ans += s[color] == 1;
}
void del(int color) {
  s[color]--;
  ans -= s[color] == 0;
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    cin >> c[i];
  int size = (int)sqrt(n);
  for (int i = 1; i <= n; i++)
    pos[i] = (i - 1) / size + 1;
  for (int i = 1; i <= m; i++) {
    cin >> q[i].l >> q[i].r;
    q[i].id = i;
  }
  sort(q + 1, q + 1 + m, cmp);
  int l = 1, r = 0;
  for (int i = 1; i <= m; i++) {
    while (q[i].l < l)
      add(c[--l]);
    while (q[i].l > l)
      del(c[l++]);
    while (q[i].r > r)
      add(c[++r]);
    while (q[i].r < r)
      del(c[r--]);
    q[i].ans = ans;
  }
  sort(q + 1, q + 1 + m, __id);
  for (int i = 1; i <= m; i++)
    cout << q[i].ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#define maxn 500005
#define For(a, b, c) for (a = b; a <= c; ++a)
using namespace std;
int n, m, id[maxn], ans[500010];
int p[maxn], cnt;
struct B {
  int c, id;
} a[maxn];
struct que {
  int l, r, id;
} q[500010];
int read() {
  char x;
  while ((x = getchar()) > '9' || x < '0')
    ;
  int u = x - '0';
  while ((x = getchar()) <= '9' && x >= '0')
    u = (u << 3) + (u << 1) + x - '0';
  return u;
}
bool cmp1(B a, B b) { return a.c < b.c; } // 编号排序
bool cmp2(que a, que b) {                 // 询问排序
  if (a.l == b.l)
    return a.r < b.r;
  return a.l < b.l;
}
void pre() { // 预处理
  int i;
  sort(q + 1, q + m + 1, cmp2);
  sort(a + 1, a + n + 1, cmp1);
  For(i, 1, n) id[a[i].id] =
      (a[i].c == a[i - 1].c) ? cnt : ++cnt; // 考虑相同编号
}
void solve() {
  int l = 1, r = 1, sum = 1, now = 1;
  ++p[id[1]];
  while (now <= m) { // 解决问题
    while (r < q[now].r)
      if (++p[id[++r]] == 1)
        ++sum;
    while (l < q[now].l)
      if (!--p[id[l++]])
        --sum;
    while (r > q[now].r)
      if (!--p[id[r--]])
        --sum;
    ans[q[now++].id] = sum; // 记录答案
  }
}
int main() {
  // #ifndef ONLINE_JUDGE
  //     freopen("L.in", "r", stdin);
  //     freopen("L.out","w",stdout);
  // #endif
  int i;
  n = read();
  m = read();
  For(i, 1, n) a[i].c = read(), a[i].id = i;

  For(i, 1, m) q[i].l = read(), q[i].r = read(), q[i].id = i;
  pre();
  solve();
  For(i, 1, m) printf("%d\\n", ans[i]);
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int ksiz, ans, c[N], s[N], anss[N];
struct node {
  int l, r, id;
} a[N];
void read(int &x) {
  int f = 1;
  x = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  x *= f;
}
bool cmp(node q, node p) {
  int t1 = (q.l - 1) / ksiz, t2 = (p.l - 1) / ksiz;
  return t1 ^ t2 ? t1 < t2 : q.r < p.r;
}
void add(int x) {
  if (!c[s[x]])
    ans++;
  c[s[x]]++;
}
void del(int x) {
  c[s[x]]--;
  if (!c[s[x]])
    ans--;
}
int main() {
  int n, m, i, x, y;
  read(n), read(m), ksiz = sqrt(n);
  for (i = 1; i <= n; i++)
    read(s[i]);
  for (i = 1; i <= m; i++)
    read(x), read(y), a[i] = (node){x, y, i};
  sort(a + 1, a + 1 + m, cmp);
  int l = 1, r = 0;
  for (i = 1; i <= m; i++) {
    int ql = a[i].l, qr = a[i].r;
    while (l < ql)
      del(l++);
    while (l > ql)
      add(--l);
    while (r < qr)
      add(++r);
    while (r > qr)
      del(r--);
    anss[a[i].id] = ans;
  }
  for (i = 1; i <= m; i++)
    printf("%d\\n", anss[i]);
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e6 + 10;
int ksiz, ans, c[N], s[N], anss[N];
struct node {
  int l, r, id;
} a[N];
void read(int &x) {
  int f = 1;
  x = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  x *= f;
}
bool cmp(node q, node p) {
  int t1 = (q.l - 1) / ksiz, t2 = (p.l - 1) / ksiz;
  return t1 ^ t2 ? t1 < t2 : q.r < p.r;
}
void add(int x) {
  if (!c[s[x]])
    ans++;
  c[s[x]]++;
}
void del(int x) {
  c[s[x]]--;
  if (!c[s[x]])
    ans--;
}
signed main() {
  int n, m, i, x, y;
  read(n), read(m), ksiz = sqrt(n);
  for (i = 1; i <= n; i++)
    cin >> s[i];
  for (i = 1; i <= m; i++) {
    read(x), read(y);
    a[i] = (node){x, y, i};
  }
  sort(a + 1, a + 1 + m, cmp);
  int l = 1, r = 0;
  for (i = 1; i <= m; i++) {
    int ql = a[i].l, qr = a[i].r;
    while (l < ql)
      del(l++);
    while (l > ql)
      add(--l);
    while (r < qr)
      add(++r);
    while (r > qr)
      del(r--);
    anss[a[i].id] = ans;
  }
  for (i = 1; i <= m; i++)
    printf("%lld\\n", anss[i]);
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#define ll long long
using namespace std;
int read() {
  int k = 0, f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = -1;
  for (; isdigit(c); c = getchar())
    k = k * 10 + c - '0';
  return k * f;
}
const int N = 1000055;
int n, m, tot, a[N], rt[N], lch[N * 30], rch[N * 30], sum[N * 30];
void add(int &k, int pre, int l, int r, int a, int b) {
  k = ++tot;
  sum[k] = sum[pre] + b;
  if (l == r)
    return;
  int mid = l + r >> 1;
  if (a <= mid)
    rch[k] = rch[pre], add(lch[k], lch[pre], l, mid, a, b);
  else
    lch[k] = lch[pre], add(rch[k], rch[pre], mid + 1, r, a, b);
}
int query(int k, int l, int r, int a, int b) {
  if (!k)
    return 0;
  if (a <= l && b >= r)
    return sum[k];
  int mid = l + r >> 1, res = 0;
  if (a <= mid)
    res = query(lch[k], l, mid, a, b);
  if (b > mid)
    res += query(rch[k], mid + 1, r, a, b);
  return res;
}
map<int, int> mp;
int main() {
  n = read();
  m = read();
  for (int i = 1; i <= n; i++)
    a[i] = read();
  for (int i = 1; i <= n; i++) {
    rt[i] = rt[i - 1];
    if (mp[a[i]])
      add(rt[i], rt[i], 1, n, mp[a[i]], -1);
    mp[a[i]] = i;
    add(rt[i], rt[i], 1, n, mp[a[i]], 1);
  }
  for (int i = 1; i <= m; i++) {
    int l, r;
    l = read();
    r = read();
    printf("%d\\n", query(rt[r], 1, n, l, r));
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#define ll long long
using namespace std;
int read() {
  int k = 0, f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = -1;
  for (; isdigit(c); c = getchar())
    k = k * 10 + c - '0';
  return k * f;
}
const int N = 200055;
int n, m, tot, a[N], rt[N], lch[N * 30], rch[N * 30], sum[N * 30];
void add(int &k, int pre, int l, int r, int a, int b) {
  k = ++tot;
  sum[k] = sum[pre] + b;
  if (l == r)
    return;
  int mid = l + r >> 1;
  if (a <= mid)
    rch[k] = rch[pre], add(lch[k], lch[pre], l, mid, a, b);
  else
    lch[k] = lch[pre], add(rch[k], rch[pre], mid + 1, r, a, b);
}
int query(int k, int l, int r, int a, int b) {
  if (!k)
    return 0;
  if (a <= l && b >= r)
    return sum[k];
  int mid = l + r >> 1, res = 0;
  if (a <= mid)
    res = query(lch[k], l, mid, a, b);
  if (b > mid)
    res += query(rch[k], mid + 1, r, a, b);
  return res;
}
map<int, int> mp;
int main() {
  n = read();
  m = read();
  for (int i = 1; i <= n; i++)
    a[i] = read();
  for (int i = 1; i <= n; i++) {
    rt[i] = rt[i - 1];
    if (mp[a[i]])
      add(rt[i], rt[i], 1, n, mp[a[i]], -1);
    mp[a[i]] = i;
    add(rt[i], rt[i], 1, n, mp[a[i]], 1);
  }
  for (int i = 1; i <= m; i++) {
    int l, r;
    l = read();
    r = read();
    printf("%d\\n", query(rt[r], 1, n, l, r));
  }
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int block_size;
struct Query {
  int l, r, idx;
  bool operator<(Query other) const {
    return make_pair(l / block_size, r) <
           make_pair(other.l / block_size, other.r);
  }
};
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, q;
  cin >> n >> q;
  block_size = sqrt(n);
  vector<int> v(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  vector<Query> vq;
  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;
    vq.push_back((Query){l, r, i});
  }
  vector<int> answers(q);
  int ans = 0;
  vector<int> mp(n + 1, 0);
  auto add = [&](int idx) {
    if (mp[v[idx]] == 0) {
      ans++;
    }
    mp[v[idx]]++;
  };
  auto remove = [&](int idx) {
    if (mp[v[idx]] == 1) {
      ans--;
    }
    mp[v[idx]]--;
  };
  sort(vq.begin(), vq.end());
  int cur_l = 0;
  int cur_r = -1;
  for (Query qu : vq) {
    while (cur_l > qu.l) {
      cur_l--;
      add(cur_l);
    }
    while (cur_r < qu.r) {
      cur_r++;
      add(cur_r);
    }
    while (cur_l < qu.l) {
      remove(cur_l);
      cur_l++;
    }
    while (cur_r > qu.r) {
      remove(cur_r);
      cur_r--;
    }
    answers[qu.idx] = ans;
  }
  for (int x : answers) {
    cout << x << "\\n";
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int block_size;
struct Query {
  int l, r, idx;
  bool operator<(Query other) const {
    return make_pair(l / block_size, r) <
           make_pair(other.l / block_size, other.r);
  }
};
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, q;
  cin >> n >> q;
  block_size = sqrt(n);
  vector<int> v(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  vector<Query> vq;
  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;
    vq.push_back((Query){l, r, i});
  }
  vector<int> answers(q);
  int ans = 0;
  unordered_map<int, int> mp;
  auto add = [&](int idx) {
    if (mp[v[idx]] == 0) {
      ans++;
    }
    mp[v[idx]]++;
  };
  auto remove = [&](int idx) {
    if (mp[v[idx]] == 1) {
      ans--;
    }
    mp[v[idx]]--;
  };
  sort(vq.begin(), vq.end());
  int cur_l = 0;
  int cur_r = -1;
  for (Query qu : vq) {
    while (cur_l > qu.l) {
      cur_l--;
      add(cur_l);
    }
    while (cur_r < qu.r) {
      cur_r++;
      add(cur_r);
    }
    while (cur_l < qu.l) {
      remove(cur_l);
      cur_l++;
    }
    while (cur_r > qu.r) {
      remove(cur_r);
      cur_r--;
    }
    answers[qu.idx] = ans;
  }
  for (int x : answers) {
    cout << x << "\\n";
  }
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define debug(a) cout << #a ": " << a << endl
#define IOS ios::sync_with_stdio(false), cin.tie(0)
using namespace std;
typedef long long ll;
inline int read(void) {
  char ch = getchar();
  int ans = 0;
  while (!isdigit(ch))
    ch = getchar();
  while (isdigit(ch))
    ans = ans * 10 + ch - '0', ch = getchar();
  return ans;
}
const int N = 5e5 + 7;
int n, m, c[N], pre[N], pos[N];
struct node {
  int l, r, id;
  bool operator<(const node &rh) const { return r < rh.r; }
} q[N];
int T[N], ans[N];
void add(int x, int y) {
  for (; x <= n; x += x & (-x))
    T[x] += y;
}
int query(int x) {
  int ans = 0;
  for (; x >= 1; x -= x & (-x))
    ans += T[x];
  return ans;
}
int main(void) {
  n = read(), m = read();
  for (int i = 1; i <= n; ++i) {
    c[i] = read();
    pre[i] = pos[c[i]];
    pos[c[i]] = i;
  }
  for (int i = 1; i <= m; ++i) {
    q[i].l = read(), q[i].r = read();
    q[i].id = i;
  }
  sort(q + 1, q + m + 1);
  int R = 0;
  // for (int i = 1; i <= n; ++i) printf("%d ", pre[i]); printf("\\n");
  for (int i = 1; i <= m; ++i) {
    while (R < q[i].r) {
      ++R;
      if (pre[R])
        add(pre[R], -1);
      add(R, 1);
    }
    ans[q[i].id] = query(q[i].r) - query(q[i].l - 1);
  }
  for (int i = 1; i <= m; ++i)
    printf("%d\\n", ans[i]);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define debug(a) cout << #a ": " << a << endl
#define IOS ios::sync_with_stdio(false), cin.tie(0)
using namespace std;
typedef long long ll;
inline int read(void) {
  char ch = getchar();
  int ans = 0;
  while (!isdigit(ch))
    ch = getchar();
  while (isdigit(ch))
    ans = ans * 10 + ch - '0', ch = getchar();
  return ans;
}
const int N = 5e5 + 7;
int block, c[N];
struct node {
  int l, r, id;
  bool operator<(const node &rh) const {
    if (l / block != rh.l / block)
      return l < rh.l;
    return r < rh.r;
  }
} q[N];
int cnt[N], ANS, ans[N];
void add(int x) {
  x = c[x];
  if (!cnt[x])
    ANS++;
  cnt[x]++;
}
void del(int x) {
  x = c[x];
  if (cnt[x] == 1)
    ANS--;
  cnt[x]--;
}
int main(void) {
  int n = read(), m = read();
  for (int i = 1; i <= n; ++i)
    c[i] = read();
  for (int i = 1; i <= m; ++i) {
    q[i].l = read(), q[i].r = read();
    q[i].id = i;
  }
  block = sqrt(n);
  sort(q + 1, q + m + 1);
  int L = q[1].l, R = L - 1;
  for (int i = 1; i <= m; ++i) {
    int l = q[i].l, r = q[i].r, id = q[i].id;
    while (R < r)
      add(++R);
    while (R > r)
      del(R--);
    while (L > l)
      add(--L);
    while (L < l)
      del(L++);
    ans[id] = ANS;
  }
  for (int i = 1; i <= m; ++i)
    printf("%d\\n", ans[i]);
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define fr first
#define sc second
#define Fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
int block;
int m[2000000];
int a[2000000];
struct query {
  int l, r, idx;
};
int value = 0;
bool comper(query a, query b) {
  if (a.l / block != b.l / block)
    return a.l < b.l;
  return a.l / block % 2 ? a.r < b.r : a.r > b.r;
}

void add(int pos) {
  int val = a[pos];
  m[val]++;
  if (m[val] == 1) {
    value++;
  } else if (!m[val]) {
    value--;
  }
}
void remove(int pos) {
  int val = a[pos];
  m[val]--;
  if (!m[val]) {
    value--;
  }
}

int main(void) {
  Fast;
  int n, q;
  cin >> n >> q;
  block = sqrt(n);

  for (int i = 1; i < n + 1; i++)
    cin >> a[i];

  query qr[q];

  for (int i = 0; i < q; i++)
    cin >> qr[i].l >> qr[i].r, qr[i].idx = i;

  sort(qr, qr + q, comper);

  set<int> s;
  int L = 1, R = 1, ml = 0, mr = 0;
  int ans[q];

  for (int i = 0; i < q; i++) {
    int L = qr[i].l;
    int R = qr[i].r;
    while (mr < R)
      mr++, add(mr);

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

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

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

    ans[qr[i].idx] = value;
  }

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define fr first
#define sc second
#define Fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
int block;
int m[2000000];
int a[2000000];
struct query {
  int l, r, idx;
};
int value = 0;
bool comper(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) {
  int val = a[pos];
  m[val]++;
  if (m[val] == 1) {
    value++;
  } else if (!m[val]) {
    value--;
  }
}
void remove(int pos) {
  int val = a[pos];
  m[val]--;
  if (!m[val]) {
    value--;
  }
}

int main(void) {
  Fast;
  int n, q;
  cin >> n >> q;
  block = sqrt(n);

  for (int i = 1; i < n + 1; i++)
    cin >> a[i];

  query qr[q];

  for (int i = 0; i < q; i++)
    cin >> qr[i].l >> qr[i].r, qr[i].idx = i;

  sort(qr, qr + q, comper);

  set<int> s;
  int L = 1, R = 1, ml = 0, mr = 0;
  int ans[q];

  for (int i = 0; i < q; i++) {
    int L = qr[i].l;
    int R = qr[i].r;
    while (mr < R)
      mr++, add(mr);

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

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

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

    ans[qr[i].idx] = value;
  }

  for (int i = 0; i < q; i++)
    cout << ans[i] << "\\n";
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

const int N = 500010;

struct Mo {
  int sz;
  vector<int> left, right, order;
  vector<bool> check;
  Mo(int n, int q) : sz((int)sqrt(n)), order(q), check(n) {}
  void insert(int l, int r) {
    left.emplace_back(l);
    right.emplace_back(r);
  }
  template <typename ADD, typename DEL, typename REM>
  void build(const ADD &add, const DEL &del, const REM &rem) {
    iota(order.begin(), order.end(), 0);
    sort(order.begin(), order.end(), [&](int a, int b) {
      return (left[a] / sz != left[b] / sz ? left[a] < left[b]
                                           : right[a] < right[b]);
    });
    int l = 0, r = 0;
    auto push = [&](int idx) {
      check[idx].flip();
      if (check[idx])
        add(idx);
      else
        del(idx);
    };
    for (int idx : order) {
      while (l > left[idx])
        push(--l);
      while (r < right[idx])
        push(r++);
      while (l < left[idx])
        push(l++);
      while (r > right[idx])
        push(--r);
      rem(idx);
    }
  }
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  int arr[n + 1];
  for (int i = 0; i < n; ++i)
    cin >> arr[i];
  Mo mo(n, m);
  for (int i = 0; i < m; ++i) {
    int l, r;
    cin >> l >> r;
    mo.insert(l - 1, r);
  }
  int cnt[n + 1] = {0}, ans[m];
  int sum = 0;
  auto add = [&](int idx) {
    if (cnt[arr[idx]]++ == 0)
      ++sum;
  };
  auto del = [&](int idx) {
    if (--cnt[arr[idx]] == 0)
      --sum;
  };
  auto rem = [&](int idx) { ans[idx] = sum; };
  mo.build(add, del, rem);
  for (int i = 0; i < m; ++i)
    cout << ans[i] << '\\n';
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long int

using namespace std;
ll n, q, c[5000000], l, r;
bool d[5000000];

int main() {
  // freopen("a.inp","r",stdin);
  ll dem;
  cin >> n >> q;
  for (ll i = 1; i <= n; i++)
    cin >> c[i];
  while (q--) {
    dem = 0;
    cin >> l >> r;
    for (ll i = l; i <= r; i++) {
      if (!d[c[i]]) {
        dem++;
        d[c[i]] = true;
      }
    }
    memset(d, 0, sizeof(d));
    cout << dem << endl;
  }

  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (long long i = 0; i < (n); ++i)
// g++ -std=c++11
//
using namespace std;

const int N = 500005;
int n, m;
int c[N];
int maxn;
int sum = 0;
int ans[N];

int cnt[N];

struct query {
  int l, r, id;
  bool operator<(const query &x) const {
    if (l / maxn != x.l / maxn)
      return l < x.l;
    return (l / maxn) & 1 ? r < x.r : r > x.r;
  }
} a[N];

inline void add(int i) {
  if (cnt[i] == 0) {
    sum += 1;
  }
  cnt[i]++;
}
inline void del(int i) {
  if (cnt[i] == 1) {
    sum -= 1;
  }
  cnt[i]--;
}

int main() {
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
  IOS cin >> n >> m;
  for (long long i = 1; i <= n; i++) {
    cin >> c[i];
  }
  maxn = sqrt(n);
  rep(i, m) {
    cin >> a[i].l >> a[i].r;
    a[i].id = i;
  }
  sort(a, a + m);
  for (int i = 0, l = 1, r = 0; i < m; i++) {
    while (r > a[i].r)
      del(c[r--]);
    while (r < a[i].r)
      add(c[++r]);
    while (l > a[i].l)
      add(c[--l]);
    while (l < a[i].l)
      del(c[l++]);

    ans[a[i].id] = sum;
  }
  for (int i = 0; i < m; i++) {
    cout << ans[i] << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (long long i = 0; i < (n); ++i)
// g++ -std=c++11
//
using namespace std;

const int N = 500005;
int n, m;
int c[N];
int maxn;
int sum = 0;
int ans[N];

int cnt[N];

struct query {
  int l, r, id;
  bool operator<(const query &x) const {
    if (l / maxn != x.l / maxn)
      return l < x.l;
    return r < x.r;
    // return (l / maxn) & 1 ? r < x.r : r > x.r;
  }
} a[N];

inline void add(int i) {
  if (cnt[i] == 0) {
    sum += 1;
  }
  cnt[i]++;
}
inline void del(int i) {
  if (cnt[i] == 1) {
    sum -= 1;
  }
  cnt[i]--;
}

int main() {
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
  IOS

          cin >>
      n >> m;
  for (long long i = 1; i <= n; i++) {
    cin >> c[i];
  }
  maxn = sqrt(n);
  rep(i, m) {
    cin >> a[i].l >> a[i].r;
    a[i].id = i;
  }
  sort(a, a + m);
  for (int i = 0, l = 1, r = 0; i < m; i++) {
    while (r > a[i].r)
      del(c[r--]);
    while (r < a[i].r)
      add(c[++r]);
    while (l > a[i].l)
      add(c[--l]);
    while (l < a[i].l)
      del(c[l++]);

    ans[a[i].id] = sum;
  }
  for (int i = 0; i < m; i++) {
    cout << ans[i] << endl;
  }
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 10 + 5e5;

int n, m, ans[MAXN], t[2 * MAXN], ri[MAXN], f[MAXN];
pair<int, int> a[MAXN], c[MAXN];

void Build() {
  for (int i = n - 1; i > 0; --i)
    t[i] = t[i << 1] + t[i << 1 | 1];
}

void Update(int p) {
  for (t[p += n] = 0; p > 1; p >>= 1)
    t[p >> 1] = t[p] + t[p ^ 1];
}

int Query(int l, int r) {
  int res = 0;
  for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
    if (l & 1)
      res += t[l++];
    if (r & 1)
      res += t[--r];
  }

  return res;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);

  cin >> n >> m;
  memset(f, -1, sizeof f);
  for (int i = 0; i < n; ++i) {
    int x;
    cin >> x;
    c[i].first = f[x];
    c[i].second = i;
    f[x] = i;
  }

  sort(c, c + n);

  for (int i = 0; i < m; ++i) {
    int x;
    cin >> a[i].first >> x;
    a[i].second = i;
    ri[i] = x;
  }

  for (int i = 0; i < n; ++i)
    t[n + i] = 1;

  Build();

  sort(a, a + m);

  int k = 0;
  for (int i = 0; i < m; ++i) {
    if (k < n)
      while (c[k].first < a[i].first - 1)
        Update(c[k++].second);

    ans[a[i].second] = ri[a[i].second] - a[i].first -
                       Query(a[i].first - 1, ri[a[i].second]) + 1;
  }

  for (int i = 0; i < m; ++i)
    cout << ans[i] << "\\n";

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 10 + 5e5;

int n, m, ans[MAXN], t[2 * MAXN], ri[MAXN], f[MAXN];
pair<int, int> a[MAXN], c[MAXN];

void Build() {
  for (int i = n - 1; i > 0; --i)
    t[i] = t[i << 1] + t[i << 1 | 1];
}

void Update(int p) {
  for (t[p += n] = 0; p > 1; p >>= 1)
    t[p >> 1] = t[p] + t[p ^ 1];
}

int Query(int l, int r) {
  int res = 0;
  for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
    if (l & 1)
      res += t[l++];
    if (r & 1)
      res += t[--r];
  }

  return res;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);

  cin >> n >> m;
  memset(f, -1, sizeof f);
  for (int i = 0; i < n; ++i) {
    int x;
    cin >> x;
    c[i].first = f[x];
    c[i].second = i;
    f[x] = i;
  }

  sort(c, c + n);

  for (int i = 0; i < m; ++i) {
    int x;
    cin >> a[i].first >> x;
    a[i].second = i;
    ri[i] = x;
  }

  for (int i = 0; i < n; ++i)
    t[n + i] = 1;

  Build();

  sort(a, a + m);

  for (int i = 0; i < m; ++i) {
    int k = 0;
    if (k < n)
      while (c[k].first < a[i].first - 1)
        Update(c[k++].second);

    ans[a[i].second] = ri[a[i].second] - a[i].first -
                       Query(a[i].first - 1, ri[a[i].second]) + 1;
  }

  for (int i = 0; i < m; ++i)
    cout << ans[i] << "\\n";

  return 0;
}