#1586 : Minimum(线段树)

描述

You are given a list of integers a0, a1, …, a2^k-1.

You need to support two types of queries:

1. Output Minx,y∈[l,r] {ax∙ay}.

2. Let ax=y.

输入

The first line is an integer T, indicating the number of test cases. (1≤T≤10).

For each test case:

The first line contains an integer k (0 ≤ k ≤ 17).

The following line contains 2k integers, a0, a1, …, a2^k-1 (-2k ≤ ai < 2k).

The next line contains a integer  (1 ≤ Q < 2k), indicating the number of queries. Then next Q lines, each line is one of:

1. 1 l r: Output Minx,y∈[l,r]{ax∙ay}. (0 ≤ l ≤ r < 2k)

2. 2 x y: Let ax=y. (0 ≤ x < 2k, -2≤ y < 2k)

输出

For each query 1, output a line contains an integer, indicating the answer.

样例输入
1
3
1 1 2 2 1 1 2 2
5
1 0 7
1 1 2
2 1 2
2 2 2
1 1 2
样例输出
1
1
4

用基本线段树维护最大值和最小值。要分析出维护的值和最终答案的关系。


代码:

#include <bits/stdc++.h>

using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int ma=134000;
int sum[ma<<2];
int mi[ma<<2];
//int a[20]={0,2 ,4 ,8 ,16 ,32 ,64 ,128 ,256 ,512 ,1024, 2048 ,4096 ,8192 ,16384 ,32768, 65536 ,131072};
void pushup(int rt)
{
    sum[rt]=max(sum[rt<<1],sum[rt<<1|1]);
    mi[rt]=min(mi[rt<<1],mi[rt<<1|1]);
}
void build(int l,int r,int rt)
{
    if(l==r)
    {
        scanf("%d",&sum[rt]);
        mi[rt]=sum[rt];
        return;
    }
    int m=(r+l)>>1;
    build(lson);
    build(rson);
    pushup(rt);
}
void update(int p,int sc,int l,int r,int rt)
{
    if(l==r)
    {
        sum[rt]=sc;
        mi[rt]=sc;
        return;
    }
    int m=(r+l)>>1;
    if(p<=m)update(p,sc,lson);
    else update(p,sc,rson);
    pushup(rt);
}
int quert1(int L,int R,int l,int r,int rt)
{
    if(L<=l&&r<=R)
    {
        return sum[rt];
    }
    int m=(l+r)>>1;
    int ret=-2222202;
    if(L<=m)ret=max(ret,quert1(L,R,lson));
    if(R>m)ret=max(ret,quert1(L,R,rson));
    return ret;
}
int quert2(int L,int R,int l,int r,int rt)
{
    if(L<=l&&r<=R)
    {
        return mi[rt];
    }
    int m=(l+r)>>1;
    int ret=2222202;
    if(L<=m)ret=min(ret,quert2(L,R,lson));
    if(R>m)ret=min(ret,quert2(L,R,rson));
    return ret;
}
int main()
{
    int n,m,t,k;
    long long ans1,ans2,ans;
    int a,b;
    cin>>t;
    while(t--)
    {
        memset(sum,0,sizeof(sum));
        memset(mi,0,sizeof(mi));
    scanf("%d",&n);
    n=1<<n;

    build(1,n,1);
    scanf("%d",&m);

    while(m--)
    {
        scanf("%d",&k);
        if(k==1)
        {
            scanf("%d%d",&a,&b);
            a++;b++;
            ans1=quert1(a,b,1,n,1);
            ans2=quert2(a,b,1,n,1);
            //cout<<ans1<<" "<<ans2<<endl;
            ans=min(ans1*ans1,ans2*ans2);
            ans=min(ans,ans1*ans2);
            printf("%lld\n",ans);

        }
        else
        {
            scanf("%d%d",&a,&b);
            a++;
            update(a,b,1,n,1);
        }
    }
    }
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值