zoukankan      html  css  js  c++  java
  • Balanced Lineup (自用线段树模板一)

    include <string.h>

    include <stdio.h>

    include

    using namespace std;

    define ll long long

    const int maxx=1e5;
    ll n,t,q,tree1[2maxx],tree2[2maxx],a[maxx];
    void pushup(ll rt)
    {
    tree1[rt]=max(tree1[rt2],tree1[rt2+1]);
    tree2[rt]=min(tree2[rt2],tree2[rt2+1]);
    }
    void build(ll l,ll r,ll rt)
    {
    if (l==r)
    {
    tree1[rt]=a[l];
    tree2[rt]=a[l];
    return;
    }
    int mid=(l+r)/2;
    build(l,mid,rt2);
    build(mid+1,r,rt
    2+1);
    pushup(rt);
    }
    ll querym(ll l,ll r,ll rt,ll L,ll R)
    {
    if (L<=l&&R>=r)
    {
    return tree1[rt];
    }
    ll mid=(l+r)/2,res=0;
    if (L<=mid)
    res=max(res,querym(l,mid,rt2,L,R));
    if (R>mid)
    res=max(res,querym(mid+1,r,rt
    2+1,L,R));
    return res;
    }
    ll querymi(ll l,ll r,ll rt,ll L,ll R)
    {
    if (L<=l&&R>=r)
    {
    return tree2[rt];
    }
    ll mid=(l+r)/2,res=0x3f3f3f;
    if (L<=mid)
    res=min(res,querymi(l,mid,rt2,L,R));
    if (R>mid)
    res=min(res,querymi(mid+1,r,rt
    2+1,L,R));
    return res;
    }
    int main()
    {
    ll i,j,x,y;
    scanf("%lld%lld",&n,&q);
    for (i=1; i<=n; i++)
    scanf("%lld",&a[i]);
    build(1,n,1);
    while(q--)
    {
    scanf("%lld %lld",&x,&y);
    printf("%lld ",querym(1,n,1,x,y)-querymi(1,n,1,x,y));

    }
    return 0;
    

    }

  • 相关阅读:
    CF359B Permutation
    CF859C Pie Rules
    Contest 156
    Contest 155
    Range Module
    python-环境
    Git 使用
    Contest 154
    生命是一种长期而持续的累积过程
    Contest 153
  • 原文地址:https://www.cnblogs.com/shidianshixuan/p/13763690.html
Copyright © 2011-2022 走看看