2017 acmicpc 北京网络赛 I Minimum(线段树求区间最值)

描述
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 2^k integers, a0, a1, …, a2^k-1 (-2^k ≤ ai < 2^k).

The next line contains a integer (1 ≤ Q < 2^k), 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 < 2^k)

  2. 2 x y: Let ax=y. (0 ≤ x < 2^k, -2^k ≤ y < 2^k)

输出
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

大致题意:告诉你2^k个数,编号从0到2^k-1,接下来有q次操作。
操作1:查询区间[l,r]内的最小乘积ax*ay,(注意x可以等于y)
操作2:将x位置上的数ax变为y

思路:用两棵线段树来分别维护区间内的最大值和最小值,假设所求的某个区间内的最大值为max,最小值为min,分析可知有如下三种情况:
1.当min>=0时,那么该区间的最小乘积即min*min
2.当max<0时,最小乘积为max*max
3.max大于0,min小于0时,最小乘积为max*min

代码如下

#include<cstring> 
#include<cstdio> 
#include<iostream>   
#include <algorithm>  
#include<cmath>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值