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

#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define x first
#define y second
#define pii pair<int, int>
#define pb push_back
#define eb emplace_back

using namespace std;
using namespace __gnu_pbds;

typedef long long int ll;
typedef unsigned long long int ull;
int MOD = 1000000007;
int MOD2 = 998244353;
vector<int> bases;
const ll LLINF = 1ll << 60;
const char en = '\\n';

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
void yes() {
  cout << "YES" << en;
  exit(0);
}
void no() {
  cout << "NO" << en;
  exit(0);
}
inline int rund() {
  int x576363482791fuweh = rng();
  return abs(x576363482791fuweh) % RAND_MAX;
}
template <class T> void prVec(vector<T> w) {
  cout << w.size() << endl;
  for (int i = 0; i < int(w.size()) - 1; ++i)
    cout << w[i] << ' ';
  if (w.size())
    cout << w[w.size() - 1] << endl;
}

void M998() { swap(MOD, MOD2); }

ll raand() {
  ll a = rund();
  a *= RAND_MAX;
  a += rund();
  return a;
}

#define rand raand

ll raaand() { return raand() * (MOD - 7) + raand(); }

string to_upper(string a) {
  for (int i = 0; i < (int)a.size(); ++i)
    if (a[i] >= 'a' && a[i] <= 'z')
      a[i] -= 'a' - 'A';
  return a;
}

string to_lower(string a) {
  for (int i = 0; i < (int)a.size(); ++i)
    if (a[i] >= 'A' && a[i] <= 'Z')
      a[i] += 'a' - 'A';
  return a;
}

ll sti(string a) {
  ll k = 0;
  for (int i = 0; i < (int)a.size(); ++i) {
    k *= 10;
    k += a[i] - '0';
  }
  return k;
}

string its(ll k) {
  if (k == 0)
    return "0";
  string a;
  while (k) {
    a.push_back((k % 10) + '0');
    k /= 10;
  }
  reverse(a.begin(), a.end());
  return a;
}

ll min(ll a, int b) {
  if (a < b)
    return a;
  return b;
}

ll min(int a, ll b) {
  if (a < b)
    return a;
  return b;
}

ll max(ll a, int b) {
  if (a > b)
    return a;
  return b;
}

ll max(int a, ll b) {
  if (a > b)
    return a;
  return b;
}

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

template <class T, class K> pair<T, K> mp(T a, K b) { return make_pair(a, b); }

inline int mult(ll a, ll b) { return (a * b) % MOD; }

inline int pot(int n, int k) {
  if (k == 0)
    return 1;
  ll a = pot(n, k / 2);
  a = mult(a, a);
  if (k % 2)
    return mult(a, n);
  else
    return a;
}

int divide(int a, int b) { return mult(a, pot(b, MOD - 2)); }

inline int sub(int a, int b) {
  if (a - b >= 0)
    return a - b;
  return a - b + MOD;
}

inline int add(int a, int b) {
  if (a + b >= MOD)
    return a + b - MOD;
  return a + b;
}

bool prime(ll a) {
  if (a == 1)
    return 0;
  for (int i = 2; i <= round(sqrt(a)); ++i) {
    if (a % i == 0)
      return 0;
  }
  return 1;
}

ll has(string x) {
  ll h1 = 0, h2 = 0;
  x = to_lower(x);
  for (char a : x) {
    h1 *= bases[0];
    h1 += a - 'a';
    h1 %= bases[3];
    h2 *= bases[1];
    h2 += a - 'a';
    h2 %= bases[4];
  }
  return h1 * (MOD + 13893829) + h2;
}

struct oph {
  static uint64_t splitmix64(uint64_t x) {
    // http://xorshift.di.unimi.it/splitmix64.c
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }

  size_t operator()(uint64_t x) const {
    static const uint64_t FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
};

const int N = 500010, M = 1000;
int n, h[N], q, la[N], fen[N], ans[N];
vector<int> s;
gp_hash_table<int, int, oph> g;
vector<pii> x[N];
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};

void upd(int i) {
  for (; i <= n; i += i & -i)
    ++fen[i];
}

int que(int i) {
  int s = 0;
  for (; i; i -= i & -i)
    s += fen[i];
  return s;
}

#define gc getchar_unlocked

