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


// Problem : F - Range Set Query
// Contest : AtCoder - AtCoder Beginner Contest 174
// URL : https://atcoder.jp/contests/abc174/tasks/abc174_f
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
using namespace std;
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#define int long long int
#define ld long double
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define MOD 1000000007
#define mod 998244353
#define bug cout << "HY" << endl
#define boost                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define numize(A) A.numize(unique(A.begin(), A.end()) - A.begin())
typedef pair<int, int> P;
int power(int a, int b) {
  if (b == 0)
    return 1;
  if (b % 2 == 0)
    return (power((a * a) % mod, b / 2)) % mod;
  return (a * (power((a * a) % mod, b / 2)) % mod) % mod;
}
int modInv(int n) { return power(n, MOD - 2); }
int nCr(int n, int r, int p = MOD) {
  if (r == 0)
    return 1;
  int Fact[n + 1];
  Fact[0] = 1;
  for (int i = 1; i <= n; i++) {
    Fact[i] = (Fact[i - 1] * i) % p;
  }
  return (((Fact[n] * modInv(Fact[r])) % MOD) * (modInv(Fact[n - r]) % MOD)) %
         MOD;
}
double PI = 3.14159265359;
struct Node {
  int l, r, ind;
};
bool comp(Node &n1, Node &n2) { return n1.r < n2.r; }
vector<int> Bit;
void Update(int ind, int n, int val) {
  while (ind <= n) {
    Bit[ind] += val;
    ind = ind + (ind & -ind);
  }
  return;
}
int Query(int ind) {
  int ans = 0;
  while (ind > 0) {
    ans += Bit[ind];
    ind = ind & (ind - 1);
  }
  return ans;
}
void solve() {
  int n, q, i;
  cin >> n >> q;
  int C[n + 1];
  for (i = 1; i <= n; i++)
    cin >> C[i];
  int Index[n + 1];
  Bit.resize(n + 1, 0);
  memset(Index, -1, sizeof(Index));
  vector<Node> V(q);
  for (i = 0; i < q; i++) {
    cin >> V[i].l >> V[i].r;
    V[i].ind = i;
  }
  sort(V.begin(), V.end(), comp);
  int j = 0;
  int l = V[j].l, r = V[j].r, ind = V[j].ind;
  int Ans[q];
  for (i = 1; i <= n && j < q; i++) {
    l = V[j].l, r = V[j].r, ind = V[j].ind;
    if (Index[C[i]] == -1) {
      // trace(C[i]);
      Index[C[i]] = i;
      Update(Index[C[i]], n, 1);
    } else {
      Update(Index[C[i]], n, -1);
      Index[C[i]] = i;
      Update(Index[C[i]], n, 1);
    }
    while (j < q && r == i) {
      l = V[j].l, r = V[j].r, ind = V[j].ind;
      Ans[ind] = Query(r) - Query(l - 1);
      j++;
      if (j < q) {
        l = V[j].l, r = V[j].r, ind = V[j].ind;
      }
    }
  }
  // trace(i,j);
  for (i = 0; i < q; i++)
    cout << Ans[i] << endl;
}
int32_t main() {
  boost;
  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
}


Error: TLE

Fixed submission:


// Problem : F - Range Set Query
// Contest : AtCoder - AtCoder Beginner Contest 174
// URL : https://atcoder.jp/contests/abc174/tasks/abc174_f
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
using namespace std;
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#define int long long int
#define ld long double
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define MOD 1000000007
#define mod 998244353
#define bug cout << "HY" << endl
#define boost                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define numize(A) A.numize(unique(A.begin(), A.end()) - A.begin())
typedef pair<int, int> P;
int power(int a, int b) {
  if (b == 0)
    return 1;
  if (b % 2 == 0)
    return (power((a * a) % mod, b / 2)) % mod;
  return (a * (power((a * a) % mod, b / 2)) % mod) % mod;
}
int modInv(int n) { return power(n, MOD - 2); }
int nCr(int n, int r, int p = MOD) {
  if (r == 0)
    return 1;
  int Fact[n + 1];
  Fact[0] = 1;
  for (int i = 1; i <= n; i++) {
    Fact[i] = (Fact[i - 1] * i) % p;
  }
  return (((Fact[n] * modInv(Fact[r])) % MOD) * (modInv(Fact[n - r]) % MOD)) %
         MOD;
}
double PI = 3.14159265359;
struct Node {
  int l, r, ind;
};
bool comp(Node &n1, Node &n2) { return n1.r < n2.r; }
vector<int> Bit;
void Update(int ind, int n, int val) {
  while (ind <= n) {
    Bit[ind] += val;
    ind = ind + (ind & -ind);
  }
  return;
}
int Query(int ind) {
  int ans = 0;
  while (ind > 0) {
    ans += Bit[ind];
    ind = ind & (ind - 1);
  }
  return ans;
}
void solve() {
  int n, q, i;
  cin >> n >> q;
  int C[n + 1];
  for (i = 1; i <= n; i++)
    cin >> C[i];
  int Index[n + 1];
  Bit.resize(n + 1, 0);
  memset(Index, -1, sizeof(Index));
  vector<Node> V(q);
  for (i = 0; i < q; i++) {
    cin >> V[i].l >> V[i].r;
    V[i].ind = i;
  }
  sort(V.begin(), V.end(), comp);
  int j = 0;
  int l = V[j].l, r = V[j].r, ind = V[j].ind;
  int Ans[q];
  for (i = 1; i <= n && j < q; i++) {
    l = V[j].l, r = V[j].r, ind = V[j].ind;
    if (Index[C[i]] == -1) {
      trace(C[i]);
      Index[C[i]] = i;
      Update(Index[C[i]], n, 1);
    } else {
      Update(Index[C[i]], n, -1);
      Index[C[i]] = i;
      Update(Index[C[i]], n, 1);
    }
    while (j < q && r == i) {
      l = V[j].l, r = V[j].r, ind = V[j].ind;
      Ans[ind] = Query(r) - Query(l - 1);
      j++;
      if (j < q) {
        l = V[j].l, r = V[j].r, ind = V[j].ind;
      }
    }
  }
  trace(i, j);
  for (i = 0; i < q; i++)
    cout << Ans[i] << endl;
}
int32_t main() {
  boost;
  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
}