B--Minimum--HihoCoder - 1586

题目链接      题目链接2

题目

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

题目大意:

给你一个K,然后给你2的K次方个数,然后给你一个N,表示有N个操作,每个操作有三个数,P,X,Y;

P==1,表示让你求X到Y个数之间任意两个数的乘积的最小值,

P==2,表示让你把地X个数变成Y;

思路:

这是一道线段树的模板题,单点更新,让你查询区间任意两个数的乘积最小值,我们可以先建线段树,然后维护每个区间的最大值和最小值(因为有可能区间的数有负数),找到我们所需要的区间,观察他们的最大值和最小值,

都为正,答案为最小值乘积,都为负,答案为最大值乘积,否则答案为最大值乘以最小值。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
int a[140000];
long long  p,x,y,ans,ansd;
struct zxc
{
    int l,r;
    long long maxx,minn;
}tree[550000];
void build(int l,int r,int k)
{
    tree[k].l=l;
    tree[k].r=r;
    if(l==r)
    {
        tree[k].minn=a[l];
        tree[k].maxx=a[r];
        return;
    }
    int m=(tree[k].l+tree[k].r)/2;
    build(l,m,2*k);
    build(m+1,r,2*k+1);
    tree[k].maxx=max(tree[2*k].maxx,tree[2*k+1].maxx);
    tree[k].minn=min(tree[2*k].minn,tree[2*k+1].minn);
}
void ask1(int l,int r,int k)
{
    if(tree[k].l>=x&&tree[k].r<=y)
    {
        ans=min(ans,tree[k].minn);
        return ;
    }
    int m=(tree[k].l+tree[k].r)/2;
    if(x<=m)
    {
        ask1(l,r,2*k);
    }
    if(y>m)
    {
        ask1(l,r,2*k+1);
    }
}
void ask2(int l,int r,int k)
{
    if(tree[k].l>=x&&tree[k].r<=y)
    {
        ansd=max(ansd,tree[k].maxx);
        return ;
    }
    int m=(tree[k].l+tree[k].r)/2;
    if(x<=m)
    {
        ask2(l,r,2*k);
    }
    if(y>m)
    {
        ask2(l,r,2*k+1);
    }
}
void add(int q,int z,int k)
{
    if(tree[k].l==tree[k].r)
    {
        tree[k].maxx=z;
        tree[k].minn=z;
        return;
    }
    int m=(tree[k].l+tree[k].r)/2;
    if(m<q)
    {
        add(q,z,2*k+1);
    }
    else
    {
        add(q,z,2*k);
    }
    tree[k].maxx=max(tree[2*k].maxx,tree[2*k+1].maxx);
    tree[k].minn=min(tree[2*k].minn,tree[2*k+1].minn);
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        int w=1;
        scanf("%d",&n);
        w=w<<n;
        memset(a,0,sizeof(a));
        for(int i=1;i<=w;i++)
        {
            scanf("%d",&a[i]);
        }
         build(1,w,1);
        int m;
        scanf("%d",&m);

        while(m--)
        {

             scanf("%lld%lld%lld",&p,&x,&y);
             x++;
             if(p==1)
             {
                 y++;
                 ans=0x3f3f3f3f;
                 ansd=-0x3f3f3f3f;
                 ask1(x,y,1);
                 ask2(x,y,1);
                 if(ans>0&&ansd>0)
                 {
                     printf("%lld\n",ans*ans);
                 }
                 else if(ans<0&&ansd<0)
                 {
                     printf("%lld\n",ansd*ansd);
                 }
                 else
                 {
                     printf("%lld\n",ans*ansd);
                 }
             }
             else
             {
                 add(x,y,1);
             }
        }
    }
    return 0;
}

 

 

全部评论

相关推荐

11-24 11:23
门头沟学院 C++
点赞 评论 收藏
分享
10-15 09:13
已编辑
天津大学 soc前端设计
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务