POJ 3264 Balanced Lineup
来源:互联网 发布:php 大文件分片上传 编辑:程序博客网 时间:2024/05/17 01:53
题目大意:牛排成一排,每只牛都有自己的高度,然后有q个询问,询问一个区间内的牛的最大身高和最小身高的差。
做法:线段树维护区间最大最小值。
#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1const int maxn = 50010;using namespace std;struct point{ int max1,min1;}sum[maxn<<2];void Pushup(int rt){ sum[rt].max1 = max(sum[rt<<1].max1,sum[rt<<1|1].max1); sum[rt].min1 = min(sum[rt<<1].min1,sum[rt<<1|1].min1);}void build(int l ,int r , int rt){ if(l == r) { scanf("%d",&sum[rt].max1); sum[rt].min1 = sum[rt].max1; return ; } int m = (l+r)>>1; build(lson); build(rson); Pushup(rt);}point query(int L ,int R,int l,int r ,int rt){ if(L<=l&&r<=R) return sum[rt]; int m = (r+l)>>1; point x ; x.max1 = -0x7ffffff; x.min1 = 0x7ffffff; if(L<=m) { point t = query(L,R,lson); x.max1 = max(x.max1,t.max1); x.min1 = min(x.min1,t.min1); } if(R>m) { point t = query(L,R,rson); x.max1 = max(x.max1,t.max1); x.min1 = min(x.min1,t.min1); } return x;}int main(){ int n ,q; scanf("%d%d",&n,&q); build(1,n,1); for(int i = 0 ; i < q ; i++) { int l,r; point x; scanf("%d%d",&l,&r); x = query(l,r,1,n,1); printf("%d\n",x.max1 - x.min1); } return 0;}
0 0
- POJ 3264 Balanced Lineup
- POJ 3264 Balanced Lineup
- poj 3264 Balanced Lineup
- poj 3264 Balanced Lineup
- poj 3264 Balanced Lineup
- poj 3264 Balanced Lineup
- Poj 3264 Balanced Lineup
- poj 3264 Balanced Lineup
- POJ-3264-Balanced Lineup
- poj 3264 Balanced Lineup
- poj 3264 Balanced Lineup
- POJ 3264-Balanced Lineup
- poj 3264 Balanced Lineup
- POJ 3264 Balanced Lineup
- POJ-3264-Balanced Lineup
- POJ 3264 Balanced Lineup
- POJ 3264 Balanced Lineup
- poj 3264 Balanced Lineup
- jghfgfggdfskjghgfgdsafgklhjhgfgdsfajlkhgfd
- CodeForces #292 div.2 题解
- 合家福郭敬明进口国付和鬼地方的发更多的是v银行的费工夫放到大幅度的地方
- 非递归遍历二叉树
- Tempter of the Bone 回溯法 还有奇偶剪枝
- POJ 3264 Balanced Lineup
- POJ 1062 昂贵的聘礼(最短路+枚举)
- CodeForces 276C Little Girl and Maximum Sum
- UIKit基础:13.UI基础控件的小结
- 深入浅出Android Support Annotations
- CodeForces 295A Greg and Array
- windows上运行mapreduce
- 可以这样去理解group by和聚合函数
- Failed to set permissions of path: