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,若干操作后输出整个序列. 解法: 本题线段树要维护的最重要的东西就是一个区间内 ...
随机推荐
- flash从数据流判断图片格式防止xss攻击
前段时间测试人员报了一个flash的xss bug,经分析用了Loader.loadBytes且没做数据流格式校验的程序都会中招,自测方法只需一行代码: ExternalInterface.call( ...
- NHibernate的调试技巧和Log4Net配置
1.查看nhibernate写在控制台里的sql语句 在配置文件中有这么个选项,假如把它设置为true,nhibernate会把执行的sql显示在控制台上. <property name=&qu ...
- 使用结构struct作为Dictionary<TKey,TValue>的键
我们经常用简单数据类型,比如int作为泛型Dictionary<TKey,TValue>的key,但有时候我们希望自定义数据类型作为Dictionary<TKey,TValue> ...
- 转 Objective-C中不同方式实现锁(一)
为什么需要使用锁,当然熟悉多线程的你,自然不会对它觉得陌生. 那你在代码中是否很好的使用了锁的机制呢?你又知道几种实现锁的方法呢? 今天一起来探讨一下Objective-C中几种不同方式实现的锁,在这 ...
- [翻译] MCProgressView 使用自定义图片做进度显示
MCProgressView 使用自定义图片做进度显示 https://github.com/Baglan/MCProgressView Progress bar view with custom i ...
- Java读取properties配置文件时,中文乱码解决方法
public static String getConfig(String key) { Properties pros = new Properties(); String value = &quo ...
- openshift 添加cron定时任务
一般linux添加cron任务是在/etc/crontab,但是由于openshift的权限木有这么开放,所以如果需要设置定时任务的话,需要在如下的文件夹下添加你的sh文件,因为我需要的是每天运行一次 ...
- jquery的each函数的用法
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- coursera课程Text Retrieval and Search Engines之Week 2 Overview
Week 2 OverviewHelp Center Week 2 On this page: Instructional Activities Time Goals and Objectives K ...
- Paint、Canvas、Matrix使用解说(一、Paint)
username=tianjian4592">我正在參加 CSDN 2015博客之星评选 感恩分享活动,假设认为文章还不错,请投个票鼓舞下吧:http://vote.blog.csdn ...