HDU 4902 Nice boat
Nice boat
Time Limit: 15000ms
Memory Limit: 131072KB
This problem will be judged on HDU. Original ID: 490264-bit integer IO format: %I64d Java class name: Main
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
Source
解题:直接拿线段树爆就是了
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn = 100010; 4 struct node { 5 int lt,rt,lazy,maxv; 6 } tree[maxn<<2]; 7 inline void pushup(int v) { 8 tree[v].maxv = max(tree[v<<1].maxv,tree[v<<1|1].maxv); 9 if(tree[v<<1].lazy == tree[v<<1|1].lazy) tree[v].lazy = tree[v<<1].lazy; 10 else tree[v].lazy = -1; 11 } 12 void pushdown(int v) { 13 if(~tree[v].lazy) { 14 tree[v<<1].lazy = tree[v].lazy; 15 tree[v<<1|1].lazy = tree[v].lazy; 16 tree[v<<1].maxv = tree[v].lazy; 17 tree[v<<1|1].maxv = tree[v].lazy; 18 tree[v].lazy = -1; 19 } 20 } 21 void build(int lt,int rt,int v) { 22 tree[v].lt = lt; 23 tree[v].rt = rt; 24 if(lt == rt) { 25 scanf("%d",&tree[v].maxv); 26 tree[v].lazy = tree[v].maxv; 27 return; 28 } 29 int mid = (lt + rt)>>1; 30 build(lt,mid,v<<1); 31 build(mid + 1,rt,v<<1|1); 32 pushup(v); 33 } 34 void update(int lt,int rt,int val,int v) { 35 if(lt <= tree[v].lt && rt >= tree[v].rt) { 36 tree[v].lazy = tree[v].maxv = val; 37 return; 38 } 39 pushdown(v); 40 if(lt <= tree[v<<1].rt) update(lt,rt,val,v<<1); 41 if(rt >= tree[v<<1|1].lt) update(lt,rt,val,v<<1|1); 42 pushup(v); 43 } 44 void update2(int lt,int rt,int x,int v) { 45 if(tree[v].maxv <= x) return; 46 if(lt <= tree[v].lt && rt >= tree[v].rt) { 47 if(tree[v].lazy != -1) { 48 tree[v].lazy = tree[v].maxv = __gcd(tree[v].lazy,x); 49 return; 50 } 51 } 52 pushdown(v); 53 if(lt <= tree[v<<1].rt) update2(lt,rt,x,v<<1); 54 if(rt >= tree[v<<1|1].lt) update2(lt,rt,x,v<<1|1); 55 pushup(v); 56 } 57 void query(int v) { 58 if(tree[v].lt == tree[v].rt) { 59 printf("%d ",tree[v].maxv); 60 return; 61 } 62 pushdown(v); 63 query(v<<1); 64 query(v<<1|1); 65 } 66 int main() { 67 int kase,n,m,op,x,y,z; 68 scanf("%d",&kase); 69 while(kase--) { 70 scanf("%d",&n); 71 build(1,n,1); 72 scanf("%d",&m); 73 while(m--) { 74 scanf("%d%d%d%d",&op,&x,&y,&z); 75 if(op == 1) update(x,y,z,1); 76 else update2(x,y,z,1); 77 } 78 query(1); 79 puts(""); 80 } 81 return 0; 82 }
夜空中最亮的星,照亮我前行