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

#pragma GCC target("sse4.2")
#include <bits/stdc++.h>

using namespace std;

#define F first
#define S second
#define PB push_back
#define ln "\\n"
#define forn(i, e) for (ll i = 0; i < e; i++)
#define forsn(i, s, e) for (ll i = s; i < e; i++)
#define rforn(i, e) for (ll i = e; i >= 0; i--)
#define rforsn(i, e, s) for (ll i = e; i >= s; i--)
#define vasort(v) sort(v.begin(), v.end());
#define vdsort(v) sort(v.begin(), v.end(), greater<ll>());
#define fio                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define mem(a, b) memset(a, b, sizeof(a))
#define sz(x) ((int)size(x))
#define EB emplace_back
#define flush fflush(stdout)
#define out1(x1) cout << x1 << ln
#define out2(x1, x2) cout << x1 << " " << x2 << endl
#define out3(x1, x2, x3) cout << x1 << " " << x2 << " " << x3 << ln
#define out4(x1, x2, x3, x4)                                                   \\
  cout << x1 << " " << x2 << " " << x3 << " " << x4 << ln
#define all(d) d.begin(), d.end()
#define unordered_set(T) unordered_set<T, custom_hash>
#define unordered_map(T1, T2) unordered_map<T1, T2, custom_hash>

#define in1(x1) cin >> x1
#define in2(x1, x2) cin >> x1 >> x2
#define in3(x1, x2, x3) cin >> x1 >> x2 >> x3
#define in4(x1, x2, x3, x4) cin >> x1 >> x2 >> x3 >> x4

#define arrin(a, n) forn(i, n) cin >> a[i];
#define arrout(a, n)                                                           \\
  forn(i, n) { cout << a[i] << " "; }                                          \\
  cout << ln;

/* ---------------- IO ----------------*/

namespace IO {
const int BUFFER_SIZE = 1 << 15;
char input_buffer[BUFFER_SIZE];
int input_pos = 0, input_len = 0;
char output_buffer[BUFFER_SIZE];
int output_pos = 0;
char number_buffer[100];
uint8_t lookup[100];
void _update_input_buffer() {
  input_len = fread(input_buffer, sizeof(char), BUFFER_SIZE, stdin);
  input_pos = 0;
  if (input_len == 0)
    input_buffer[0] = EOF;
}
inline char next_char(bool advance = true) {
  if (input_pos >= input_len)
    _update_input_buffer();

  return input_buffer[advance ? input_pos++ : input_pos];
}

template <typename T> inline void read_int(T &number) {
  bool negative = false;
  number = 0;

  while (!isdigit(next_char(false)))
    if (next_char() == '-')
      negative = true;

  do {
    number = 10 * number + (next_char() - '0');
  } while (isdigit(next_char(false)));

  if (negative)
    number = -number;
}

template <typename T, typename... Args>
inline void read_int(T &number, Args &...args) {
  read_int(number);
  read_int(args...);
}

void _flush_output() {
  fwrite(output_buffer, sizeof(char), output_pos, stdout);
  output_pos = 0;
}

inline void write_char(char c) {
  if (output_pos == BUFFER_SIZE)
    _flush_output();

  output_buffer[output_pos++] = c;
}

template <typename T> inline void write_int(T number, char after = '\\0') {
  if (number < 0) {
    write_char('-');
    number = -number;
  }
  int length = 0;
  while (number >= 10) {
    uint8_t lookup_value = lookup[number % 100];
    number /= 100;
    number_buffer[length++] = (lookup_value & 15) + '0';
    number_buffer[length++] = (lookup_value >> 4) + '0';
  }
  if (number != 0 || length == 0)
    write_char(number + '0');
  for (int i = length - 1; i >= 0; i--)
    write_char(number_buffer[i]);
  if (after)
    write_char(after);
}
void IOinit() {
  // Make sure _flush_output() is called at the end of the program.
  bool exit_success = atexit(_flush_output) == 0;
  assert(exit_success);
  for (int i = 0; i < 100; i++)
    lookup[i] = (i / 10 << 4) + i % 10;
}
} // namespace IO

using namespace IO;

/* ---------------- typedefs ----------------*/
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

/* ---------------- utility functions ---------------- */

int dr[] = {0, 1, -1, 0, 1, -1, 1, -1};
int dc[] = {1, 0, 0, -1, 1, -1, -1, 1};
bool chk(int a, int b, int n, int m) {
  return 0 <= a && a < n && 0 <= b && b < m;
}

ll exp(ll n, ll m, ll md) {
  ll a = 1;
  while (m > 0) {
    if (m & 1)
      a = (a * n % md) % md;
    m /= 2;
    n = (n * n) % md;
  }
  return a % md;
}

