POJ3264Balanced Lineup(最基础的线段树)

采用一维数组建树。(因为一维数组建的是完全二叉树,时间上比用孩子节点指针建树慢,不过基本可以忽略=-=)

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int INF = 0xffffff0;
int minV=INF;
int maxV=-INF;
struct Node
{
    int L,R;
    int minV,maxV;
    int Mid()
    {
        return (L+R)/2;
    }
};
Node tree[800010];

void BuildTree(int root,int L,int R)
{
    tree[root].L=L;
    tree[root].R=R;
    tree[root].maxV=-INF;
    tree[root].minV=INF;
    if(L!=R)
    {
        BuildTree(2*root+1,L,(L+R)/2);
        BuildTree(2*root+2,1+(L+R)/2,R);
    }
}

void Insert(int root, int i,int v)
{
    if(tree[root].L==tree[root].R)
    {
        tree[root].maxV=tree[root].minV=v;
        return;
    }
    tree[root].minV=min(tree[root].minV,v);
    tree[root].maxV=max(tree[root].maxV,v);
    if(i<=tree[root].Mid())
    {
        Insert(2*root+1,i,v);
    }
    else
    {
        Insert(2*root+2,i,v);
    }
}

void Query(int root,int s,int e)
{
    if(tree[root].minV>=minV&&tree[root].maxV<=maxV)
    {
        return;
    }
    if(tree[root].L==s&&tree[root].R==e)
    {
        minV=min(tree[root].minV,minV);
        maxV=max(tree[root].maxV,maxV);
        return;
    }
    if(e<=tree[root].Mid())
    {
        Query(1+root*2,s,e);
    }
    else if(s>tree[root].Mid())
    {
        Query(2+root*2,s,e);
    }
    else
    {
        Query(2*root+1,s,tree[root].Mid());
        Query(2*root+2,tree[root].Mid()+1,e);
    }
}

int main()
{
    int n,q,h;
    int i;
    //freopen("d:\\test.txt","r",stdin);
    scanf("%d%d",&n,&q);
    BuildTree(0,1,n);
    for( i= 1; i <= n; i++ )
    {
        scanf("%d",&h);
        Insert(0,i,h);
    }
    for( i= 0; i < q; i++ )
    {
        int s,e;
        scanf("%d%d", &s,&e);
        minV= INF;
        maxV= -INF;
        Query(0,s,e);
        printf("%d\n",maxV-minV);
    }
    return 0;
}
时间: 2024-08-08 22:06:21

POJ3264Balanced Lineup(最基础的线段树)的相关文章

POJ 3264 Balanced Lineup(st或者线段树)

A - Balanced Lineup Time Limit:5000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 3264 Appoint description:  System Crawler  (2015-08-03) Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,00

Balanced Lineup POJ 3264(线段树)

http://poj.org/problem?id=3264 题意:现有N个数字,问你在区间[a, b]之间最大值与最小值的差值为多少? 分析:线段树模板,不过需要有两个查询,一个查询在该区间的最大值,一个查询在该区间的最小值,最后两者结果相减即可. #include <iostream> #include <stdio.h> #include <string.h> #include <string> #include <vector> #inc

线段树基础

关于线段树的原理学习,可以参看杨弋大牛的论文<线段树>以及刘汝佳老师的<算法竞赛入门经典(训练指南)>,代码风格学习hzwer或者notonlysuccess均可. 一.单点更新 最基础的线段树 题目:codevs1080 链接:http://codevs.cn/problem/1080/ 分析:最简单的线段树,单点更新,区间求和 1 #include<iostream> 2 #include<cstdio> 3 using namespace std; 4

HDU 1698 Just a Hook (线段树 区间更新基础)

Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 21856    Accepted Submission(s): 10963 Problem Description In the game of DotA, Pudge's meat hook is actually the most horrible thing

HDU 1754 I Hate It 基础线段树

用区间值m表示这段区间的最大值,一直更新这个区间的最大值,很基础的线段树 #include<iostream> #include<stdio.h> using namespace std; #define N 200005 struct node{ int l,r,m; }tree[N*4]; int a[N]; void build(int left,int right,int i){ tree[i].l=left; tree[i].r=right; if(tree[i].l==

线段树基础知识----(基础数据结构)--(一)

1.定义 引入:为什么要使用线段树而不用数组模拟呢? answer:因为有些题用数组来做就要超时,用线段树的O(log(n))的时间复杂度刚好可以求解 毫无疑问线段树是一种数据结构,但是它实际是一个类似树状的链表结构(个人认为) ///还是要正经一点(照搬教科书)----------- / ////////////////////////////////////////////////////////////////////// 线段树定义:线段树是一种二叉搜索树,与区间树相似,它将一个区间划分

线段树

看了下gdkoi2014……觉得完蛋了……除了一道莫队还可以写写一眼看出,其他基本不行…… 发现学了一大堆东西都不会用…… 那就从最基础的线段树还是吧. 都说了只是开坑还没写呢⁄(⁄ ⁄•⁄ω⁄•⁄ ⁄)⁄

hdu 1166 敌兵布阵(单点更新线段树)

敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 54411    Accepted Submission(s): 22830 Problem Description C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务

HDU-------(2795)Billboard(线段树区间更新)

Billboard Time Limit: 20000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 10594    Accepted Submission(s): 4686 Problem Description At the entrance to the university, there is a huge rectangular billboard of s