其他分享
首页 > 其他分享> > Balanced Lineup (自用线段树模板一)

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\n",querym(1,n,1,x,y)-querymi(1,n,1,x,y));

}
return 0;

}

标签:rt,res,ll,tree1,mid,rt2,Balanced,Lineup,模板
来源: https://www.cnblogs.com/shidianshixuan/p/13763690.html