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

/*!enjoy karo yaar!*/

// CF,CC,AtC,SPOJ: hp1999
// HE: hemant269
// HR: hemant2132

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

/*-----<Defines>-----*/
// #define     int             long long int
#define fast()                                                                 \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define all(x) begin(x), end(x)
#define rz(x) resize(x)
#define asn(x, y) assign(x, y)
#define mem(a, b) memset(a, b, sizeof(a))
#define sz(x) ((int)size(x))
#define eb emplace_back
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define ins insert
#define vi vector<int>
#define pii pair<int, int>
#define mii map<int, int>
#define F first
#define S second
#define remax(a, b) a = max(a, b)
#define remin(a, b) a = min(a, b)
#define bitcount(x) __builtin_popcountll(x)
#define iceil(n, x) (((n)-1) / (x) + 1)
#define flush fflush(stdout)

using ull = unsigned long long;
using ll = long long;
using ld = long double;
/*-----</Defines>-----*/

/*-----<Debugging Stuff>-----*/
string to_string(char s) {
  string res = "'''";
  res[1] = s;
  return res;
}

string to_string(string s) { return '"' + s + '"'; }

string to_string(const char *s) { return to_string((string)s); }

string to_string(bool b) { return (b ? "true" : "false"); }

template <typename A, typename B> string to_string(pair<A, B> p) {
  return "(" + to_string(p.F) + ", " + to_string(p.S) + ")";
}

template <typename A> string to_string(A v) {
  bool first = 1;
  string res = "{";
  for (const auto &x : v) {
    if (!first)
      res += ", ";
    first = 0;
    res += to_string(x);
  }
  res += "}";
  return res;
}

void debug() { cout << "\\n"; }
template <typename Head, typename... Tail> void debug(Head H, Tail... T) {
  cout << " " << to_string(H);
  debug(T...);
}

#define show(...) cout << "[" << #__VA_ARGS__ << "]:", debug(__VA_ARGS__)
/*-----</Debugging Stuff>-----*/

/*-----<Mod Operations>-----*/
const int M = 1e9 + 7;
inline int add(int a, int b, int p = M) {
  a = a + b;
  if (a >= p)
    a -= p;
  return a;
}
inline int sub(int a, int b, int p = M) {
  a = a - b;
  if (a < 0)
    a += p;
  return a;
}
inline int mul(int a, int b, int p = M) {
  a = (a * 1ll * b) % p;
  return a;
}
/*-----</Mod Operations>-----*/

/*-----<Constants>-----*/
const ld pi = acos(-1), eps = 1e-9;
const ll inf = 1e18;
const int N = 5e5 + 5;
/*-----</Constants>-----*/

vi v(N);

int freq[N];
int abhi = 0;

inline void rem(int idx) {
  if (freq[v[idx]] == 1)
    --abhi;

  --freq[v[idx]];
}

inline void add(int idx) {
  if (freq[v[idx]] == 0)
    ++abhi;

  ++freq[v[idx]];
}

const int SZ = 800; // block size

struct Query {
  int l, r, idx;

  bool operator<(Query other) const {
    return make_pair(l / SZ, r) < make_pair(other.l / SZ, other.r);
  }
};

vector<int> mo(vector<Query> queries) {
  vector<int> answers(queries.size());

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

  int cur_l = 0;
  int cur_r = -1;

  for (Query q : queries) {
    while (cur_l > q.l) {
      cur_l--;
      add(cur_l);
    }

    while (cur_r < q.r) {
      cur_r++;
      add(cur_r);
    }

    while (cur_l < q.l) {
      rem(cur_l);
      cur_l++;
    }

    while (cur_r > q.r) {
      rem(cur_r);
      cur_r--;
    }

    answers[q.idx] = abhi;
  }

  return answers;
}

void solve() {
  mem(freq, 0);

  int n, q;
  cin >> n >> q;

  for (int i = 0; i < n; ++i)
    cin >> v[i];

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

  vi ans = mo(qu);
  for (auto c : ans)
    cout << c << "\\n";
}

int32_t main() {
  fast();

  int t = 1;
  // cin>>t;
  for (int z = 1; z <= t; ++z) {
    solve();
    // cout<<"\\n";
  }

  return 0;
}


Error: TLE

Fixed submission:

/*!enjoy karo yaar!*/

// CF,CC,AtC,SPOJ: hp1999
// HE: hemant269
// HR: hemant2132

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

