您的位置:首页 > 其它

hdu5443 The Water Problem(线段树)

2015-09-14 16:20 239 查看
求区间最大值 写了一颗线段树

比赛的时候第一眼就看见了这个 - -

但是手速太慢了还是写了8分多钟

#include <bits/stdc++.h>
#define lson num<<1
#define rson num<<1|1
#define gl l,m,lson
#define gr m+1,r,rson
#define PARA int l=1,int r=n,int num=1
using namespace std;
const int MAXN = 1e6+10;

int n;
struct SegTree
{
int st[MAXN<<2];
void init(PARA)
{
if(l==r)
scanf("%d",&st[num]);
else
{
int m=l+r>>1;
init(gl);
init(gr);
st[num]=max(st[lson],st[rson]);
}
}
int query(int a,int b,PARA)
{
if(a<=l&&r<=b)
return st[num];
else
{
int m=l+r>>1;
if(b<=m)
return query(a,b,gl);
else if(a>m)
return query(a,b,gr);
else return max(query(a,b,gl),query(a,b,gr));
}
}
}soul;

int main()
{
int T,m;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
soul.init();
int a,b;
scanf("%d",&m);
while(m--)
{
scanf("%d%d",&a,&b);
printf("%d\n",soul.query(a,b));
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: