Nice boat

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=4902

Description


There is an old country and the king fell in love with 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.

Input


The first line contains an integer T, denoting the 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=0
a_i,x is in the range of int32(C++)

Output


For each test case, output a line with n integers separated by a single space representing the final sequence.
Please output a single more space after end of the sequence

Sample Input


1
10
16807 282475249 1622650073 984943658 1144108930 470211272 101027544 1457850878 1458777923 2007237709
10
1 3 6 74243042
2 4 8 16531729
1 3 4 1474833169
2 1 8 1131570933
2 7 9 1505795335
2 3 7 101929267
1 4 10 1624379149
2 2 8 2110010672
2 6 7 156091745
1 2 5 937186357

Sample Output


16807 937186357 937186357 937186357 937186357 1 1 1624379149 1624379149 1624379149


##题意:

对一个数组进行若干操作后,输出结果数组.
1. 将区间内的值都变为x.
2. 将区间内的大于x的值ai变为gcd(ai,x).


##题解:

很容易想到用线段树维护,关键是如何操作2的gcd.
对于树中的每个结点维护一个equal, 表示当前结点的子节点是否相等. (若相等就等于子节点的值,否则为-1).
当更新到某区间时,若区间内的值都相同,则只更新到这里即可,下面的结点利用pushdown来更新.


##代码:
``` cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LL long long
#define eps 1e-8
#define maxn 101000
#define mod 100000007
#define inf 0x3f3f3f3f
#define mid(a,b) ((a+b)>>1)
#define IN freopen("in.txt","r",stdin);
using namespace std;

LL gcd(LL a, LL b) {

return !b? a:gcd(b, a%b);

}

int n;

LL num[maxn];

struct Tree

{

int left,right;

LL lazy, equl;

}tree[maxn<<2];

void build(int i,int left,int right)

{

tree[i].left=left;

tree[i].right=right;

tree[i].lazy=0;

if(left==right){
tree[i].equl = num[left];
return ;
} int mid=mid(left,right); build(i<<1,left,mid);
build(i<<1|1,mid+1,right); tree[i].equl = tree[i<<1].equl==tree[i<<1|1].equl ? tree[i<<1].equl : -1;

}

void pushdown(int i)

{

if(tree[i].equl != -1) {

tree[i<<1].equl = tree[i].equl;

tree[i<<1|1].equl = tree[i].equl;

tree[i].lazy = 0;

tree[i<<1].lazy = 0;

tree[i<<1|1].lazy = 0;

}

if(tree[i].lazy) {

tree[i<<1].lazy = tree[i].lazy;

tree[i<<1|1].lazy = tree[i].lazy;

if(tree[i<<1].equl != -1) {

tree[i<<1].equl = tree[i].lazy;

tree[i<<1].lazy = 0;

}

if(tree[i<<1|1].equl != -1) {

tree[i<<1|1].equl = tree[i].lazy;

tree[i<<1|1].lazy = 0;

}

tree[i].lazy = 0;

}

}

void update(int i,int left,int right,LL d)

{

if(tree[i].leftleft&&tree[i].rightright)

{

if(tree[i].equl == -1) tree[i].lazy = d;

else tree[i].equl = d;

return ;

}

pushdown(i);

int mid=mid(tree[i].left,tree[i].right);

if(right<=mid) update(i<<1,left,right,d);
else if(left>mid) update(i<<1|1,left,right,d);
else {
update(i<<1,left,mid,d);
update(i<<1|1,mid+1,right,d);
} tree[i].equl = tree[i<<1].equl==tree[i<<1|1].equl ? tree[i<<1].equl : -1;

}

void update_gcd(int i,int left,int right, LL x)

{

if(tree[i].leftleft&&tree[i].rightright && tree[i].equl!=-1)

{

if(tree[i].equl > x) {

tree[i].equl = gcd(tree[i].equl, x);

tree[i].lazy = 0;

}

return ;

}

pushdown(i);

int mid=mid(tree[i].left,tree[i].right);

if(right<=mid) update_gcd(i<<1,left,right,x);
else if(left>mid) update_gcd(i<<1|1,left,right,x);
else {
update_gcd(i<<1,left,mid,x);
update_gcd(i<<1|1,mid+1,right,x);
} tree[i].equl = tree[i<<1].equl==tree[i<<1|1].equl ? tree[i<<1].equl : -1;

}

LL query(int i,int left,int right)

{

if(tree[i].leftleft&&tree[i].rightright)

return tree[i].equl;

pushdown(i);

int mid=mid(tree[i].left,tree[i].right);

if(right<=mid) return query(i<<1,left,right);
else if(left>mid) return query(i<<1|1,left,right);
else return query(i<<1,left,mid)+query(i<<1|1,mid+1,right);

}

int main(int argc, char const *argv[])

{

//IN;

int t; cin >> t;
while(t--)
{
int m;
scanf("%d", &n);
for(int i=1; i<=n; i++)
scanf("%lld", &num[i]);
build(1, 1, n); scanf("%d", &m);
while(m--) {
int op, l, r; LL x;
scanf("%d %d %d %lld", &op,&l,&r,&x);
if(op == 1) {
update(1, l, r, x);
}
else if(op == 2) {
update_gcd(1, l, r, x);
}
} for(int i=1; i<=n; i++) {
printf("%lld ", query(1, i, i));
}
printf("\n");
} return 0;

}

最新文章

  1. 缩放因子和UI设计
  2. Java 实现HTML富文本导出至word完美解决方案
  3. Redis_密码管理(转)
  4. 2015年我国IT行业发展趋势分析(转)
  5. Nodejs开发(1.Sublime Text 3配置)
  6. Java魔法堂:String.format详解
  7. view的封装
  8. mysql 按年度、季度、月度、周、日SQL统计查询
  9. 函数查询(Function Query)
  10. css-布局1-基本属性
  11. HTTP&#160;错误
  12. Unity3d中如何混用三种脚本语言?
  13. 步步为营-104-SQL语句(截取字符串)
  14. 长字符串换行word-break
  15. Keras.applications.models权重:存储路径及加载
  16. jfinal如何设置使用哪种模板引擎(视图)
  17. Day 34 黏包
  18. TObjectList
  19. 从上面的集合框架图可以看到,Java 集合框架主要包括两种类型的容器,一种是集合(Collection),存储一个元素集合,另一种是图(Map),存储键/值对映射
  20. PCLK怎么获得?

热门文章

  1. Android中常见的MVC模式
  2. Ibatis,Spring整合(注解方式注入)
  3. 新浪实时股票数据接口http://hq.sinajs.cn/list=code
  4. NSAutoReleasePool
  5. UVa 11572 Unique snowflakes【滑动窗口】
  6. The resource could not be loaded because the App Transport
  7. HDU 2433 Travel (最短路,BFS,变形)
  8. django - 好的 获取 参数值 方法
  9. uestc 1722 吴神的表白
  10. poj 1787 Charlie&#39;s Change