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

#pragma optimization_level 3
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
#include <bits/stdc++.h>

#define for0(i, n) for (int i = 0; i < (n); i++)
#define for1(i, n) for (int i = 1; i <= (n); i++)
#define for2(i, n) for (ll i = 2; i <= (n); i++)
#define rep(i, st, en, cnt) for (ll i = st; i <= en; i += cnt)
#define PB push_back
#define MP make_pair
#define fastio                                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);                                                                 \\
  ios::sync_with_stdio(0)
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
#define bit(x, n) (x & (1 << (n)))
#define EPS 1e-8
#define INF 0x3f3f3f3f3
#define NEGINF -INF
#define TYPEMAX(type) std::numeric_limits<type>::max()
#define inp_arr(a, n)                                                          \\
  for0(i, n) { cin >> a[i]; }
#define out_arr(a, n)                                                          \\
  for0(i, n) { cout << a[i] << " "; }                                          \\
  cout << endl;
#define deb1(a) cout << (a) << endl;
#define deb2(a, b) cout << (a) << " " << (b) << endl;
#define deb3(a, b, c) cout << (a) << " " << (b) << " " << (c) << endl;

using namespace std;

typedef pair<int, int> ii;
typedef pair<long long, long long> pll;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef long long ll;
typedef vector<long long> vl;
typedef vector<vector<ll>> vvl;
typedef vector<pair<long long, long long>> vll;
typedef long double LD;

ii DR8[] = {{-1, 0}, {0, 1},   {1, 0}, {0, -1},
            {-1, 1}, {-1, -1}, {1, 1}, {1, -1}};
ii DR4[] = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}};

const ll mod = 1e9 + 7;

// ll nop = 2;
// vi primes(nop+1,1);

// void sieve(){
//     primes[1] = primes[0] = 0;
//     for(ll i = 2;i * i <= nop;i++){
//         if(primes[i]){
//             for(ll j = i*i; j <= nop;j+=i){
//                 primes[j] = 0;
//             }
//         }
//     }
// }

// ll phi(ll n) {
//     ll tot = n;
//     for(ll i = 2;i*i <= n;i++) {
//         if(n%i == 0) {
//             tot -= tot/i;
//         }
//         while(n%i == 0) {
//             n /= i;
//         }
//     }
//     if(n > 1) {
//         tot -= tot/n;
//     }
//     return tot;
// }

ll gcd(ll a, ll b) { // a larger, b smaller
  if (b > a)
    swap(b, a);
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

void extgcd(ll a, ll b, ll &x, ll &y) { // a smaller b larger
  if (a == 0) {
    x = a;
    y = b;
    return;
  }
  extgcd(b % a, a, x, y);
  ll y1 = y, x1 = x;
  x = y1 - (b / a) * x1;
  y = x1;
}

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

ll bexp(ll num, ll e) {
  ll ans = 1;
  while (e > 0) {
    if (e & 1) {
      ans *= num;
    }
    num *= num;
    e >>= 1;
  }
  return ans;
}

ll mexp(ll num, ll e) {
  ll ans = 1;
  while (e > 0) {
    if (e & 1) {
      ans = (ans * num) % mod;
    }
    num = (num * num) % mod;
    e >>= 1;
  }
  return ans % mod;
}

ll modinv(ll a) { return mexp(a, mod - 2); }

class FenwickTree {
private:
  vi ft;

public:
  FenwickTree(int n) { ft.assign(n + 1, 0); }

  int LSOne(int a) { return a & (-a); }

  int query(int b) {
    int sum = 0;
    for (; b > 0; b -= LSOne(b))
      sum += ft[b];
    return sum;
  }

  int query(int a, int b) { return query(b) - (a == 1 ? 0 : query(a - 1)); }

  void update(int k, int v) {
    for (; k < (int)ft.size(); k += LSOne(k))
      ft[k] += v;
  }
};

void solve() {
  int n, q;
  cin >> n >> q;
  vi a(n + 1);
  for1(i, n) cin >> a[i];
  vii qr(q);
  for0(i, q) cin >> qr[i].F >> qr[i].S;
  vi p(q);
  for0(i, q) p[i] = i;
  sort(all(p), [&](int x, int y) { return (qr[x].S < qr[y].S); });
  vi last_ind(n + 1, -1);
  FenwickTree ft(n);
  int j = 0;
  vi ans(q);
  for0(i, q) {
    int ind = p[i];
    while (j + 1 <= qr[ind].S) {
      j++;
      if (last_ind[a[j]] != -1) {
        ft.update(last_ind[a[j]], -1);
      }
      last_ind[a[j]] = j;
      ft.update(j, 1);
    }
    ans[ind] = ft.query(qr[ind].F, qr[ind].S);
  }
  for0(i, q) deb1(ans[i])
}

int main() {
  fastio;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int t = 1;
  // cin>>t;
  for1(T, t) {
    // cout<<"Case #"<<T<<": ";
    solve();
  }
  /* #ifndef ONLINE_JUDGE
  cout<<"\\nTime Elapsed: " << 1.0*clock() / CLOCKS_PER_SEC << " sec\\n";
  #endif */
  return 0;
}


Error: TLE

Fixed submission:

#pragma optimization_level 3
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
#include <bits/stdc++.h>

#define for0(i, n) for (int i = 0; i < (n); i++)
#define for1(i, n) for (int i = 1; i <= (n); i++)
#define for2(i, n) for (ll i = 2; i <= (n); i++)
#define rep(i, st, en, cnt) for (ll i = st; i <= en; i += cnt)
#define PB push_back
#define MP make_pair
#define fastio                                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);                                                                 \\
  ios::sync_with_stdio(0)
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
#define bit(x, n) (x & (1 << (n)))
#define EPS 1e-8
#define INF 0x3f3f3f3f3
#define NEGINF -INF
#define TYPEMAX(type) std::numeric_limits<type>::max()
#define inp_arr(a, n)                                                          \\
  for0(i, n) { cin >> a[i]; }
#define out_arr(a, n)                                                          \\
  for0(i, n) { cout << a[i] << " "; }                                          \\
  cout << endl;
#define deb1(a) cout << (a) << endl;
#define deb2(a, b) cout << (a) << " " << (b) << endl;
#define deb3(a, b, c) cout << (a) << " " << (b) << " " << (c) << endl;

using namespace std;

typedef pair<int, int> ii;
typedef pair<long long, long long> pll;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef long long ll;
typedef vector<long long> vl;
typedef vector<vector<ll>> vvl;
typedef vector<pair<long long, long long>> vll;
typedef long double LD;

ii DR8[] = {{-1, 0}, {0, 1},   {1, 0}, {0, -1},
            {-1, 1}, {-1, -1}, {1, 1}, {1, -1}};
ii DR4[] = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}};

