POJ
来源:互联网 发布:死侍蜘蛛侠 知乎 编辑:程序博客网 时间:2024/04/29 22:42
题目链接:http://poj.org/problem?id=3264点击打开链接
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
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
Sample Input
6 31734251 54 62 2
Sample Output
630
#include <iostream>#include <queue>#include <stdio.h>#include <stdlib.h>#include <stack>#include <limits>#include <string>#include <string.h>#include <vector>#include <set>#include <map>#include <algorithm>#include <math.h>using namespace std;#define maxn 300000+5struct xjy{ int left; int right; int mmin; int mmax;};xjy tree[maxn<<2];int a[maxn];int minans,maxans;void build(int i,int left,int right){ if(left==right) { tree[i].left=left; tree[i].right=right; tree[i].mmin=a[left]; tree[i].mmax=a[left]; return ; } int mid=(left+right)>>1; build(i<<1,left,mid); build (i<<1|1,mid+1,right); tree[i].left=left; tree[i].right=right; tree[i].mmax=max(tree[i<<1].mmax,tree[i<<1|1].mmax); tree[i].mmin=min(tree[i<<1].mmin,tree[i<<1|1].mmin);}void query(int i,int left,int right){ if(tree[i].left==left&&right==tree[i].right) { maxans=max(maxans,tree[i].mmax); minans=min(minans,tree[i].mmin); return; } int mid=(tree[i].left+tree[i].right)>>1; if(right<=mid) query(i<<1,left,right); else if(left>mid) query(i<<1|1,left,right); else { query(i<<1,left,mid); query(i<<1|1,mid+1,right); }}int main(){ int n,m; int t; int flag=0; while(~scanf("%d%d",&n,&m)) { for(int i=0;i<maxn;i++) { tree[i].left=0; tree[i].right=0; tree[i].mmin=0; tree[i].mmax=0; a[i]=0; } for(int i=1;i<=n;i++) scanf("%d",&a[i]); build(1,1,n); int l,r; for(int i=1;i<=m;i++) { scanf("%d%d",&l,&r); minans=INT_MAX; maxans=0; query(1,l,r); printf("%d\n",maxans-minans); } } return 0;}
- POJ
- poj
- POJ
- POJ
- poj
- poj
- POJ
- POJ
- poj
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- 利用Frida从TeamViewer内存中提取密码
- python 访问属性和属性装饰器
- python运算符
- Leetcode206. Reverse Linked List总结
- Java使用poi包读取Excel文档
- POJ
- python 方法、实例方法、静态方法、类方法
- 【Python3.6爬虫学习记录】(八)Selenium模拟登录新浪邮箱并发送邮件
- [Java基础]Java接口
- 原型模式
- python 特殊方法、运算符重载
- 从0开始写一个基于注解的轻量级分布式RPC框架(1)RPC原理和准备工作
- windows下cmd使用
- Springboot实现阿里云通信短信服务有关短信验证码的发送