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的值改变的时候才改变。

  1. //Memory Time
  2. // 5376K 651MS
  3. #include<algorithm>
  4. #include<cstdio>
  5. #include<cstring>
  6. #include<cstdlib>
  7. #include<iostream>
  8. #include<vector>
  9. #include<queue>
  10. #include<stack>
  11. #include<iomanip>
  12. #include<string>
  13. #include<climits>
  14. #include<cmath>
  15. #define MAX 100100
  16. #define LL long long
  17. using namespace std;
  18. int n,m;
  19. int ans;
  20. int num[MAX];
  21. struct Tree
  22. {
  23. int l,r;
  24. bool flag;
  25. int val,temp;
  26. };
  27. Tree tree[MAX<<2];
  28.  
  29. int gcd(int x,int y)
  30. {
  31. return y?gcd(y,x%y):x;
  32. }
  33.  
  34. void pushup(int x)
  35. {
  36. int tmp=x<<1;
  37. tree[x].val=max(tree[tmp].val,tree[tmp+1].val);
  38. tree[x].flag=(tree[tmp].val==tree[tmp+1].val&&tree[tmp].flag&&tree[tmp+1].flag);
  39. }
  40.  
  41. void pushdown(int x)
  42. {
  43. if(tree[x].temp==-1)return;
  44. int tmp=x<<1;
  45. int mid=(tree[x].l+tree[x].r)>>1;
  46. tree[tmp].val=tree[tmp+1].val=tree[tmp].temp=tree[tmp+1].temp=tree[x].temp;
  47. tree[x].temp=-1;
  48. }
  49.  
  50. void build(int l,int r,int x)
  51. {
  52. tree[x].flag=0;
  53. tree[x].temp=-1;
  54. tree[x].l=l,tree[x].r=r;
  55. if(l==r)
  56. {
  57. scanf("%d",&tree[x].val);
  58. tree[x].flag=1;
  59. return;
  60. }
  61. int tmp=x<<1;
  62. int mid=(l+r)>>1;
  63. build(l,mid,tmp);
  64. build(mid+1,r,tmp+1);
  65. pushup(x);
  66. }
  67.  
  68. void update(int l,int r,int num,int x)
  69. {
  70. if(r<tree[x].l||l>tree[x].r)return;
  71. if(l<=tree[x].l&&r>=tree[x].r)
  72. {
  73. tree[x].flag=1;
  74. tree[x].val=num;
  75. tree[x].temp=num;
  76. return;
  77. }
  78. pushdown(x);
  79. int tmp=x<<1;
  80. int mid=(tree[x].l+tree[x].r)>>1;
  81. if(r<=mid)
  82. update(l,r,num,tmp);
  83. else if(l>mid)
  84. update(l,r,num,tmp+1);
  85. else
  86. {
  87. update(l,mid,num,tmp);
  88. update(mid+1,r,num,tmp+1);
  89. }
  90. pushup(x);
  91. }
  92.  
  93. void change(int l,int r,int num,int x)
  94. {
  95. if(r<tree[x].l||l>tree[x].r)return;
  96. if(tree[x].flag&&tree[x].val<=num)return;
  97. if(l<=tree[x].l&&r>=tree[x].r&&tree[x].flag)
  98. {
  99. tree[x].val=gcd(tree[x].val,num);
  100. tree[x].temp=tree[x].val;
  101. return;
  102. }
  103. pushdown(x);
  104. int tmp=x<<1;
  105. int mid=(tree[x].l+tree[x].r)>>1;
  106. if(r<=mid)
  107. change(l,r,num,tmp);
  108. else if(l>mid)
  109. change(l,r,num,tmp+1);
  110. else
  111. {
  112. change(l,mid,num,tmp);
  113. change(mid+1,r,num,tmp+1);
  114. }
  115. pushup(x);
  116. }
  117.  
  118. void query(int l,int r,int k,int x)
  119. {
  120. if(k<tree[x].l||k>tree[x].r)return;
  121. if(tree[x].flag)
  122. {
  123. ans=tree[x].val;
  124. return;
  125. }
  126. pushdown(x);
  127. int tmp=x<<1;
  128. int mid=(tree[x].l+tree[x].r)>>1;
  129. if(k<=mid)
  130. query(l,mid,k,tmp);
  131. else
  132. query(mid+1,r,k,tmp+1);
  133. }
  134.  
  135. int main()
  136. {
  137. int T;
  138. int t,l,r,num;
  139. scanf("%d",&T);
  140. while(T--)
  141. {
  142. scanf("%d",&n);
  143. build(1,n,1);
  144. scanf("%d",&m);
  145. while(m--)
  146. {
  147. scanf("%d %d %d %d",&t,&l,&r,&num);
  148. if(t==1)
  149. update(l,r,num,1);
  150. else
  151. change(l,r,num,1);
  152. }
  153. for(int i=1;i<=n;i++)
  154. {
  155. ans=0;
  156. query(1,n,i,1);
  157. printf("%d ",ans);
  158. }
  159. puts("");
  160. }
  161. return 0;
  162. }

  


