51Nod 1174 区间中最大的数(RMQ)

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstring>
 4
 5 using namespace std;
 6 const int maxn = 10000 + 5;
 7 int a[maxn];
 8 int f[maxn][15];
 9
10 void rmq(int cnt){
11     memset(f, 0, sizeof(f));
12     for (int i = 1; i <= cnt; i++){
13         f[i][0] = a[i];
14     }
15
16     //O(nlogn)
17     for (int j = 1; j < 15; j++){
18         for (int i = 1; i <= cnt; i++){
19             if (i + (1 << j) - 1 <= cnt){
20                 f[i][j] = max(f[i][j - 1], f[i + (1 << (j - 1))][j - 1]);
21             }
22         }
23     }
24 }
25
26
27 int Query(int x, int y){
28     int k = 0;
29     while ((1 << (k + 1)) <= y - x + 1)
30         k++;
31     return max(f[x][k], f[y - (1 << k) + 1][k]);
32 }
33
34 int main(){
35     ios::sync_with_stdio(false);
36
37     int n;
38     cin >> n;
39     for (int i = 1; i <= n; i++){
40         cin >> a[i];
41     }
42     rmq(n);
43     int m;
44     cin >> m;
45     while (m--){
46         int x, y;
47         cin >> x >> y;
48         x++, y++;
49         int ans = Query(x, y);
50         cout << ans << endl;
51     }
52
53     //system("pause");
54     return 0;
55 }

原文地址:https://www.cnblogs.com/jaydenouyang/p/8666599.html

时间: 2024-08-28 12:16:06

51Nod 1174 区间中最大的数(RMQ)的相关文章

51Nod 1174 区间中最大的数

1174 区间中最大的数 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 描述 给出一个有N个数的序列,编号0 - N - 1.进行Q次查询,查询编号i至j的所有数中,最大的数是多少. 例如: 1 7 6 3 1.i = 1, j = 3,对应的数为7 6 3,最大的数为7.(该问题也被称为RMQ问题) Input 第1行:1个数N,表示序列的长度.(2 <= N <= 10000) 第2 - N + 1行:每行1个数,对应序列中的元素.(0 <

(DP ST表 线段树)51NOD 1174 区间中最大的数

给出一个有N个数的序列,编号0 - N - 1.进行Q次查询,查询编号i至j的所有数中,最大的数是多少. 例如: 1 7 6 3 1.i = 1, j = 3,对应的数为7 6 3,最大的数为7.(该问题也被称为RMQ问题) Input 第1行:1个数N,表示序列的长度.(2 <= N <= 10000) 第2 - N + 1行:每行1个数,对应序列中的元素.(0 <= S[i] <= 10^9) 第N + 2行:1个数Q,表示查询的数量.(2 <= Q <= 1000

51Nod—1174 区间中最大的数 线段树模版

在大佬们题解的帮助下算是看懂了线段树吧...在这mark下防一手转头就忘. #include<iostream> #include<stdio.h> using namespace std; struct ki { int m,l,r; }tree[40005]; int ans=-1,a[10005]; void build(int n,int l,int r) { tree[n].l=l; tree[n].r=r; if(l==r) { tree[n].m=a[l];retur

51nod 1174 1174 区间中最大的数

题目链接:51nod 1174 1174 区间中最大的数 ST(Sparse Table)算法学习参考博客:http://blog.csdn.net/niushuai666/article/details/6624672 O(nlogn)预处理,O(1)查询 1 #include<cstdio> 2 #include<algorithm> 3 #include<cmath> 4 using namespace std; 5 const int N = 10001; 6

区间中最大的数RMQ

1174 区间中最大的数 dmax[i][j]表示区间[i,i+j<<2) 1 #include <iostream> 2 #include <stdio.h> 3 using namespace std; 4 int n,q,dmax[10010][25],a[10010]; 5 void init(){ 6 for(int i = 1; i <= n; i ++){ 7 dmax[i][0] = a[i]; 8 } 9 for(int j = 1; (1<

51nod 1062 序列中最大的数(打表预处理)

1062 序列中最大的数 题目来源: Ural 1079 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 收藏 关注 取消关注 有这样一个序列a: a[0] = 0 a[1] = 1 a[2i] = a[i] a[2i+1] = a[i] + a[i+1] 输入一个数N,求a[0] - a[n]中最大的数. a[0] = 0, a[1] = 1, a[2] = 1, a[3] = 2, a[4] = 1, a[5] = 3, a[6] = 2, a[7] =

51Nod 1062 序列中最大的数

1062 序列中最大的数 题目来源: Ural 1079 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 描述 有这样一个序列a: a[0] = 0 a[1] = 1 a[2i] = a[i] a[2i+1] = a[i] + a[i+1] 输入一个数N,求a[0] - a[n]中最大的数. a[0] = 0, a[1] = 1, a[2] = 1, a[3] = 2, a[4] = 1, a[5] = 3, a[6] = 2, a[7] = 3, a[8]

51Nod 1062 序列中最大的数 | 简单DP

#include "iostream" #include "cstdio" using namespace std; #define LL long long #define N 100020 int a[N],b[N]; void init() { a[0]=0;a[1]=a[2]=1; b[0]=0;b[1]=b[2]=1; for(int i=3;i<N;i++){ if(i%2) a[i]=a[i/2]+a[i/2+1]; else a[i]=a[i/

CDOJ 1104 求两个数列的子列的交集 查询区间小于A的数有多少个 主席树

求两个数列的子列的交集 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/show/1104 Description 给两个数列A, B,长度分别为n1, n2,保证A中每个元素互不相同,保证B中每个元素互不相同..进行Q次询问,每次查找A[l1...r1]和B[l2..r2]的交集 集合 大小是多少.. 比如 A = {1,2,3,4,5,6,7},B = {7,6,5,4,3,2,1}