POJ3264Balanced Lineup 线段树练手

题目意思:给定Q(1<=Q<=200000)个数A1,A2,```,AQ,多次求任一区间Ai-Aj中最大数和最小数的差
//1085422276
#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <string>
#include <stack>
#include <math.h>
#include <vector>
#include <string.h>
using namespace std;

typedef __int64 ll;
const int inf = (int)1E9+10;
inline ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
    while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
    return x*f;
}

//*******************************
struct ss
{
    int l,r,mn,ma;
}tr[800001];
int n,q,a[200001],nn,mm;
void build(int k,int s,int t)
{
    tr[k].l=s;
    tr[k].r=t;
    if(s==t){
        tr[k].mn=tr[k].ma=a[s];
        return ;
    }
    int mid=(s+t)>>1;
    build(k<<1,s,mid);
    build(k<<1|1,mid+1,t);
    tr[k].ma=max(tr[k<<1].ma,tr[k<<1|1].ma);
    tr[k].mn=min(tr[k<<1].mn,tr[k<<1|1].mn);
}
void ask(int k,int s,int t)
{
    if(tr[k].ma<=nn&&tr[k].mn>=mm)return;
    if(s==tr[k].l&&t==tr[k].r)
    {
        nn=max(tr[k].ma,nn);
        mm=min(tr[k].mn,mm);
        return;
    }
    int mid=(tr[k].l+tr[k].r)>>1;
    if(t<=mid) ask(k<<1,s,t);
    else if(s>mid) ask(k<<1|1,s,t);
    else {
       ask(k<<1,s,mid);
       ask(k<<1|1,mid+1,t);
    }
}

int main()
{

    while(scanf("%d%d",&n,&q)!=EOF)
    {
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i]);
        }build(1,1,n);int x,y;
        for(int i=1;i<=q;i++){
                nn=-inf;
               mm=inf;
            scanf("%d%d",&x,&y);
            ask(1,x,y);
            printf("%d\n",nn-mm);
        }
    }
    return 0;
}
时间: 2024-10-13 03:15:20

POJ3264Balanced Lineup 线段树练手的相关文章

POJ3264_Balanced Lineup(线段树/单点更新)

解题报告 题意: 求区间内最大值和最小值的差值. 思路: 裸线段树,我的线段树第一发.区间最值. #include <iostream> #include <cstring> #include <cstdio> #define inf 99999999 #define LL long long using namespace std; LL minn[201000],maxx[201000]; void update(LL root,LL l,LL r,LL p,LL

【POJ】3264 Balanced Lineup ——线段树 区间最值

Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 34140   Accepted: 16044 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 Balanced Lineup(线段树)

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

Balanced Lineup(线段树之区间查找最大最小值)

传送门 线段树的区间查找最大最小值模板. 裸的线段树 #include <iostream> #include <cstring> #include <cstdio> #include <cmath> #include <set> #include <queue> #include <vector> #include <cstdlib> #include <algorithm> #define ls

POJ 3264 Balanced Lineup (线段树单点更新 区间查询)

Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 36820   Accepted: 17244 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 Balanced Lineup (线段树)

Balanced Lineup 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

POJ 3264 Balanced Lineup 线段树 第三题

Balanced Lineup 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

POJ3264 Balanced Lineup 线段树区间最大值 最小值

Q个数 问区间最大值-区间最小值 1 // #pragma comment(linker, "/STACK:1024000000,1024000000") 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <sstream> 6 #include <string> 7 #include <algorithm> 8 #i