【解法二】

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

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

  1. //Memory Time
  2. // 2232K 250MS
  3. #include<algorithm>
  4. #include<cstdio>
  5. #include<cstring>
  6. #include<cstdlib>
  7. #include<iostream>
  8. #include<vector>
  9. #include<queue>
  10. #include<stack>
  11. #include<iomanip>
  12. #include<string>
  13. #include<climits>
  14. #include<cmath>
  15. #define MAX 100100
  16. #define LL long long
  17. using namespace std;
  18. int T,n,q;
  19. int num[MAX];
  20. int t[MAX],l[MAX],r[MAX],x[MAX];
  21. int prime[MAX];
  22. int gcd(int x,int y)
  23. {
  24. return y?gcd(y,x%y):x;
  25. }
  26. int main()
  27. {
  28. // freopen("cin.txt","r",stdin);
  29. // freopen("cout.txt","w",stdout);
  30. scanf("%d",&T);
  31. while(T--)
  32. {
  33. scanf("%d",&n);
  34. for(int i=1;i<=n;++i)
  35. scanf("%d",&num[i]);
  36. scanf("%d",&q);
  37. for(int i=1;i<=q;++i)
  38. scanf("%d %d %d %d",&t[i],&l[i],&r[i],&x[i]);
  39. for(int i=1;i<=n;++i)
  40. {
  41. int tp=num[i];
  42. int index=-1;
  43. for(int j=q;j>=1;--j)
  44. {
  45. if(i>=l[j]&&i<=r[j])
  46. {
  47. if(t[j]==1)
  48. {
  49. tp=x[j];
  50. break;
  51. }
  52. else prime[++index]=x[j];
  53. }
  54. }
  55. for(int j=index;j>=0;--j)
  56. {
  57. if(tp>prime[j])
  58. tp=gcd(tp,prime[j]);
  59. }
  60. printf("%d ",tp);
  61. }
  62. puts("");
  63. }
  64. return 0;
  65. }

  

线段树 + 区间更新 ----- HDU 4902 : Nice boat的更多相关文章

  1. 线段树-区间更新-HDU 1689

    #include <iostream> #include <cstdio> #include <string> #include <cstring> # ...

  2. HDU 4902 Nice boat 2014杭电多校训练赛第四场F题(线段树区间更新)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4902 解题报告:输入一个序列,然后有q次操作,操作有两种,第一种是把区间 (l,r) 变成x,第二种是 ...

  3. HDU 5023 A Corrupt Mayor's Performance Art(线段树区间更新)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5023 解题报告:一面墙长度为n,有N个单元,每个单元编号从1到n,墙的初始的颜色是2,一共有30种颜色 ...

  4. hdu 4031 attack 线段树区间更新

    Attack Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)Total Subm ...

  5. hdu 3966(树链剖分+线段树区间更新)

    传送门:Problem 3966 https://www.cnblogs.com/violet-acmer/p/9711441.html 学习资料: [1]线段树区间更新:https://blog.c ...

  6. HDU.1556 Color the ball (线段树 区间更新 单点查询)

    HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...

  7. HDU 1556 Color the ball(线段树区间更新)

    Color the ball 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...

  8. HDU 1698 线段树 区间更新求和

    一开始这条链子全都是1 #include<stdio.h> #include<string.h> #include<algorithm> #include<m ...

  9. (简单) HDU 1698 Just a Hook , 线段树+区间更新。

    Description: In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of ...

随机推荐

  1. 请教Mysql如何删除 不包含 某些字符的记录

    删除包含指定字符的记录 delete from `表` where `字段` like '%指定字符1%' or like '%指定字符2%' or like '%指定字符3%' 删除不包含指定字符的 ...

  2. 单例模式在JDBC数据库连接操作里的应用

    设计模式之单例模式一般应用在在数据库操作里,数据库操作就要常常创建实例,然后进行数据库操作,全部就能够 将数据库操作的方法.进行封装,然后採用单例模式进行设计,然后採用单例模式之后,就能够节约系统资源 ...

  3. Project Euler:Problem 32 Pandigital products

    We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly o ...

  4. yum 卸载 error: %preun(tengine-2.1.0-1.el6.x86_64) scriptlet failed, exit status 6

    error: %preun(tengine-2.1.0-1.el6.x86_64) scriptlet failed, exit status 6 Error in PREUN scriptlet i ...

  5. CCParallaxNode

    // 创建cat精灵 CCSprite* cat = CCSprite::create("Image\\grossini.png"); //change the transform ...

  6. Kprobes

    https://landley.net/kdocs/ols/2007/ols2007v1-pages-215-224.pdf https://www.kernel.org/doc/Documentat ...

  7. How lock works?

    Eliminating Synchronization-Related Atomic Operations with Biased Locking and Bulk Rebiasing http:// ...

  8. Asp.Net MVC App_Code无法识别

    Asp.Net MVC需要写公共类的时候 右击添加 App_Code 文件夹,新建类—>右击类—>属性,生成操作 —>选择 —>编译 Asp.Net MVC项目本身是个应用程序 ...

  9. Windows API 错误码

    在多数情况下,windows API在发生错误时很少抛出异常,多数是通过函数返回值进行处理.(windows api中无返回值的函数很少.) windows api错误处理通常按照以下方式:首先api ...

  10. django后台显示图片 而不是图片地址

    修改admin代码 class Ad_CampaingAdmin(admin.ModelAdmin): list_display = ("content","previe ...