首页 > 代码库 > HDU 4902 (牛叉的线段树)
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.
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++)
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
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;
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 }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。