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

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <vector>

using namespace std;

typedef long long ll;

typedef struct node {
  int s, e, val, pos;
  bool operator<(const node &n) const {
    if (e != n.e)
      return e < n.e;
    return val > n.val;
  }
  node(){};
  node(int ss, int ee, int v, int p) { s = ss, e = ee, val = v, pos = p; }
} node;

int occs[(int)1e6 + 5], tree[2000000], n, ans[2000000];

void update(int x, int val) {
  while (x <= n) {
    tree[x] += val;
    x += x & -x;
  }
}

int query(int x) {
  int ret = 0;
  while (x > 0) {
    ret += tree[x];
    x -= x & -x;
  }
  return ret;
}

int main() {
  // #ifndef ONLINE_JUDGE
  //     freopen("in.in", "r", stdin);
  // #endif

  int q, s, e;
  vector<node> v;
  scanf("%d", &n);
  scanf("%d", &q);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &s);
    v.push_back(node(i, i, s, -1));
  }

  for (int i = 0; i < q; i++) {
    scanf("%d %d", &s, &e);
    v.push_back(node(s, e, 0, i));
  }

  sort(v.begin(), v.end());

  for (int i = 0; i < v.size(); i++) {
    if (v[i].val == 0) {
      ans[v[i].pos] = query(v[i].e) - query(v[i].s - 1);
    } else {
      if (occs[v[i].val] != 0)
        update(occs[v[i].val], -1);
      update(v[i].s, 1);
      occs[v[i].val] = v[i].s;
    }
  }

  for (int i = 0; i < q; i++) {
    printf("%d\\n", ans[i]);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
// #include <ext/pb_ds/tree_policy.hpp>
// #include <ext/pb_ds/assoc_container.hpp>
#include <chrono>
using namespace std;
using namespace std::chrono;
// using namespace __gnu_pbds;
#define fastio                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define fi first
#define se second
#define int long long
#define pb push_back
#define emp emplace_back
#define vv(x) vector<x>
#define mp(x, y) map<x, y>
#define dq(x) deque<x>
#define pql(x) priority_queue<x>
#define pqs(x) priority_queue<x, vv(x), greater<x>>
#define mod 1000000007
#define forf(i, a, b) for (int i = a; i < b; i++)
#define it(x) x::iterator
#define ll long long
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#define time__(d)                                                              \\
  for (long blockTime = 0;                                                     \\
       (blockTime == 0 ? (blockTime = clock()) != 0 : false);                  \\
       debug("%s time : %.4fs", d,                                             \\
             (double)(clock() - blockTime) / CLOCKS_PER_SEC))
#define vii vector<int>
#define big 3e18
#define sm -2e9
#define mkr make_pair
#define vpi vector<pair<int, int>>
#define pii pair<int, int>
#define rng 500005
#define sz(x) (int)x.size()
#define rv(x) reverse(x.begin(), x.end())
#define out(x) cout << x.fi << " " << x.se << endl;

// #define ordered_set tree<pii, null_type,less<pii>,
// rb_tree_tag,tree_order_statistics_node_update>

void pr_init() {

#ifndef ONLINE_JUDGE
  freopen("gin.txt", "r", stdin);
  freopen("gout.txt", "w", stdout);

#endif
}
vector<set<int>> segment;
void build(int i, int s, int e, int arr[]) {
  if (s == e) {
    segment[i].insert(arr[s]);
    return;
  }
  build(2 * i, s, (s + e) / 2, arr);
  build(1 + 2 * i, 1 + (s + e) / 2, e, arr);

  segment[i].insert(segment[2 * i].begin(), segment[2 * i].end());

  segment[i].insert(segment[2 * i + 1].begin(), segment[2 * i + 1].end());
}
set<int> query(int node, int l, int r, int a, int b) {
  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;
}
int getDistinct(int l, int r, int n) {
  set<int> ans = query(1, 0, n - 1, l, r);

  return sz(ans);
}

const int Mxn = 5e5;

int arr[Mxn + 1];
void solve() {
  int n, q;
  cin >> n >> q;
  segment.assign(4 * n + 4, set<int>());

  forf(i, 0, n) cin >> arr[i];

  build(1, 0, n - 1, arr);

  while (q--) {
    int l, r;
    cin >> l >> r;
    --l, --r;
    cout << getDistinct(l, r, n) << "\\n";
  }
}

int32_t main() {
  pr_init();
  fastio;

  auto start = high_resolution_clock::now();

  solve();
  auto stop = high_resolution_clock::now();
  auto duration = duration_cast<microseconds>(stop - start);
  // cout << "Time taken by function: "
  //       << duration.count() << " microseconds" << endl;
}