#include <bits/stdc++.h>
using namespace std;
#define fo(i,n) for(i=0;i<n;i++)
#define ll long long
#define pb push_back
#define mp make_pair
typedef pair<int, int> pii;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
const int N = 3e5, M = N;
//=======================
const int MAX = 1e6;
int a[N];
struct wavelet_tree
{
#define vi vector<int>
#define pb push_back
int lo, hi;
wavelet_tree *l=0, *r=0;
vi b;
vi c; // c holds the prefix sum of elements
//nos are in range [x,y]
//array indices are [from, to)
wavelet_tree(int *from, int *to, int x, int y)
{
lo = x, hi = y;
if( from >= to)
return;
if( hi == lo )
{
b.reserve(to-from+1);
b.pb(0);
c.reserve(to-from+1);
c.pb(0);
for(auto it = from; it != to; it++)
{
b.pb(b.back() + 1);
c.pb(c.back()+*it);
}
return ;
}
int mid = (lo+hi)/2;
auto f = [mid](int x)
{
return x <= mid;
};
b.reserve(to-from+1);
b.pb(0);
c.reserve(to-from+1);
c.pb(0);
for(auto it = from; it != to; it++)
{
b.pb(b.back() + f(*it));
c.pb(c.back() + *it);
}
//see how lambda function is used here
auto pivot = stable_partition(from, to, f);
l = new wavelet_tree(from, pivot, lo, mid);
r = new wavelet_tree(pivot, to, mid+1, hi);
}
// swap a[i] with a[i+1] , if a[i]!=a[i+1] call swapadjacent(i)
void swapadjacent(int i)
{
if(lo == hi)
return ;
b[i]= b[i-1] + b[i+1] - b[i];
c[i] = c[i-1] + c[i+1] - c[i];
if( b[i+1]-b[i] == b[i] - b[i-1])
{
if(b[i]-b[i-1])
return this->l->swapadjacent(b[i]);
else
return this->r->swapadjacent(i-b[i]);
}
else
return ;
}
//kth smallest element in [l, r]
int kth(int l, int r, int k)
{
if(l > r)
return 0;
if(lo == hi)
return lo;
int inLeft = b[r] - b[l-1];
int lb = b[l-1]; //amt of nos in first (l-1) nos that go in left
int rb = b[r]; //amt of nos in first (r) nos that go in left
if(k <= inLeft)
return this->l->kth(lb+1, rb, k);
return this->r->kth(l-lb, r-rb, k-inLeft);
}
//count of nos in [l, r] Less than or equal to k
int LTE(int l, int r, int k)
{
if(l > r or k < lo)
return 0;
if(hi <= 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);
}
//count of nos in [l, r] equal to k
int count(int l, int r, int k)
{
if(l > r or k < lo or k > hi)
return 0;
if(lo == hi)
return r - l + 1;
int lb = b[l-1], rb = b[r], mid = (lo+hi)/2;
if(k <= mid)
return this->l->count(lb+1, rb, k);
return this->r->count(l-lb, r-rb, k);
}
//sum of nos in [l ,r] less than or equal to k
int sumk(int l, int r, int k)
{
if(l > r or k < lo)
return 0;
if(hi <= k)
return c[r] - c[l-1];
int lb = b[l-1], rb = b[r];
return this->l->sumk(lb+1, rb, k) + this->r->sumk(l-lb, r-rb, k);
}
~wavelet_tree()
{
if(l)
delete l;
if(r)
delete r;
}
};
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
srand(time(NULL));
int i,n,k,j,q,l,r;
cin >> n;
fo(i, n) cin >> a[i+1];
wavelet_tree T(a+1, a+n+1, 1, MAX);
cin >> q;
while(q--)
{
int x;
cin >> x;
cin >> l >> r >> k;
if(x == 0)
{
//kth smallest
cout << "Kth smallest: ";
cout << T.kth(l, r, k) << endl;
}
if(x == 1)
{
//less than or equal to K
cout << "LTE: ";
cout << T.LTE(l, r, k) << endl;
}
if(x == 2)
{
//count occurence of K in [l, r]
cout << "Occurence of K: ";
cout << T.count(l, r, k) << endl;
}
if(x == 3)
{
//sum of elements less than or equal to K in [l, r]
cout << "Sum: ";
cout << T.sumk(l, r, k) << endl;
}
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiNkZWZpbmUgZm8oaSxuKSBmb3IoaT0wO2k8bjtpKyspCiNkZWZpbmUgbGwgbG9uZyBsb25nCgojZGVmaW5lIHBiIHB1c2hfYmFjawojZGVmaW5lIG1wIG1ha2VfcGFpcgoKCnR5cGVkZWYgcGFpcjxpbnQsIGludD4JcGlpOwp0eXBlZGVmIHBhaXI8bGwsIGxsPglwbDsKdHlwZWRlZiB2ZWN0b3I8aW50PgkJdmk7Cgpjb25zdCBpbnQgTiA9IDNlNSwgTSA9IE47Ci8vPT09PT09PT09PT09PT09PT09PT09PT0KY29uc3QgaW50IE1BWCA9IDFlNjsKCmludCBhW05dOwpzdHJ1Y3Qgd2F2ZWxldF90cmVlCnsKI2RlZmluZSB2aSB2ZWN0b3I8aW50PgojZGVmaW5lIHBiIHB1c2hfYmFjawogICAgaW50IGxvLCBoaTsKICAgIHdhdmVsZXRfdHJlZSAqbD0wLCAqcj0wOwogICAgdmkgYjsKICAgIHZpIGM7IC8vIGMgaG9sZHMgdGhlIHByZWZpeCBzdW0gb2YgZWxlbWVudHMKCiAgICAvL25vcyBhcmUgaW4gcmFuZ2UgW3gseV0KICAgIC8vYXJyYXkgaW5kaWNlcyBhcmUgW2Zyb20sIHRvKQogICAgd2F2ZWxldF90cmVlKGludCAqZnJvbSwgaW50ICp0bywgaW50IHgsIGludCB5KQogICAgewogICAgICAgIGxvID0geCwgaGkgPSB5OwogICAgICAgIGlmKCBmcm9tID49IHRvKQogICAgICAgICAgICByZXR1cm47CiAgICAgICAgaWYoIGhpID09IGxvICkKICAgICAgICB7CiAgICAgICAgICAgIGIucmVzZXJ2ZSh0by1mcm9tKzEpOwogICAgICAgICAgICBiLnBiKDApOwogICAgICAgICAgICBjLnJlc2VydmUodG8tZnJvbSsxKTsKICAgICAgICAgICAgYy5wYigwKTsKICAgICAgICAgICAgZm9yKGF1dG8gaXQgPSBmcm9tOyBpdCAhPSB0bzsgaXQrKykKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgYi5wYihiLmJhY2soKSArIDEpOwogICAgICAgICAgICAgICAgYy5wYihjLmJhY2soKSsqaXQpOwogICAgICAgICAgICB9CiAgICAgICAgICAgIHJldHVybiA7CiAgICAgICAgfQogICAgICAgIGludCBtaWQgPSAobG8raGkpLzI7CiAgICAgICAgYXV0byBmID0gW21pZF0oaW50IHgpCiAgICAgICAgewogICAgICAgICAgICByZXR1cm4geCA8PSBtaWQ7CiAgICAgICAgfTsKICAgICAgICBiLnJlc2VydmUodG8tZnJvbSsxKTsKICAgICAgICBiLnBiKDApOwogICAgICAgIGMucmVzZXJ2ZSh0by1mcm9tKzEpOwogICAgICAgIGMucGIoMCk7CiAgICAgICAgZm9yKGF1dG8gaXQgPSBmcm9tOyBpdCAhPSB0bzsgaXQrKykKICAgICAgICB7CiAgICAgICAgICAgIGIucGIoYi5iYWNrKCkgKyBmKCppdCkpOwogICAgICAgICAgICBjLnBiKGMuYmFjaygpICsgKml0KTsKICAgICAgICB9CiAgICAgICAgLy9zZWUgaG93IGxhbWJkYSBmdW5jdGlvbiBpcyB1c2VkIGhlcmUKICAgICAgICBhdXRvIHBpdm90ID0gc3RhYmxlX3BhcnRpdGlvbihmcm9tLCB0bywgZik7CiAgICAgICAgbCA9IG5ldyB3YXZlbGV0X3RyZWUoZnJvbSwgcGl2b3QsIGxvLCBtaWQpOwogICAgICAgIHIgPSBuZXcgd2F2ZWxldF90cmVlKHBpdm90LCB0bywgbWlkKzEsIGhpKTsKICAgIH0KCiAgICAvLyBzd2FwIGFbaV0gd2l0aCBhW2krMV0gICwgaWYgYVtpXSE9YVtpKzFdIGNhbGwgc3dhcGFkamFjZW50KGkpCiAgICB2b2lkIHN3YXBhZGphY2VudChpbnQgaSkKICAgIHsKICAgICAgICBpZihsbyA9PSBoaSkKICAgICAgICAgICAgcmV0dXJuIDsKICAgICAgICBiW2ldPSBiW2ktMV0gKyBiW2krMV0gLSBiW2ldOwogICAgICAgIGNbaV0gPSBjW2ktMV0gKyBjW2krMV0gLSBjW2ldOwogICAgICAgIGlmKCBiW2krMV0tYltpXSA9PSBiW2ldIC0gYltpLTFdKQogICAgICAgIHsKICAgICAgICAgICAgaWYoYltpXS1iW2ktMV0pCiAgICAgICAgICAgICAgICByZXR1cm4gdGhpcy0+bC0+c3dhcGFkamFjZW50KGJbaV0pOwogICAgICAgICAgICBlbHNlCiAgICAgICAgICAgICAgICByZXR1cm4gdGhpcy0+ci0+c3dhcGFkamFjZW50KGktYltpXSk7CiAgICAgICAgfQogICAgICAgIGVsc2UKICAgICAgICAgICAgcmV0dXJuIDsKICAgIH0KCiAgICAvL2t0aCBzbWFsbGVzdCBlbGVtZW50IGluIFtsLCByXQogICAgaW50IGt0aChpbnQgbCwgaW50IHIsIGludCBrKQogICAgewogICAgICAgIGlmKGwgPiByKQogICAgICAgICAgICByZXR1cm4gMDsKICAgICAgICBpZihsbyA9PSBoaSkKICAgICAgICAgICAgcmV0dXJuIGxvOwogICAgICAgIGludCBpbkxlZnQgPSBiW3JdIC0gYltsLTFdOwogICAgICAgIGludCBsYiA9IGJbbC0xXTsgLy9hbXQgb2Ygbm9zIGluIGZpcnN0IChsLTEpIG5vcyB0aGF0IGdvIGluIGxlZnQKICAgICAgICBpbnQgcmIgPSBiW3JdOyAvL2FtdCBvZiBub3MgaW4gZmlyc3QgKHIpIG5vcyB0aGF0IGdvIGluIGxlZnQKICAgICAgICBpZihrIDw9IGluTGVmdCkKICAgICAgICAgICAgcmV0dXJuIHRoaXMtPmwtPmt0aChsYisxLCByYiwgayk7CiAgICAgICAgcmV0dXJuIHRoaXMtPnItPmt0aChsLWxiLCByLXJiLCBrLWluTGVmdCk7CiAgICB9CgogICAgLy9jb3VudCBvZiBub3MgaW4gW2wsIHJdIExlc3MgdGhhbiBvciBlcXVhbCB0byBrCiAgICBpbnQgTFRFKGludCBsLCBpbnQgciwgaW50IGspCiAgICB7CiAgICAgICAgaWYobCA+IHIgb3IgayA8IGxvKQogICAgICAgICAgICByZXR1cm4gMDsKICAgICAgICBpZihoaSA8PSBrKQogICAgICAgICAgICByZXR1cm4gciAtIGwgKyAxOwogICAgICAgIGludCBsYiA9IGJbbC0xXSwgcmIgPSBiW3JdOwogICAgICAgIHJldHVybiB0aGlzLT5sLT5MVEUobGIrMSwgcmIsIGspICsgdGhpcy0+ci0+TFRFKGwtbGIsIHItcmIsIGspOwogICAgfQoKICAgIC8vY291bnQgb2Ygbm9zIGluIFtsLCByXSBlcXVhbCB0byBrCiAgICBpbnQgY291bnQoaW50IGwsIGludCByLCBpbnQgaykKICAgIHsKICAgICAgICBpZihsID4gciBvciBrIDwgbG8gb3IgayA+IGhpKQogICAgICAgICAgICByZXR1cm4gMDsKICAgICAgICBpZihsbyA9PSBoaSkKICAgICAgICAgICAgcmV0dXJuIHIgLSBsICsgMTsKICAgICAgICBpbnQgbGIgPSBiW2wtMV0sIHJiID0gYltyXSwgbWlkID0gKGxvK2hpKS8yOwogICAgICAgIGlmKGsgPD0gbWlkKQogICAgICAgICAgICByZXR1cm4gdGhpcy0+bC0+Y291bnQobGIrMSwgcmIsIGspOwogICAgICAgIHJldHVybiB0aGlzLT5yLT5jb3VudChsLWxiLCByLXJiLCBrKTsKICAgIH0KCiAgICAvL3N1bSBvZiBub3MgaW4gW2wgLHJdIGxlc3MgdGhhbiBvciBlcXVhbCB0byBrCiAgICBpbnQgc3VtayhpbnQgbCwgaW50IHIsIGludCBrKQogICAgewogICAgICAgIGlmKGwgPiByIG9yIGsgPCBsbykKICAgICAgICAgICAgcmV0dXJuIDA7CiAgICAgICAgaWYoaGkgPD0gaykKICAgICAgICAgICAgcmV0dXJuIGNbcl0gLSBjW2wtMV07CiAgICAgICAgaW50IGxiID0gYltsLTFdLCByYiA9IGJbcl07CiAgICAgICAgcmV0dXJuIHRoaXMtPmwtPnN1bWsobGIrMSwgcmIsIGspICsgdGhpcy0+ci0+c3VtayhsLWxiLCByLXJiLCBrKTsKICAgIH0KCiAgICB+d2F2ZWxldF90cmVlKCkKICAgIHsKICAgICAgICBpZihsKQogICAgICAgIGRlbGV0ZSBsOwogICAgICAgIGlmKHIpCiAgICAgICAgZGVsZXRlIHI7CiAgICB9Cn07CmludCBtYWluKCkKewogICAgaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbyhmYWxzZSk7CiAgICBjaW4udGllKE5VTEwpOwogICAgc3JhbmQodGltZShOVUxMKSk7CiAgICBpbnQgaSxuLGssaixxLGwscjsKICAgIGNpbiA+PiBuOwogICAgZm8oaSwgbikgY2luID4+IGFbaSsxXTsKICAgIHdhdmVsZXRfdHJlZSBUKGErMSwgYStuKzEsIDEsIE1BWCk7CiAgICBjaW4gPj4gcTsKICAgIHdoaWxlKHEtLSkKICAgIHsKICAgICAgICBpbnQgeDsKICAgICAgICBjaW4gPj4geDsKICAgICAgICBjaW4gPj4gbCA+PiByID4+IGs7CiAgICAgICAgaWYoeCA9PSAwKQogICAgICAgIHsKICAgICAgICAgICAgLy9rdGggc21hbGxlc3QKICAgICAgICAgICAgY291dCA8PCAiS3RoIHNtYWxsZXN0OiAiOwogICAgICAgICAgICBjb3V0IDw8IFQua3RoKGwsIHIsIGspIDw8IGVuZGw7CiAgICAgICAgfQogICAgICAgIGlmKHggPT0gMSkKICAgICAgICB7CiAgICAgICAgICAgIC8vbGVzcyB0aGFuIG9yIGVxdWFsIHRvIEsKICAgICAgICAgICAgY291dCA8PCAiTFRFOiAiOwogICAgICAgICAgICBjb3V0IDw8IFQuTFRFKGwsIHIsIGspIDw8IGVuZGw7CiAgICAgICAgfQogICAgICAgIGlmKHggPT0gMikKICAgICAgICB7CiAgICAgICAgICAgIC8vY291bnQgb2NjdXJlbmNlIG9mIEsgaW4gW2wsIHJdCiAgICAgICAgICAgIGNvdXQgPDwgIk9jY3VyZW5jZSBvZiBLOiAiOwogICAgICAgICAgICBjb3V0IDw8IFQuY291bnQobCwgciwgaykgPDwgZW5kbDsKICAgICAgICB9CiAgICAgICAgaWYoeCA9PSAzKQogICAgICAgIHsKICAgICAgICAgICAgLy9zdW0gb2YgZWxlbWVudHMgbGVzcyB0aGFuIG9yIGVxdWFsIHRvIEsgaW4gW2wsIHJdCiAgICAgICAgICAgIGNvdXQgPDwgIlN1bTogIjsKICAgICAgICAgICAgY291dCA8PCBULnN1bWsobCwgciwgaykgPDwgZW5kbDsKICAgICAgICB9CgogICAgfQogICAgcmV0dXJuIDA7Cn0KCg==