int read() {
  char c = gc();
  // cout<<int(c)<<endl;
  int zz = 0;
  while (c == 0 || c == -1 || c == ' ' || c == '\\n' || c == '\\r') {
    c = gc();
    //++zz;
  }
  int t = 0, z = 0;
  while (c != ' ' && c != '\\n' && c != -1 && c && c != '\\r') {
    t = t * 10 + c - '0';
    //++z;
    c = gc();
  }
  return t;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  for (int i = 0; i < 10; ++i)
    bases.push_back(rand() % (MOD - 13893829) + 13893829);
  const bool rea = 1;
  if (rea)
    n = read(), q = read();
  else
    n = q = N - 10;
  for (int i = 0; i < n; ++i) {
    if (rea)
      h[i] = read();
    else
      h[i] = rand() % M + 1;
    s.pb(h[i]);
  }
  int i = 0;
  for (auto a : s)
    g[a] = i++;
  for (int i = 0; i < n; ++i)
    h[i] = g[h[i]];
  for (int i = 0; i < n; ++i) {
    int a = h[i];
    h[i] = la[a] + 1;
    la[a] = i + 1;
    // cout<<h[i]<<' '<<a<<en;
  }
  // exit(0);
  for (int i = 0; i < q; ++i) {
    int a, b;
    if (rea)
      a = read(), b = read();
    else
      b = rand() % n + 1, a = rand() % b + 1;
    x[b - 1].pb({i, a});
  }
  // exit(0);
  for (int i = 0; i < n; ++i) {
    upd(h[i]);
    for (auto w : x[i]) {
      ans[w.x] = que(w.y) - w.y + 1;
    }
  }
  ll S = 0;
  for (int i = 0; i < q; ++i)
    if (rea)
      cout << ans[i] << en;
    else
      S += ans[i];
  if (!rea)
    cout << S;
}

Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

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

using namespace std;
template <class T>
using ordered_set =
    __gnu_pbds::tree<T, __gnu_pbds::null_type, less<T>, __gnu_pbds::rb_tree_tag,
                     __gnu_pbds::tree_order_statistics_node_update>;
template <class T>
using ordered_multiset =
    __gnu_pbds::tree<T, __gnu_pbds::null_type, less_equal<T>,
                     __gnu_pbds::rb_tree_tag,
                     __gnu_pbds::tree_order_statistics_node_update>;

#define PI atan2(0, -1)
#define EPS 1e-9
#define INF 5e18
#define MOD 1000000007

#define all(x) begin(x), end(x)
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound

int N, Q, id = 1, blockSize = 700, li = 0, ri = 0, total = 0, arr[500010],
          cnt[500010], ret[500010];
pair<pair<int, int>, int> queries[500010];
map<int, int> remap;

bool cmp(pair<pair<int, int>, int> a, pair<pair<int, int>, int> b) {
  if (a.f.f / blockSize != b.f.f / blockSize)
    return a.f.f / blockSize < b.f.f / blockSize;
  return a.f.s < b.f.s;
}

void add(int pos) {
  cnt[arr[pos]]++;
  if (cnt[arr[pos]] == 1)
    total++;
}

void subtract(int pos) {
  cnt[arr[pos]]--;
  if (cnt[arr[pos]] == 0)
    total--;
}

int main() {
  // freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout << fixed << setprecision(10);
  cin >> N >> Q;
  for (int i = 0; i < N; i++) {
    cin >> arr[i];
    if (remap.find(arr[i]) == remap.end())
      remap[arr[i]] = id++;
    arr[i] = remap[arr[i]];
  }
  for (int i = 0; i < Q; i++) {
    cin >> queries[i].f.f >> queries[i].f.s;
    queries[i].f.f--;
    queries[i].f.s--;
    queries[i].s = i;
  }
  sort(queries, queries + Q, cmp);
  for (int i = 0; i < Q; i++) {
    int L = queries[i].f.f, R = queries[i].f.s;
    while (li < L)
      subtract(li++);
    while (li > L)
      add(--li);
    while (ri <= R)
      add(ri++);
    while (ri > R + 1)
      subtract(--ri);
    ret[queries[i].s] = total;
  }
  for (int i = 0; i < Q; i++)
    cout << ret[i] << '\\n';
  return 0;
}

/******************************
Kateba ii dake no hanashi darou
Success is only a victory away
- No Game No Life Opening
******************************/