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