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[5000005], n, ans[5000005];

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() {
  int q, s, e;
  vector<node> v;
  cin >> n;
  cin >> q;
  for (int i = 1; i <= n; i++) {
    cin >> s;
    v.push_back(node(i, i, s, -1));
  }
  for (int i = 0; i < q; i++) {
    cin >> 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++) {
    cout << ans[i] << "\\n";
  }

  return 0;
}

Error: TLE

Fixed submission:

/***************************************** START OF TEMPLATE
 * *********************************************/

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

#define SPEED                                                                  \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);                                                                 \\
  cout << fixed;                                                               \\
  cout << setprecision(10);
#define randomINIT                                                             \\
  mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
#define foo(i, a, n) for (ll i = (a); i <= n; i++)
#define frr(i, a, n) for (ll i = (a); i >= n; i--)
#define fo(i, n) for (ll i = 0; i < n; i++)
#define newl cout << "\\n";
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define sym(s) s = "#" + s + "#";
#define sqr(v) v *v;
#define all(x) (x).begin(), (x).end()
#define alll(x, n) x + 1, x + n + 1
#define uniq(a) a.resize(unique(a.begin(), a.end()) - a.begin());
#define mset(x, val) memset(x, val, sizeof(x))
#define mset1d(a, n, v) foo(i, 0, n + 5) a[i] = v;
#define mset2d(a, n, m, v) foo(i, 0, n + 5) foo(j, 0, m + 5) a[i][j] = v;
#define deb1(a) cout << a << "\\n";
#define deb2(a, b) cout << a << " " << b << "\\n";
#define deb3(a, b, c) cout << a << " " << b << " " << c << "\\n";
#define deb4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << "\\n";
#define debp(a) cout << a.f << " " << a.s << "\\n";
#define debv(a)                                                                \\
  for (auto it : a)                                                            \\
    cout << it << " ";                                                         \\
  newl;
#define debm(a)                                                                \\
  for (auto it : a)                                                            \\
    cout << "{" << it.f << "," << it.s << "}, ";                               \\
  newl;
#define deb1d(a, n)                                                            \\
  foo(i, 1, n) cout << a[i] << " ";                                            \\
  newl;
#define deb2d(a, n, m)                                                         \\
  foo(i, 1, n) {                                                               \\
    foo(j, 1, m) { cout << a[i][j] << " "; }                                   \\
    newl;                                                                      \\
  }

template <typename T>
using ordered_set =
    tree<T, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update>;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pll = pair<ll, ll>;
using vll = vector<ll>;
using vpll = vector<pll>;

const ld PI = 3.14159265358979323846;
const ll MOD = 1e+9 + 7;
const ll INF = LLONG_MAX;
const int INFi = INT_MAX;
const ll N = 5e+5 + 8;

vll adj[N];
ll vis[N];
ll dx4[] = {0, 1, 0, -1}, dy4[] = {1, 0, -1, 0};

ll test_cases = 1;
randomINIT;

/*********************************************************************************************************/
/***************************************** VARIABLE DECLARATIONS
 * *****************************************/
ll n, q, a[N], b[N];
map<ll, ll> nxt;

/***************************************** FUNCTION IMPLEMENTATIONS
 * **************************************/
vll seg[4 * N + 8];

ll left(ll nd) { return nd << 1; }
ll right(ll nd) { return (nd << 1) + 1; }

void build(ll node, ll st, ll en) {
  if (st == en) {
    seg[node].pb(b[st]);
    return;
  }

  ll mid = (st + en) / 2;
  build(left(node), st, mid);
  build(right(node), mid + 1, en);
  merge(all(seg[2 * node]), all(seg[2 * node + 1]), back_inserter(seg[node]));
}

ll query(ll node, ll st, ll en, ll l, ll r, ll val) {
  if (l <= st && en <= r)
    return seg[node].end() - (upper_bound(all(seg[node]), val));
  if (st > r || en < l)
    return 0;

  ll mid = (st + en) / 2;
  ll lq = query(left(node), st, mid, l, r, val);
  ll rq = query(right(node), mid + 1, en, l, r, val);

  return lq + rq;
}

/***************************************** START OF MAIN FUNCTION
 * ****************************************/
void MAIN(ll tc) {
  cin >> n >> q;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }

  for (int i = n; i >= 1; i--) {
    if (nxt[a[i]] == 0)
      b[i] = INF;
    else
      b[i] = nxt[a[i]];
    nxt[a[i]] = i;
  }

  build(1, 1, n);
  for (int i = 1; i <= q; i++) {
    int lb, rb;
    cin >> lb >> rb;
    cout << query(1, 1, n, lb, rb, rb) << "\\n";
  }
}

int main() {
  SPEED;

  // cin>>test_cases;
  foo(i, 1, test_cases) { MAIN(i); }
}