Problem Nice boat(HDU 4902)

题目大意 

  维护一个序列,两种操作。

  第一种操作,将一段区间[l,r]赋值为x。

  第二种操作,将一段区间[l,r]中大于等于x的数与x求gcd。

  询问所有操作结束后的序列。

解题分析

  用线段树开一个标记same,表示这段区间中的数是否相同,若相同则为该数,否则为-1。

  对于第二种操作,对于覆盖区间内的same不为-1的子区间暴力修改。

  虽然时限有15s,但貌似跑得挺快的,只用了1s,不知是数据水还是什么缘故。

参考程序

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <cmath>
  4. #include <algorithm>
  5. using namespace std;
  6.  
  7. #define N 100008
  8. #define lson l,m,rt<<1
  9. #define rson m+1,r,rt<<1|1
  10. int same[N*];
  11. int n;
  12.  
  13. int gcd(int x,int y){
  14. return y ? gcd(y,x % y): x;
  15. }
  16.  
  17. void pushup(int rt){
  18. if (same[rt<<]==same[rt<<|]) same[rt]=same[rt<<];
  19. else same[rt]=-;
  20. }
  21.  
  22. void pushdown(int rt){
  23. if (~same[rt]){
  24. same[rt<<]=same[rt<<|]=same[rt];
  25. same[rt]=-;
  26. }
  27. }
  28.  
  29. void build(int l,int r,int rt){
  30. same[rt]=-;
  31. if (l==r){
  32. scanf("%d",&same[rt]);
  33. return;
  34. }
  35. int m=(l+r) / ;
  36. build(lson);
  37. build(rson);
  38. pushup(rt);
  39. }
  40.  
  41. void update_1(int L,int R,int val,int l,int r,int rt){
  42. if (L<=l && r<=R){
  43. same[rt]=val;
  44. return;
  45. }
  46. pushdown(rt);
  47. int m=(l+r) /;
  48. if (L<=m) update_1(L,R,val,lson);
  49. if (m< R) update_1(L,R,val,rson);
  50. pushup(rt);
  51. }
  52.  
  53. void update_2(int L,int R,int val,int l,int r,int rt){
  54. if (L<=l && r<=R){
  55. if (~same[rt]){
  56. if (same[rt]>=val) same[rt]=gcd(same[rt],val);
  57. return;
  58. }
  59. }
  60. pushdown(rt);
  61. int m=(l+r) /;
  62. if (L<=m) update_2(L,R,val,lson);
  63. if (m< R) update_2(L,R,val,rson);
  64. pushup(rt);
  65. }
  66.  
  67. void query(int l,int r,int rt){
  68. if (l==r){
  69. printf("%d ",same[rt]);
  70. return;
  71. }
  72. pushdown(rt);
  73. int m=(l+r) / ;
  74. query(lson);
  75. query(rson);
  76. }
  77.  
  78. int main(){
  79. int T;
  80. scanf("%d",&T);
  81. while (T--){
  82. scanf("%d",&n);
  83. build(,n,);
  84. int type,a,b,val,q;
  85. scanf("%d",&q);
  86. while (q--){
  87. scanf("%d %d %d %d",&type,&a,&b,&val);
  88. if (type==) update_1(a,b,val,,n,);
  89. if (type==) update_2(a,b,val,,n,);
  90. }
  91. query(,n,);
  92. printf("%\n");
  93. }
  94. }

HDU 4902 (线段树)的更多相关文章

  1. hdu 4902 线段树+逆向模拟

    http://acm.hdu.edu.cn/showproblem.php?pid=4902 出n个数,然后对这n个数进行两种操作: 如果是 1 l r x,则把 [l, r] 区间里面的每一个数都变 ...

  2. hdu 5877 线段树(2016 ACM/ICPC Asia Regional Dalian Online)

    Weak Pair Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...

  3. hdu 3974 线段树 将树弄到区间上

    Assign the task Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  4. hdu 3436 线段树 一顿操作

    Queue-jumpers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

  5. hdu 3397 线段树双标记

    Sequence operation Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  6. hdu 4578 线段树(标记处理)

    Transformation Time Limit: 15000/8000 MS (Java/Others)    Memory Limit: 65535/65536 K (Java/Others) ...

  7. hdu 4533 线段树(问题转化+)

    威威猫系列故事——晒被子 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Tot ...

  8. hdu 2871 线段树(各种操作)

    Memory Control Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  9. hdu 4052 线段树扫描线、奇特处理

    Adding New Machine Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

随机推荐

  1. Binary Tree Level Order Traversal [LeetCode]

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  2. ocument的createDocumentFragment()方法

    在<javascript高级程序设计>一书的6.3.5:创建和操作节点一节中,介绍了几种动态创建html节点的方法,其中有以下几种常见方法: · crateAttribute(name): ...

  3. Mahout推荐算法基础

    转载自(http://www.geek521.com/?p=1423) Mahout推荐算法分为以下几大类 GenericUserBasedRecommender 算法: 1.基于用户的相似度 2.相 ...

  4. LevelDb简单介绍和原理——本质:类似nedb,插入数据文件不断增长(快照),再通过删除老数据做更新

    转自:http://www.cnblogs.com/haippy/archive/2011/12/04/2276064.html 有时间再好好看下整个文章! 说起LevelDb也许您不清楚,但是如果作 ...

  5. 使用Linq快速的操作XML

    开始内容之前先分享一段话 有时候,当你知道要做什么的时候就做的很快,比如你要实现个功能,码字的活儿不算很难,做个检索也不会有什么难倒你的.但是,做着做着,你发现好像世界上的工作都在重复,于是你有种心要 ...

  6. plsql记住登录密码

    登录plsql:tools(工具)->preference(首选项)->Login history(登录历史):选择"Store with password"(带口令存 ...

  7. 获取手机通讯录放入PinnedSectionListView中,按名字首字母排序,并且实现拨打电话功能。

    package com.lixu.tongxunlu; import java.util.ArrayList; import com.lixu.tongxunlu.PinnedSectionListV ...

  8. nginx 配置优化的几个参数

    nginx 配置优化的几个参数 2011-04-22 本文地址: http://blog.phpbean.com/a.cn/7/ --水平有限欢迎指正-- -- 最近在服务器上搞了一些nginx 研究 ...

  9. windows常见已知熟悉操作命令

    WIN+R--->输入CMD---->回车有关某个命令的详细信息,请键入 HELP 命令名ASSOC          显示或修改文件扩展名关联.ATTRIB         显示或更改文 ...

  10. 告别硬编码-发个获取未导出函数地址的Dll及源码

    还在为找内核未导出函数地址而苦恼嘛? 还在为硬编码通用性差而不爽吗? 还在为暴搜内核老蓝屏而痛苦吗? 请看这里: 最近老要用到内核未导出的函数及一些结构,不想再找特征码了,准备到网上找点符号文件解析的 ...