void disp(vi v) {
  for (auto u : v)
    cout << u << " ";
  cout << "\\n";
}
void disp(vll v) {
  for (auto u : v)
    cout << u << " ";
  cout << "\\n";
}

int getSqr(int x) {
  if (x < 0)
    return -1;
  int l = sqrt(x);
  for (int i = -2; i <= 2; ++i)
    if (l + i >= 0 && (l + i) * (l + i) == x)
      return l;
  return -1;
}

const int NPRR = 1e7;
vi lp;
vector<int> prr;
void prfac(int n = NPRR) {
  assert(n <= NPRR);
  lp.assign(n + 1, 0);
  int i;
  for (i = 2; i <= n; ++i) {
    if (lp[i] == 0) {
      lp[i] = i;
      prr.push_back(i);
    }
    for (int u : prr) {
      if (i * u > n || u > lp[i])
        break;
      lp[i * u] = u;
    }
  }
}

bool check_composite_for_prime_function(ll n, ll a, ll d, int s) {
  ll x = exp(a, d, n);
  if (x == 1 || x == n - 1)
    return false;
  for (int r = 1; r < s; ++r) {
    x = x * x % n;
    x %= n;
    if (x == n - 1)
      return false;
  }
  return true;
}
bool prime(ll n) {
  if (n < 4)
    return n == 2 || n == 3;
  if (!(n & 1))
    return false;
  int s = 0;
  ll d = n - 1;
  while (!(d & 1)) {
    d >>= 1;
    s++;
  }
  for (int a : {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37}) {
    if (n == a)
      return true;
    if (check_composite_for_prime_function(n, a, d, s))
      return false;
  }
  return true;
}

ll binomialCoeff(ll n, ll k) {
  ll res = 1;

  // Since C(n, k) = C(n, n-k)
  if (k > n - k)
    k = n - k;

  // Calculate value of
  // [n * (n-1) *---* (n-k+1)] / [k * (k-1) *----* 1]
  for (ll i = 0; i < k; ++i) {
    res *= (n - i);
    res /= (i + 1);
  }

  return res;
}
/* custom hash for unordered map */
struct custom_hash {
  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);
  }
};

inline void read(int &x) { scanf("%d", &x); }

/* ---------------- constants ---------------- */
const ll MAXn = 5e5 + 33, MAXlg = __lg(MAXn) + 2;
const ll MOD = 1000000007;
const ll INF = ll(1e15);

/* ---------------- start of code ---------------- */

template <typename T> class fenwick {
public:
  vector<T> fenw;
  vi a;
  int n;

  fenwick(int _n) : n(_n) {
    fenw.assign(n + 1, 0);
    a.assign(n + 1, 0);
  }

  void modify(int x, T v) {
    while (x <= n) {
      fenw[x] += v;
      x += (x & -x);
    }
  }

  T get(int x) {
    T v{};
    while (x > 0) {
      v += fenw[x];
      x -= (x & -x);
    }
    return v;
  }

  /*
   * custom set
   * modify(p, 1) to add p; p -> [1, n]
   * ithpos(POS) to get POSth order statistics (POSth smallest element)
   */
  int ithpos(int i) {
    int low = 0, high = n - 1;
    while (low < high) {
      int mid = (low + high) >> 1;
      if (this->get(mid) >= i + 1) {
        high = mid;
      } else {
        low = mid + 1;
      }
    }
    return low;
  }
};

void go1() {

  int n, q;
  in2(n, q);
  int c[n + 1];
  forn(i, n) { cin >> c[i + 1]; }
  fenwick<int> f(n);
  vector<vi> ql(n + 1), qid(n + 1);
  vi ans(q);
  forn(i, q) {
    int l, r;
    in2(l, r);
    ql[r].PB(l);
    qid[r].PB(i);
  }
  int a[n + 1];
  forn(i, n + 1) a[i] = 0;
  forsn(i, 1, n + 1) {
    if (a[c[i]] > 0)
      f.modify(a[c[i]], -1);
    a[c[i]] = i;
    f.modify(a[c[i]], 1);
    forn(j, ql[i].size()) { ans[qid[i][j]] = f.get(i) - f.get(ql[i][j] - 1); }
  }
  for (auto u : ans)
    cout << u << ln;
}

int main() {
  fio;
  // cout<<fixed<<setprecision(25);
  // cerr<<fixed<<setprecision(10);
  auto start = std::chrono::high_resolution_clock::now();

  // freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);

  ll tt = 1;

  /*

      cin >> tt;
      ll t = tt;

      //*/

  while (tt--) {
    // cout << "Case #" << t - tt << ": ";
    go1();
  }
  auto stop = std::chrono::high_resolution_clock::now();
  auto duration =
      std::chrono::duration_cast<std::chrono::nanoseconds>(stop - start);
  cerr << "Time taken : "
       << ((long double)duration.count()) / ((long double)1e9) << "s " << endl;
  return 0;
}
/* ---------------- end of code ---------------- */


