HDU 4902 (牛叉的线段树)

Nice boat

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 

线段树:我是先把每个点标记,然后向上push的时候能合并的合并,然后暴利就过了,算是水过去的。 在学习下别人的。

1 // by caonima
  2 // hehe
  3 #include <cstdio>
  4 #include <cstring>
  5 #include <vector>
  6 #include <algorithm>
  7 #include <queue>
  8 #include <stack>
  9 #include <string>
 10 #include <iostream>
 11 #include <set>
 12 using namespace std;
 13 const int MAX= 1e5+10;
 14 const int inf = 0x3f3f3f3f;
 15 const int MOD = 1e9+10;
 16 const double eps= 1e-8;
 17 typedef long long LL ;
 18 typedef vector<int> vec;
 19 typedef vector<vec> mat;
 20 
 21 LL gcd(LL a,LL b) {
 22     return b==0?a:gcd(b,a%b);
 23 }
 24 
 25 LL ans[MAX<<2],col[MAX<<2],a[MAX];
 26 
 27 void push_up(int o) {
 28     if(col[o<<1]==col[o<<1|1]) col[o]=col[o<<1];
 29 }
 30 void push_down(int o) {
 31     if( [o]!=-1) {
 32         col[o<<1]=col[o<<1|1]=col[o];
 33         col[o]=-1;
 34     }
 35     return ;
 36 }
 37 void build(int L,int R,int o) {
 38     col[o]=-1;
 39     if(L==R) {
 40         col[o]=a[L];
 41         return ;
 42     }
 43     int mid=(L+R)>>1;
 44     build(L,mid,o<<1);
 45     build(mid+1,R,o<<1|1);
 46     push_up(o);
 47 }
 48 
 49 void modify1(int L,int R,int o,int ls,int rs,LL x) {
 50     if(ls<=L&&rs>=R) {
 51         col[o]=x;
 52         return ;
 53     }
 54     push_down(o);
 55     int mid=(L+R)>>1;
 56     if(ls<=mid) modify1(L,mid,o<<1,ls,rs,x);
 57     if(rs>mid) modify1(mid+1,R,o<<1|1,ls,rs,x);
 58     push_up(o);
 59 }
 60 void modify2(int L,int R,int o,int ls,int rs,LL x) {
 61     if(ls<=L&&rs>=R&&col[o]!=-1) {
 62         if(col[o]>x) {
 63             col[o]=gcd(col[o],x);
 64         }
 65         return ;
 66     }
 67     push_down(o);
 68     int mid=(L+R)>>1;
 69     if(ls<=mid) modify2(L,mid,o<<1,ls,rs,x);
 70     if(rs>mid) modify2(mid+1,R,o<<1|1,ls,rs,x);
 71     push_up(o);
 72 }
 73 LL Query(int L,int R,int o,int k) {
 74     if(L==R) {
 75         return col[o];
 76     }
 77     push_down(o);
 78     int mid=(L+R)>>1;
 79     if(k<=mid) return Query(L,mid,o<<1,k);
 80     else return Query(mid+1,R,o<<1|1,k);
 81 }
 82 int main() {
 83     int cas,n,op,ls,rs,m;
 84     LL x;
 85     scanf("%d",&cas);
 86     while(cas--) {
 87         scanf("%d",&n);
 88         for(int i=1;i<=n;i++) scanf("%I64d",&a[i]);
 89         build(1,n,1);
 90         scanf("%d",&m);
 91         for(int i=0;i<m;i++) {
 92             scanf("%d %d %d %I64d",&op,&ls,&rs,&x);
 93             if(op==1) {
 94                 modify1(1,n,1,ls,rs,x);
 95             }
 96             else modify2(1,n,1,ls,rs,x);
 97         }
 98 
 99         for(int i=1;i<=n;i++) {
100            // if(i==1) printf("%I64d",Query(1,n,1,i));
101            printf("%I64d ",Query(1,n,1,i));
102         }
103         printf("\n");
104         //printf(" ");
105     }
106     return 0;

107 }

HDU 4902 (牛叉的线段树),布布扣,bubuko.com

时间: 2024-10-22 18:47:59

HDU 4902 (牛叉的线段树)的相关文章

HDU 4902 Nice boat(线段树 区间更新)

Nice boat 大意:给你一个区间,每次可以进行两种操作,1:把区间中的数全都变成x  2:把区间中大于x的数变成gcd(a[i], x),最后输出序列. 思路:线段树成段更行,用num数组的叶子存储数据,节点当作lazy来使用. 1 #include <stdio.h> 2 const int maxn = 100005; 3 4 int num[maxn<<2]; 5 6 int gcd(int a, int b){ 7 return b?gcd(b, a%b):a; 8

hdu 4902 Nice boat(线段树区间修改,输出最终序列)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4902 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 peopl

hdu 2852 KiKi&#39;s K-Number (线段树)

hdu 2852 题意: 一个容器,三种操作: (1) 加入一个数 e (2) 删除一个数 e,如果不存在则输出 No Elment! (3) 查询比a大的数中的第k小数,不存在就输出 Not Find! 解法: 关于第三点,可以先查询小于等于a的数的个数cnt,然后直接查询第cnt+k小数就行了 . 二分+树状数组 或者 主席树(有点杀鸡用牛刀的感觉 ...) 也是可以做的  _(:з」∠)_ code:线段树 1 #include <iostream> 2 #include <cst

hdu 1754 I Hate It 线段树 点修改

// hdu 1754 I Hate It 线段树 点修改 // // 不多说,裸的点修改 // // 继续练 #include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <cfloat> #include <climits> #include <cmath> #include <complex> #i

hdu 4893 Wow! Such Sequence!(线段树)

题目链接:hdu 4983 Wow! Such Sequence! 题目大意:就是三种操作 1 k d, 修改k的为值增加d 2 l r, 查询l到r的区间和 3 l r, 间l到r区间上的所以数变成最近的斐波那契数,相等的话取向下取. 解题思路:线段树,对于每个节点新增一个bool表示该节点以下的位置是否都是斐波那契数. #include <cstdio> #include <cstring> #include <cstdlib> #include <algor

Bestcoder round #65 &amp;&amp; hdu 5592 ZYB&#39;s Premutation 线段树

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 175    Accepted Submission(s): 74 Problem Description ZYB has a premutation P,but he only remeber the reverse log of each prefix of the premutat

hdu 1166 敌兵布阵 线段树 点更新

// hdu 1166 敌兵布阵 线段树 点更新 // // 这道题裸的线段树的点更新,直接写就可以了 // // 一直以来想要进线段树的坑,结果一直没有跳进去,今天算是跳进去吧, // 虽然十分简单,十分的水,继续加油 #include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <cfloat> #include <climits

HDU 1698 Just a Hook (线段树,区间更新)

Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 17214    Accepted Submission(s): 8600 Problem Description In the game of DotA, Pudge’s meat hook is actually the most horrible thing f

HDU 4107 Gangster Segment Tree线段树

这道题也有点新意,就是需要记录最小值段和最大值段,然后成段更新这个段,而不用没点去更新,达到提高速度的目的. 本题过的人很少,因为大部分都超时了,我严格按照线段树的方法去写,一开始居然也超时. 然后修补了两个地方就过了,具体修改的地方请参看程序. 知道最大值段和最小值段,然后修补一下就能过了.不是特别难的题目. #include <stdio.h> #include <string> #include <algorithm> using namespace std; c