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

#include <bits/stdc++.h>
#define ll long long
#define mod 1000000007
#define maxn 200005
#define vi vector<int>
#define vl vector<ll>
#define mii map<int, int>
#define mli map<ll, int>
#define lc '\\n'
#define forit(it, v) for (auto it = v.begin(); it != v.end(); ++it)
#define fastIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
using namespace std;
struct raj {
  int l;
  int r;
  int id;
};
raj rj[500001];
bool comp(raj &p, raj &q) { return p.r < q.r; }
int N = 500001;
vector<int> bit(N, 0);
ll last[500001];
void update(int x, int i, int n) {
  for (; i <= n; i = i + (i & -i)) {
    bit[i] += x;
  }
}
int query(int i) {
  int sum = 0;
  for (; i > 0; i = i - (i & -i)) {
    sum += bit[i];
  }
  return sum;
}
void solve() {
  int n, k;
  cin >> n >> k;
  memset(last, -1, sizeof(last));
  vl a(n), ans(k);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  for (int i = 0; i < k; ++i) {
    cin >> rj[i].l >> rj[i].r;
    rj[i].id = i;
  }
  sort(rj, rj + k, comp);
  int p = 0;
  for (int i = 0; i < n; ++i) {
    if (last[a[i]] != -1) {
      update(-1, last[a[i]] + 1, n);
    }
    last[a[i]] = i;
    update(1, i + 1, n);
    while (p < k && rj[p].r == i + 1) {
      ans[rj[p].id] = query(rj[p].r) - query(rj[p].l - 1);
      p++;
    }
  }
  for (int i = 0; i < k; ++i) {
    cout << ans[i] << lc;
  }
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  fastIO;
  // sieve();
  int t = 1;
  // int t;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define mod 1000000007
#define maxn 200005
#define vi vector<int>
#define vl vector<ll>
#define mii map<int, int>
#define mli map<ll, int>
#define forit(it, v) for (auto it = v.begin(); it != v.end(); ++it)
#define fastIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
using namespace std;
void solve() {
  int n, k;
  cin >> n >> k;
  vi a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  for (int i = 0; i < k; ++i) {
    int x, y;
    cin >> x >> y;
    unordered_map<int, int> s;
    for (int i = x - 1; i <= y - 1; ++i) {
      s[a[i]]++;
      ;
    }
    cout << s.size() << "\\n";
  }
  // ll n,k;
  // cin>>n>>k;
  // priority_queue<float> q;
  // for (int i = 0; i < n; ++i)
  // {
  //     float x;
  //     cin>>x;
  //     q.push(x*1.0);
  // }
  // int i=0;
  // while(i<k)
  // {
  //     i++;
  //     float x=q.top();
  //     cout<<x<<" ";
  //     q.pop();
  //     q.push(x/2.0f);
  //     q.push(x-x/2.0f);
  // }
  // ll v=q.top();
  // cout<<v;
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  fastIO;
  // sieve();
  int t = 1;
  // int t;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}