Error: TLE

Fixed submission:

#pragma GCC target("sse4.2")
#include <bits/stdc++.h>

using namespace std;

#define F first
#define S second
#define PB push_back
#define ln "\\n"
#define forn(i, e) for (ll i = 0; i < e; i++)
#define forsn(i, s, e) for (ll i = s; i < e; i++)
#define rforn(i, e) for (ll i = e; i >= 0; i--)
#define rforsn(i, e, s) for (ll i = e; i >= s; i--)
#define vasort(v) sort(v.begin(), v.end());
#define vdsort(v) sort(v.begin(), v.end(), greater<ll>());
#define fio                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define out1(x1) cout << x1 << ln
#define out2(x1, x2) cout << x1 << " " << x2 << endl
#define out3(x1, x2, x3) cout << x1 << " " << x2 << " " << x3 << ln
#define out4(x1, x2, x3, x4)                                                   \\
  cout << x1 << " " << x2 << " " << x3 << " " << x4 << ln
#define all(d) d.begin(), d.end()
#define unordered_set(T) unordered_set<T, custom_hash>
#define unordered_map(T1, T2) unordered_map<T1, T2, custom_hash>

#define in1(x1) cin >> x1
#define in2(x1, x2) cin >> x1 >> x2
#define in3(x1, x2, x3) cin >> x1 >> x2 >> x3
#define in4(x1, x2, x3, x4) cin >> x1 >> x2 >> x3 >> x4

#define arrin(a, n) forn(i, n) cin >> a[i];
#define arrout(a, n)                                                           \\
  forn(i, n) { cout << a[i] << " "; }                                          \\
  cout << ln;

/* ---------------- IO ----------------*/

namespace IO {
const int BUFFER_SIZE = 1 << 15;
char input_buffer[BUFFER_SIZE];
int input_pos = 0, input_len = 0;
char output_buffer[BUFFER_SIZE];
int output_pos = 0;
char number_buffer[100];
uint8_t lookup[100];
void _update_input_buffer() {
  input_len = fread(input_buffer, sizeof(char), BUFFER_SIZE, stdin);
  input_pos = 0;
  if (input_len == 0)
    input_buffer[0] = EOF;
}
inline char next_char(bool advance = true) {
  if (input_pos >= input_len)
    _update_input_buffer();

  return input_buffer[advance ? input_pos++ : input_pos];
}

template <typename T> inline void read_int(T &number) {
  bool negative = false;
  number = 0;

  while (!isdigit(next_char(false)))
    if (next_char() == '-')
      negative = true;

  do {
    number = 10 * number + (next_char() - '0');
  } while (isdigit(next_char(false)));

  if (negative)
    number = -number;
}

template <typename T, typename... Args>
inline void read_int(T &number, Args &...args) {
  read_int(number);
  read_int(args...);
}

void _flush_output() {
  fwrite(output_buffer, sizeof(char), output_pos, stdout);
  output_pos = 0;
}

inline void write_char(char c) {
  if (output_pos == BUFFER_SIZE)
    _flush_output();

  output_buffer[output_pos++] = c;
}

template <typename T> inline void write_int(T number, char after = '\\0') {
  if (number < 0) {
    write_char('-');
    number = -number;
  }
  int length = 0;
  while (number >= 10) {
    uint8_t lookup_value = lookup[number % 100];
    number /= 100;
    number_buffer[length++] = (lookup_value & 15) + '0';
    number_buffer[length++] = (lookup_value >> 4) + '0';
  }
  if (number != 0 || length == 0)
    write_char(number + '0');
  for (int i = length - 1; i >= 0; i--)
    write_char(number_buffer[i]);
  if (after)
    write_char(after);
}
void IOinit() {
  // Make sure _flush_output() is called at the end of the program.
  bool exit_success = atexit(_flush_output) == 0;
  assert(exit_success);
  for (int i = 0; i < 100; i++)
    lookup[i] = (i / 10 << 4) + i % 10;
}
} // namespace IO

using namespace IO;

/* ---------------- typedefs ----------------*/
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

/* ---------------- utility functions ---------------- */

int dr[] = {0, 1, -1, 0, 1, -1, 1, -1};
int dc[] = {1, 0, 0, -1, 1, -1, -1, 1};
bool chk(int a, int b, int n, int m) {
  return 0 <= a && a < n && 0 <= b && b < m;
}