const ll mod = 1e9 + 7;

// ll nop = 2;
// vi primes(nop+1,1);

// void sieve(){
//     primes[1] = primes[0] = 0;
//     for(ll i = 2;i * i <= nop;i++){
//         if(primes[i]){
//             for(ll j = i*i; j <= nop;j+=i){
//                 primes[j] = 0;
//             }
//         }
//     }
// }

// ll phi(ll n) {
//     ll tot = n;
//     for(ll i = 2;i*i <= n;i++) {
//         if(n%i == 0) {
//             tot -= tot/i;
//         }
//         while(n%i == 0) {
//             n /= i;
//         }
//     }
//     if(n > 1) {
//         tot -= tot/n;
//     }
//     return tot;
// }

ll gcd(ll a, ll b) { // a larger, b smaller
  if (b > a)
    swap(b, a);
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

void extgcd(ll a, ll b, ll &x, ll &y) { // a smaller b larger
  if (a == 0) {
    x = a;
    y = b;
    return;
  }
  extgcd(b % a, a, x, y);
  ll y1 = y, x1 = x;
  x = y1 - (b / a) * x1;
  y = x1;
}

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

ll bexp(ll num, ll e) {
  ll ans = 1;
  while (e > 0) {
    if (e & 1) {
      ans *= num;
    }
    num *= num;
    e >>= 1;
  }
  return ans;
}

ll mexp(ll num, ll e) {
  ll ans = 1;
  while (e > 0) {
    if (e & 1) {
      ans = (ans * num) % mod;
    }
    num = (num * num) % mod;
    e >>= 1;
  }
  return ans % mod;
}

ll modinv(ll a) { return mexp(a, mod - 2); }

class SegmentTree {
private:
  vi A;
  vector<set<int>> st;
  int n;

  int left(int p) { return p << 1; }
  int right(int p) { return (p << 1) + 1; }

  void build(int p, int L, int R) {
    if (L == R)
      st[p].insert(A[L]);
    else {
      build(left(p), L, (L + R) / 2);
      build(right(p), (L + R) / 2 + 1, R);
      st[p].insert(all(st[left(p)]));
      st[p].insert(all(st[right(p)]));
    }
  }

  set<int> rmq(int p, int L, int R, int i, int j) {
    if (i > j)
      return set<int>();
    if (L == i && R == j)
      return st[p];
    int mid = (L + R) / 2;
    set<int> res, p1 = rmq(left(p), L, mid, i, min(j, mid)),
                  p2 = rmq(right(p), mid + 1, R, max(i, mid + 1), j);
    res.insert(all(p1));
    res.insert(all(p2));
    return res;
  }

public:
  SegmentTree(const vi &_A) {
    A = _A;
    n = (int)A.size();
    st.resize(4 * n);
    build(1, 0, n - 1);
  }

  int rmq(int i, int j) { return rmq(1, 0, n - 1, i, j).size(); }
};

void solve() {
  int n, q;
  cin >> n >> q;
  vi a(n);
  inp_arr(a, n);
  SegmentTree st(a);
  for0(i, q) {
    int l, r;
    cin >> l >> r;
    l--, r--;
    deb1(st.rmq(l, r))
  }
}

int main() {
  fastio;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int t = 1;
  // cin>>t;
  for1(T, t) {
    // cout<<"Case #"<<T<<": ";
    solve();
  }
  /* #ifndef ONLINE_JUDGE
  cout<<"\\nTime Elapsed: " << 1.0*clock() / CLOCKS_PER_SEC << " sec\\n";
  #endif */
  return 0;
}