#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long int
#define lc ((n)<<1)
#define rc ((n)<<1|1)
#define pb push_back
using namespace std;
using namespace __gnu_pbds;
typedef tree<pair<ll,ll>, null_type, less<pair<ll,ll>>, rb_tree_tag,
tree_order_statistics_node_update> pds;
ll n;
const ll N=463005;
vector <ll> arr(N);
pds mst[N];
void init(ll pp)
{
for(ll i=0;i<4*pp;i++)
{
mst[i].clear();
}
}
void build(ll n,ll b,ll e)
{
if(b==e)
{
mst[n].insert({arr[b],b});
return;
}
for(ll i=b;i<=e;i++)
mst[n].insert({arr[i],i});
ll mid=(b+e)/2;
build(lc,b,mid);
build(rc,mid+1,e);
}
ll query(ll n,ll b,ll e,ll i,ll j,ll v,ll idx)
{
if(b>j or e<i) return 0;
if(b>=i and e<=j)
{
ll k=mst[n].order_of_key({v,idx});
return k;
}
ll mid=(b+e)/2;
return query(lc,b,mid,i,j,v,idx) + query(rc,mid+1,e,i,j,v,idx);
}
void update(ll n,ll b,ll e,ll i,ll v,ll nw)
{
if(i<b or e<i) return;
if(b==i and e==i)
{
mst[n].erase(mst[n].find({v,i}));
mst[n].insert({nw,i});
return;
}
ll mid=(b+e)/2;
update(lc,b,mid,i,v,nw);
update(rc,mid+1,e,i,v,nw);
mst[n].erase(mst[n].find({v,i}));
mst[n].insert({nw,i});
}
int main()
{
ll test,i,j,queries;
scanf("%lld",&test); // number of test cases
while(test--)
{
scanf("%lld",&n); //number of elements in the array
init(n); // initializing the policy based tree
for(i=1;i<=n;i++)
{
scanf("%lld",&arr[i]); //scanning the array
}
build(1,1,n);
cin>>queries; //number of queries
while(queries--)
{
ll choice;
scanf("%lld",&choice); //if choice is 0 -> it represents query, choice 1 -> represents update
if(choice==0)
{
ll l,r,x;
scanf("%lld %lld %lld",&l,&r,&x); // the x-th number in sorted a[l ... r] segment
ll low = mst[1].find_by_order(0)->first, high = mst[1].find_by_order(n-1)->first , mid, ans ;
// binary search to find the x-th number
while(low <= high)
{
mid = low + high >> 1;
ll k = query(1,1,n,l,r,mid,1005); // i have considered the highest element in the array to be 1000, change according to your program
if(k >=x )
{
ans = mid;
high = mid-1;
}
else low = mid+1;
}
printf("%lld\n",ans);
}
else
{
// Update the profit of the idx-th shop to x
ll idx,x;
scanf("%lld %lld",&idx,&x);
update(1,1,n,idx,arr[idx],x);
arr[idx]=x;
}
}
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNpbmNsdWRlIDxleHQvcGJfZHMvYXNzb2NfY29udGFpbmVyLmhwcD4gCiNpbmNsdWRlIDxleHQvcGJfZHMvdHJlZV9wb2xpY3kuaHBwPiAKI2RlZmluZSBsbCBsb25nIGxvbmcgaW50CiNkZWZpbmUgbGMgICAgICAgICAgICAgICAgICAgICAgKChuKTw8MSkKI2RlZmluZSByYyAgICAgICAgICAgICAgICAgICAgICAoKG4pPDwxfDEpCiNkZWZpbmUgcGIgcHVzaF9iYWNrCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CnVzaW5nIG5hbWVzcGFjZSBfX2dudV9wYmRzOwoKdHlwZWRlZiB0cmVlPHBhaXI8bGwsbGw+LCBudWxsX3R5cGUsIGxlc3M8cGFpcjxsbCxsbD4+LCByYl90cmVlX3RhZywgCiAgICAgICAgICAgICB0cmVlX29yZGVyX3N0YXRpc3RpY3Nfbm9kZV91cGRhdGU+IHBkczsgCgoKbGwgbjsKY29uc3QgbGwgTj00NjMwMDU7CnZlY3RvciA8bGw+IGFycihOKTsKcGRzIG1zdFtOXTsKCnZvaWQgaW5pdChsbCBwcCkKewogICAgZm9yKGxsIGk9MDtpPDQqcHA7aSsrKQogICAgewogICAgICAgIG1zdFtpXS5jbGVhcigpOwogICAgfQp9Cgp2b2lkIGJ1aWxkKGxsIG4sbGwgYixsbCBlKQp7CiAgICBpZihiPT1lKQogICAgewogICAgICAgIG1zdFtuXS5pbnNlcnQoe2FycltiXSxifSk7CiAgICAgICAgc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c3QgY2FzZXMKICAgIHdoaWxlKHRlc3QtLSkKICAgIHsKICAgICAgICAKICAgICAgICBzY2FuZigiJWxsZCIsJm4pOyAgLy9udW1iZXIgb2YgZWxlbWVudHMgaW4gdGhlIGFycmF5CiAgICAgICAgaW5pdChuKTsgICAgICAgIC8vIGluaXRpYWxpemluZyB0aGUgcG9saWN5IGJhc2VkIHRyZWUKICAgICAgICAKICAgICAgICBmb3IoaT0xO2k8PW47aSsrKQogICAgICAgIHsKICAgICAgICAgICAgc2NhbmYoIiVsbGQiLCZhcnJbaV0pOyAgLy9zY2FubmluZyB0aGUgYXJyYXkKICAgICAgICB9CiAgICAgICAgCiAgICAgICAgYnVpbGQoMSwxLG4pOwogICAgICAgIAogICAgICAgIGNpbj4+cXVlcmllczsgICAgICAgLy9udW1iZXIgb2YgcXVlcmllcwogICAgICAgIHdoaWxlKHF1ZXJpZXMtLSkKICAgICAgICB7CiAgICAgICAgICAgIGxsIGNob2ljZTsKICAgICAgICAgICAgc2NhbmYoIiVsbGQiLCZjaG9pY2UpOyAgICAgIC8vaWYgY2hvaWNlIGlzIDAgLT4gaXQgcmVwcmVzZW50cyBxdWVyeSwgY2hvaWNlIDEgLT4gcmVwcmVzZW50cyB1cGRhdGUKICAgICAgICAgICAgaWYoY2hvaWNlPT0wKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBsbCBsLHIseDsKICAgICAgICAgICAgICAgIHNjYW5mKCIlbGxkICVsbGQgJWxsZCIsJmwsJnIsJngpOyAgICAgICAvLyAgdGhlIHgtdGggbnVtYmVyIGluIHNvcnRlZCBhW2wgLi4uIHJdIHNlZ21lbnQKICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgbGwgbG93ID0gbXN0WzFdLmZpbmRfYnlfb3JkZXIoMCktPmZpcnN0LCBoaWdoID0gbXN0WzFdLmZpbmRfYnlfb3JkZXIobi0xKS0+Zmlyc3QgLCBtaWQsIGFucyA7CiAgICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgIC8vIGJpbmFyeSBzZWFyY2ggdG8gZmluZCB0aGUgeC10aCBudW1iZXIKICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgd2hpbGUobG93IDw9IGhpZ2gpIAogICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgIG1pZCA9IGxvdyArIGhpZ2ggPj4gMTsKICAgICAgICAgICAgICAgICAgICBsbCBrID0gcXVlcnkoMSwxLG4sbCxyLG1pZCwxMDA1KTsgICAgICAgLy8gaSBoYXZlIGNvbnNpZGVyZWQgdGhlIGhpZ2hlc3QgZWxlbWVudCBpbiB0aGUgYXJyYXkgdG8gYmUgMTAwMCwgY2hhbmdlIGFjY29yZGluZyB0byB5b3VyIHByb2dyYW0KICAgICAgICAgICAgICAgICAgICBpZihrID49eCApIAogICAgICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAgICAgYW5zID0gbWlkOwogICAgICAgICAgICAgICAgICAgICAgICBoaWdoID0gbWlkLTE7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIGVsc2UgbG93ID0gbWlkKzE7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgIHByaW50ZigiJWxsZFxuIixhbnMpOwogICAgICAgICAgICAgICAgCiAgICAgICAgICAgIH0KICAgICAgICAgICAgZWxzZQogICAgICAgICAgICB7ICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgICAvLyBVcGRhdGUgdGhlIHByb2ZpdCBvZiB0aGUgaWR4LXRoIHNob3AgdG8geAogICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgICBsbCBpZHgseDsKICAgICAgICAgICAgICAgIHNjYW5mKCIlbGxkICVsbGQiLCZpZHgsJngpOwogICAgICAgICAgICAgICAgdXBkYXRlKDEsMSxuLGlkeCxhcnJbaWR4XSx4KTsKICAgICAgICAgICAgICAgIGFycltpZHhdPXg7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgCiAgICAgICAgfQogICAgICAgIAogICAgfQogICAgcmV0dXJuIDA7CiAgICAKfQ==