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


#include <bits/stdc++.h>

#define FAST_INPUT                                                             \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define ll long long
using namespace std;

const int MAX = 1000000;

int block;

struct Query {
  int L, R, index, result;
};

bool compare(Query x, Query y) {
  if (x.L / block != y.L / block)
    return x.L / block < y.L / block;

  return x.R < y.R;
}

bool compare1(Query x, Query y) { return x.index < y.index; }

void queryResults(int a[], int n, Query q[], int m) {
  block = (int)sqrt(n);

  sort(q, q + m, compare);

  int currL = 0, currR = 0;
  int curr_Diff_elements = 0;

  int freq[MAX] = {0};

  for (int i = 0; i < m; i++) {

    int L = q[i].L, R = q[i].R;

    while (currL < L) {

      freq[a[currL]]--;
      if (freq[a[currL]] == 0)
        curr_Diff_elements--;

      currL++;
    }

    while (currL > L) {
      freq[a[currL - 1]]++;

      if (freq[a[currL - 1]] == 1)
        curr_Diff_elements++;

      currL--;
    }
    while (currR <= R) {
      freq[a[currR]]++;

      if (freq[a[currR]] == 1)
        curr_Diff_elements++;

      currR++;
    }

    while (currR > R + 1) {

      freq[a[currR - 1]]--;

      if (freq[a[currR - 1]] == 0)
        curr_Diff_elements--;

      currR--;
    }
    q[i].result = curr_Diff_elements;
  }
}

void printResults(Query q[], int m) {
  sort(q, q + m, compare1);
  for (int i = 0; i < m; i++) {
    cout << q[i].result << endl;
  }
}

void solve() {
  int n, q;
  cin >> n >> q;
  int colors[n];
  Query queries[q];
  for (int i = 0; i < n; i++) {
    cin >> colors[i];
  }

  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;
    Query &query = queries[i];
    query.L = l;
    query.R = r;
    query.index = i;
  }

  queryResults(colors, n, queries, q);
  printResults(queries, q);
}

int main() {
  FAST_INPUT;

  solve();

  return 0;
}


Error: TLE

Fixed submission:


#include <bits/stdc++.h>

#define FAST_INPUT                                                             \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define ll long long
using namespace std;
//
// struct SegTree {
//    int leftmost, rightmost;
//    set<int> values;
//    SegTree* lchild;
//    SegTree* rchild;
//
//    SegTree(int l, int r, vector<int>& nums) {
//        leftmost = l;
//        rightmost = r;
//        if (l==r) {
//            // leaf
//            values.insert(nums[l]);
//        } else {
//            int mid = (l+r)/2;
//            lchild = new SegTree(l, mid, nums);
//            rchild = new SegTree(mid+1, r, nums);
//            values.insert(lchild->values.begin(), lchild->values.end());
//            values.insert(rchild->values.begin(), rchild->values.end());
//        }
//    }
//
//    set<int> query_range(int l, int r) {
//        if (l>rightmost || r<leftmost) {
//            return set<int>();
//        }
//        if (l<=leftmost && r>=rightmost) {
//            return values;
//        }
//
//        set<int> res;
//        set<int> left=lchild->query_range(l, r);
//        set<int> right=rchild->query_range(l, r);
//
//        res.insert(left.begin(), left.end());
//        res.insert(right.begin(), right.end());
//        return res;
//    }
//};

set<int> *segment;

void build(int i, int s, int e, vector<int> &arr) {
  if (s == e) {
    segment[i].insert(arr[s]);
    return;
  }

  build(2 * i, s, (s + e) / 2, arr);
  build(1 + 2 * i, 1 + (s + e) / 2, e, arr);

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

set<int> query(int node, int l, int r, int a, int b) {
  set<int> left, right, result;

  if (b < l || a > r)
    return result;

  if (a <= l && r <= b) {
    return segment[node];
  }

  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 result;
}

void init(int n) {
  int h = (int)ceil(log2(n));
  h = (2 * (pow(2, h)));
  segment = new set<int>[h];
}

void solve() {
  int n, q;
  cin >> n >> q;
  vector<int> colors(n);
  for (int i = 0; i < n; i++) {
    cin >> colors[i];
  }
  init(n);
  build(1, 0, n - 1, colors);

  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;
    set<int> res = query(1, 0, n - 1, l, r);
    cout << (int)res.size() << endl;
  }
}

int main() {
  FAST_INPUT;

  solve();

  return 0;
}