Friday, July 31, 2015

1082 - Array Queries

#include<bits/stdc++.h>
using namespace std;
 
long long arry[100010],segment[400050];
 
 
void build(long long at, long long L, long long R)
{
    if(L==R)
    {
        segment[at] = arry[L];
        return;
    }
    long long mid = (L+R)/2;
    build(at*2,L,mid);
    build(at*2+1,mid+1,R);
    segment[at] = segment[at*2] > segment[at*2+1]?segment[at*2+1]:segment[at*2];
}
 
long long query(long long at,long long L, long long R, long long l, long long r)
{
    if(r<L || l>R)
        return 1000000;
    if(L==R)
        return segment[at];
    if(l <= L && r >= R)
        return segment[at];
    long long mid = (L+R)/2;
    long long x = query(at*2, L, mid, l, r);
    long long y = query(at*2+1, mid+1, R, l, r);
    return x > y? y:x;
}
 
int main()
{
    //freopen("L - Array Queries.txt", "r",stdin);
    long long T,n,q,i,a,b,c=1;
    scanf("%lld",&T);
    while(T--)
    {
        scanf("%lld %lld", &n, &q);
        for(i=1; i<=n; i++)
            scanf("%lld",&arry[i]);
        build(1,1,n);
        printf("Case %lld:\n",c++);
        for(i=1; i<=q; i++)
        {
            scanf("%lld %lld", &a, &b);
            printf("%lld\n",query(1,1,n,a,b));
        }
    }
    return 0;
}

No comments:

Post a Comment

Triathlon

Triathlon - CodeChef # include < bits/stdc++.h > using namespace std ; # define fi first # define se second # define mp ...