/*-----<Defines>-----*/
// #define     int             long long int
#define fast()                                                                 \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define all(x) begin(x), end(x)
#define rz(x) resize(x)
#define asn(x, y) assign(x, y)
#define mem(a, b) memset(a, b, sizeof(a))
#define sz(x) ((int)size(x))
#define eb emplace_back
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define ins insert
#define vi vector<int>
#define pii pair<int, int>
#define mii map<int, int>
#define F first
#define S second
#define remax(a, b) a = max(a, b)
#define remin(a, b) a = min(a, b)
#define bitcount(x) __builtin_popcountll(x)
#define iceil(n, x) (((n)-1) / (x) + 1)
#define flush fflush(stdout)

using ull = unsigned long long;
using ll = long long;
using ld = long double;
/*-----</Defines>-----*/

/*-----<Debugging Stuff>-----*/
string to_string(char s) {
  string res = "'''";
  res[1] = s;
  return res;
}

string to_string(string s) { return '"' + s + '"'; }

string to_string(const char *s) { return to_string((string)s); }

string to_string(bool b) { return (b ? "true" : "false"); }

template <typename A, typename B> string to_string(pair<A, B> p) {
  return "(" + to_string(p.F) + ", " + to_string(p.S) + ")";
}

template <typename A> string to_string(A v) {
  bool first = 1;
  string res = "{";
  for (const auto &x : v) {
    if (!first)
      res += ", ";
    first = 0;
    res += to_string(x);
  }
  res += "}";
  return res;
}

void debug() { cout << "\\n"; }
template <typename Head, typename... Tail> void debug(Head H, Tail... T) {
  cout << " " << to_string(H);
  debug(T...);
}

#define show(...) cout << "[" << #__VA_ARGS__ << "]:", debug(__VA_ARGS__)
/*-----</Debugging Stuff>-----*/

/*-----<Mod Operations>-----*/
const int M = 1e9 + 7;
inline int add(int a, int b, int p = M) {
  a = a + b;
  if (a >= p)
    a -= p;
  return a;
}
inline int sub(int a, int b, int p = M) {
  a = a - b;
  if (a < 0)
    a += p;
  return a;
}
inline int mul(int a, int b, int p = M) {
  a = (a * 1ll * b) % p;
  return a;
}
/*-----</Mod Operations>-----*/

/*-----<Constants>-----*/
const ld pi = acos(-1), eps = 1e-9;
const ll inf = 1e18;
const int N = 5e5 + 5;
/*-----</Constants>-----*/

vi v(N);

int freq[N];
int abhi = 0;

inline void rem(int idx) {
  if (freq[v[idx]] == 1)
    --abhi;

  --freq[v[idx]];
}

inline void add(int idx) {
  if (freq[v[idx]] == 0)
    ++abhi;

  ++freq[v[idx]];
}

const int SZ = 800; // block size

// ref: https://codeforces.com/blog/entry/61203
inline int64_t gilbertOrder(int x, int y, int pow, int rotate) {
  if (pow == 0) {
    return 0;
  }
  int hpow = 1 << (pow - 1);
  int seg = (x < hpow) ? ((y < hpow) ? 0 : 3) : ((y < hpow) ? 1 : 2);
  seg = (seg + rotate) & 3;
  const int rotateDelta[4] = {3, 0, 0, 1};
  int nx = x & (x ^ hpow), ny = y & (y ^ hpow);
  int nrot = (rotate + rotateDelta[seg]) & 3;
  int64_t subSquareSize = int64_t(1) << (2 * pow - 2);
  int64_t ans = seg * subSquareSize;
  int64_t add = gilbertOrder(nx, ny, pow - 1, nrot);
  ans += (seg == 1 || seg == 2) ? add : (subSquareSize - add - 1);
  return ans;
}

struct Query {
  int l, r, idx;
  int64_t ord;

  inline void calcOrder() { ord = gilbertOrder(l, r, 21, 0); }
};

inline bool operator<(const Query &a, const Query &b) { return a.ord < b.ord; }

vector<int> mo(vector<Query> queries) {
  vector<int> answers(queries.size());

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

  int cur_l = 0;
  int cur_r = -1;

  for (Query q : queries) {
    while (cur_l > q.l) {
      cur_l--;
      add(cur_l);
    }

    while (cur_r < q.r) {
      cur_r++;
      add(cur_r);
    }

    while (cur_l < q.l) {
      rem(cur_l);
      cur_l++;
    }

    while (cur_r > q.r) {
      rem(cur_r);
      cur_r--;
    }

    answers[q.idx] = abhi;
  }

  return answers;
}

void solve() {
  mem(freq, 0);

  int n, q;
  cin >> n >> q;

  for (int i = 0; i < n; ++i)
    cin >> v[i];

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

  vi ans = mo(qu);
  for (auto c : ans)
    cout << c << "\\n";
}

int32_t main() {
  fast();

  int t = 1;
  // cin>>t;
  for (int z = 1; z <= t; ++z) {
    solve();
    // cout<<"\\n";
  }

  return 0;
}