poj 3264 线段树区间最大最小值
2015-08-07 23:22
323 查看
题意:
给n组数据,q个询问,每次询问区间l,r最大值与最小值的差是多少。
解析:
睡前一水,却错了2发。
询问的时候写错了,并不是直接去更新maxx-minn的最大值。。。
是直接更新maxx,minn,然后最后一减就行了。
代码:
给n组数据,q个询问,每次询问区间l,r最大值与最小值的差是多少。
解析:
睡前一水,却错了2发。
询问的时候写错了,并不是直接去更新maxx-minn的最大值。。。
是直接更新maxx,minn,然后最后一减就行了。
代码:
#include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> #include <cstring> #include <cmath> #include <stack> #include <vector> #include <queue> #include <map> #include <set> #include <climits> #include <cassert> #define LL long long #define lson lo, mi, rt << 1 #define rson mi + 1, hi, rt << 1 | 1 using namespace std; const int maxn = 50000 + 10; const int inf = 0x3f3f3f3f; const double eps = 1e-8; const double pi = acos(-1.0); const double ee = exp(1.0); int minn, maxx; int maxPoint[maxn << 2]; int minPoint[maxn << 2]; void pushup(int rt) { maxPoint[rt] = max(maxPoint[rt << 1], maxPoint[rt << 1 | 1]); minPoint[rt] = min(minPoint[rt << 1], minPoint[rt << 1 | 1]); } void build(int lo, int hi, int rt) { if (lo == hi) { scanf("%d", &maxPoint[rt]); minPoint[rt] = maxPoint[rt]; return; } int mi = (lo + hi) >> 1; build(lson); build(rson); pushup(rt); } void query(int L, int H, int lo, int hi, int rt) { if (L <= lo && hi <= H) { maxx = max(maxx, maxPoint[rt]); minn = min(minn, minPoint[rt]); return; } int mi = (lo + hi) >> 1; if (L <= mi) query(L, H, lson); if (mi < H) query(L, H, rson); } int main() { #ifdef LOCAL freopen("in.txt", "r", stdin); #endif // LOCAL int n, q; while (~scanf("%d%d", &n, &q)) { memset(maxPoint, 0, sizeof(maxPoint)); memset(minPoint, 0, sizeof(minPoint)); build(1, n, 1); while (q--) { minn = inf, maxx = 0; int fr, to; scanf("%d%d", &fr, &to); query(fr, to, 1, n, 1); printf("%d\n", maxx - minn); } } return 0; }
相关文章推荐
- Lala Land and Apple Trees
- unity 反编译 step1 disUnity
- [leedcode 223] Rectangle Area
- -bash:wget command not found的解决方法
- [USACO2015February,Silver] Problem1.Censoring (Silver)
- ACM今日学习总结(2015.08.07)
- -bash:wget command not found的解决方法
- Leetcode45 Jump Game II
- HDU 5006 Resistance (2014年鞍山赛区网络赛J题)
- C语言多级指针
- [翻译] PNChart
- css3绘制中国结
- C++模板函数的小问题解决
- gulp将请求转为请求本地JSON
- Kernel KNN ( K-Nearest Neighbors )
- 最小和
- Codeforces Round #Pi (Div. 2) ABCD
- C语言函数相关
- 【转】 Mysql全文搜索match...against的用法
- !HDU 4380 三角屋内有奇数个宝藏的三角形有多少个-计算几何-(向量叉乘&线段与点的关系&暴力枚举)