区间最小值(2)

Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 26   Accepted Submission(s) : 9

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

Problem Description

给定一个数字序列以及一些操作,查询随意给定区间内数字的最小值。

Input

输入包括多组測试数据,最多5组。

输入包括多组測试用例。每组測试用例的开头为一个整数n(1<=n<=100000),代表数字序列的长度。

接下去一行给出n个数字,代表数字序列。数字在int范围内。

下一行为一个整数t(1<=t<=10000)。代表操作的次数。

最后t行。每行给出一个操作,1 l r代表查询区间 [l,r]的最小值,0 l r c代表将区间[l r]的值所有替换为c (1<=l<=r<=n),c在整形范围内。

Output

对于每一个查询,输出区间[l,r]内的最小值。

Sample Input

5
1 2 3 4 5
7
1 1 5
0 1 3 6
1 1 4
1 2 3
0 2 2 0
1 1 2
1 3 5

Sample Output

1
4
6
0
4

Author

吴迎

迟到了五分钟才做了。

。感觉做的是对的。

尽管没能提交上。

找了一天了。。TM我最终找到怎么做了。。上午的时候事实上就知道怎么更新区间了。仅仅只是一个地方写错了

导致结果不正确 就放弃了。

今天下午尝试了那么多。

。o(︶︿︶)o 唉 刚刚发现上午写的是对的。

<pre name="code" class="cpp">#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int num[100005];
struct node
{
int left,right,val;
}c[100005*4];
void build_tree(int l,int r,int root)
{
c[root].left=l;
c[root].right=r;
if(l==r)
{
c[root].val=num[l];
return ;
}
int mid=(l+r)/2;
build_tree(l,mid,root*2);
build_tree(mid+1,r,root*2+1);
c[root].val=min(c[root*2].val,c[root*2+1].val);
}
void find_tree(int l,int r,int &min1,int root)
{
if(c[root].left==l&&c[root].right==r)
{
min1=c[root].val;
return ;
}
int mid=(c[root].left+c[root].right)/2;
if(mid<l)
find_tree(l,r,min1,root*2+1);
else if(mid>=r)
find_tree(l,r,min1,root*2);
else
{
int min2;
find_tree(l,mid,min1,root*2);
find_tree(mid+1,r,min2,root*2+1);
min1=min(min1,min2);
} }
void update_tree(int l,int r,int x,int root)
{
if(c[root].left==c[root].right)
{
c[root].val=x;
return ;
}
int mid=(c[root].left+c[root].right)/2;
if(mid<l)
update_tree(l,r,x,root*2+1);
else if(mid>=r)
update_tree(l,r,x,root*2);
else
{
update_tree(l,mid,x,root*2);
update_tree(mid+1,r,x,root*2+1);
}
c[root].val=min(c[root*2].val,c[root*2+1].val);
}
int main()
{
int n,k;
while(scanf("%d",&n)!=EOF)
{
for(int i=1;i<=n;i++)
scanf("%d",&num[i]);
build_tree(1,n,1);
scanf("%d",&k);
while(k--)
{
int a,b,min1,flag;
scanf("%d",&flag);
if(flag)
{
scanf("%d %d",&a,&b);
find_tree(a,b,min1,1);
printf("%d\n",min1);
}
else
{
int x;
scanf("%d %d %d",&a,&b,&x);
update_tree(a,b,x,1);
//for(int i=1;i<=9;i++)
//printf("[%d %d] %d\n",c[i].left,c[i].right,c[i].val);//自己測试的。 。
}
}
}
return 0;
}

