HDU 5443 The Water Problem

The Water Problem

Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 153    Accepted Submission(s): 123


Problem Description
In Land waterless, water is a very limited resource. People always fight for the biggest source of water. Given a sequence of water sources with a1,a2,a3,...,anrepresenting the size of the water source. Given a set of queries each containing 2 integers l and r, please find out the biggest water source between al and ar.
 

 

Input
First you are given an integer T(T≤10) indicating the number of test cases. For each test case, there is a number n(0≤n≤1000) on a line representing the number of water sources. n integers follow, respectively a1,a2,a3,...,an, and each integer is in {1,...,106}. On the next line, there is a number q(0≤q≤1000) representing the number of queries. After that, there will be q lines with two integers l and r(1≤l≤r≤n) indicating the range of which you should find out the biggest water source.
 

 

Output
For each query, output an integer representing the size of the biggest water source.
 

 

Sample Input
3 1 100 1 1 1 5 1 2 3 4 5 5 1 2 1 3 2 4 3 4 3 5 3 1 999999 1 4 1 1 1 2 2 3 3 3
 

 

Sample Output
100 2 3 4 4 5 1 999999 999999 1

 区间最值问题 rmq和线段树都可以解决

/* ***********************************************
Author        :pk28
Created Time  :2015/9/13 19:47:01
File Name     :4.cpp
************************************************ */
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 0x3f3f3f3f
#define maxn 1000+10
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << 61;
const double eps=1e-5;
using namespace std;

bool cmp(int a,int b){
    return a>b;
}
int t,n,m;
int a[1100];
int dp[maxn][20];

void rmq_init(){
    //cle(dp);
    for(int i=0;i<n;i++)
        dp[i][0]=a[i];
    for(int j=1;(1<<j)<=n;j++)
        for(int i=0;i+(1<<j)-1<n;i++){
            dp[i][j]=max(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
        }
}
int rmq(int l,int r){
    int k=0;
    while(1<<(k+1)<=r+1-l)k++;
    return max(dp[l][k],dp[r-(1<<k)+1][k]);
}
int main()
{
    #ifndef ONLINE_JUDGE
    //freopen("in.txt","r",stdin);
    #endif
    //freopen("out.txt","w",stdout);
    cin>>t;
    while(t--){
        int b;
        scanf("%d",&n);
        for(int i=0;i<n;i++){
            scanf("%d",&a[i]);
        }
        rmq_init();
        int l,r;
        scanf("%d",&m);
        for(int i=1;i<=m;i++){
            scanf("%d %d",&l,&r);
            printf("%d\n",rmq(l-1,r-1));
        }
    }
    return 0;
}

 

posted on 2015-09-13 20:08  Beserious  阅读(221)  评论(0)    收藏  举报