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];return;
    }
    else
    {
        build(n*2,l,(l+r)/2);
        build(n*2+1,(l+r)/2+1,r);
        tree[n].m=tree[n*2].m>tree[n*2+1].m?tree[n*2].m:tree[n*2+1].m;
    }
}
void find(int n,int a,int b)
{
    if(a<=tree[n].l&&b>=tree[n].r) ans=tree[n].m>ans?tree[n].m:ans;//注意a,b,r,l的关系!!!
    else if(a>tree[n].r||b<tree[n].l) return;
    else
    {
        find(n*2,a,b);
        find(n*2+1,a,b);
    }
}
int main()
{
    int n,m,i,j,r,l;
    scanf("%d",&n);
    for(i=1;i<=n;i++) scanf("%d",&a[i]);
    build(1,1,n);
    scanf("%d",&m);
    while(m--)
    {
        scanf("%d%d",&l,&r);
        ans=-1;
        l++;r++;
        find(1,l,r);
        printf("%d\n",ans);
    }
}

哼叽~

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

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

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 区间中最大的数(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

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

51NOD 1962 区间计数 单调栈+二分 / 线段树+扫描线

 区间计数 基准时间限制:1.5 秒 空间限制:262144 KB 分值: 80 两个数列 {An} , {Bn} ,请求出Ans, Ans定义如下: Ans:=Σni=1Σnj=i[max{Ai,Ai+1,...,Aj}=max{Bi,Bi+1,...,Bj}] 注:[ ]内表达式为真,则为1,否则为0. 1≤N≤3.5×1051≤Ai,Bi≤N 样例解释: 7个区间分别为:(1,4),(1,5),(2,4),(2,5),(3,3),(3,5),(4,5) Input 第一行一个整数N 第二行

区间中最大的数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<

SPOJ 1043 Can you answer these queries I 求任意区间最大连续子段和 线段树

题目链接:点击打开链接 维护区间左起连续的最大和,右起连续的和.. #include <cstdio> #include <iostream> #include <algorithm> #include <string.h> #include <math.h> #include <vector> #include <map> using namespace std; #define N 50050 #define Lson

线段树模版

#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <set> #include <map> #include <queue> #include <string> #define maxn 8080 #define lson l,m,rt<<1 #define rson m+1,r,

二维线段树模版

HDU 4819 二维线段树模版题 #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int INF = 999999999; const int maxn = 810; int a[maxn][maxn]; int st_min[maxn<<2][maxn<<2]; int st_max[maxn<<2][maxn