hihocoder 1586 ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛-题目9 : Minimum【线段树】
https://hihocoder.com/problemset/problem/1586
线段树操作,原来题并不难。。。。。
当时忽略了一个重要问题,就是ax*ay要最小时,x、y可以相等,那就简单了!结果显然是模最小的数的平方或者是个负数。
要求乘积最小只要求区间内最大值、最小值和绝对值小的数,再判断min,max正负就行了。
下面的代码相当于建了三个树分别存Min,Max,Mid(abs(Min))
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
typedef long long ll;
const int maxn=(<<)+;
int Max[maxn<<],Min[maxn<<],Mid[maxn<<]; int mid(int a,int b)
{
if(abs(a)<abs(b)) return a;
else return b;
} void PushUpMax(int rt)
{
Max[rt]=max(Max[rt<<],Max[rt<<|]);
} void PushUpMin(int rt)
{
Min[rt]=min(Min[rt<<],Min[rt<<|]);
} void PushUpMid(int rt)
{
Mid[rt]=mid(Mid[rt<<],Mid[rt<<|]);
} void Build(int l,int r,int rt)
{
if(l==r){
scanf("%d",&Max[rt]);
Min[rt]=Mid[rt]=Max[rt];
return;
}
int m=(l+r)>>;
Build(lson);
Build(rson);
PushUpMax(rt);
PushUpMin(rt);
PushUpMid(rt);
} void Update(int pos,int val,int l,int r,int rt)
{
if(l==r){
Mid[rt]=Min[rt]=Max[rt]=val;
return;
}
int m=(l+r)>>;
if(pos<=m) Update(pos,val,lson);
else Update(pos,val,rson);
PushUpMax(rt);
PushUpMin(rt);
PushUpMid(rt);
} int QueryMax(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R){
return Max[rt];
}
int m=(l+r)>>;
int res=-maxn-;
if(L<=m) res=max(res,QueryMax(L,R,lson));
if(R>m) res=max(res,QueryMax(L,R,rson));
return res;
} int QueryMin(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R){
return Min[rt];
}
int m=(l+r)>>;
int res=maxn+;
if(L<=m) res=min(res,QueryMin(L,R,lson));
if(R>m) res=min(res,QueryMin(L,R,rson));
return res;
} int QueryMid(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R){
return Mid[rt];
}
int m=(l+r)>>;
int res=maxn;
if(L<=m) res=mid(res,QueryMid(L,R,lson));
if(R>m) res=mid(res,QueryMid(L,R,rson));
return res;
} int main()
{
int T,n,m,k,a,b;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
n=<<n;
Build(,n,);
scanf("%d",&m);
while(m--)
{
scanf("%d%d%d",&k,&a,&b);
a++;
if(k==){
b++;
ll t1=(ll)QueryMax(a,b,,n,);
ll t2=(ll)QueryMin(a,b,,n,);
ll t3=(ll)QueryMid(a,b,,n,);
if((t1>=)&&(t2<=))
printf("%lld\n",t1*t2);
else if(t1<||t2>)
printf("%lld\n",t3*t3);
}
else Update(a,b,,n,);
}
}
return ;
}
从[0,n-1]建树
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
typedef long long ll;
const int maxn=(<<)+;
int Max[maxn<<],Min[maxn<<],Mid[maxn<<]; int mid(int a,int b)
{
if(abs(a)<abs(b)) return a;
return b;
} void PushUpMax(int rt)
{
Max[rt]=max(Max[rt<<],Max[rt<<|]);
} void PushUpMin(int rt)
{
Min[rt]=min(Min[rt<<],Min[rt<<|]);
} void PushUpMid(int rt)
{
Mid[rt]=mid(Mid[rt<<],Mid[rt<<|]);
} void Build(int l,int r,int rt)
{
if(l==r){
scanf("%d",&Max[rt]);
Min[rt]=Mid[rt]=Max[rt];
return;
}
int m=(l+r)>>;
Build(lson);
Build(rson);
PushUpMax(rt);
PushUpMin(rt);
PushUpMid(rt);
} void Update(int pos,int val,int l,int r,int rt)
{
if(l==r){
Max[rt]=Min[rt]=Mid[rt]=val;
return;
}
int m=(l+r)>>;
if(pos<=m) Update(pos,val,lson);
else Update(pos,val,rson);
PushUpMax(rt);
PushUpMin(rt);
PushUpMid(rt);
} int QueryMax(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R){
return Max[rt];
}
int m=(l+r)>>;
int res=-maxn;
if(L<=m) res=max(res,QueryMax(L,R,lson));
if(R>m) res=max(res,QueryMax(L,R,rson));
return res;
} int QueryMin(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R){
return Min[rt];
}
int m=(l+r)>>;
int res=maxn;
if(L<=m) res=min(res,QueryMin(L,R,lson));
if(R>m) res=min(res,QueryMin(L,R,rson));
return res;
} int QueryMid(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R){
return Mid[rt];
}
int m=(l+r)>>;
int res=maxn;
if(L<=m) res=mid(res,QueryMid(L,R,lson));
if(R>m) res=mid(res,QueryMid(L,R,rson));
return res;
} int main()
{
int T,n,m,c,a,b;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
n=<<n;
Build(,n-,);
scanf("%d",&m);
while(m--)
{
scanf("%d%d%d",&c,&a,&b);
if(c==){
ll t1=QueryMax(a,b,,n-,);
ll t2=QueryMin(a,b,,n-,);
ll t3=QueryMid(a,b,,n-,);
if(t1>=&&t2<=)
printf("%lld\n",t1*t2);
else
printf("%lld\n",t3*t3);
}
else Update(a,b,,n-,);
}
}
return ;
}
hihocoder 1586 ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛-题目9 : Minimum【线段树】的更多相关文章
- ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛 题目9 : Minimum
时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 You are given a list of integers a0, a1, …, a2^k-1. You need t ...
- ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛 i题 Minimum(线段树)
描述 You are given a list of integers a0, a1, …, a2^k-1. You need to support two types of queries: 1. ...
- hihoCoder 1586 Minimum 【线段树】 (ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛)
#1586 : Minimum 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 You are given a list of integers a0, a1, …, a2 ...
- hihoCoder #1586 : Minimum-结构体版线段树(单点更新+区间最值求区间两数最小乘积) (ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛)
#1586 : Minimum Time Limit:1000ms Case Time Limit:1000ms Memory Limit:256MB Description You are give ...
- 【线段树】hihocoder 1586 ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛 I. Minimum
题意:给你一个序列(长度不超过2^17),支持两种操作:单点修改:询问区间中最小的ai*aj是多少(i可以等于j). 只需要线段树维护区间最小值和最大值,如果最小值大于等于0,那答案就是minv*mi ...
- hihoCoder 1582 Territorial Dispute 【凸包】(ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛)
#1582 : Territorial Dispute 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 In 2333, the C++ Empire and the Ja ...
- hihoCoder 1584 Bounce 【数学规律】 (ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛)
#1584 : Bounce 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 For Argo, it is very interesting watching a cir ...
- hihoCoder 1578 Visiting Peking University 【贪心】 (ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛)
#1578 : Visiting Peking University 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 Ming is going to travel for ...
- 【分类讨论】【计算几何】【凸包】hihocoder 1582 ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛 E. Territorial Dispute
题意:平面上n个点,问你是否存在一种黑白染色方案,使得对于该方案,无法使用一条直线使得黑色点划分在直线一侧,白色点划分在另一侧.如果存在,输出一种方案. 如果n<=2,显然不存在. 如果所有点共 ...
随机推荐
- js数组操作方法
数组的操作方法在js中可谓是诸子百家,各种某乱的方法让人眼花缭乱,有时候就会分不清什么是自带的什么是曾今写的api了 数组的增删 1 . push() , pop() 和 shift() unshi ...
- C# 多线程操作之异步委托
标签: 多线程任务nullstringhtml工作 2012-06-29 23:00 1276人阅读 评论(0) 收藏 举报 分类: C/C++/C#/dotnet(126) 目录(?)[+] ...
- Shell 常用特性
管道(|) 管道 (|): 将一个命令的输出作为另外一个命令的输入. 管道同样可以在标准输入输出和标准错误输出间做代替工作,这样一来,可以将某一个程序的输出送到另一个程序的输入,其语法如下: ...
- 使用Jedis操作Redis-使用Java语言在客户端操作---String类型
前提:需要引入Jedis的jar包. /** * 我的redis在Linux虚拟机Centos7中,192.168.222.129是我虚拟机的ip地址. */ private static Jedis ...
- jQ-点击查看更多
<style type="text/css"> .hi { width: 200px; height: 18vw; background-color: pink; fo ...
- 解决Cesium1.50对gltf2.0/3dtiles数据读取的问题
问题说明 Cesium 1.50(2018/10/01)版本打开3dtiles可能会出现加载不上导致渲染停止的错误. 错误说明为:RuntimeError: Unsupported glTF Exte ...
- 根据网站运行日志猜测的百度蜘蛛ip
da大部分文章都是吵来吵去,不准确 所以就不参考那些沙雕的文章了,直接自己统计一个 123.125.71.117 123.125.71.58 220.181.108.115 220.181.108.1 ...
- linux 下建立桌面快捷方式
这段时间从windows转到了Linux,发现桌面上没有快捷方式很不适应,找了好久资料,找到解决方法,记录下来以后备用 1.首先建立一个新文件 ``` vi quick.desktop //后缀为de ...
- 洛谷P1315 [NOIP2011提高组Day2T3] 观光公交
P1315 观光公交 题目描述 风景迷人的小城Y 市,拥有n 个美丽的景点.由于慕名而来的游客越来越多,Y 市特意安排了一辆观光公交车,为游客提供更便捷的交通服务.观光公交车在第 0 分钟出现在 1号 ...
- 微信小程序--轮播图,标题,盒子,tab栏的合成例子
小程序是什么? 微信小程序,是一种不需要下载安装即可使用的应用,用户扫一扫或搜一下即可打开应用,在微信-发现-小程序可打开应用. 一.小程序的样式编写: 目录结构: app.json { " ...