线段树 + 区间更新 ----- HDU 4902 : Nice boat

Nice boat#

Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 968    Accepted Submission(s): 441


Problem 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<=2,n,Q<=100000
a_i,x >=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
 

 

Author
WJMZBMR
 

 

Source
 

 

【题目大意】

给你一串数字,有两个操作,1表示把一段区间内的数变成x,2表示把一段区间内的数如果这个数大于x则变为这个数与x的最小公约数,否则不变。最后输出变化后的一组数。

 

【题目分析】

这题和其他线段树有一些区别,这题是在所有的处理结束后才全部输出。
我们在每个结点中加一个flag标记该区间内的数字是否都是同一个,如果区间是同一个数的话我们就可以进行批处理,这将会大大降低时间复杂度。
再用一个temp来存储该结点的val,然后在pushdown函数将temp的值一层一层的传递下去。
这里的temp既起到了该结点是否已经向下更新的作用(相当于lazy),又起到了记录子节点需要更新的值的作用。
temp只有在val的值改变的时候才改变。

 

  


【解法二】 

这题不知道出题人怎么搞的,数据弱爆了,直接暴力还比线段树还快。

当然也不是单纯的暴力,从后往前搜,用一个数组prime来记录需要求gcd的值,遇到type=1就退出,最后来求一下gcd即可。

 

 

 

 

  

 

posted @   北岛知寒  阅读(179)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
点击右上角即可分享
微信分享提示
主题色彩