hdu 4902 Nice boat(线段树区间改动,输出终于序列)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4902
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.
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++)
Please output a single more space after end of the sequence
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
16807 937186357 937186357 937186357 937186357 1 1 1624379149 1624379149 1624379149
给出长度为n的数组,m个操作。操作有两种:1、1, l, r, x即把[l,r]段的元素全变为x。2、2,l,r,x即把[l,r]段的大于x的元素全变成该元素与x的最大公约数。
代码例如以下:
#include <cstdio>
#include <algorithm>
#include <iostream>
using namespace std;
#define LL long long
const int maxn = 111111;
#define ls (rt << 1)
#define rs (rt << 1 | 1)
#define lson l, mid, ls
#define rson mid+1, r, rs
//num数组表示该区间是否都是同一个数,假设是num就等于这个数,否则等-1
int num[maxn << 2];
//flag标记,表示该区间是否应该被纯色化(变成一个数)。
int flag[maxn << 2];
//mmax数组表示该区间的最大值,由于gcd仅仅对该区间比x大的起作用
int mmax[maxn << 2];
int GCD(int a, int b)
{
return a ? GCD(b%a,a) : b;
}
void Pushup(int rt)
{
if(num[ls] == num[rs])
num[rt] = num[ls];
else
num[rt] = -1;
mmax[rt] = max(mmax[ls], mmax[rs]);
} void Pushdown(int rt)
{
if(flag[rt] != -1)
{
flag[ls] = flag[rs] = flag[rt];
mmax[ls] = mmax[rs] = mmax[rt];
num[ls] = num[rs] = num[rt];
flag[rt] = -1;
}
} void build(int l, int r, int rt)
{
flag[rt] = -1;
if(l == r)
{
scanf("%d",&num[rt]);
mmax[rt] = num[rt];//初始化
return ;
}
int mid = (l+r) >> 1;
build(lson);
build(rson);
Pushup(rt);
} void update(int L, int R, int x, int l, int r, int rt)
{
if(L <= l && r <= R)
{
flag[rt] = num[rt] = mmax[rt] = x;
return ;
}
Pushdown(rt);
int mid = (l+r) >> 1;
if(L <= mid)
update(L, R, x, lson);
if(mid < R)
update(L, R, x, rson);
Pushup(rt);
} void modify(int L, int R, int x, int l, int r, int rt)
{
if(L <= l && r <= R && num[rt] > x)
{
flag[rt] = num[rt] = mmax[rt] = GCD(num[rt], x);
return ;
}
Pushdown(rt);
int mid = (l+r) >> 1;
if(L <= mid && mmax[ls] > x)
modify(L, R, x, lson);
if(mid < R && mmax[rs] > x)
modify(L, R, x, rson);
Pushup(rt);
} void Cout(int l, int r, int rt)
{
if(l == r)
{
printf("%d ",num[rt]);
return ;
}
Pushdown(rt);
int mid = (l+r) >> 1;
Cout(lson);
Cout(rson);
} int main()
{
int T;
int R, L;
int n, m;
int op,l, r, x;
int i, j, k;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
build(1,n,1);
scanf("%d",&m);
for(i = 0; i < m; i++)
{
scanf("%d%d%d%d",&op,&l,&r,&x);
if(op == 1)
{
update(l, r, x, 1, n, 1);
}
else if(op == 2)
{
modify(l, r, x, 1, n, 1);
}
}
Cout(1, n, 1);
printf("\n");
}
return 0;
}
hdu 4902 Nice boat(线段树区间改动,输出终于序列)的更多相关文章
- HDU 4902 Nice boat --线段树(区间更新)
题意:给一个数字序列,第一类操作是将[l,r]内的数全赋为x ,第二类操作是将[l,r]中大于x的数赋为该数与x的gcd,若干操作后输出整个序列. 解法: 本题线段树要维护的最重要的东西就是一个区间内 ...
- hdu 4902 Nice boat 线段树
题目链接 给n个数, 两种操作, 第一种是将区间内的数变成x, 第二种是将区间内大于x的数变为gcd(x, a[i]). 开三个数组, 一个记录区间最大值, 这样可以判断是否更新这一区间, 一个laz ...
- HDU 4902 Nice boat 线段树+离线
据说暴力也过了.还傻逼地写了这么长. . . #include <stdio.h> #include <string.h> #include <math.h> #i ...
- HDU 6638 - Snowy Smile 线段树区间合并+暴力枚举
HDU 6638 - Snowy Smile 题意 给你\(n\)个点的坐标\((x,\ y)\)和对应的权值\(w\),让你找到一个矩形,使这个矩阵里面点的权值总和最大. 思路 先离散化纵坐标\(y ...
- hdu 1540 Tunnel Warfare(线段树区间统计)
Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- HDU 3577 Fast Arrangement (线段树区间更新)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3577 题意不好理解,给你数字k表示这里车最多同时坐k个人,然后有q个询问,每个询问是每个人的上车和下车 ...
- hdu 3397 Sequence operation (线段树 区间合并 多重标记)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=3397 题意: 给你一串01串,有5种操作 0. 区间全部变为0 1.区间全部变为1 2.区间异或 3.询问 ...
- hdu 1698+poj 3468 (线段树 区间更新)
http://acm.hdu.edu.cn/showproblem.php?pid=1698 这个题意翻译起来有点猥琐啊,还是和谐一点吧 和涂颜色差不多,区间初始都为1,然后操作都是将x到y改为z,注 ...
- SPOJ D-query && HDU 3333 Turing Tree (线段树 && 区间不相同数个数or和 && 离线处理)
题意 : 给出一段n个数的序列,接下来给出m个询问,询问的内容SPOJ是(L, R)这个区间内不同的数的个数,HDU是不同数的和 分析 : 一个经典的问题,思路是将所有问询区间存起来,然后按右端点排序 ...
- HDU 1540 Tunnel Warfare 线段树区间合并
Tunnel Warfare 题意:D代表破坏村庄,R代表修复最后被破坏的那个村庄,Q代表询问包括x在内的最大连续区间是多少 思路:一个节点的最大连续区间由(左儿子的最大的连续区间,右儿子的最大连续区 ...
随机推荐
- iOS中的MD5(base64)加密
MD5(base64)是一种结合MD5摘要和base64编码的密文处理方式,加密后的结果为24位字符串,且后两位为==,例如:1的加密结果为xMpCOKC5I4INzFCab3WEmw==. 下面是加 ...
- 重要的ui组件——Behavior
v7包下的组件类似CoordinatorLayout推出也有一段时间了,大家使用的时候应该会体会到其中很多的便利,今天这篇文章带大家来了解一个比较重要的ui组件——Behavior.从字面意思上就可以 ...
- Could not find com.android.tools.build:gradle:3.0.0-alpha3
最近使用Android Studio 3.0 canary 3 时新建项目遇到标题所示错误,后网上找到解决办法.记录如下: 在项目的build.gradle文件中添加如下内容即可解决. reposit ...
- hibernate,mybatis,beetlsql 全面比較
这是我的一个综合评分.总共分为12个单项.每一个单项最高5分.最低0分. 注意.评价仅仅包括这些软件提供的标准功能,不包括第三方提供的功能,如代码生成等. 开发效率 hibernate 能获取数据库 ...
- ansible自动化工具使用
1.服务端配置 安装即可,无需启动,在安装ansible之前需要配置epel源 [root@m01 ~]# wget -O /etc/yum.repos.d/epel.repo http://mirr ...
- 清理memcached缓存
清理memcached缓存 学习了:https://blog.csdn.net/allus0918/article/details/50481927 使用telnet登录 flush_all 命令:
- 转:Eclipse常见问题,快捷键收集
Eclipse的编辑功能非常强大,掌握了Eclipse快捷键功能,能够大大提高开发效率.Eclipse中有如下一些和编辑相关的快捷键. 1.[ALT+/] Sysout+ System.out.pri ...
- mvn 更改打包的名称
在pom.xml中加入以下代码 <build> <finalName>moon</finalName> <pluginManagement> <p ...
- Sending SMS And Dialing Numbers without User Consent(Context is not needed)
Sending SMS And Dialing Numbers without User Consent Sending SMS does not require context or user in ...
- Python<1>List
list里的元素以逗号隔开,以[]包围,当中元素的类型随意 官方一点的说:list列表是一个随意类型的对象的位置相关的有序集合. 它没有固定的大小(1).通过对偏移量 (2)进行赋值以及其它各种列表的 ...