描述
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, -2k ≤ 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;
}