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

#pragma GCC optimize("O3")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("unroll-loops")

#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 pb push_back
#define pf push_front
#define eb emplace_back
#define mp make_pair
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define f first
#define s second
#define sz(x) (int)x.size()
#define endl "\\n"
#define forn(i, n) for (int i = 0; i < n; ++i)
#define fore(i, l, r) for (int i = int(l); i <= int(r); ++i)
#define rep(i, begin, end) for (__typeof(end) i = (begin); i != (end); i++)
#define fill(a, value) memset(a, value, sizeof(a));
#define gcd(a, b) __gcd((a), (b))
#define watch1(x) cout << (x) << endl
#define watch2(x, y) cout << (x) << " " << (y) << endl
#define watch3(x, y, z) cout << (x) << " " << (y) << " " << (z) << endl
#define fastio                                                                 \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vpii;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    oset;

const int INF = 9e18;
const int mod = 1e9 + 7;
const int N = 5e5 + 5;

int i, n, q, arr[N];

struct Query {
  int l, r, idx;
};

bool cmp(Query x, Query y) { return x.r < y.r; }

void update(int idx, int val, int bit[], int n) {
  for (; idx <= n; idx += idx & -idx)
    bit[idx] += val;
}

int query(int idx, int bit[], int n) {
  int sum = 0;
  for (; idx > 0; idx -= idx & -idx)
    sum += bit[idx];
  return sum;
}

void answeringQueries(int n, Query queries[], int q) {
  int bit[N];
  memset(bit, 0, sizeof(bit));
  int last_visit[N];
  memset(last_visit, -1, sizeof(last_visit));
  int ans[q];
  int query_counter = 0;
  for (int i = 0; i < n; i++) {
    if (last_visit[arr[i]] != -1)
      update(last_visit[arr[i]] + 1, -1, bit, n);
    last_visit[arr[i]] = i;
    update(i + 1, 1, bit, n);
    while (query_counter < q && queries[query_counter].r == i) {
      ans[queries[query_counter].idx] =
          query(queries[query_counter].r + 1, bit, n) -
          query(queries[query_counter].l, bit, n);
      query_counter++;
    }
  }
  for (int i = 0; i < q; i++)
    cout << ans[i] << endl;
}

void solve() {
  cin >> n >> q;
  for (i = 0; i < n; i++) {
    cin >> arr[i];
  }
  Query queries[q];
  for (i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    queries[i].l = l - 1;
    queries[i].r = r - 1;
    queries[i].idx = i;
  }
  sort(queries, queries + q, cmp);
  answeringQueries(n, queries, q);
}

signed main() {
  fastio;
  int t;
  // cin>>t;
  t = 1;
  while (t--) {
    solve();
  }
  return 0;
}


Error: TLE

Fixed submission:

#pragma GCC optimize("O3")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("unroll-loops")

#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 pb push_back
#define pf push_front
#define eb emplace_back
#define mp make_pair
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define f first
#define s second
#define sz(x) (int)x.size()
#define endl "\\n"
#define forn(i, n) for (int i = 0; i < n; ++i)
#define fore(i, l, r) for (int i = int(l); i <= int(r); ++i)
#define rep(i, begin, end) for (__typeof(end) i = (begin); i != (end); i++)
#define fill(a, value) memset(a, value, sizeof(a));
#define gcd(a, b) __gcd((a), (b))
#define watch1(x) cout << (x) << endl
#define watch2(x, y) cout << (x) << " " << (y) << endl
#define watch3(x, y, z) cout << (x) << " " << (y) << " " << (z) << endl
#define fastio                                                                 \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vpii;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    oset;

const int INF = 9e18;
const int mod = 1e9 + 7;
const int N = 5e5 + 5;

int i, n, q, arr[N];

unordered_set<int> *segment;

void build(int i, int s, int e) {
  if (s == e) {
    segment[i].insert(arr[s]);
    return;
  }
  build(2 * i, s, (s + e) / 2);
  build(1 + 2 * i, 1 + (s + e) / 2, e);
  segment[i].insert(segment[2 * i].begin(), segment[2 * i].end());
  segment[i].insert(segment[2 * i + 1].begin(), segment[2 * i + 1].end());
}

unordered_set<int> query(int node, int l, int r, int a, int b) {
  unordered_set<int> left, right, result;
  if (b < l || a > r)
    return result;
  if (a <= l && r <= b)
    return segment[node];
  left = query(2 * node, l, (l + r) / 2, a, b);
  result.insert(left.begin(), left.end());
  right = query(1 + 2 * node, 1 + (l + r) / 2, r, a, b);
  result.insert(right.begin(), right.end());
  return result;
}

void init(int n) {
  int h = (int)ceil(log2(n));
  h = (3 * (pow(2, h))) - 1;
  segment = new unordered_set<int>[h];
}

void solve() {
  cin >> n >> q;
  for (i = 0; i < n; i++) {
    cin >> arr[i];
  }
  init(n);
  build(1, 0, n - 1);
  while (q--) {
    int l, r;
    cin >> l >> r;
    l--, r--;
    cout << query(1, 0, n - 1, l, r).size() << endl;
  }
}

signed main() {
  fastio;
  int t;
  // cin>>t;
  t = 1;
  while (t--) {
    solve();
  }
  return 0;
}