区间最小值(2) (线段树 更新区间)2015年 JXNU_ACS 算法组暑假第一次周赛的更多相关文章

  1. 区间最小值 线段树 (2015年 JXNU_ACS 算法组暑假第一次周赛)

    区间最小值 Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other) Total Submiss ...

  2. CSU-1110 RMQ with Shifts (单点更新+区间最小值 zkw线段树)

    In the traditional RMQ (Range Minimum Query) problem, we have a static array A. Then for each query ...

  3. hdu 1556:Color the ball(线段树,区间更新,经典题)

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  4. hdu 1698:Just a Hook(线段树,区间更新)

    Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  5. UVA 12436-Rip Van Winkle's Code(线段树的区间更新)

    题意: long long data[250001]; void A( int st, int nd ) { for( int i = st; i \le nd; i++ ) data[i] = da ...

  6. hdu1698线段树的区间更新区间查询

    Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  7. hihoCoder #1078 : 线段树的区间修改(线段树区间更新板子题)

    #1078 : 线段树的区间修改 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 对于小Ho表现出的对线段树的理解,小Hi表示挺满意的,但是满意就够了么?于是小Hi将问题 ...

  8. HDU 1556 Color the ball(线段树:区间更新)

    http://acm.hdu.edu.cn/showproblem.php?pid=1556 题意: N个气球,每次[a,b]之间的气球涂一次色,统计每个气球涂色的次数. 思路: 这道题目用树状数组和 ...

  9. zoj3686(线段树的区间更新)

    对线段树的区间更新有了初步的了解... A Simple Tree Problem Time Limit: 3 Seconds      Memory Limit: 65536 KB Given a ...

随机推荐

  1. ThinkPHP5 连接 PostgreSQL

    $request = Db::connect( [ 'type' => 'pgsql', 'hostname' => '127.0.0.1', 'database' => 'keyw ...

  2. centos 7 配置ip

    1.动态获取ip(前提是你的路由器已经开启了DHCP) 修改网卡配置文件 vi /etc/sysconfig/network-scripts/ifcfg-ens32    (最后一个为网卡名称) 动态 ...

  3. pip更新产生的问题及其解决方法?

    运行 pip3 install --upgrade pip 发生错误: from pip import main ImportError: cannot import name 'main' 将以下代 ...

  4. maven学习(四)——maven项目构建过程

    一.创建Maven项目 1.1.建立Hello项目 1.首先建立Hello项目,同时建立Maven约定的目录结构和pom.xml文件 Hello | --src | -----main | ----- ...

  5. 商城商品购买数量增减的完美JS效果

    近期在开发一个地方O2O租书项目,使用ASP.NET MVC技术,其中在图书详情页,用户可以输入借阅的数量,这里使用了js来控制数量的增减和校验. 数量一定是数字 点击增减按钮的时候要能自动加1或减1 ...

  6. Centos定时自动执行脚本

    检查本机crond的基本情况 1.crond的运行状况 2.crond是否开机自启动 如何将脚本添加进自动运行的时间内 直接编辑 vim /etc/crontab ,默认的文件形式如后面的图:   我 ...

  7. TTL 和 RS-232

    下面详细讲解下面这三种的区别 USB转TTL串口板(常用芯片PL2303,CH340) USB转RS-232串口线 TTL转RS-232串口板,RS-232转TTL串口板(常用芯片MAX3232,MA ...

  8. 球形空间产生器sphere(bzoj 1013)

    Description 有一个球形空间产生器能够在n维空间中产生一个坚硬的球体.现在,你被困在了这个n维球体中,你只知道球面上n+1个点的坐标,你需要以最快的速度确定这个n维球体的球心坐标,以便于摧毁 ...

  9. Vijos P1404 遭遇战

    背景 你知道吗,SQ Class的人都很喜欢打CS.(不知道CS是什么的人不用参加这次比赛). 描述 今天,他们在打一张叫DUSTII的地图,万恶的恐怖分子要炸掉藏在A区的SQC论坛服务器!我们SQC ...

  10. Array拼接字符串

    原文发布时间为:2011-01-12 -- 来源于本人的百度文章 [由搬家工具导入] Array拼接字符串本来就是一种投机取巧的无聊玩意,来源是IE6对字符串的+实现错误一般情况下,如果是语义性的字符 ...