codevs 2173 忠诚

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

线段树:

上一节去下几结min

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 using namespace std;
 5 #define N 300006
 6 int a[N],sum[N];
 7 
 8 void update(int rt){
 9     sum[rt]=min(sum[rt*2],sum[rt*2+1]);
10 }
11 
12 void build(int l,int r,int rt){
13     if(l==r)
14     {
15         sum[rt]=a[l];
16         return;
17     }
18     int m=(l+r)/2;
19     build(l,m,rt*2);
20     build(m+1,r,rt*2+1);
21     update(rt);
22 }
23 int ans;
24 int nowr,nowl;
25     
26 void query(int l,int r,int rt){
27     if(nowr>=r&&nowl<=l)
28     {
29         ans=min(ans,sum[rt]);return ;
30     }
31     int m=(r+l)/2;
32     if(nowl<=m)query(l,m,rt*2);
33     if(nowr>m)query(m+1,r,rt*2+1);
34 }
35 
36 int ans1;    
37 int main(){
38     int n,m;
39     scanf("%d%d",&n,&m);
40     for(int i=1;i<=n;i++)
41     scanf("%d",a+i);
42     build(1,n,1);
43     for(int i=1;i<=m;i++){
44         scanf("%d%d",&nowl,&nowr);
45         ans=0x7fffffff;
46         query(1,n,1);
47         printf("%d ",ans);//线段树 
48     }
49     return 0;
50 }
原文地址:https://www.cnblogs.com/sssy/p/6792326.html