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;

#define int long long
#define db double
#define mod 1000000007
#define pb push_back
#define pp pair<int, int>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repe(i, a, b) for (int i = a; i <= b; i++)
#define repr(i, a, b) for (int i = b; i >= a; i--)
#define maxn 500005
#define ss second
#define ff first
#define all(a) a.begin(), a.end()
#define lb lower_bound
#define ub upper_bound
#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
int inv(int a, int b) { return 1 < a ? b - inv(b % a, a) * b / a : 1; }
int n, q;
int arr[maxn];
int fen[maxn];
void update(int idx, int val) {
  while (idx <= n) {
    fen[idx] += val;
    idx += (idx & -idx);
  }
}
int query(int idx) {
  int tmp = 0;
  while (idx > 0) {
    tmp += fen[idx];
    idx -= (idx & -idx);
  }
  return tmp;
}
void solve() {
  cin >> n >> q;
  rep(i, 0, n) cin >> arr[i + 1];
  vector<pair<pp, int>> v;
  // build(1,0,n-1);
  int i = 0, m = q;
  while (q--) {
    int l, r;
    cin >> l >> r;
    v.pb({{l, r}, i++});
  }
  sort(all(v));
  reverse(all(v));
  int j = 0, mp[maxn] = {0}, ans[m];
  repr(i, 1, n) {
    int col = arr[i];
    if (mp[col])
      update(mp[col], -1);
    update(i, 1);
    mp[col] = i;
    while (j < v.size() and v[j].ff.ff == i) {
      int tmp = -query(i - 1) + query(v[j].ff.ss);
      ans[v[j].ss] = tmp;
      j++;
    }
  }
  rep(i, 0, m) cout << ans[i] << "\\n";
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  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>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define int long long
#define db double
#define mod 1000000007
#define pb push_back
#define pp pair<int, int>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repe(i, a, b) for (int i = a; i <= b; i++)
#define repr(i, a, b) for (int i = b; i >= a; i--)
#define maxn 500005
#define ss second
#define ff first
#define all(a) a.begin(), a.end()
#define lb lower_bound
#define ub upper_bound
#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
int inv(int a, int b) { return 1 < a ? b - inv(b % a, a) * b / a : 1; }
int n, q;
int arr[maxn];
set<int> st[5 * maxn];
void build(int p, int l, int r) {
  if (l == r) {
    st[p].insert(arr[l]);
    return;
  }
  int mid = (l + r) / 2;
  build(2 * p, l, mid);
  build(2 * p + 1, mid + 1, r);
  for (auto x : st[2 * p])
    st[p].insert(x);
  for (auto x : st[2 * p + 1])
    st[p].insert(x);
}
set<int> query(int p, int l, int r, int i, int j) {
  set<int> s1, s2, s3;
  if (l > j || r < i)
    return s1;
  if (i <= l and r <= j)
    return st[p];
  int mid = (l + r) / 2;
  s1 = query(2 * p, l, mid, i, j);
  s2 = query(2 * p + 1, mid + 1, r, i, j);
  for (auto x : s1)
    s3.insert(x);
  for (auto x : s2)
    s3.insert(x);
  return s3;
}
void solve() {
  cin >> n >> q;
  rep(i, 0, n) cin >> arr[i];
  build(1, 0, n - 1);
  while (q--) {
    int l, r;
    cin >> l >> r;
    set<int> s = query(1, 0, n - 1, --l, --r);
    cout << s.size() << "\\n";
  }
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  // cin >> t;
  while (t--)
    solve();
  return 0;
}