poj3264

 1 //Accepted    2384K    1766MS
2 #include <cstdio>
3 #include <cstring>
4 #define imax 50005
5 struct node
6 {
7 int l,r;
8 int tmax,tmin;
9 }f[3*imax];
10 int num[imax];
11 int n,m;
12 int max(int a,int b)
13 {
14 return a>b?a:b;
15 }
16 int min(int a,int b)
17 {
18 return a>b?b:a;
19 }
20 void build(int t,int l,int r)
21 {
22 f[t].l=l;
23 f[t].r=r;
24 if (f[t].l==f[t].r)
25 {
26 f[t].tmin=f[t].tmax=num[l];
27 return ;
28 }
29 int mid=(f[t].l+f[t].r)/2;
30 build(2*t,l,mid);
31 build(2*t+1,mid+1,r);
32 f[t].tmax=max(f[2*t].tmax,f[2*t+1].tmax);
33 f[t].tmin=min(f[2*t].tmin,f[2*t+1].tmin);
34 }
35 void query(int t,int l,int r,int &tmax,int &tmin)
36 {
37 if (f[t].l==l && f[t].r==r)
38 {
39 tmax=f[t].tmax;
40 tmin=f[t].tmin;
41 return ;
42 }
43 int mid=(f[t].l+f[t].r)/2;
44 if (r<=mid) query(2*t,l,r,tmax,tmin);
45 else
46 {
47 if (l>mid) query(2*t+1,l,r,tmax,tmin);
48 else
49 {
50 int max1,max2,min1,min2;
51 query(2*t,l,mid,max1,min1);
52 query(2*t+1,mid+1,r,max2,min2);
53 tmax=max(max1,max2);
54 tmin=min(min1,min2);
55 }
56 }
57 }
58 int main()
59 {
60 while (scanf("%d%d",&n,&m)!=EOF)
61 {
62 for (int i=1;i<=n;i++)
63 scanf("%d",&num[i]);
64 build(1,1,n);
65 for (int i=1;i<=m;i++)
66 {
67 int x,y;
68 scanf("%d%d",&x,&y);
69 int tmax=0,tmin=0;
70 query(1,x,y,tmax,tmin);
71 printf("%d\n",tmax-tmin);
72 }
73 }
74 return 0;
75 }

poj3264

时间: 2025-01-07 23:41:42

poj3264的相关文章

poj3264 Balanced Lineup(求区间的最大值与最小值之差)

Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 37869   Accepted: 17751 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 Joh

POJ3264 Balanced Lineup RMQ 线段树

求区间内最大数和最小数的差,用两棵线段树,一个维护区间最大值,一个维护区间最小值. #include <stdio.h> #include <vector> #include <math.h> #include <string.h> #include <string> #include <iostream> #include <queue> #include <list> #include <algori

Poj3264(ST算法)

以前也没怎么听过这个算法,网络赛中有个题好像是什么最近公共祖先,看了一下这个算法,是一个动态规划,核心思想是倍增. 用途:解决rmq问题,例如给一个序列{an},询问是任意一个区间(l,r)中最小的数或者最大的数. 时间复杂度:预处理的时间是O(nlogn) 查询:O(1) 局限性:只能处理序列不变的情况,因为只能针对一个序列进行预处理. 状态定义:dp[i][j],表示一个序列中从第i个数长度为2^j的连续序列中的最大值(根据实际需要). 状态转移方程(以最大值为例):dp[i][j]=max

POJ3264——Balanced Lineup(线段树)

本文出自:http://blog.csdn.net/svitter 题意:在1~200,000个数中,取一段区间,然后在区间中找出最大的数和最小的数字,求这两个数字的差. 分析:按区间取值,很明显使用的线段树.区间大小取200000 * 4 = 8 * 10 ^5; 进行查询的时候,注意直接判断l, r 与mid的关系即可,一开始写的时候直接与tree[root].L判断,多余了, 逻辑不正确. #include <iostream> #include <stdio.h> #inc

RMQ、POJ3264

这里说几篇博客,建议从上到下看 https://blog.csdn.net/qq_31759205/article/details/75008659 https://blog.csdn.net/sgh666666/article/details/80448284 https://www.cnblogs.com/kuangbin/p/3227420.html ----------------------------------------------------------------------

poj--3264 Balanced Lineup(裸的RMQ)

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 rang

POJ3264 Balanced Lineup 线段树 RMQ ST算法应用

Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 36813 Accepted: 17237 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 de

2017ecjtu-summer training #3 POJ3264

Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 53868   Accepted: 25299 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 Joh

poj3264 Balanced Lineup

题意: 给定Q (1 ≤ Q ≤ 200,000)个数A1,A2 - AQ,,多次求任一区间Ai – Aj中最大数和最小数的差. 思路: 线段树. 实现: 1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 using namespace std; 5 6 const int MAXN = 65536; 7 const int INF = 0x3f3f3f3f; 8 9 struct no