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

// verma_ankit484

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

#define int long long
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl "\\n"
#define pb push_back
const int mod = (int)1e9 + 7;

using namespace std;
// using namespace __gnu_pbds;

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

vector<int> arr, seg;

int query(int v, int l, int r, int s, int e) {
  if (l > r || s > e || r < s || l > e)
    return 0; // if not in range we returned 0 so that it doesn't make any
              // change
  if (s >= l && e <= r)
    return seg[v];
  int mid = (s + e) / 2;
  return query(2 * v, l, r, s, mid) + query(2 * v + 1, l, r, mid + 1, e);
}

void update(int v, int l, int r, int s, int e, int val) {
  if (l > r || s > e || r < s || l > e)
    return;
  if (s >= l && e <= r) {
    seg[v] = val;
    return;
  }
  int mid = (s + e) / 2;
  update(2 * v, l, r, s, mid, val);
  update(2 * v + 1, l, r, mid + 1, e, val);
  seg[v] = seg[2 * v] + seg[2 * v + 1];
}

bool srt(const pair<pair<int, int>, int> &p1,
         const pair<pair<int, int>, int> &p2) {
  return p1.first.second < p2.first.second;
}

int32_t main() {
  IOS
#ifdef AV
      freopen("input.txt", "r", stdin);
#endif
  int N = 500005;
  arr.resize(N, 0);
  seg.resize(4 * N + 1, 0);
  int n, q;
  cin >> n >> q;
  vector<int> vc(n);
  for (int i = 0; i < n; i++)
    cin >> vc[i];
  vector<pair<pair<int, int>, int>> qu(q);
  for (int i = 0; i < q; i++) {
    cin >> qu[i].first.first >> qu[i].first.second;
    qu[i].first.first--, qu[i].first.second--;
    qu[i].second = i;
  }
  sort(qu.begin(), qu.end(), srt);
  vector<int> last(500005, -1);
  vector<int> ans(q, 0);
  int lidx = 0;
  for (int i = 0; i < q; i++) {
    while (lidx <= qu[i].first.second) {
      int v = vc[lidx];
      if (last[v] != -1) {
        update(1, last[v], last[v], 0, N - 1, 0);
      }
      update(1, lidx, lidx, 0, N - 1, 1);
      last[v] = lidx;
      lidx++;
    }
    int val = query(1, qu[i].first.first, qu[i].first.second, 0, N - 1);
    ans[qu[i].second] = val;
  }
  for (auto i : ans)
    cout << i << endl;
  return 0;
}


Error: TLE

Fixed submission:

// verma_ankit484

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

typedef int ll;
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl "\\n"
#define pb push_back
const ll mod = (ll)1e9 + 7;

using namespace std;
// using namespace __gnu_pbds;

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

const ll N = 500002;
struct Node {
  ll x;
  Node *l, *r;
  Node(Node *_l = NULL, Node *_r = NULL, ll _x = 0) {
    l = _l;
    r = _r;
    x = _x;
  }
};
ll ar[N];
Node *versions[N];
void build(Node *tree, ll l, ll r) {
  if (r < l)
    return;
  if (l == r) {
    tree->x = ar[l];
    return;
  }
  ll mid = (l + r) >> 1;
  tree->l = new Node();
  tree->r = new Node();
  build(tree->l, l, mid);
  build(tree->r, mid + 1, r);
  tree->x = tree->l->x + tree->r->x;
}
void update(Node *old, Node *curr, ll l, ll r, ll idx, ll val) {
  if (idx < l || idx > r || r < l)
    return;
  if (l == r) {
    curr->x += val + old->x;
    return;
  }
  ll mid = (l + r) >> 1;
  if (idx <= mid) {
    curr->r = old->r;
    curr->l = new Node();
    update(old->l, curr->l, l, mid, idx, val);
  } else {
    curr->l = old->l;
    curr->r = new Node();
    update(old->r, curr->r, mid + 1, r, idx, val);
  }
  curr->x = curr->l->x + curr->r->x;
}
ll query(Node *root, ll l, ll r, ll lo, ll hi) {
  if (lo > r || hi < l || r < l)
    return 0;
  if (root == NULL)
    assert(false);
  if (lo <= l && r <= hi) {
    return root->x;
  }
  ll mid = (l + r) >> 1;
  ll a = query(root->l, l, mid, lo, hi);
  ll b = query(root->r, mid + 1, r, lo, hi);
  return a + b;
}

int main() {
  IOS
#ifndef ONLINE_JUDGE
      freopen("input.txt", "r", stdin);
#endif
  memset(ar, 0, sizeof(ar));
  versions[0] = new Node();
  ll n, q;
  cin >> n >> q;
  vector<ll> vc(n + 1);
  for (ll i = 1; i <= n; i++) {
    cin >> vc[i];
  }
  build(versions[0], 0, n);
  // map<ll, ll> mp, mpp;
  vector<int> mp(500001, 0), mpp(500001, 0);
  for (ll i = 1; i <= n; i++) {
    versions[i] = new Node();
    mp[vc[i]] = mpp[vc[i]];
    update(versions[i - 1], versions[i], 0, n, mp[vc[i]], 1);
    mpp[vc[i]] = i;
  }
  // ll q;
  // cin >> q;
  while (q--) {
    ll a, b;
    cin >> a >> b;
    ll p = query(versions[b], 0, n, 0, a - 1);
    p -= query(versions[a - 1], 0, n, 0, a - 1);
    cout << p << endl;
  }
  return 0;
}