【线段树】HDU 5443 The Water Problem

题目链接:

  http://acm.hdu.edu.cn/showproblem.php?pid=5443

题目大意

  T组数据。n个值,m个询问,求区间l到r里的最大值。(n,m<=1000)

题目思路:

  【线段树】

  线段树裸题。求区间最大值。

 1 //
 2 //by coolxxx
 3 //
 4 #include<iostream>
 5 #include<algorithm>
 6 #include<string>
 7 #include<iomanip>
 8 #include<memory.h>
 9 #include<time.h>
10 #include<stdio.h>
11 #include<stdlib.h>
12 #include<string.h>
13 //#include<stdbool.h>
14 #include<math.h>
15 #define min(a,b) ((a)<(b)?(a):(b))
16 #define max(a,b) ((a)>(b)?(a):(b))
17 #define abs(a) ((a)>0?(a):(-(a)))
18 #define lowbit(a) (a&(-a))
19 #define sqr(a) ((a)*(a))
20 #define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
21 #define eps (1e-8)
22 #define J 10000000
23 #define MAX 0x7f7f7f7f
24 #define PI 3.1415926535897
25 #define N 1004
26 using namespace std;
27 typedef long long LL;
28 int cas,cass;
29 int n,m,lll,ans;
30 int t[N<<2];
31 void change(int l,int r,int a,int k,int x)
32 {
33     if(a>r || a<l || l>r)return;
34     if(l==r && l==a){t[k]=x;return;}
35     change(l,(l+r)/2,a,k+k,x);
36     change((l+r)/2+1,r,a,k+k+1,x);
37     t[k]=max(t[k+k],t[k+k+1]);
38 }
39 int query(int l,int r,int a,int b,int k)
40 {
41     if(l>r || l>b || r<a)return 0;
42     if(a<=l && r<=b)return t[k];
43     int mid=(l+r)/2,x1,x2;
44     x1=query(l,mid,a,b,k+k);
45     x2=query(mid+1,r,a,b,k+k+1);
46     return max(x1,x2);
47 }
48 int main()
49 {
50     #ifndef ONLINE_JUDGE
51 //    freopen("1.txt","r",stdin);
52 //    freopen("2.txt","w",stdout);
53     #endif
54     int i,j,l,r,x;
55     for(scanf("%d",&cas);cas;cas--)
56 //    for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
57 //    while(~scanf("%s",s))
58 //    while(~scanf("%d",&n))
59     {
60         memset(t,0,sizeof(t));
61         scanf("%d",&n);
62         for(i=1;i<=n;i++)
63         {
64             scanf("%d",&x);
65             change(1,n,i,1,x);
66         }
67         scanf("%d",&m);
68         for(i=1;i<=m;i++)
69         {
70             scanf("%d%d",&l,&r);
71             printf("%d
",query(1,n,l,r,1));
72         }
73     }
74     return 0;
75 }
76 /*
77 //
78 
79 //
80 */
千万不要点
原文地址:https://www.cnblogs.com/Coolxxx/p/5760642.html