HDU 3264 Balanced Lineup(线段树,最值查询)
来源:互联网 发布:网络专科学位证有用吗 编辑:程序博客网 时间:2024/05/22 11:46
HDU 3264 Balanced Lineup(线段树,最值查询)
ACM
题目地址:POJ 3264 Balanced Lineup
题意:
求区间内最大值-最小值。
分析:
不用写修改,容易多了。
代码:
/** Author: illuz <iilluzen[at]gmail.com>* Blog: http://blog.csdn.net/hcbbt* File: 3264.cpp* Create Date: 2014-08-05 00:15:22* Descripton: */#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;#define repf(i,a,b) for(int i=(a);i<=(b);i++)#define lson(x) ((x) << 1)#define rson(x) ((x) << 1 | 1)typedef long long ll;const int N = 50000;const int ROOT = 1;// below is sement point updatedstruct seg {ll mmax;ll mmin;};struct segment_tree { seg node[N << 2];void update(int pos) {node[pos].mmax = max(node[lson(pos)].mmax, node[rson(pos)].mmax);node[pos].mmin = min(node[lson(pos)].mmin, node[rson(pos)].mmin);}void build(int l, int r, int pos) {if (l == r) {scanf("%lld", &node[pos].mmin);node[pos].mmax = node[pos].mmin;return;}int m = (l + r) >> 1;build(l, m, lson(pos));build(m + 1, r, rson(pos));update(pos);}// query the segment [x, y]ll query(int l, int r, int pos, int x, int y, bool ismax) {if (x <= l && r <= y) {return ismax ? node[pos].mmax : node[pos].mmin;}int m = (l + r) >> 1;ll res = ismax ? 0 : 1e6 + 1;if (x <= m)res = query(l, m, lson(pos), x, y, ismax);if (y > m) {res = ismax ? max(res, query(m + 1, r, rson(pos), x, y, ismax)) :min(res, query(m + 1, r, rson(pos), x, y, ismax));}return res;}} sgm;int n, q, l, r;int main() {while (~scanf("%d%d", &n, &q)) {sgm.build(1, n, ROOT);while (q--) {scanf("%d%d", &l, &r);printf("%lld\n", sgm.query(1, n, ROOT, l, r, 1) - sgm.query(1, n, ROOT, l, r, 0));}}return 0;}
0 0
- HDU 3264 Balanced Lineup(线段树,最值查询)
- POJ 3264 Balanced Lineup 求线段树区间最值
- poj 3264 Balanced Lineup(线段树 区间最值)
- poj Balanced Lineup (线段树区间最值+优化)
- POJ 3264 Balanced Lineup(线段树区间查询)
- poj 3264 -- Balanced Lineup (区间最值,线段树/RMQ)
- POJ 3264 Balanced Lineup (区间最值 RMQ模板/线段树)
- pku 3264 Balanced Lineup(线段树)
- POJ 3264 - Balanced Lineup(线段树)
- Balanced Lineup(POJ-3264)(线段树)
- POJ - 3264 - Balanced Lineup (线段树)
- poj 3264 Balanced Lineup(线段树)
- POJ 3264Balanced Lineup(线段树)
- poj 3264 Balanced Lineup(线段树)
- POJ 3264Balanced Lineup (线段树)
- 3264 Balanced Lineup 线段树
- Balanced Lineup(线段树)
- POJ3264 Balanced Lineup(线段树,区间最值)
- 猜想 QQ好友分组 (微信好友)多层次分组策略 QQ好友过多
- HDU 1166 敌兵布阵(线段树入门,单点更新)
- [笔记]javascript面向对象 - 继承的几种模式
- HDU 1686 Lazy Math Instructor(中缀转后缀,栈模拟)
- JavaSe基础XX04——多维数组
- HDU 3264 Balanced Lineup(线段树,最值查询)
- 布线问题
- 输出路径的最小编辑距离
- 安慰奶牛
- 译稿:UserAgent的历史变迁
- 15.2 Palindrome Number
- 浏览器userAgent大全
- 故事过户到撒啊啊啊一一一啊啊啊啊
- 的故事的分数高投影机梵蒂冈的人