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

#include <bits/stdc++.h>
using namespace std;

const bool ready = []() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(12);
  return true;
}();

const double PI = acos(-1);
using ll = long long;
#define int ll
#define all(v) (v).begin(), (v).end()
#define fori(n) for (int i = 0; i < int(n); i++)

#define cini(i)                                                                \\
  int i;                                                                       \\
  cin >> i;
#define cins(s)                                                                \\
  string s;                                                                    \\
  cin >> s;
#define cind(d)                                                                \\
  double d;                                                                    \\
  cin >> d;
#define cinai(a, n)                                                            \\
  vi a(n);                                                                     \\
  fori(n) { cin >> a[i]; }
#define cinas(s, n)                                                            \\
  vs s(n);                                                                     \\
  fori(n) { cin >> s[i]; }
#define cinad(a, n)                                                            \\
  vd a(n);                                                                     \\
  fori(n) { cin >> a[i]; }

using pii = pair<int, int>;
using pdd = pair<double, double>;
using vd = vector<double>;
using vb = vector<bool>;
using vi = vector<int>;
using vvi = vector<vi>;
using vs = vector<string>;

/** stolen from tourist, Problem D,
 * https://codeforces.com/contest/1208/standings/page/1 */
template <typename T> struct fenwick {
  vector<T> fenw;
  int n;

  fenwick(int _n) : n(_n) { fenw.resize(n); }

  /* update add */
  void update(int x, T v) {
    while (x < n) {
      fenw[x] += v;
      x |= (x + 1);
    }
  }

  /* get sum of range (0,x), including x */
  T query(int x) {
    T v{};
    while (x >= 0) {
      v += fenw[x];
      x = (x & (x + 1)) - 1;
    }
    return v;
  }
};

struct Query {
  int l, r, idx;

  bool operator<(Query other) const { return r < other.r; }
};

/*
 */
void solve() {
  cini(n);
  cini(q);

  cinai(aa, n);

  vector<Query> quer(q);
  for (int i = 0; i < q; i++) {
    cin >> quer[i].l >> quer[i].r;
    quer[i].l--;
    quer[i].r--;
    quer[i].idx = i;
  }

  sort(all(quer));

  fenwick<int> fen(n);

  map<int, int> pos; /* pos[a]= previous position of a */
  int r = 0;
  int cnt = 0;
  vi ans(q);
  for (int i = 0; i < q; i++) {
    while (r <= quer[i].r) {
      auto it = pos.find(aa[r]);
      if (it != pos.end()) {
        fen.update(it->second, -1);
        it->second = r;
      } else {
        cnt++;
      }
      pos[aa[r]] = r;
      fen.update(r, 1);
      r++;
    }

    ans[quer[i].idx] = cnt - fen.query(quer[i].l - 1);
  }

  for (int i = 0; i < q; i++)
    cout << ans[i] << endl;
}

signed main() { solve(); }


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
#define ll long long
#define ull unsigned long long
// #define ordered_set tree<pair<ll, ll>, null_type, less<pair<ll, ll>>,
// rb_tree_tag, tree_order_statistics_node_update>
ll mod = 1e9 + 7;
#define PI 3.1415926535897932385
#define inf 9e18
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
string char_to_str(char c) {
  string tem(1, c);
  return tem;
}
typedef pair<long long, long long> ii;
#define S second
#define F first
ll max(ll a, ll b) {
  if (a > b) {
    return a;
  }
  return b;
}
ll min(ll a, ll b) {
  if (a < b) {
    return a;
  }
  return b;
}
#define MAXN 200005
// Comment this out for interactice problem
// #define endl '\\n'
// string to integer stoi() Remember: it takes string not character
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();

// To compile--> g++ -std=c++0x -o output one.cpp
// To run--> ./output
const int rootN = 500;

int freq[500005];
int distinct;
int ar[500005];

struct query {
  int L;
  int R;
  int q_no;
  int block_no;
  bool operator<(query &q2) {
    if (block_no < q2.block_no)
      return 1;
    else if (block_no > q2.block_no)
      return 0;
    else
      return R < q2.R;
  }
};

void Add(int &elem) {
  if (!freq[elem])
    distinct++;
  freq[elem]++;
}
void Remove(int &elem) {
  freq[elem]--;
  if (!freq[elem])
    distinct--;
}

void adjust(int &curr_l, int &curr_r, query &q) {
  while (curr_l > q.L) {
    curr_l--;
    Add(ar[curr_l]);
  }
  while (curr_r < q.R) {
    curr_r++;
    Add(ar[curr_r]);
  }
  while (curr_l < q.L) {
    Remove(ar[curr_l]);
    curr_l++;
  }
  while (curr_r > q.R) {
    Remove(ar[curr_r]);
    curr_r--;
  }
}

void solve(vector<query> &queries) {
  vector<int> answer(queries.size());
  sort(queries.begin(), queries.end());
  memset(freq, 0, sizeof freq);
  distinct = 1;

  int curr_l = queries[0].L;
  int curr_r = queries[0].L;

  freq[ar[curr_l]]++;

  for (query &qr : queries) {
    adjust(curr_l, curr_r, qr);
    answer[qr.q_no] = distinct;
  }

  for (int x : answer)
    cout << x << '\\n';
}

int main() {
  fastio;
  int n, q;
  cin >> n >> q;

  map<int, int> coordinateCompress;
  int compressed_Num = 1;

  for (int i = 0; i < n; i++) {
    cin >> ar[i];
    if (coordinateCompress.find(ar[i]) != coordinateCompress.end()) {
      ar[i] = coordinateCompress[ar[i]];
    } else {
      coordinateCompress[ar[i]] = compressed_Num;
      ar[i] = compressed_Num++;
    }
  }

  vector<query> queries(q);
  for (int i = 0; i < q; i++) {
    int u, v;
    cin >> u >> v;
    queries[i].L = u - 1;
    queries[i].R = v - 1;
    queries[i].q_no = i;
    queries[i].block_no = u / rootN;
  }
  solve(queries);
  return 0;
}