博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
kuangbin专题七 POJ3264 Balanced Lineup (线段树最大最小)
阅读量:6609 次
发布时间:2019-06-24

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

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 线段树维护最大最小,不涉及更改,只用pushup query就可以了
1 #include 
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 #include
13 #include
14 using namespace std;15 #define FO freopen("in.txt","r",stdin);16 #define rep(i,a,n) for (int i=a;i
=a;i--)18 #define pb push_back19 #define mp make_pair20 #define all(x) (x).begin(),(x).end()21 #define fi first22 #define se second23 #define SZ(x) ((int)(x).size())24 #define debug(x) cout << "&&" << x << "&&" << endl;25 #define lowbit(x) (x&-x)26 #define mem(a,b) memset(a, b, sizeof(a));27 typedef vector
VI;28 typedef long long ll;29 typedef pair
PII;30 const ll mod=1000000007;31 const int inf = 0x3f3f3f3f;32 ll powmod(ll a,ll b) {ll res=1;a%=mod;for(;b;b>>=1){ if(b&1)res=res*a%mod;a=a*a%mod;}return res;}33 ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}34 //head35 36 const int maxn=200010;37 int minn[maxn<<2],maxx[maxn<<2],n,q,a[maxn],maxpos,minpos;38 39 void pushup(int rt) {40 minn[rt]=min(minn[rt<<1],minn[rt<<1|1]);41 maxx[rt]=max(maxx[rt<<1],maxx[rt<<1|1]);42 }43 44 void build(int rt,int L,int R){45 minn[rt]=0;46 maxx[rt]=0;47 if(L==R) {48 scanf("%d",&a[rt]);49 minn[rt]=maxx[rt]=a[rt];50 return;51 }52 int mid=(L+R)>>1;53 build(rt<<1,L,mid);54 build(rt<<1|1,mid+1,R);55 pushup(rt);56 }57 58 void query(int rt,int L,int R,int l,int r) {59 if(L>=l&&R<=r) {60 minpos=min(minpos,minn[rt]);61 maxpos=max(maxpos,maxx[rt]);62 return;63 }64 int mid=(L+R)>>1;65 if(l<=mid) query(rt<<1,L,mid,l,r);66 if(r>mid) query(rt<<1|1,mid+1,R,l,r);67 }68 69 int main() {70 while(~scanf("%d%d",&n,&q)) {71 build(1,1,n);72 int l,r;73 while(q--) {74 maxpos=-1,minpos=inf;75 scanf("%d%d",&l,&r);76 query(1,1,n,l,r);77 printf("%d\n",l==r?0:maxpos-minpos);78 }79 }80 }

 

 

转载于:https://www.cnblogs.com/ACMerszl/p/9865238.html

你可能感兴趣的文章
PHP
查看>>
Python运行效率低的原因
查看>>
CentOS中zip压缩和unzip解压缩命令详解
查看>>
server 2008 r2 hyper-v 硬盘扩容
查看>>
Android之DOM解析XML
查看>>
关于字符串输入的问题
查看>>
NSD基础交换-子网划分
查看>>
rsync的配置部署
查看>>
Java的新项目学成在线笔记-day11(二)
查看>>
思科 DHCP服务器配置及DHCP中继
查看>>
以太坊DAO之时间锁定Multisig
查看>>
这样的APP你还不满意吗?不满意算我输
查看>>
百度城市大会绽放蓉城,弘和受邀“智”创未来
查看>>
深入理解QtCreator的插件设计架构
查看>>
JVM源码分析之Object.wait/notify实现
查看>>
网卡调试
查看>>
零基础web前端学习路线
查看>>
静态路由
查看>>
根据供词确定谁是凶手
查看>>
mongrel
查看>>