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>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

// Macros

#define ll long long
#define db long double
#define pb push_back
#define eb emplace_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
#define sz(x) (int)x.size()
#define mod 1000000007
#define EPS 1e-9
#define io                                                                     \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define inf 1e10
#define PI acos(-1.0)
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define N 500005

// Solution

int a[N], n, q;
int cnt[N], cur_ans;

void remove(int idx) {
  cnt[a[idx]]--;
  if (cnt[a[idx]] == 0)
    cur_ans--;
}
void add(int idx) {
  if (cnt[a[idx]] == 0)
    cur_ans++;
  cnt[a[idx]]++;
}
int get_answer() { return cur_ans; }

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);
  }
};

vector<Query> queries;

vector<int> mo_s_algorithm(vector<Query> &queries) {
  vector<int> answers(queries.size());
  sort(queries.begin(), queries.end());

  // TODO: initialize data structure

  int cur_l = 0;
  int cur_r = -1;
  // invariant: data structure will always reflect the range [cur_l, cur_r]
  for (Query q : queries) {
    while (cur_l > q.l) {
      cur_l--;
      add(cur_l);
    }
    while (cur_r < q.r) {
      cur_r++;
      add(cur_r);
    }
    while (cur_l < q.l) {
      remove(cur_l);
      cur_l++;
    }
    while (cur_r > q.r) {
      remove(cur_r);
      cur_r--;
    }
    answers[q.idx] = get_answer();
  }
  return answers;
}

void solve() {
  cin >> n >> q;
  block_size = sqrt(n) + 5;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  queries.resize(q);
  for (int i = 0; i < q; i++) {
    cin >> queries[i].l >> queries[i].r;
    queries[i].l--;
    queries[i].r--;
    queries[i].idx = i;
  }
  vi ans = mo_s_algorithm(queries);
  for (int i : ans)
    cout << i << '\\n';
}

signed main() {
  io;
  // freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
  int tests;
  // cin>>tests;
  tests = 1;
  while (tests--) {
    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>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

// Macros

#define ll long long
#define db long double
#define pb push_back
#define eb emplace_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
#define sz(x) (int)x.size()
#define mod 1000000007
#define EPS 1e-9
#define io                                                                     \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define inf 1e10
#define PI acos(-1.0)
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define N 500005

// Solution

int a[N], n, q;
mi cnt;

void remove(int idx) {
  cnt[a[idx]]--;
  if (cnt[a[idx]] == 0)
    cnt.erase(a[idx]);
}
void add(int idx) { cnt[a[idx]]++; }
int get_answer() { return sz(cnt); }

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);
  }
};

vector<Query> queries;

vector<int> mo_s_algorithm(vector<Query> &queries) {
  vector<int> answers(queries.size());
  sort(queries.begin(), queries.end());

  // TODO: initialize data structure

  int cur_l = 0;
  int cur_r = -1;
  // invariant: data structure will always reflect the range [cur_l, cur_r]
  for (Query q : queries) {
    while (cur_l > q.l) {
      cur_l--;
      add(cur_l);
    }
    while (cur_r < q.r) {
      cur_r++;
      add(cur_r);
    }
    while (cur_l < q.l) {
      remove(cur_l);
      cur_l++;
    }
    while (cur_r > q.r) {
      remove(cur_r);
      cur_r--;
    }
    answers[q.idx] = get_answer();
  }
  return answers;
}

void solve() {
  cin >> n >> q;
  block_size = sqrt(n) + 5;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  queries.resize(q);
  for (int i = 0; i < q; i++) {
    cin >> queries[i].l >> queries[i].r;
    queries[i].l--;
    queries[i].r--;
    queries[i].idx = i;
  }
  vi ans = mo_s_algorithm(queries);
  for (int i : ans)
    cout << i << '\\n';
}

signed main() {
  io;
  // freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
  int tests;
  // cin>>tests;
  tests = 1;
  while (tests--) {
    solve();
  }
  return 0;
}