描述

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, -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 分析得出,一区间的乘积最小值,只有三种情况
1.最大值的平方(最大值为负数)
2.最小值的平方(最小值为正数)
3.最大值与最小值的乘积(最大值是正,最小值为负)
得到此结果后,只需改一下线段树的模板,将每段区间的最大最小值保存下来即可。
 #include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
const long long Max=;
int n;
struct node
{
long long b,s;
}Tree[Max<<];
long long bb,ss;
void build(int k,int l,int r)//建线段树,k表示子节点坐标
{
if(l == r)
{
scanf("%lld",&Tree[k].b);
Tree[k].s=Tree[k].b;
}
else
{
int mid = (l+r)/;
build(k*,l,mid);
build(k*+,mid+,r);
Tree[k].b=max(Tree[k*].b, Tree[k*+].b);
Tree[k].s=min(Tree[k*].s, Tree[k*+].s);
}
}
void query(int a,int b,int k,int l,int r)//a,b是当前查询区间,k是当前的根节点,l,r是要求查询区间
{
if(a >= l && b <= r)
{
bb=max(bb,Tree[k].b);
ss=min(ss,Tree[k].s);
//return min(min(Tree[k].b*Tree[k].b,Tree[k].b*Tree[k].s),Tree[k].s*Tree[k].s);
return;
}
else
{
//long long ans = Max*Max;
int mid = (a+b)/;
if(l <= mid)query(a,mid,k*,l,r);
if(r > mid) query(mid+,b,k*+,l,r);
return;
//return ans;
}
}
void update(int l,int r,int k,int pos,long long v)//l,r是查询区间,k是当前根节点,pos是查询位置
{
if(l == r)
{
Tree[k].b=v;
Tree[k].s=v;
}
else{
int mid = (l+r)/;
if(pos <= mid) update(l,mid,k*,pos,v);
if(pos > mid) update(mid+,r,k*+,pos,v);
Tree[k].b=max(Tree[k*].b, Tree[k*+].b);
Tree[k].s=min(Tree[k*].s, Tree[k*+].s);
}
} int main()
{
int T,l,r,pos;
long long val;
int k,c,order;
scanf("%d",&T);
for(int t=;t<=T;t++)
{ scanf("%d",&k);
n=pow(2.0,k);
build(,,n);
scanf("%d",&c);
while(c--)
{
scanf("%d",&order);
if(order==)
{
scanf("%d%d",&l,&r);
bb=-Max;ss=Max;
long long tmpans;
query(,n,,l+,r+);
tmpans= min(min(bb*bb,bb*ss),ss*ss);
printf("%lld\n",tmpans);
}
else if(order==)//点更新
{
scanf("%d%lld",&pos,&val);
update(,n,,pos+,val);
}
}
}
return ;
}

2017ICPC北京赛区网络赛 Minimum(数学+线段树)的更多相关文章

  1. 2017ICPC南宁赛区网络赛 Minimum Distance in a Star Graph (bfs)

    In this problem, we will define a graph called star graph, and the question is to find the minimum d ...

  2. 2017ICPC北京赛区网络赛 Visiting Peking University(简单思维)

    描述 Ming is going to travel for n days and the date of these days can be represented by n integers: 0 ...

  3. hdu 4031 2011成都赛区网络赛A题 线段树 ***

    就是不知道时间该怎么处理,想了好久,看了别人的题解发现原来是暴力,暴力也很巧妙啊,想不出来的那种  -_-! #include<cstdio> #include<iostream&g ...

  4. HDU 4046 Panda (ACM ICPC 2011北京赛区网络赛)

    HDU 4046 Panda (ACM ICPC 2011北京赛区网络赛) Panda Time Limit: 10000/4000 MS (Java/Others)    Memory Limit: ...

  5. HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011亚洲北京赛区网络赛)

    HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011 亚洲北京赛区网络赛题目) Eliminate Witches! Time Limit: 2000/1000 ...

  6. 徐州网络赛G-Trace【线段树】

    There's a beach in the first quadrant. And from time to time, there are sea waves. A wave ( xx , yy  ...

  7. Subsequence Count 2017ccpc网络赛 1006 dp+线段树维护矩阵

    Problem Description Given a binary string S[1,...,N] (i.e. a sequence of 0's and 1's), and Q queries ...

  8. HDU 4417 Super Mario(2012杭州网络赛 H 离线线段树)

    突然想到的节约时间的方法,感觉6翻了  给你n个数字,接着m个询问.每次问你一段区间内不大于某个数字(不一定是给你的数字)的个数 直接线段树没法做,因为每次给你的数字不一样,父节点无法统计.但是离线一 ...

  9. ACM学习历程—HDU 5023 A Corrupt Mayor's Performance Art(广州赛区网赛)(线段树)

    Problem Description Corrupt governors always find ways to get dirty money. Paint something, then sel ...

随机推荐

  1. 把springboot的项目打包运行指南

    受到传统mvc模式的开发影响,多数人都会想到把springboot项目打成war包在服务器容器里运行,笔者试过很多种方法打成war包部署tomcat上运行.运行成功但是怎么也访问不了,一直报404的错 ...

  2. 链接SQL Server服务器

    链接SQL Server服务器:      1.使用 ODBC 的 Microsoft OLE DB 提供程序         EXEC sp_addlinkedserver '别名','','MSD ...

  3. 大数据分析引擎Apache Flink

    Apache Flink是一个高效.分布式.基于Java实现的通用大数据分析引擎,它具有分布式 MapReduce一类平台的高效性.灵活性和扩展性以及并行数据库查询优化方案,它支持批量和基于流的数据分 ...

  4. 用swoole简单实现MySQL连接池

    MySQL连接池 在传统的网站开发中,比如LNMP模式,由Nginx的master进程接收请求然后分给多个worker进程,每个worker进程再链接php-fpm的master进程,php-fpm再 ...

  5. SpringBoot系列之Hikari连接池

    1.springboot 2.0 中默认连接池是Hikari,在引用parents后不用专门再添加依赖 2.application.yml中的配置 # jdbc_config datasource s ...

  6. 【LeetCode】矩阵操作

    1. 矩阵旋转 将 n × n 矩阵顺时针旋转 90°. 我的思路是 “ 从外到内一层一层旋转 ”. 一个 n × n 矩阵有 (n + 1) / 2 层,每层有 4 部分,将这 4 部分旋转. 顺时 ...

  7. TOYS

    TOYS Calculate the number of toys that land in each bin of a partitioned toy box. Mom and dad have a ...

  8. 牛客网第一场E题 Removal

    链接:https://www.nowcoder.com/acm/contest/139/E 来源:牛客网 Bobo has a sequence of integers s1, s2, ..., sn ...

  9. JavaScript中 null 的 typeof是object

    JavaScript中  null 的 typeof是object

  10. xmanager运行报错:bash: /usr/bin/xterm: No such file or directory

    1.原因 xterm没有安装,解决办法是下载然后安装即可. 如果机器可以用yum,则直接使用yum安装xterm即可: yum install -y xterm 如果没有网络,则在某台操作系统大版本相 ...