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


// Problem: F - Range Set Query
// Contest: AtCoder - AtCoder Beginner Contest 174
// URL: https://atcoder.jp/contests/abc174/tasks/abc174_f
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
#define pb push_back
#define mp make_pair
#define y1 y114514
#define y2 y1919810
// #define int long long
#define lsh(i) (1 << (i))
#define lshll(i) (1LL << (i))
#define repn(i, n) for (int i = 1; i <= (int)n; i++)
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define range(i, s, t) for (int i = s; i <= (int)t; i++)
#define RESET(x) memset((x), 0, sizeof(x))
const int INF = 0x3f3f3f3f;
const long double EPS = 1e-6;
const int N = 500050;
int cnt[N], ans, a[N];
void move(int pos, int sign) {
  if (sign) {
    if (++cnt[a[pos]] == 1)
      ++ans;
  } else {
    if (--cnt[a[pos]] == 0)
      --ans;
  }
}
int n, Q;
struct Query {
  int l, r, id;
} q[N];
int answer[N];
void solve() {
  scanf("%d%d", &n, &Q);
  rep(i, n) cin >> a[i];
  int BL = 1000;
  rep(i, Q) {
    int l, r;
    scanf("%d%d", &l, &r);
    --l;
    q[i] = {l, r, i};
  }
  sort(q, q + Q, [BL](const Query &a, const Query &b) {
    int al = a.l / BL, bl = b.l / BL;
    return (al != bl) ? al < bl : a.r < b.r;
  });
  int l = 0, r = 0;
  rep(i, Q) {
    while (l > q[i].l)
      move(--l, true);
    while (r < q[i].r)
      move(r++, true);
    while (l < q[i].l)
      move(l++, false);
    while (r > q[i].r)
      move(--r, false);
    answer[q[i].id] = ans;
  }
  rep(i, Q) printf("%d\\n", answer[i]);
}
signed main() {
  int T = 1;
  // cin >> T;
  while (T--)
    solve();
  return 0;
}


Error: TLE

Fixed submission:


// Problem: F - Range Set Query
// Contest: AtCoder - AtCoder Beginner Contest 174
// URL: https://atcoder.jp/contests/abc174/tasks/abc174_f
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
#define pb push_back
#define mp make_pair
#define y1 y114514
#define y2 y1919810
// #define int long long
#define lsh(i) (1 << (i))
#define lshll(i) (1LL << (i))
#define repn(i, n) for (int i = 1; i <= (int)n; i++)
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define range(i, s, t) for (int i = s; i <= (int)t; i++)
#define RESET(x) memset((x), 0, sizeof(x))
const int INF = 0x3f3f3f3f;
const long double EPS = 1e-6;
const int N = 500050;
int cnt[N], ans, a[N];
void move(int pos, int sign) {
  if (sign) {
    if (++cnt[a[pos]] == 1)
      ++ans;
  } else {
    if (--cnt[a[pos]] == 0)
      --ans;
  }
}
int n, Q;
struct Query {
  int l, r, id;
} q[N];
int answer[N];
void solve() {
  cin >> n >> Q;
  rep(i, n) cin >> a[i];
  int BL = 1000;
  rep(i, Q) {
    int l, r;
    cin >> l >> r;
    --l;
    q[i] = {l, r, i};
  }
  sort(q, q + Q, [BL](const Query &a, const Query &b) {
    int al = a.l / BL, bl = b.l / BL;
    return (al != bl) ? al < bl : a.r < b.r;
  });
  int l = 0, r = 0;
  rep(i, Q) {
    while (l > q[i].l)
      move(--l, true);
    while (r < q[i].r)
      move(r++, true);
    while (l < q[i].l)
      move(l++, false);
    while (r > q[i].r)
      move(--r, false);
    answer[q[i].id] = ans;
  }
  rep(i, Q) cout << answer[i] << endl;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int T = 1;
  // cin >> T;
  while (T--)
    solve();
  return 0;
}