2017ICPC北京赛区网络赛 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 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(数学+线段树)的更多相关文章
- 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 ...
- 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 ...
- hdu 4031 2011成都赛区网络赛A题 线段树 ***
就是不知道时间该怎么处理,想了好久,看了别人的题解发现原来是暴力,暴力也很巧妙啊,想不出来的那种 -_-! #include<cstdio> #include<iostream&g ...
- HDU 4046 Panda (ACM ICPC 2011北京赛区网络赛)
HDU 4046 Panda (ACM ICPC 2011北京赛区网络赛) Panda Time Limit: 10000/4000 MS (Java/Others) Memory Limit: ...
- HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011亚洲北京赛区网络赛)
HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011 亚洲北京赛区网络赛题目) Eliminate Witches! Time Limit: 2000/1000 ...
- 徐州网络赛G-Trace【线段树】
There's a beach in the first quadrant. And from time to time, there are sea waves. A wave ( xx , yy ...
- 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 ...
- HDU 4417 Super Mario(2012杭州网络赛 H 离线线段树)
突然想到的节约时间的方法,感觉6翻了 给你n个数字,接着m个询问.每次问你一段区间内不大于某个数字(不一定是给你的数字)的个数 直接线段树没法做,因为每次给你的数字不一样,父节点无法统计.但是离线一 ...
- ACM学习历程—HDU 5023 A Corrupt Mayor's Performance Art(广州赛区网赛)(线段树)
Problem Description Corrupt governors always find ways to get dirty money. Paint something, then sel ...
随机推荐
- python-day97--django-ModelForm
Model Form :直接利用你的models里的字段 应用场景: - ModelForm - 中小型应用程序(model是你自己写的) - Form - 大型应用程序 注意事项: - 1. 类 f ...
- poj-2142-exgcd/解的和最小
The Balance Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 8816 Accepted: 3833 Descr ...
- WDA基础三:简单的INPUT选择,简单的TABLE显示
先从基本的开始,简单的单选和TABLE显示 1.创建选择条件节点,CONTEXT页签,右键CONTEXT创建NODE,对应1:1 1:1 lead selection 2.创建结果节点,对应0:n ...
- 在ASP.NET MVC 框架中调用 html文件及解析get请求中的参数值
在ASP.NET MVC 框架中调用 html文件: public ActionResult Index() { using (StreamReader sr = new StreamReader(P ...
- 【LeetCode】跳跃游戏
给定一组非负整数,初始时处于数组的第一位下标 0 的位置,数组的每个元素代表那个位置可以跳跃的最大长度.判断你是否能够到达数组的最后一位下标. e.g. A = [2, 3, 1, 1, 4],返回 ...
- POJ2393奶酪工厂
Yogurt factory Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14771 Accepted: 7437 D ...
- 【转】在使用实体框架(Entity Framework)的应用中加入审计信息(Audit trail)跟踪数据的变动
在一些比较重要的业务系统中,通常会要求系统跟踪数据记录的变动情况.系统要记录什么时间,什么人,对那些信息进行了变动. 比较简单的实现方式是在每个表中加入两个字段CreatedBy和CreatedAt, ...
- linux文件管理 文件权限
文件权限介绍 [root@ssgao1987 ~]# ls -l 总用量 24 -rw-------. 1 root root 1161 7月 8 10:30 anaconda-ks.cfg - ...
- Win10系列:WinJS库控件
在介绍了如何使用标准的HTML控件以及WinJS库中提供的新控件之后,下面来着重介绍WinJS库中几种常用的控件. (1)ListView控件 在开发Windows应用商店应用时可以使用ListVie ...
- C++解析四-友员函数、内联函数、静态成员
友元函数 类的友元函数是定义在类外部,但有权访问类的所有私有(private)成员和保护(protected)成员.尽管友元函数的原型有在类的定义中出现过,但是友元函数并不是成员函数.友元可以是一个函 ...