2173
时间限制: 1 s
空间限制: 32000 KB
题目等级 : 钻石 Diamond
题目描述 Description
老管家是一个聪明能干的人。他为财主工作了整整10年,财主为了让自已账目更加清楚。要求管家每天记k次账,由于管家聪明能干,因而管家总是让财主十分满意。但是由于一些人的挑拨,财主还是对管家产生了怀疑。于是他决定用一种特别的方法来判断管家的忠诚,他把每次的账目按1,2,3…编号,然后不定时的问管家问题,问题是这样的:在a到b号账中最少的一笔是多少?为了让管家没时间作假他总是一次问多个问题。
输入描述 Input Description
输入中第一行有两个数m,n表示有m笔账,n表示有n个问题。
第二行为m个数,分别是账目的钱数
后面n行分别是n个问题,每行有2个数字说明开始结束的账目编号。
输出描述 Output Description
输出文件中为每个问题的答案。具体查看样例。
样例输入 Sample Input
10 3
1 2 3 4 5 6 7 8 9 10
2 7
3 9
1 10
样例输出 Sample Output
2 3 1
数据范围及提示 Data Size & Hint
m<=100000
n<=100000
思路:线段树。
1 #include<iostream> 2 #include<algorithm> 3 #include<cstdio> 4 5 using namespace std; 6 7 const int N = 300010 ; 8 int num[N]; 9 int minn[N]; 10 int n,m,k,ans; 11 12 void buil(int l,int r,int rt) 13 { 14 if(l==r) 15 { 16 minn[rt]=num[l];return; 17 } 18 int m=(l+r)/2; 19 buil(l,m,rt*2); 20 buil(m+1,r,rt*2+1) ; 21 minn[rt]=min(minn[rt*2],minn[rt*2+1]); 22 } 23 void ask(int l,int r,int rt,int nowl,int nowr) 24 { 25 if(nowl<=l && nowr>=r) 26 { 27 ans=min(ans,minn[rt]); 28 return ; 29 } 30 int m=(l+r)/2; 31 if(nowl<=m)ask(l,m,rt*2,nowl,nowr); 32 if(nowr>=m+1) ask(m+1,r,rt*2+1,nowl,nowr); 33 } 34 int main() 35 { 36 scanf("%d%d",&m,&n); 37 for(int i=1;i<=m;++i) 38 scanf("%d",&num[i]); 39 buil(1,m,1); 40 for(int i=1;i<=n;++i) 41 { 42 ans=999999; 43 int a,b; 44 scanf("%d%d",&a,&b); 45 ask(1,m,1,a,b); 46 printf("%d ",ans); 47 } 48 return 0; 49 }