博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj 3264(简单线段树)
阅读量:6698 次
发布时间:2019-06-25

本文共 2745 字,大约阅读时间需要 9 分钟。

思路很明确,直接用线段树.

 

Balanced Lineup
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 26161   Accepted: 12250
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 31734251 54 62 2

Sample Output

630

Source

 

#include 
#include
#include
using namespace std;#define N 50050#define INF 0x3fffffffint n,q;struct node{ int b,d; int mx,mi; node* Tl; node* Tr;}edge[10*N];int cnt;node *Tbuild(int s,int t){ node* tmp=&edge[cnt++]; tmp->mi=INF; tmp->mx=-1; tmp->Tl=tmp->Tr=NULL; tmp->b=s; tmp->d=t; int mid=(s+t)/2; if(s!=t) { tmp->Tl=Tbuild(s,mid); tmp->Tr=Tbuild(mid+1,t); } return tmp;}node* head;void insert(node* tmp,int s,int k){ if(s>tmp->mx) tmp->mx=s; if(s
mi) tmp->mi=s; if(tmp->b==tmp->d&&tmp->d==k) return ; int mid = (tmp->b+tmp->d)/2; if(k<=mid) { insert(tmp->Tl,s,k); } else insert(tmp->Tr,s,k);}int findmx(node * tmp,int s,int t){ if(tmp->b==s&&tmp->d==t) { return tmp->mx; } int mid=(tmp->b+tmp->d)/2; if(s>mid) return findmx(tmp->Tr,max(mid+1,s),t); if(t<=mid) return findmx(tmp->Tl,s,min(mid,t)); return max(findmx(tmp->Tl,s,mid),findmx(tmp->Tr,mid+1,t));}int findmi(node * tmp,int s,int t){ if(tmp->b==s&&tmp->d==t) { return tmp->mi; } int mid=(tmp->b+tmp->d)/2; if(s>mid) return findmi(tmp->Tr,max(mid+1,s),t); if(t<=mid) return findmi(tmp->Tl,s,min(mid,t)); return min(findmi(tmp->Tl,s,mid),findmi(tmp->Tr,mid+1,t));}int main(){ cnt=0; scanf("%d%d",&n,&q); head=Tbuild(1,n); for(int i=1;i<=n;i++) { int x; scanf("%d",&x); insert(head,x,i); } for(int i=0;i

 

转载地址:http://yiloo.baihongyu.com/

你可能感兴趣的文章
基于用户投票的排名算法(一):Delicious和Hacker News
查看>>
JavaScript原生对象常用方法总结
查看>>
工作者对象HttpWorkerRequest
查看>>
云数据库·ApsaraDB 产品6月刊
查看>>
JS中的prototype
查看>>
【译】什么导致了Context泄露:Handler&内部类
查看>>
限制MySQL Binlog的传输速率
查看>>
Xcode 5.1 编译模拟器以及真机都能使用的静态库
查看>>
山寨“饿了么”应用中添加菜品数量按钮效果
查看>>
WWDC 2013 Session笔记 - iOS7中弹簧式列表的制作
查看>>
iOS开发出错whose view is not in the window hierarchy!的解决
查看>>
Linux学习之CentOS(三)----将Cent0S 7的网卡名称eno16777736改为eth0
查看>>
解说redis中如何实现高可用
查看>>
小程序类似抖音视频整屏切换
查看>>
NG客制项目下的I18n国际化标准方案
查看>>
19-03-25
查看>>
activity idea编写bpmn流程文件
查看>>
windows Virtualbox下配置Ubuntu,且用ssh连接
查看>>
知识点漏缺总结
查看>>
【Java】Mybatis mapper动态代理方式
查看>>