HDU-4902 Nice boat
Nice boat
Time Limit: 30000/15000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1038 Accepted Submission(s):
465
a devil. The devil always asks the king to do some crazy things. Although the
king used to be wise and beloved by his people. Now he is just like a boy in
love and can’t refuse any request from the devil. Also, this devil is looking
like a very cute Loli.
Let us continue our story, z*p(actually you)
defeat the 'MengMengDa' party's leader, and the 'MengMengDa' party dissolved.
z*p becomes the most famous guy among the princess's knight party.
One
day, the people in the party find that z*p has died. As what he has done in the
past, people just say 'Oh, what a nice boat' and don't care about why he
died.
Since then, many people died but no one knows why and everyone is
fine about that. Meanwhile, the devil sends her knight to challenge you with
Algorithm contest.
There is a hard data structure problem in the
contest:
There are n numbers a_1,a_2,...,a_n on a line, everytime you can
change every number in a segment [l,r] into a number x(type 1), or change every
number a_i in a segment [l,r] which is bigger than x to gcd(a_i,x) (type
2).
You should output the final sequence.
number of the test cases.
For each test case, the first line contains a
integers n.
The next line contains n integers a_1,a_2,...,a_n separated by a
single space.
The next line contains an integer Q, denoting the number of the
operations.
The next Q line contains 4 integers t,l,r,x. t denotes the
operation type.
T<=2,n,Q<=100000
a_i,x >=0
a_i,x is in
the range of int32(C++)
separated by a single space representing the final sequence.
Please output
a single more space after end of the sequence
最后输出变化后的一组数。
用线段树写的,主要是利用mark标记 ,全部变成x的话直接把这段变成x,然后加个tree[i].mark就行了,如果往下更新,要把标记和数一起往下传具体看pushdown函数,查询的时候如果延迟标记存在的话直接把这一段输出,因为标记存在这一段肯定是相同的数。。
第2步操作主要是,如果什么已经标记了则全都是相同的数,比较一下求公约数。
如果没标记就要tree[i].left==tree[i].right。比较一下这个数,就行了。
#include<iostream>
#include<cstring>
#include<cstdio>
const int maxx=;
using namespace std;
int a[*maxx];
struct tree
{
int right,left;
int mark,num;
}tree[*maxx];
int gcd(int a,int b)
{
return b==?a:gcd(b,a%b);
}
void build(int i,int left,int right)
{
tree[i].left=left;
tree[i].right=right;
tree[i].mark=;
if(left==right)
{
tree[i].num=a[left];
return ;
}
int mid=(left+right)/;
build(*i,left,mid);
build(*i+,mid+,right);
}
void pushdown(int r)
{
if(tree[r].mark)
{
tree[*r].num=tree[*r+].num=tree[r].num;
tree[*r].mark=tree[*r+].mark=tree[r].mark;
tree[r].mark=;
}
}
void update(int r,int i,int j,int k)
{
int mid;
mid=(tree[r].left+tree[r].right)/;
if(tree[r].left>=i&&tree[r].right<=j)
{
tree[r].num=k;
tree[r].mark=;
return;
}
pushdown(r);//向下传递。
if(i>mid)
update(*r+,i,j,k);
else if(j<=mid)
update(r*,i,j,k);
else
{
update(*r,i,mid,k);
update(*r+,mid+,j,k);
}
}
void change(int r,int i,int j,int x)
{
int mid;
mid=(tree[r].left+tree[r].right)/;
if(tree[r].mark)
{
if(i<=tree[r].left&&j>=tree[r].right)
{
if(tree[r].num<=x)
return ;
else
{
tree[r].num=gcd(tree[r].num,x);
return ;
}
}
}
if(tree[r].left==tree[r].right)
{
if(tree[r].num>x)
{
tree[r].num=gcd(tree[r].num,x);
return ;
}
else
return ;
}
pushdown(r);
if(i>mid)
change(*r+,i,j,x);
else if(j<=mid)
change(r*,i,j,x);
else
{
change(*r,i,mid,x);
change(*r+,mid+,j,x);
}
}
void prit(int r)
{
int i;
if(tree[r].left==tree[r].right)
{
printf("%d ",tree[r].num);
return ;
}//
if(tree[r].mark)
{
for(i=tree[r].left;i<=tree[r].right;i++)
printf("%d ",tree[r].num);
return;
} prit(*r);
prit(*r+); }
int main()
{
int t,n,x,y,m,k,v,i;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i=;i<=n;i++)
scanf("%d",&a[i]); scanf("%d",&m);
build(,,n);
for(i=;i<=m;i++)
{
scanf("%d",&k);
if(k==)
{
scanf("%d%d%d",&x,&y,&v);
update(,x,y,v);
}
else
{
scanf("%d%d%d",&x,&y,&v);
change(,x,y,v);
} }
prit();
printf("\n");
}
return ;
}
HDU-4902 Nice boat的更多相关文章
- 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(线段树区间改动,输出终于序列)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4902 Problem Description There is an old country and ...
- HDU 4902 Nice boat 2014杭电多校训练赛第四场F题(线段树区间更新)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4902 解题报告:输入一个序列,然后有q次操作,操作有两种,第一种是把区间 (l,r) 变成x,第二种是 ...
- 线段树 + 区间更新 ----- 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,若干操作后输出整个序列. 解法: 本题线段树要维护的最重要的东西就是一个区间内 ...
- 2014多校第四场1006 || HDU 4902 Nice boat (线段树 区间更新)
题目链接 题意 : 给你n个初值,然后进行两种操作,第一种操作是将(L,R)这一区间上所有的数变成x,第二种操作是将(L,R)这一区间上所有大于x的数a[i]变成gcd(x,a[i]).输出最后n个数 ...
- hdu 4902 Nice boat 线段树
题目链接 给n个数, 两种操作, 第一种是将区间内的数变成x, 第二种是将区间内大于x的数变为gcd(x, a[i]). 开三个数组, 一个记录区间最大值, 这样可以判断是否更新这一区间, 一个laz ...
- HDU 4902 Nice boat 成段线段树
操作1 的时候标记deng[rt]表示以下一段数都是与当前节点的值同样 下次操作2时直接对有deng标记的节点gcd更新 (可能还能够更简单) #include <stdio.h> #in ...
- HDU 4902 Nice boat 线段树+离线
据说暴力也过了.还傻逼地写了这么长. . . #include <stdio.h> #include <string.h> #include <math.h> #i ...
- HDU 4902 Nice boat 多校4 线段树
给定n个数 第一个操作和普通,区间覆盖性的,把l-r区间的所有值改成固定的val 第二个操作是重点,输入l r x 把l-r区间的所有大于x的数,变成gcd(a[i],x) a[i]即指满足条件的序列 ...
随机推荐
- CSS自动控制图片大小的代码
img { max-width: 800px; height: auto; } 代码中的max-width:800px限制图片的最大宽度为800像素,而下面的hight:auto很关键,可以保证图片有 ...
- [C# 基础知识系列]专题一:深入解析委托——C#中为什么要引入委托
转自http://www.cnblogs.com/zhili/archive/2012/10/22/Delegate.html 引言: 对于一些刚接触C# 不久的朋友可能会对C#中一些基本特性理解的不 ...
- T-SQL中的透视和逆透视
透视 今天抽一点时间来看看透视和逆透视语句,简单的说就是行列转换.假设一个销售表中存放着产品号,产品折扣,产品价格三个列,每一种产品号可能有多种折扣,每一种折扣只对应一个产品价格.下面贴出建表语句和插 ...
- ipad屏幕旋转后的代理
- (void)statusBarOrientationChange:(NSNotification *)notification { UIInterfaceOrientation o ...
- ios 用LLDB查看模拟器文件路径以及一些常用的命令
我看网络上有好多有关lldb调试命令的介绍,我都看了一遍,都没有这个方法,所以我在这里补充出来,帮助需要的人. 另外附上一些 实用LLDB命令 我们可以使用e命令定义变量 (lldb) e NSStr ...
- Object-C — KVC
1:使用kvc存取对象属性 如果要更改对象属性可以通过什么方法达到呢? (1)通过setter和getter方法. (2)属性. (3)直接设置实例变量. 今天学习新的一种方法:键值编码-kvc.通过 ...
- XML&DTD&XML Schema学习
XML(eXtensible Markup Language)可扩展的标记语言.xml在web service编程中尤为重要.在网络传输中可以作为传输数据的载体.xml作为元语言,它可以用来标记数据. ...
- 关于MVC中使用dynamic
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA2kAAAB6CAIAAACqQIxZAAAgAElEQVR4nO2dT2wcx53v6zgXAgsYvA
- 【实习记】2014-08-22试用SSH客户端XShell与SecuretyCRT与MobaXterm总结
虚拟机下来了,是32位8G内存双核的win7系统. 测试显示实习生可以用办公机登录虚拟机在ssh到linux编译机.办公机虽ping通但不可以ssh上去. 只说这么多. 用惯linux下的sh ...
- ZOJ 2015 10月份 月赛 3911 Prime Query
这道题我改啊,改啊,交啊,就对了. #include <stdio.h> #include <stdlib.h> #include <math.h> #includ ...