POJ 3264.Balanced Lineup【线段树】【4月28】
来源:互联网 发布:jquery.form.js data 编辑:程序博客网 时间:2024/06/16 08:57
Balanced Lineup
Time Limit: 5000MS Memory Limit: 65536KTotal Submissions: 43653 Accepted: 20473Case Time Limit: 2000MS
Description
For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer John decides to organize a game of Ultimate Frisbee with some of the cows. To keep things simple, he will take a contiguous range of cows from the milking lineup to play the game. However, for all the cows to have fun they should not differ too much in height.
Farmer John has made a list of Q (1 ≤ Q ≤ 200,000) potential groups of cows and their heights (1 ≤height ≤ 1,000,000). For each group, he wants your help to determine the difference in height between the shortest and the tallest cow in the group.
Input
Line 1: Two space-separated integers, N andQ.
Lines 2..N+1: Line i+1 contains a single integer that is the height of cowi
Lines N+2..N+Q+1: Two integers A and B (1 ≤A ≤ B ≤ N), representing the range of cows from A toB inclusive.
Lines 2..N+1: Line i+1 contains a single integer that is the height of cowi
Lines N+2..N+Q+1: Two integers A and B (1 ≤A ≤ B ≤ N), representing the range of cows from A toB inclusive.
Output
Lines 1..Q: Each line contains a single integer that is a response to a reply and indicates the difference in height between the tallest and shortest cow in the range.
Sample Input
6 31734251 54 62 2
Sample Output
630很基础的一道线段树,而且没有更新操作。
#include<iostream>#include<cstdio>#include<cstring>using namespace std;const int MAX = 50005;struct ss{ int left, right, minh, maxh;}segtree[MAX*4];int N, Q, x, y, maxn, minx;void build(int node, int l, int r){ segtree[node].left = l; segtree[node].right = r; if(l == r) { scanf("%d", &segtree[node].minh); segtree[node].maxh = segtree[node].minh; return; } build(node*2, l, (l+r)/2); build(node*2+1, (l+r)/2+1, r); segtree[node].minh = min(segtree[node*2].minh, segtree[node*2+1].minh); segtree[node].maxh = max(segtree[node*2].maxh, segtree[node*2+1].maxh);}void query(int node, int l, int r){ if(l > segtree[node].right || r < segtree[node].left) return; if(l <= segtree[node].left && r >= segtree[node].right) { minx = min(minx, segtree[node].minh); maxn = max(maxn, segtree[node].maxh); return; } query(node*2, l, r); query(node*2+1, l ,r);}int main(){ scanf("%d %d", &N, &Q); build(1, 1, N); while(Q--) { minx = 0x7FFFFFFF; maxn = 0; scanf("%d %d", &x, &y); query(1, x, y); cout << maxn-minx << endl; } return 0;}
0 0
- POJ 3264.Balanced Lineup【线段树】【4月28】
- poj 3264 Balanced Lineup rmq/线段树
- POJ 3264 Balanced Lineup RMQ / 线段树
- 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 (RMQ线段树)
- POJ 3264 Balanced Lineup 线段树RMQ
- [线段树] POJ 3264 balanced lineup
- poj 3264 Balanced Lineup 线段树
- POJ 3264 Balanced Lineup //简单线段树
- poj 3264 Balanced Lineup(线段树)
- POJ 3264 Balanced Lineup( 线段树&&RMQ )
- 啊哈!算法—坑爹的奥数
- 告诉你一些鲜为人知的编程真相
- 如何把控工作过程中的风险?
- 删除链表中的元素
- Hadoop新旧集群迁移
- POJ 3264.Balanced Lineup【线段树】【4月28】
- log4j.xml配置详解
- Fragment里面的Viewpaper不显示内容不执行getItem的解决方案
- 诡异的volatile
- Java工具类---数组操作ArrayUtil
- 01背包和完全背包代码
- 如何看待阿里的去IOE
- hdu 1711 水kmp
- iOS开发之高效绘图,通过一个绘图应用进行讲解