• A


    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,...,an representing 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

    求区间内最大的水源,之前区间dp没法做,会T,用rmq

    #include<iostream>
    #include<stdio.h>
    #include<stdlib.h>
    #include <iomanip>
    #include<cmath>
    #include<float.h> 
    #include<string.h>
    #include<algorithm>
    #define sf scanf
    #define pf printf
    #define scf(x) scanf("%d",&x)
    #define scff(x,y) scanf("%d%d",&x,&y)
    #define prf(x) printf("%d
    ",x) 
    #define mm(x,b) memset((x),(b),sizeof(x))
    #include<vector>
    #include<queue>
    //#include<map>
    #define rep(i,a,n) for (int i=a;i<n;i++)
    #define per(i,a,n) for (int i=a;i>=n;i--)
    typedef long long ll;
    const ll mod=1e9+7;
    const double eps=1e-8;
    const int inf=0x3f3f3f3f;
    using namespace std;
    const double pi=acos(-1.0);
    const int N=1e3+10;
    int a[N],MAX[N][20];
    void first(int n)
    {
    	for(int j=1;(1<<j)<=n;j++)
    	{
    		for(int i=1;i+(1<<j)-1<=n;i++)
    		{
    			MAX[i][j]=max(MAX[i][j-1],MAX[i+(1<<(j-1))][j-1]);
    			//MIN[i][j]=min(MIN[i][j-1],MIN[i+(1<<(j-1))][j-1];
    		}
    	}
    }
    int solve(int l,int r)
    {
    	int x=0;
    	while(l-1+(1<<x)<=r) x++;
    	x--;
    	return max(MAX[l][x],MAX[r-(1<<x)+1][x]);
    }
    int main()
    {
    	int re,n,tot,l,r;scf(re);
    	while(re--)
    	{
    		mm(a,0);
    		mm(MAX,0);
    		scf(n);
    		rep(i,1,n+1)
    		{
    			scf(a[i]);
    			MAX[i][0]=a[i];
    		}
    		first(n);
    		scf(tot);
    		while(tot--)
    		{
    			scff(l,r);
    			prf(solve(l,r));
    		}
    	}
    	return 0;
    }
    
  • 相关阅读:
    vue的devTools谷歌插件安装使用
    springcloud(14)seata分布式事务
    springcloud(13)zipkin工具的使用
    springcloud(12)SentinelResource注解配置应用及其他
    springcloud(11)sentinel安装和基本使用
    springcloud(10)Nacos集群(windows环境)
    springcloud(9)Nacos基本操作
    springcloud(8)GateWay网关
    springcloud(7)hystrix服务熔断和dashboard
    通用语言
  • 原文地址:https://www.cnblogs.com/wzl19981116/p/9577868.html
Copyright © 2020-2023  润新知