ll exp(ll n, ll m, ll md) {
  ll a = 1;
  while (m > 0) {
    if (m & 1)
      a = (a * n % md) % md;
    m /= 2;
    n = (n * n) % md;
  }
  return a % md;
}

void disp(vi v) {
  for (auto u : v)
    cout << u << " ";
  cout << "\\n";
}
void disp(vll v) {
  for (auto u : v)
    cout << u << " ";
  cout << "\\n";
}

int getSqr(int x) {
  if (x < 0)
    return -1;
  int l = sqrt(x);
  for (int i = -2; i <= 2; ++i)
    if (l + i >= 0 && (l + i) * (l + i) == x)
      return l;
  return -1;
}

const int NPRR = 1e7;
vi lp;
vector<int> prr;
void prfac(int n = NPRR) {
  assert(n <= NPRR);
  lp.assign(n + 1, 0);
  int i;
  for (i = 2; i <= n; ++i) {
    if (lp[i] == 0) {
      lp[i] = i;
      prr.push_back(i);
    }
    for (int u : prr) {
      if (i * u > n || u > lp[i])
        break;
      lp[i * u] = u;
    }
  }
}

bool check_composite_for_prime_function(ll n, ll a, ll d, int s) {
  ll x = exp(a, d, n);
  if (x == 1 || x == n - 1)
    return false;
  for (int r = 1; r < s; ++r) {
    x = x * x % n;
    x %= n;
    if (x == n - 1)
      return false;
  }
  return true;
}
bool prime(ll n) {
  if (n < 4)
    return n == 2 || n == 3;
  if (!(n & 1))
    return false;
  int s = 0;
  ll d = n - 1;
  while (!(d & 1)) {
    d >>= 1;
    s++;
  }
  for (int a : {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37}) {
    if (n == a)
      return true;
    if (check_composite_for_prime_function(n, a, d, s))
      return false;
  }
  return true;
}

ll binomialCoeff(ll n, ll k) {
  ll res = 1;

  // Since C(n, k) = C(n, n-k)
  if (k > n - k)
    k = n - k;

  // Calculate value of
  // [n * (n-1) *---* (n-k+1)] / [k * (k-1) *----* 1]
  for (ll i = 0; i < k; ++i) {
    res *= (n - i);
    res /= (i + 1);
  }

  return res;
}
/* custom hash for unordered map */
struct custom_hash {
  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);
  }
};

inline void read(int &x) { scanf("%d", &x); }

/* ---------------- constants ---------------- */
const ll MAXn = 1e5 + 33, MAXlg = __lg(MAXn) + 2;
const ll MOD = 1000000007;
const ll INF = ll(1e15);

/* ---------------- start of code ---------------- */

const int N = 5e5; // limit for array size
int n;             // array size
set<int> t[2 * N];

set<int> combine(set<int> &a, set<int> &b) {
  set<int> res;
  for (auto &u : a)
    res.insert(u);
  for (auto &u : b)
    res.insert(u);
  return res;
}

void build() { // build the tree
  for (int i = n - 1; i > 0; --i) {
    t[i] = combine(t[i << 1], t[i << 1 | 1]);
  }
}

set<int> query(int l, int r) {
  set<int> resl, resr;
  for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
    if (l & 1)
      resl = combine(resl, t[l++]);
    if (r & 1)
      resr = combine(t[--r], resr);
  }
  return combine(resl, resr);
}

void go1() {
  int q;
  in2(n, q);
  int a[n];
  int n1 = n;
  //    int d = 0;
  //    while(n > 0) {
  //        n >>= 1;
  //        d++;
  //    }
  //    n = (1 << (d));
  //    cout << n << endl;
  forn(i, n1) {
    cin >> a[i];
    t[n + i].insert(a[i]);
  }
  build();
  //    forn(i, 2*n) {
  //        //cout << t[i].size() << " ";
  //    }
  forn(i, q) {
    int l, r;
    in2(l, r);
    l--;
    r--;
    cout << query(l, r + 1).size() << ln;
  }
}

int main() {
  fio;
  // cout<<fixed<<setprecision(25);
  // cerr<<fixed<<setprecision(10);
  // freopen("output.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  auto start = std::chrono::high_resolution_clock::now();

  ll tt = 1;

  /*

      cin >> tt;
      ll t = tt;

      //*/

  while (tt--) {
    // cout << "Case #" << t - tt << ": ";
    go1();
  }

  auto stop = std::chrono::high_resolution_clock::now();
  auto duration =
      std::chrono::duration_cast<std::chrono::nanoseconds>(stop - start);
  // cerr << "Time taken : " << ((long double)duration.count())/((long double)
  // 1e9) <<"s "<< endl;
  return 0;
}
/* ---------------- end of code ---------------- */