【北大夏令营笔记-线段树】POJ3264-Balanced Lineup
来源:互联网 发布:java怎么判断文件大小 编辑:程序博客网 时间:2024/05/22 17:50
Balanced Lineup
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 32777 Accepted: 15424
Case 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 and Q.
Lines 2..N+1: Line i+1 contains a single integer that is the height of cow i
Lines N+2..N+Q+1: Two integers A and B (1 ≤ A ≤ B ≤ N), representing the range of cows from A to B 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 3
1
7
3
4
2
5
1 5
4 6
2 2
Sample Output
6
3
0
Source
USACO 2007 January Silver
题意:求给定Q(1<=Q<=200000)个数a1,a2...aq,多次求任意区间内ai-aj中最大数和最小数的差;
思路:线段树查询;
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 32777 Accepted: 15424
Case 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 and Q.
Lines 2..N+1: Line i+1 contains a single integer that is the height of cow i
Lines N+2..N+Q+1: Two integers A and B (1 ≤ A ≤ B ≤ N), representing the range of cows from A to B 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 3
1
7
3
4
2
5
1 5
4 6
2 2
Sample Output
6
3
0
Source
USACO 2007 January Silver
题意:求给定Q(1<=Q<=200000)个数a1,a2...aq,多次求任意区间内ai-aj中最大数和最小数的差;
思路:线段树查询;
AC代码:
#include<stdio.h>#include<string.h>#define MAX_INF 9999999#define MIN_INF -9999999struct Node{ int l,r; int max,min;}a[200005];int max(int n,int m){ return n>m?n:m;}int min(int n,int m){ return n<m?n:m;}void Build(int n,int l,int r){ a[n].l=l; a[n].r=r; a[n].max=MIN_INF; a[n].min=MAX_INF; if(l==r) return; Build(2*n,l,(l+r)/2); Build(2*n+1,(l+r)/2+1,r); }void Insert(int n,int v,int num){ if(a[n].max<num) a[n].max=num; if(a[n].min>num) a[n].min=num; if(a[n].l==a[n].r) return; if(v<=(a[n].l+a[n].r)/2) Insert(n*2,v,num); else Insert(n*2+1,v,num);}int QMax(int n,int l,int r){ if(a[n].l==l&&a[n].r==r) return a[n].max; if(r<=(a[n].l+a[n].r)/2) return QMax(n*2,l,r); else if(l>=(a[n].l+a[n].r)/2+1) return QMax(n*2+1,l,r); else { return max(QMax(n*2,l,(a[n].l+a[n].r)/2),QMax(n*2+1,(a[n].l+a[n].r)/2+1,r)); }}int QMin(int n,int l,int r){ if(a[n].l==l&&a[n].r==r) return a[n].min; if(r<=(a[n].l+a[n].r)/2) return QMin(n*2,l,r); else if(l>=(a[n].l+a[n].r)/2+1) return QMin(n*2+1,l,r); else { return min(QMin(n*2,l,(a[n].l+a[n].r)/2),QMin(n*2+1,(a[n].l+a[n].r)/2+1,r)); }}void QMinus(int n,int l,int r){ if(a[n].l==l&&a[n].r==r) { printf("%d\n",a[n].max-a[n].min); } else { if(r<=(a[n].l+a[n].r)/2) QMinus(n*2,l,r); else if(l>=(a[n].l+a[n].r)/2+1) QMinus(n*2+1,l,r); else { int m1=QMax(n*2,l,(a[n].l+a[n].r)/2); int m2=QMax(n*2+1,(a[n].l+a[n].r)/2+1,r); int m3=QMin(n*2,l,(a[n].l+a[n].r)/2); int m4=QMin(n*2+1,(a[n].l+a[n].r)/2+1,r); printf("%d\n",max(m1,m2)-min(m3,m4)); } }}int main(){ int i,j,n,m,x,l,r; while(scanf("%d %d",&n,&m)!=EOF) { Build(1,1,n); for(i=1;i<=n;i++) { scanf("%d",&x); Insert(1,i,x); } for(i=0;i<m;i++) { scanf("%d %d",&l,&r); QMinus(1,l,r); } } return 0;}
0 0
- 【北大夏令营笔记-线段树】POJ3264-Balanced Lineup
- Balanced Lineup poj3264 线段树
- poj3264,Balanced Lineup,线段树
- poj3264 Balanced Lineup(线段树)
- poj3264 Balanced Lineup(线段树)
- POJ3264 Balanced Lineup(线段树)
- POJ3264 Balanced Lineup 【线段树】
- POJ3264 Balanced Lineup(线段树应用)
- POJ3264 Balanced Lineup 线段树基础
- Balanced Lineup(poj3264,线段树入门)
- POJ3264 Balanced Lineup 【线段树】+【单点更新】
- POJ3264 Balanced Lineup RMQ 线段树
- POJ3264 Balanced Lineup(线段树入门)
- POJ3264--Balanced Lineup(线段树模板题)
- POJ3264 Balanced Lineup 线段树|ST表
- 线段树之POJ3264 Balanced Lineup
- POJ3264 Balanced Lineup 线段树||RMQ
- 【POJ3264】Balanced Lineup,线段树入门
- B-树小结
- Ruby基础知识-求50以内的素数
- 浅谈测试驱动开发(TDD)
- 黑马程序员-数组的几种初始化方式
- Java PreparedStatement
- 【北大夏令营笔记-线段树】POJ3264-Balanced Lineup
- cocos2d-x常用工具
- urlEncode
- Ruby基础知识-times , upto , downto , each ,step
- 【Android】资源id(0xPPTTSSSS): PackageId+TypeId+SeqNum
- 80端口被屏蔽的解决方法,及外网访问内网P2P穿透软件实现
- 《C++ primer 5》 chapter 2.1
- 发现个好东西啊,计算圆周率
- mac上安装node