Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- const int maxN = 100;
- int a[maxN];
- struct WavletTree {
- int low,high;
- WavletTree *l, *r;
- vector<int> b;
- WavletTree (int *from,int *to,int x,int y) {
- low = x,high = y;
- if (low==high or from >= to) return;
- int mid = low + (high - low) / 2;
- b.reserve(to - from + 1);
- b.push_back(0);
- auto f = [mid] (int x) {return x <= mid;};
- for (auto it = from; it != to; ++it) b.push_back(b.back() + f(*it));
- auto pivot = stable_partition(from,to,f);
- l = new WavletTree(from,pivot,low,mid);
- r = new WavletTree(pivot,to,mid + 1,high);
- }
- int kth (int l,int r,int k) {
- //kth smallest in range [l,r]
- if (l > r) return 0;
- if (high == low) return low;
- int left = b[r] - b[l - 1];
- int lb = b[l - 1],rb = b[r];
- if (k <= left) return this->l->kth(lb + 1,rb,k);
- return this->r->kth(l - lb,r - rb,k-left);
- }
- int LTE (int l,int r,int k) {
- //Number of elements in subarray A[L...R] that are less than or equal to y.
- if (l > r or k < low) return 0;
- if (high <= k) return r - l + 1;
- int lb = b[l - 1],rb = b[r];
- return this->l->LTE(lb + 1,rb,k) + this->r->LTE(l - lb,r - rb,k);
- }
- int count (int l,int r,int k) {
- //Number of occurrences of element x in subarray A[L...R].
- if (l > r or k < low or k > high) return 0;
- if (low == high) return r - l + 1;
- int lb = b[l - 1],rb = b[r];
- int mid = low + (high - low) / 2;
- if (k <= mid) return this->l->count(lb + 1,rb,k);
- return this->r->count(l - lb,r - rb,k);
- }
- ~WavletTree () {delete l; delete r;};
- };
- int main () {
- ios::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- int T = 1;
- //~ cin >> T;
- for (int test_case = 1; test_case <= T; ++test_case) {
- int n;
- cin >> n;
- for (int i = 1; i <= n; ++i) cin >> a[i];
- WavletTree T(a + 1,a + n + 1,1,10000);
- cout << T.LTE(2,6,4) << '\n';
- }
- //cerr << "Time elapsed :" << clock() * 1000.0 / CLOCKS_PER_SEC << " ms" << '\n';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement