Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>//rimi
- using namespace std;
- #define max 100005
- #define ll long long
- int ar[max];
- vector<ll >tree(max*4);
- ll x = 0;
- void update(ll node,ll b,ll e,ll x,ll y)
- {
- if(b>e||x<b||x>e)
- {
- return;
- }
- else if(b==e && e==x )
- {
- tree[node] = y;
- ar[x]=y;
- return;
- }
- else{
- ll left = node*2;
- ll right = (node*2)+1;
- ll mid = (b+e)/2;
- update(left ,b,mid, x,y);
- update(right,mid+1,e,x,y);
- if(tree[left]>=tree[right])tree[node] = tree[left];
- else tree[node] = tree[right];
- return ;
- }
- }
- ll mm =0;
- ll m=0;
- ll query(ll node,ll b,ll e,ll x,ll y)
- {
- if(b>e||e<x||y<b)
- {
- return -INT_MAX;
- }
- else if(x==y)
- {
- return ar[x];
- }
- else if(b>=x && e<=y)
- {
- return tree[node];
- cout<<"tree "<<tree[node]<<endl;
- }
- else{
- ll left = node*2;
- ll right = (node*2)+1;
- ll mid = (b+e)/2;
- ll a = query(left,b,mid,x,y);
- ll c = query(right,mid+1,e,x,y);
- //cout<<"a c "<<a<<" "<<c<<endl;
- if(a>c)return a;
- else return c;
- }
- }
- int main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(NULL);
- ll x,y,z,a,b,c,t,i,j,n,k,h,e,w,p,q,l,o,d;
- string s1,s,s2,str;
- //tree.resize(4*n);
- map<char, ll>ma;
- cin>>n;
- for(i=1;i<=n;i++)
- {
- cin>>ar[i];
- update(1,1,n,i,ar[i]);
- }
- // input(1,1,n);
- cin>>t;
- for(i=1;i<=t;i++)
- {
- cin>>p>>q;
- mm=m=-INT_MAX;
- cout<<query(1,1,n,p,q)<<endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement