HDU 4902 Nice boat 成段线段树
操作1 的时候标记deng[rt]表示以下一段数都是与当前节点的值同样
下次操作2时直接对有deng标记的节点gcd更新
(可能还能够更简单)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <malloc.h>
#include <ctype.h>
#include <math.h>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#define MAXN 11111
#include <queue>
#include <vector>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mid (r+l)>>1
const int maxn=100010;
int sum[maxn<<2] ,col[maxn<<2],deng[maxn<<2];
int gcd(int a,int b)
{
while(1)
{
if(b==0)
return a;
int t=b;
b=a%b;
a=t;
}
}
void pushup(int rt)
{
sum[rt]=max(sum[rt<<1],sum[rt<<1|1]);
}
void pushdown(int rt){
if(col[rt])
{
col[rt<<1]=col[rt<<1|1]=col[rt];
sum[rt<<1]=sum[rt<<1|1]=sum[rt];
col[rt]=0;
}
if(deng[rt])
{
deng[rt<<1]=deng[rt<<1|1]=deng[rt];
sum[rt<<1]=sum[rt<<1|1]=sum[rt];
deng[rt]=0;
}
}
void build(int l,int r,int rt)
{
col[rt]=deng[rt]=0;
if(r==l)
{
scanf("%d",&sum[rt]);
return;
}
int m=mid;
build(lson);
build(rson);
pushup(rt);
}
void out(int l,int r,int rt)
{
if(r==l)
{
printf("%d ",sum[rt]);
return;
}
pushdown(rt);
int m=mid;
out(lson);
out(rson);
}
void update1(int L,int R,int num,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
col[rt]=1;
deng[rt]=1;
sum[rt]=num;
return ;
}
pushdown(rt);
int m=mid;
if(L<=m) update1(L,R,num,lson);
if(m<R) update1(L,R,num,rson);
pushup(rt);
}
void update2(int L,int R,int num,int l,int r,int rt)
{
if(sum[rt]<num)
return ;
if(deng[rt]&&L<=l&&r<=R)
{
sum[rt]=gcd(sum[rt],num);
col[rt]=1;
deng[rt]=0;
return ;
}
if(l==r)
{
sum[rt]=gcd(sum[rt],num);
return ;
}
pushdown(rt);
int m=mid;
if(L<=m) update2(L,R,num,lson);
if(m<R) update2(L,R,num,rson);
pushup(rt);
}
int main()
{
int t,n,q;
// freopen("in.txt","r",stdin);
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
build(1,n,1);
scanf("%d",&q);
int op,l,r,x;
for(int i=0;i<q;i++)
{
scanf("%d%d%d%d",&op,&l,&r,&x);
if(op==1)
update1(l,r,x,1,n,1);
else
update2(l,r,x,1,n,1);
}
out(1,n,1);
printf("\n");
}
return 0;
}
HDU 4902 Nice boat 成段线段树的更多相关文章
- HDU 4902 (牛叉的线段树)
		
Nice boat Problem Description There is an old country and the king fell in love with a devil. The de ...
 - hdu 5274 Dylans loves tree(LCA + 线段树)
		
Dylans loves tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
 - HDU 3074.Multiply game-区间乘法-线段树(单点更新、区间查询),上推标记取模
		
Multiply game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
 - HDU 1394 Minimum Inversion Number(线段树求最小逆序数对)
		
HDU 1394 Minimum Inversion Number(线段树求最小逆序数对) ACM 题目地址:HDU 1394 Minimum Inversion Number 题意: 给一个序列由 ...
 - HDU 4902 Nice boat (线段树)
		
Nice boat 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4902 Description There is an old country a ...
 - HDU 4902 Nice boat  2014杭电多校训练赛第四场F题(线段树区间更新)
		
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4902 解题报告:输入一个序列,然后有q次操作,操作有两种,第一种是把区间 (l,r) 变成x,第二种是 ...
 - hdu 1698 Just a Hook(线段树之 成段更新)
		
Just a Hook Time Limit: ...
 - 线段树 + 区间更新 ----- HDU 4902 : Nice boat
		
Nice boat Time Limit: 30000/15000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Tot ...
 - HDU 4902 Nice boat --线段树(区间更新)
		
题意:给一个数字序列,第一类操作是将[l,r]内的数全赋为x ,第二类操作是将[l,r]中大于x的数赋为该数与x的gcd,若干操作后输出整个序列. 解法: 本题线段树要维护的最重要的东西就是一个区间内 ...
 
随机推荐
- git pull的时候出错: Git Couldn't reserve space for cygwin's heap
			
具体: 1. 运行CMD,以管理员身份打开 2. 运行:rebase.exe -b 0x50000000 msys-1.0.dll 再次git pull的时候,不再报错 转自:http://doc.o ...
 - 理解linux下源码、yum和rpm安装方法的特点
			
1.yum可看作在线安装,只需yum install 软件名,系统就自动根据yum源配置文件中的镜像位置去下载安装包,并可以自动分析所需的软件依赖关系,自动安装所需的依赖软件包.简单方便,不易出错,不 ...
 - windows下git命令的使用
			
一.写在前面 关于git,出于自己的爱好,前段时间玩了一下,也自己上网查了一下资料,现简单记录一下,以备查看. 当然,本文并不是介绍配置git服务器的文章,而是以github服务器作为git的远程仓库 ...
 - Maven使用(转)
			
说明:文章转自http://www.cnblogs.com/JeffreySun/archive/2013/03/14/2960573.html 创建project 先去官方网站下载一个最新版本htt ...
 - Android R资源文件无法更新或丢失
			
开发Android应用的时候,经常容易发生R文件丢失的事. 根据我的经验是当你更新了drawable里面的文件时,正好xml文件有错误, 这样会导致R文件出错. 此时如果你clean整个project ...
 - 背景建模或前景检測之PBAS
			
申明,本文非笔者原创,原文转载自:http://blog.csdn.net/kcust/article/details/9931575 Pixel-Based Adaptive Segmenter(P ...
 - maven 之dependencyManagement 和 pluginManagement
			
一个大中型Java项目中,一般由若干个module组成,各个module各司其职,担任整个工程中不同角色.大多数情况下,大多数module都会用到相同的jar包,或者插件.如果每个module中都引入 ...
 - Netdata----Linux 性能实时监测工具
			
https://my-netdata.io/ https://github.com/firehol/netdata/wiki http://soluck.iteye.com/blog/2291618
 - CLR基础,CLR运行过程,使用dos命令创建、编译、运行C#文件,查看IL代码
			
CLR是Common Language Runtime的缩写,是.NET程序集或可执行程序运行的一个虚拟环境.CLR用于管理托管代码,但是它本身是由非托管代码编写的,并不是一个包含了托管代码的程序集, ...
 - C# 中提取表中的某一项数据