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

//Memory   Time
// 5376K 651MS
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<iomanip>
#include<string>
#include<climits>
#include<cmath>
#define MAX 100100
#define LL long long
using namespace std;
int n,m;
int ans;
int num[MAX];
struct Tree
{
int l,r;
bool flag;
int val,temp;
};
Tree tree[MAX<<2]; int gcd(int x,int y)
{
return y?gcd(y,x%y):x;
} void pushup(int x)
{
int tmp=x<<1;
tree[x].val=max(tree[tmp].val,tree[tmp+1].val);
tree[x].flag=(tree[tmp].val==tree[tmp+1].val&&tree[tmp].flag&&tree[tmp+1].flag);
} void pushdown(int x)
{
if(tree[x].temp==-1)return;
int tmp=x<<1;
int mid=(tree[x].l+tree[x].r)>>1;
tree[tmp].val=tree[tmp+1].val=tree[tmp].temp=tree[tmp+1].temp=tree[x].temp;
tree[x].temp=-1;
} void build(int l,int r,int x)
{
tree[x].flag=0;
tree[x].temp=-1;
tree[x].l=l,tree[x].r=r;
if(l==r)
{
scanf("%d",&tree[x].val);
tree[x].flag=1;
return;
}
int tmp=x<<1;
int mid=(l+r)>>1;
build(l,mid,tmp);
build(mid+1,r,tmp+1);
pushup(x);
} void update(int l,int r,int num,int x)
{
if(r<tree[x].l||l>tree[x].r)return;
if(l<=tree[x].l&&r>=tree[x].r)
{
tree[x].flag=1;
tree[x].val=num;
tree[x].temp=num;
return;
}
pushdown(x);
int tmp=x<<1;
int mid=(tree[x].l+tree[x].r)>>1;
if(r<=mid)
update(l,r,num,tmp);
else if(l>mid)
update(l,r,num,tmp+1);
else
{
update(l,mid,num,tmp);
update(mid+1,r,num,tmp+1);
}
pushup(x);
} void change(int l,int r,int num,int x)
{
if(r<tree[x].l||l>tree[x].r)return;
if(tree[x].flag&&tree[x].val<=num)return;
if(l<=tree[x].l&&r>=tree[x].r&&tree[x].flag)
{
tree[x].val=gcd(tree[x].val,num);
tree[x].temp=tree[x].val;
return;
}
pushdown(x);
int tmp=x<<1;
int mid=(tree[x].l+tree[x].r)>>1;
if(r<=mid)
change(l,r,num,tmp);
else if(l>mid)
change(l,r,num,tmp+1);
else
{
change(l,mid,num,tmp);
change(mid+1,r,num,tmp+1);
}
pushup(x);
} void query(int l,int r,int k,int x)
{
if(k<tree[x].l||k>tree[x].r)return;
if(tree[x].flag)
{
ans=tree[x].val;
return;
}
pushdown(x);
int tmp=x<<1;
int mid=(tree[x].l+tree[x].r)>>1;
if(k<=mid)
query(l,mid,k,tmp);
else
query(mid+1,r,k,tmp+1);
} int main()
{
int T;
int t,l,r,num;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
build(1,n,1);
scanf("%d",&m);
while(m--)
{
scanf("%d %d %d %d",&t,&l,&r,&num);
if(t==1)
update(l,r,num,1);
else
change(l,r,num,1);
}
for(int i=1;i<=n;i++)
{
ans=0;
query(1,n,i,1);
printf("%d ",ans);
}
puts("");
}
return 0;
}

  


【解法二】

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

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

//Memory   Time
// 2232K 250MS
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<iomanip>
#include<string>
#include<climits>
#include<cmath>
#define MAX 100100
#define LL long long
using namespace std;
int T,n,q;
int num[MAX];
int t[MAX],l[MAX],r[MAX],x[MAX];
int prime[MAX];
int gcd(int x,int y)
{
return y?gcd(y,x%y):x;
}
int main()
{
// freopen("cin.txt","r",stdin);
// freopen("cout.txt","w",stdout);
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=1;i<=n;++i)
scanf("%d",&num[i]);
scanf("%d",&q);
for(int i=1;i<=q;++i)
scanf("%d %d %d %d",&t[i],&l[i],&r[i],&x[i]);
for(int i=1;i<=n;++i)
{
int tp=num[i];
int index=-1;
for(int j=q;j>=1;--j)
{
if(i>=l[j]&&i<=r[j])
{
if(t[j]==1)
{
tp=x[j];
break;
}
else prime[++index]=x[j];
}
}
for(int j=index;j>=0;--j)
{
if(tp>prime[j])
tp=gcd(tp,prime[j]);
}
printf("%d ",tp);
}
puts("");
}
return 0;
}

  

线段树 + 区间更新 ----- 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. nginx增加ssl支持 - 编译时参数详情列表

    ./configure \ --with-http_ssl_module \ make && make install     nginx编译参数说明如下:   --prefix=&l ...

  2. [hihoCoder] 第四十九周: 欧拉路·一

    题目1 : 欧拉路·一 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho最近在玩一个解密类的游戏,他们需要控制角色在一片原始丛林里面探险,收集道具,并找到最 ...

  3. Window 7 + Ubuntu 双系统安装

    硬件: ThinkPad X260 i5-6200U/8G/480G 当前系统: Window 7 旗舰版 64位 下载 Ubuntu 官网 下载桌面版,当前 Ubuntu 版本为:16.04 镜像安 ...

  4. VS2010如何重置开发环境

    在利用VS进行软件开发的过程中,我们时不时要因为各种原因,对VS的开发环境进行变动,对于很多初次接触VS这样一个十分好用方便的编程工具的人来说,更改编程环境成了一个难题,今天我们就来讲解一下,如何更改 ...

  5. Debugging Java Native Memory Leaks

    GZIP造成JAVA Native Memory泄漏案例 https://www.elastic.co/blog/tracking-down-native-memory-leaks-in-elasti ...

  6. 数据库的ACID

    一.事务 定义:所谓事务,它是一个操作序列,这些操作要么都执行,要么都不执行,它是一个不可分割的工作单位. 准备工作:为了说明事务的ACID原理,我们使用银行账户及资金管理的案例进行分析. [sql] ...

  7. 【C/C++】嵌入式程序员应该知道的0X10个C语言问题

    一.预处理器(Preprocessor) 1 . 用预处理指令#define 声明一个常数,用以表明 1 年中有多少秒(忽略闰年问题) #define SECONDS_PER_YEAR (60 * 6 ...

  8. 【ARM】2440裸机系列-图片显示

    功能 LCD显示字汉字,字符和图片 说明 汉字,字符和图片需要用相应的取模软件得到相应的c文件,然后包含到工程中 主要代码   1)绘制背景 void Brush_ U32 c) { int x,y ...

  9. 项目bug

    word-break: normal|break-all|keep-all; 值 描述 normal 使用浏览器默认的换行规则. break-all 允许在单词内换行. keep-all 只能在半角空 ...

  10. 三角形(css3)

    .userCard .sanjiao {//三角形的制作: width: 0; height: 0; border-left: 10px solid transparent; border-right ...