描述
You are given a list of integers a0, a1, …, a2^k-1.
You need to support two types of queries:
Output Minx,y∈[l,r] {ax∙ay}.
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 l r: Output Minx,y∈[l,r]{ax∙ay}. (0 ≤ l ≤ r < 2k)
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 <stdio.h>
#include <algorithm>
#include <string.h>
#include <math.h>
#define LL long long
using namespace std;
struct node
{
LL ma;
LL mi;
}a[150000],tree[4*150000];
LL min(LL a,LL b) {return a<b?a:b;}
LL max(LL a, LL b) {return a>b?a:b;}
void build(LL p, LL l, LL r)
{
if(l == r)
{
tree[p].mi = a[l].mi;
tree[p].ma = a[l].ma;
return ;
}
LL mid = (l + r) / 2;
build(p*2 , l ,mid);
build(p*2+1, mid + 1, r);
tree[p].mi = min(tree[p*2].mi , tree[p*2+1].mi);
tree[p].ma = max(tree[p*2].ma , tree[p*2+1].ma);
}
void update(LL p, LL l, LL r, LL x, LL num)
{
if(l == r)
{
tree[p].mi = num;
tree[p].ma = num;
return ;
}
LL mid = (l + r) >> 1;
if(x <= mid)
{
update(p*2,l,mid,x,num);
}
else
{
update(p*2+1,mid+1,r,x,num);
}
tree[p].ma = max(tree[p*2].ma , tree[p*2+1].ma);
tree[p].mi = min(tree[p*2].mi , tree[p*2+1].mi);
}
LL querymi(LL p, LL l, LL r, LL x, LL y)
{
if(l>=x && r<=y)
{
return tree[p].mi;
}
LL mid = (l + r) >> 1;
if(y <= mid)
{
return querymi(p*2, l, mid, x, y);
}
if(x > mid)
{
return querymi(p*2+1, mid+1, r, x, y);
}
return min(querymi(p*2, l, mid, x, mid) , querymi(p*2+1, mid+1, r, mid+1, y));
}
LL queryma(LL p, LL l, LL r, LL x, LL y)
{
if(l>=x && r<=y)
{
return tree[p].ma;
}
LL mid = (l + r) >> 1;
if(y <= mid)
{
return queryma(p*2, l, mid, x, y);
}
if(x > mid)
{
return queryma(p*2+1, mid+1, r, x, y);
}
return max(queryma(p*2, l, mid, x, mid) , queryma(p*2+1, mid+1, r, mid+1, y));
}
int main()
{
LL t;
scanf("%lld",&t);
while(t--)
{
LL k;
scanf("%lld",&k);
LL n = pow(2,k);
for(int i = 1; i <= n; i++)
{
scanf("%lld",&a[i].mi);
a[i].ma = a[i].mi;
}
build(1,1,n);
int q;
scanf("%d",&q);
for(LL i = 0; i < q; i++)
{
LL temp,x,y;
scanf("%lld%lld%lld",&temp,&x,&y);
if(temp == 2)
{
update(1,1,n,x+1,y);
}
if(temp == 1)
{
LL ans = querymi(1,1,n,x+1,y+1);
if(ans >= 0)
{
printf("%lld\n",ans*ans);
continue;
}
else
{
LL ans2 = queryma(1,1,n,x+1,y+1);
if(ans2 >= 0)
{
printf("%lld\n",ans*ans2);
continue;
}
else
{
printf("%lld\n",ans2*ans2);
}
}
}
}
}
return 0;
}