hihoCoder #1586 : Minimum-结构体版线段树(单点更新+区间最值求区间两数最小乘积) (ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛)

#1586 : Minimum

Time Limit:1000ms
Case Time Limit:1000ms
Memory Limit:256MB

Description

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<cstring>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#define inf 0x7fffffff
#define lson l,m,rt<<1
typedef long long ll;
#define rson m+1,r,rt<<1|1
using namespace std;
const int maxn=1e6+;
struct node{
int left,right,maxx,minn;
}tree[maxn*];
int st_min[maxn<<],st_max[maxn<<];
inline int minn(int a,int b){return a>b?b:a;}
inline int maxx(int a,int b){return a>b?a:b;}
void PushUP(int rt){
st_min[rt]=minn(st_min[rt<<],st_min[rt<<|]);
st_max[rt]=maxx(st_max[rt<<],st_max[rt<<|]);
}
void build(int l,int r,int rt) {
if(l==r){
scanf("%d",&st_min[rt]);
st_max[rt]=st_min[rt];
return ;
}
int m=(l+r)>>;
build(lson);
build(rson);
PushUP(rt);
}
void update(int p,int num,int l,int r,int rt){
if(l==r){
st_max[rt]=num;
st_min[rt]=num;
return ;
}
int m=(l+r)>>;
if(p<=m)update(p,num,lson);
else update(p,num,rson);
PushUP(rt);
}
int query_min(int L,int R,int l,int r,int rt){
if (L<=l&&r<=R){
return st_min[rt];
}
int m=(l+r)>>;
int ret1=inf,ret2=inf;
if(L<=m)ret1=query_min(L,R,lson);
if(R>m) ret2=query_min(L,R,rson);
return minn(ret1,ret2);
}
int query_max(int L,int R,int l,int r,int rt){
if(L<=l&&r<=R){
return st_max[rt];
}
int m=(l+r)>>;
int ret1=-inf,ret2=-inf;
if(L<=m)ret1=query_max(L,R,lson);
if(R>m) ret2=query_max(L,R,rson);
return maxx(ret1,ret2);
}
int main(){
int t;
scanf("%d",&t);
while(t--){
int n,m;
scanf("%d",&n);
int k=pow(,n);
build(,k,);
scanf("%d",&m);
while(m--){
int h,a,b;
scanf("%d%d%d",&h,&a,&b);
if(h==)update(a+,b,,k,);
else{
ll ans1=query_min(a+,b+,,k,);
ll ans2=query_max(a+,b+,,k,);
if(ans1<&&ans2>=)
printf("%lld\n",ans2*ans1);
else if(ans1<&&ans2<)
printf("%lld\n",ans2*ans2);
else
printf("%lld\n",ans1*ans1);
}
}
}
return ;
}
 
上一篇:JAVA中的COPYONWRITE容器


下一篇:CSS学起来并不难