HihoCoder - 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.

Input
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, -2k ≤ y < 2k)

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

Sample Input
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
Sample Output
1
1
4
存在负数的情况,所以有三种:都是正数,最小数的平方,都是负数,最大数的平方,一正一负是相乘的情况最小,所以有两个询问,之后在判断。

#include<iostream>
#include<stdio.h>
#include<cstring>
using namespace std;
#define N 200005
#define inf 1e9
int minn[N<<2],a[N],maxn[N<<2];
void pushup(int root)
{
     minn[root]=min(minn[root<<1],minn[root<<1|1]);
     maxn[root]=max(maxn[root<<1],maxn[root<<1|1]);
}
void build(int l,int r,int root)
{
     if(l==r)
     {
          minn[root]=maxn[root]=a[l];
          return ;
     }
     int mid=l+r>>1;
     build(l,mid,root<<1);
     build(mid+1,r,root<<1|1);
     pushup(root);
}
void update(int l,int r,int root,int x,int y)
{
     if(l==r)
     {
          minn[root]=maxn[root]=y;
          return ;
     }
     int mid=l+r>>1;
     if(mid>=x)
         update(l,mid,root<<1,x,y);
     else
         update(mid+1,r,root<<1|1,x,y);
     pushup(root);
}
int query1(int l,int r,int root,int x,int y)
{
     if(l==x&&r==y)
         return minn[root];
     int mid=l+r>>1;
     if(mid>=y)
         return query1(l,mid,root<<1,x,y);
     else if(mid<x)
         return query1(mid+1,r,root<<1|1,x,y);
     else
         return min(query1(l,mid,root<<1,x,mid),query1(mid+1,r,root<<1|1,mid+1,y)); 
}
int query2(int l,int r,int root,int x,int y)
{
     if(l==x&&r==y)
         return maxn[root];
     int mid=l+r>>1;
     if(mid>=y)
         return query2(l,mid,root<<1,x,y);
     else if(mid<x)
         return query2(mid+1,r,root<<1|1,x,y);
     else
     return max(query2(l,mid,root<<1,x,mid),query2(mid+1,r,root<<1|1,mid+1,y)); 
}
int main()
{
     int t;
     scanf("%d",&t);
     while(t--)
     {
          int n;
          scanf("%d",&n);
          n=(1<<n);
          for(int i=0;i<n;i++)
              scanf("%d",&a[i]);
          build(0,n-1,1);
          int m;
          scanf("%d",&m);
          while(m--)
          {
               int q,x,y;
               scanf("%d%d%d",&q,&x,&y);
               if(q==1)
               {
                    int maxnn=query2(0,n-1,1,x,y);
                    int minnn=query1(0,n-1,1,x,y);
                    long long ans=min(1ll*minnn*minnn,1ll*maxnn*maxnn);
                    ans=min(1ll*minnn*maxnn,ans);
                    printf("%lld\n",ans);
               }
           else
               update(0,n-1,1,x,y);
          }
     }
 return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值