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,若干操作后输出整个序列. 解法: 本题线段树要维护的最重要的东西就是一个区间内 ...
随机推荐
- Mac安装homebrew安装到指定目录
第一种直接安装在/usr/local目录下 mac 打开终端输入 ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebr ...
- Tomcat启动异常 java.net.BindException: Cannot assign requested address: JVM_Bind
从Apache官网下载的tomcat7,在MyEclipse中启动时抛出如下异常: 严重: StandardServer.await: create[localhost:8005]: java.net ...
- HDU 3726 Graph and Queries (离线处理+splay tree)
Graph and Queries Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- blkblock 2工具
http://blog.yufeng.info/archives/tag/blktrace
- .Net高级技术——IDisposable
IDisposable概述 GC(垃圾收集器)只能回收托管(Managed)内存资源,对于数据库连接.文件句柄.Socket连接等这些资源(非托管资源,UnManaged)就无能为例,必须程序员自己控 ...
- GIF图片合集(用于网络请求图片用)
GIF图片合集(用于网络请求图片用)
- 进程操作篇atexit execl exit fprintf fscanf getpid nice get priority printf setpid system vfork wait waitpid
atexit(设置程序正常结束前调用的函数) 相关函数 _exit,exit,on_exit 表头文件 #include<stdlib.h> 定义函数 int atexit (void ( ...
- Maven中<dependencies>节点和<dependencyManagement>节点的区别
dependencyManagement只是插件管理,并不是真正的插件依赖,所以里面包含的插件在没有子项目使用的时候,并不会真正下载 1 .使用项目继承 利用项目继承可以将结构信息,部署信息,共同的依 ...
- poj 1469 COURSES 题解
COURSES Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 21515 Accepted: 8455 Descript ...
- 数学图形之莫比乌斯带(mobius)
莫比乌斯带,又被译作:莫比斯环,梅比斯環或麦比乌斯带.是一种拓扑学结构,它只有一个面(表面),和一个边界.即它的正反两面在同一个曲面上,左右两个边在同一条曲线上.看它的名字很洋气,听它的特征很玄乎,实 ...