hihoCoder 1586 Minimum 【线段树】 (ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛)

#1586 : Minimum

时间限制: 1000ms
单点时限: 1000ms
内存限制: 256MB

描述

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, -2≤ 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


题目链接:

  http://hihocoder.com/problemset/problem/1586

题目大意:

  给2n个数,两种操作

  1 输出Minx,y∈[l,r] {ax*ay}.

  2 令ax=y

题目思路:

  【线段树】

  记录每个区间的最大最小值,并维护,询问时获取当前区间最大最小值,分情况讨论

  0在最小值左边,0在最小值和最大值之间,0在最大值右边,共3种情况。

  分别计算答案即可。

  修改时修改单点值。



/****************************************************

	Author : Coolxxx
	Copyright 2017 by Coolxxx. All rights reserved.
	BLOG : http://blog.csdn.net/u010568270

****************************************************/
#include<bits/stdc++.h>
#pragma comment(linker,"/STACK:1024000000,1024000000")
#define abs(a) ((a)>0?(a):(-(a)))
#define lowbit(a) (a&(-a))
#define sqr(a) ((a)*(a))
#define mem(a,b) memset(a,b,sizeof(a))
const double EPS=0.00001;
const int J=10;
const int MOD=1000000007;
const int MAX=0x7f7f7f7f;
const double PI=3.14159265358979323;
const int N=150004;
using namespace std;
typedef long long LL;
double anss;
LL aans;
int cas,cass;
int n,m,lll,ans;
int min1[N+N],max1[N+N];
void update(int k)
{
	min1[k]=min(min1[k+k],min1[k+k+1]);
	max1[k]=max(max1[k+k],max1[k+k+1]);
}
void change(int l,int r,int x,int y,int k)
{
	if(r<x || x<l)return;
	if(l==r)
	{
		min1[k]=y;
		max1[k]=y;
		return;
	}
	int mid=(l+r)>>1;
	change(l,mid,x,y,k+k);
	change(mid+1,r,x,y,k+k+1);
	update(k);
}
void query(int l,int r,int a,int b,int k,int d[])
{
	if(r<a || b<l)
	{
		d[0]=MAX;
		d[1]=-MAX;
		return;
	}
	if(a<=l && r<=b)
	{
		d[0]=min1[k];
		d[1]=max1[k];
		return;
	}
	if(l==r)
	{
		d[0]=min1[k];
		d[1]=max1[k];
		return;
	}
	int mid=(l+r)>>1;
	int d1[2],d2[2];
	query(l,mid,a,b,k+k,d1);
	query(mid+1,r,a,b,k+k+1,d2);
	update(k);
	
	d[0]=min(d1[0],d2[0]);
	d[1]=max(d1[1],d2[1]);
}
int main()
{
	#ifndef ONLINE_JUDGE
	freopen("1.txt","r",stdin);
//	freopen("2.txt","w",stdout);
	#endif
	int i,j,k;
	int x,y,z;
	for(scanf("%d",&cass);cass;cass--)
//	init();
//	for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
//	while(~scanf("%d",&n))
	{
		scanf("%d",&n);
		n=pow(2,n);
		for(i=1;i<=n;i++)
		{
			scanf("%d",&x);
			min1[n+i-1]=x;
			max1[n+i-1]=x;
		}
		for(i=n-1;i;i--)
			update(i);
		scanf("%d",&m);
		for(i=1;i<=m;i++)
		{
			scanf("%d%d%d",&z,&x,&y);
			if(z==1)
			{
				x++,y++;
				int d[2];
				query(1,n,x,y,1,d);
				if(0<=d[0])
					aans=1LL*d[0]*d[0];
				else if(d[0]<0 && 0<=d[1])
					aans=1LL*d[0]*d[1];
				else  aans=1LL*d[1]*d[1];
				printf("%lld\n",aans);
			}
			else
			{
				x++;
				change(1,n,x,y,1);
			}
		}
	}
	return 0;
}
/*
//

//
*/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值