Problem Nice boat(HDU 4902)

题目大意 

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

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

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

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

解题分析

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

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

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

参考程序

 #include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std; #define N 100008
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
int same[N*];
int n; int gcd(int x,int y){
return y ? gcd(y,x % y): x;
} void pushup(int rt){
if (same[rt<<]==same[rt<<|]) same[rt]=same[rt<<];
else same[rt]=-;
} void pushdown(int rt){
if (~same[rt]){
same[rt<<]=same[rt<<|]=same[rt];
same[rt]=-;
}
} void build(int l,int r,int rt){
same[rt]=-;
if (l==r){
scanf("%d",&same[rt]);
return;
}
int m=(l+r) / ;
build(lson);
build(rson);
pushup(rt);
} void update_1(int L,int R,int val,int l,int r,int rt){
if (L<=l && r<=R){
same[rt]=val;
return;
}
pushdown(rt);
int m=(l+r) /;
if (L<=m) update_1(L,R,val,lson);
if (m< R) update_1(L,R,val,rson);
pushup(rt);
} void update_2(int L,int R,int val,int l,int r,int rt){
if (L<=l && r<=R){
if (~same[rt]){
if (same[rt]>=val) same[rt]=gcd(same[rt],val);
return;
}
}
pushdown(rt);
int m=(l+r) /;
if (L<=m) update_2(L,R,val,lson);
if (m< R) update_2(L,R,val,rson);
pushup(rt);
} void query(int l,int r,int rt){
if (l==r){
printf("%d ",same[rt]);
return;
}
pushdown(rt);
int m=(l+r) / ;
query(lson);
query(rson);
} int main(){
int T;
scanf("%d",&T);
while (T--){
scanf("%d",&n);
build(,n,);
int type,a,b,val,q;
scanf("%d",&q);
while (q--){
scanf("%d %d %d %d",&type,&a,&b,&val);
if (type==) update_1(a,b,val,,n,);
if (type==) update_2(a,b,val,,n,);
}
query(,n,);
printf("%\n");
}
}

最新文章

  1. MS SQL Server 数据库分离-SQL语句
  2. Haproxy ssl 配置方式
  3. Myeclipse 2015 stable 2.0 完美破解方法
  4. TOMCAT 关闭报错:Tomcat did not stop in time. PID file was not removed
  5. vagrant yii2 Exception &#39;yii\db\Exception&#39; with message &#39;SQLSTATE[HY000] [2002]
  6. 栈的链式存储 - API实现
  7. 读 Zepto 源码之神奇的 $
  8. 【.NET异步编程系列3】取消异步操作
  9. Tensorflow的验证码识别
  10. 安卓工作室Android Studio 快捷键
  11. django 函数装饰器 变为 类装饰器
  12. 学习总结(ASP.NET MVC 5)
  13. Linux-(chgrp,chown,chmod)
  14. Centos7 Nginx 443端口反向代理springboot项目
  15. Hibernate连接数据库一直报NullPointerException
  16. HDU 2087 剪花布条 (KMP 不允许重叠的匹配)
  17. [GO]new函数的使用
  18. 数据降维(Dimensionality reduction)
  19. 2017.4.7 e.toString() 与 e.getMessage()的区别
  20. js滚轮事件需要注意的兼容性问题

热门文章

  1. webBrowser1执行函数
  2. js字符串处理
  3. 九度 题目1437:To Fill or Not to Fill
  4. Java script基础
  5. 5月25日 DOM
  6. Python Twisted介绍
  7. EL表达式详解
  8. POJ 1125 Stockbroker Grapevine 最短路 难度:0
  9. Android再次激活Activity时触发事件用于列表重新读取载入
  10. POJ 3384