poj 3264 RMQ 水题

题意:找到一段数字里最大值和最小值的差

水题

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<cmath>
 6 #include<queue>
 7 using namespace std;
 8 const int maxn=550;
 9 const int INF=0x3f3f3f3f;
10 int n,m,t;
11 const int MAXN = 50010;
12 int dpMAX[MAXN][20],dpMIN[MAXN][20];
13 int mm[MAXN];
14 int cow[MAXN];
15 //初始化RMQ, b数组下标从1开始,从0开始简单修改
16 void initMAXRMQ(int n,int b[])
17 {
18     mm[0] = -1;
19     for(int i = 1; i <= n;i++)
20     {
21         mm[i] = ((i&(i-1)) == 0)?mm[i-1]+1:mm[i-1];
22         dpMAX[i][0] = b[i];
23     }
24     for(int j = 1; j <= mm[n];j++)
25     for(int i = 1;i + (1<<j) -1 <= n;i++)
26     dpMAX[i][j] = max(dpMAX[i][j-1],dpMAX[i+(1<<(j-1))][j-1]);
27 }
28 //查询最大值
29 int rmqmMAX(int x,int y)
30 {
31     int k = mm[y-x+1];
32     return max(dpMAX[x][k],dpMAX[y-(1<<k)+1][k]);
33 }
34 void initMINRMQ(int n,int b[])
35 {
36     mm[0] = -1;
37     for(int i = 1; i <= n;i++)
38     {
39         mm[i] = ((i&(i-1)) == 0)?mm[i-1]+1:mm[i-1];
40         dpMIN[i][0] = b[i];
41     }
42     for(int j = 1; j <= mm[n];j++)
43     for(int i = 1;i + (1<<j) -1 <= n;i++)
44     dpMIN[i][j] = min(dpMIN[i][j-1],dpMIN[i+(1<<(j-1))][j-1]);
45 }
46 //查询最大值
47 int rmqMIN(int x,int y)
48 {
49     int k = mm[y-x+1];
50     return min(dpMIN[x][k],dpMIN[y-(1<<k)+1][k]);
51 }
52 int main()
53 {
54     int i,j,k;
55     #ifndef ONLINE_JUDGE
56     freopen("1.in","r",stdin);
57     #endif
58     int q;
59     while(scanf("%d%d",&n,&q)!=EOF)
60     {
61         for(i=1;i<=n;i++)
62         {
63             scanf("%d",&cow[i]);
64         }
65         initMAXRMQ(n,cow);
66         initMINRMQ(n,cow);
67         for(i=1;i<=q;i++)
68         {
69             int a,b;
70             scanf("%d%d",&a,&b);
71             printf("%d\n",rmqmMAX(a,b)-rmqMIN(a,b));
72         }
73     }
74     return 0;
75 }
时间: 2024-08-13 18:00:25

poj 3264 RMQ 水题的相关文章

POJ 3264 RMQ水题

题目大意就是有很多牛.告诉你每只牛的高度.然后有很多个询问.输出该区间内的最大身高差.也就是用RMQ求最大值最小值.貌似还可以用线段树.然而,我还不会线段树.....T_T 可能是太多组数据了.cin和cout会TLE.换成scanf和printf就顺当的AC了....啦啦啦. RMQ还是只会用模板..T_T 附代码:#include<stdio.h>#include<string.h>#include<iostream>#include<math.h>#d

POJ 3264 RMQ裸题

POJ 3264 题意:n个数,问a[i]与a[j]间最大值与最小值之差. 总结:看了博客,记下了模板,但有些地方还是不太理解. #include<iostream> #include<cstdio> #include<cstdlib> #include<algorithm> #include<cstring> #include<string> #include<cmath> #include<queue> #

POJ百道水题列表

以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight Moves1101 Gamblers1204 Additive equations 1221 Risk1230 Legendary Pokemon1249 Pushing Boxes 1364 Machine Schedule1368 BOAT1406 Jungle Roads1411 Annive

POJ 3264 RMQ Spare Table算法

今天下午大帝讲的,我以前也不懂,所以也就跟着学学了,把中间的那个状态转移方程学错了好几次,于是就wa了 好几发. #include<iostream> #include<cstdio> #include<algorithm> #define maxn 200010 using namespace std; int a[maxn],m,n,b[maxn],fl[maxn][50],fr[maxn][50]; void solve() { b[1]=0;//其实就是用来计算

poj 1979 dfs水题

// 练练水题,夯实基础吧 #include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib

poj 3264 RMQ

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

POJ 1000(水题)

这道题就很水啦,只要看懂题...所以我受了这个影响,再去hoj的时候想都没想就还按照这个打了,结果就像之前那篇说的那样WA. #include <stdio.h> int main() { int a,b; scanf("%d %d",&a, &b); printf("%d\n",a+b); return 0; }

poj 3264(RMQ或者线段树)

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

POJ 3264 -----RMQ问题

Balanced Lineup Time Limit: 5000 MS Memory Limit: 0 KB 64-bit integer IO format: %I64d , %I64u Java class name: Main 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 decide