Problem p02599 (C++) — buggy submission, verdict: Runtime Error


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

#define fastio                                                                 \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);                                                                 \\
  cout << fixed;                                                               \\
  cout << setprecision(12);
#define randomINT                                                              \\
  mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
#define newl cout << "\\n"
#define DISP(as)                                                               \\
  for (auto it : as)                                                           \\
    cout << it << " ";                                                         \\
  newl;
#define all(x) (x).begin(), (x).end()
#define mset(x, val) memset(x, val, sizeof(x))
#define newl cout << "\\n"
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define dline cerr << "///REACHED///\\n";
#define deb1(x) cerr << #x << " = " << x << '\\n';
#define deb2(x, y)                                                             \\
  cerr << '[' << #x << ',' << #y << "] = " << '[' << x << ',' << y << ']'      \\
       << '\\n';
#define deb3(x, y, z)                                                          \\
  cerr << '[' << #x << ',' << #y << ',' << #z << "] = " << '[' << x << ','     \\
       << y << ',' << z << ']' << '\\n';
#define N 1000000007 // prime modulo value

typedef long long ll;
typedef long double ld;
typedef vector<ll> vll;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pld;
typedef unordered_map<ll, ll> um;
typedef vector<pll> vpll;

const ll MAX5 = 1e+5 + 7;
const ll MAX7 = 1e+7 + 7;
const ll MAXN = MAX7;
const ll INF = 0x7f7f7f7f7f7f7f7f;
const int INFi = 0x7f7f7f7f;
const ll MOD = 1e9 + 7;

// <------------- Declare Variables Here ------------> //

ll T = 1;
ll n, x, k, m, y, q;
string s, t;
ld w;

// <------------- Implement Functions Here ----------> //

ll tree[4000000];
ll a[600000];
ll arr[600000];
ll pos[600000];
ll quer[600000];
vector<pair<pll, ll>> ans;

bool comp(pair<pll, ll> &a, pair<pll, ll> &b) {
  if (a.f.s == b.f.s && a.f.f == b.f.f)
    return a.s < b.s;
  if (a.f.s == b.f.s)
    return a.f.f < b.f.f;
  return a.f.s < b.f.s;
}

void build(ll start = 1, ll end = n, ll node = 1) {
  if (start == end) {

    tree[node] = a[start];
  } else {
    ll mid = (start + end) / 2;
    build(start, mid, 2 * node);
    build(mid + 1, end, 2 * node + 1);
    tree[node] = tree[2 * node] + tree[2 * node + 1];
  }
}

void update(ll indx, ll val, ll start = 1, ll end = n, ll node = 1) {
  if (start == end) {
    a[indx] = val;
    tree[node] = val;
  } else {
    ll mid = (start + end) / 2;
    if (start <= indx && indx <= mid)
      update(indx, val, start, mid, 2 * node);
    else
      update(indx, val, mid + 1, end, 2 * node + 1);
    tree[node] = tree[2 * node] + tree[2 * node + 1];
  }
}

ll query(ll l, ll r, ll start = 1, ll end = n, ll node = 1) {
  if (r < start || end < l)
    return 0;
  else if (l <= start && end <= r) {
    return tree[node];
  } else {
    ll mid = (start + end) / 2;
    ll p1 = query(l, r, start, mid, 2 * node);
    ll p2 = query(l, r, mid + 1, end, 2 * node + 1);
    return p1 + p2;
  }
}

// <------------- Start of main() -------------------> //

void MAIN() {
  cin >> n >> q;
  for (ll i = 1; i <= n; i++)
    cin >> arr[i];

  for (ll i = 0; i < q; i++) {
    cin >> x >> y;
    ans.pb({{x, y}, i});
  }

  sort(all(ans), comp);
  build();
  ll cur = 1;

  /*for(auto it : ans) {
          cout<<it.f.f<<" "<<it.f.s<<"\\n";
  }*/

  for (ll i = 0; i < q; i++) {
    for (ll j = cur; j <= ans[i].f.s; j++) {
      update(pos[arr[j]], 0);
      pos[arr[j]] = j;
      update(j, 1);
    }
    cur = ans[i].f.s;
    // for(ll i=1;i<=n;i++) cout<<a[i]<<" ";newl;
    quer[ans[i].s] = query(ans[i].f.f, ans[i].f.s);
  }

  for (ll i = 0; i < q; i++) {
    cout << quer[i] << "\\n";
  }
}

int main() {
  fastio;
  randomINT;
  // cin >> T;
  while (T--) {
    MAIN();
  }
  return 0;
}

Error: 0

Fixed submission:


// C++ implementation of above approach
#include <bits/stdc++.h>
using namespace std;

// Each segment of the segment tree would be a set
// to maintain distinct elements
set<int> *segment;

// Build the segment tree
// i denotes current node, s denotes start and
// e denotes the end of range for current node
void build(int i, int s, int e, int arr[]) {

  // If start is equal to end then
  // insert the array element
  if (s == e) {
    segment[i].insert(arr[s]);
    return;
  }

  // Else divide the range into two halves
  // (start to mid) and (mid+1 to end)
  // first half will be the left node
  // and the second half will be the right node
  build(2 * i, s, (s + e) / 2, arr);
  build(1 + 2 * i, 1 + (s + e) / 2, e, arr);

  // Insert the sets of right and left
  // node of the segment tree
  segment[i].insert(segment[2 * i].begin(), segment[2 * i].end());

  segment[i].insert(segment[2 * i + 1].begin(), segment[2 * i + 1].end());
}

// Query in an range a to b
set<int> query(int node, int l, int r, int a, int b) {
  set<int> left, right, result;

  // If the range is out of the bounds
  // of this segment
  if (b < l || a > r)
    return result;

  // If the range lies in this segment
  if (a <= l && r <= b)
    return segment[node];

  // Else query for the right and left
  // leaf node of this subtree
  // and insert them into the set
  left = query(2 * node, l, (l + r) / 2, a, b);
  result.insert(left.begin(), left.end());

  right = query(1 + 2 * node, 1 + (l + r) / 2, r, a, b);
  result.insert(right.begin(), right.end());

  // Return the result
  return result;
}

// Initialize the segment tree
void init(int n) {
  // Get the height of the segment tree
  int h = (int)ceil(log2(n));
  h = (2 * (pow(2, h))) - 1;

  // Initialize the segment tree
  segment = new set<int>[h];
}

// Function to get the result for the
// subarray from arr[l] to arr[r]
void getDistinct(int l, int r, int n) {
  // Query for the range set
  set<int> ans = query(1, 0, n - 1, l, r);

  cout << ans.size() << endl;
}

int arr[5000000];

// Driver code
int main() {
  int n, q;
  cin >> n >> q;

  // int n = sizeof(arr) / sizeof(arr[0]);
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  init(n);

  // Bulid the segment tree
  build(1, 0, n - 1, arr);

  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;

    getDistinct(l - 1, r - 1, n);
  }
  // Query in range 0 to 4

  return 0;
}