题目来源:
https://vjudge.net/contest/293466#problem/B
https://vjudge.net/problem/1087342/origin
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
中文题意:
给一个数组,有两种操作
1:求 x,y 属于 [l,r],求MinAx∗Ay
2:将 Ax替换值为 v
思路:挺显然的线段树,分别求区间最大和最小值,如果区间最小值是正数,那么直接是这个数的平方。否则就要看看最大值,如果最大值是正数,那么就是最小值乘最大值(因为是负数),否则的话就是最大值的平方(因为出来是正数)。 (线段树单点更新板子
参考板子:https://blog.csdn.net/nuoyanli/article/details/89039581
板子记得没问题,为啥超时==(就将结构体Tree改为了双数组查询就a了emmm)好吧不是结构体的锅,将比赛代码重新打一遍就a了,原来是用了两次询问
参考代码:
修改之后的比赛代码(ac!!!):
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define ll long long
const int inf = 0x3f3f3f3f;
const int N=15e4+5;
using namespace std;
int n,k;
struct Tree
{
int maxx,minn;
}ans[N<<2];
void pushup(int root)
{
ans[root].maxx=max(ans[root<<1].maxx,ans[root<<1|1].maxx);
ans[root].minn=min(ans[root<<1].minn,ans[root<<1|1].minn);
}
void build(int l,int r,int root)
{
if(l==r)
{
scanf("%d",&ans[root].maxx);
ans[root].minn=ans[root].maxx;
return;
}
int mid=(l+r)>>1;
build(lson);
build(rson);
pushup(root);
}
void update(int p,int sc,int l,int r,int root)
{
if(l==r)
{
ans[root].maxx=sc;
ans[root].minn=sc;
return;
}
int mid=(l+r)>>1;
if(p<=mid)update(p,sc,lson);
else
update(p,sc,rson);
pushup(root);
}
Tree query(int L,int R,int l,int r,int root)
{
if(L<=l&&r<=R)
{
return ans[root];
}
int mid=(l+r)>>1;
Tree ret;
ret.maxx=-inf,ret.minn=inf;
if(L<=mid)
{
Tree left=query(L,R,lson);
ret.maxx=max( ret.maxx,left.maxx);
ret.minn=min(ret.minn,left.minn);
}
if(R>mid)
{
Tree right=query(L,R,rson);
ret.maxx=max(ret.maxx,right.maxx);
ret.minn=min(ret.minn,right.minn);
}
return ret;
}
int power(int a,int b)
{
int ans=1;
while(b)
{
if(b&1)
ans*=a;
a*=a;
b>>=1;
}
return ans;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&k);
int kk=power(2,k);
build(1,kk,1);
scanf("%d",&n);
while(n--)
{
int x;
scanf("%d",&x);
int a,b;
scanf("%d%d",&a,&b);
if(x==1)
{
Tree sum=query(a+1,b+1,1,kk,1);
ll minnn=sum.minn;
ll maxxx=sum.maxx;
if(minnn>0)
{
printf("%lld\n",minnn*minnn);
}
else if(maxxx>0)
{
printf("%lld\n",maxxx*minnn);
}
else
printf("%lld\n",maxxx*maxxx);
}
else
update(a+1,b,1,kk,1);
}
}
return 0;
}
非结构体:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#define inf 0x3f3f3f3f
#define lson l,m,root<<1
#define ll long long
#define rson m+1,r,root<<1|1
using namespace std;
const int N=1e6+10;
int st_min[N<<2],st_max[N<<2];
void PushUP(int root)
{
st_min[root]=min(st_min[root<<1],st_min[root<<1|1]);
st_max[root]=max(st_max[root<<1],st_max[root<<1|1]);
}
void build(int l,int r,int root)
{
if(l==r)
{
scanf("%d",&st_min[root]);
st_max[root]=st_min[root];
return ;
}
int m=(l+r)>>1;
build(lson);
build(rson);
PushUP(root);
}
void update(int p,int sc,int l,int r,int root)
{
if(l==r)
{
st_max[root]=sc;
st_min[root]=sc;
return ;
}
int m=(l+r)>>1;
if(p<=m)update(p,sc,lson);
else update(p,sc,rson);
PushUP(root);
}
int query_min(int L,int R,int l,int r,int root)
{
if (L<=l&&r<=R)
{
return st_min[root];
}
int m=(l+r)>>1;
int ret1=inf,ret2=inf;
if(L<=m)ret1=query_min(L,R,lson);
if(R>m) ret2=query_min(L,R,rson);
return min(ret1,ret2);
}
int query_max(int L,int R,int l,int r,int root)
{
if(L<=l&&r<=R)
{
return st_max[root];
}
int m=(l+r)>>1;
int ret1=-inf,ret2=-inf;
if(L<=m)ret1=query_max(L,R,lson);
if(R>m) ret2=query_max(L,R,rson);
return max(ret1,ret2);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,m;
scanf("%d",&n);
int k=pow(2,n);
build(1,k,1);
scanf("%d",&m);
while(m--)
{
int x,a,b;
scanf("%d%d%d",&x,&a,&b);
if(x==1)
{
ll minnn=query_min(a+1,b+1,1,k,1);
ll maxxx=query_max(a+1,b+1,1,k,1);
if(minnn>0)
{
printf("%lld\n",minnn*minnn);
}
else if(maxxx>0)
{
printf("%lld\n",maxxx*minnn);
}
else
printf("%lld\n",maxxx*maxxx);
}
else
update(a+1,b,1,k,1);
}
}
return 0;
}