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
 
题解:线段树的每个结点存的是这个区间的最大值。Update2很绕,如果这个区间没更过,那么一定要更新到被标记过的子结点(叶子结点在建树的过程中已被标记)!
  1 #pragma warning(disable:4996)
  2 #include<queue>
  3 #include<cmath>
  4 #include<string>
  5 #include<cstdio>
  6 #include<bitset>
  7 #include<cstring>
  8 #include<iostream>
  9 #include<algorithm>
 10 using namespace std;
 11 
 12 #define lson root<<1
 13 #define rson root<<1|1
 14 
 15 const int maxn = 1e5 + 5;
 16 
 17 struct node {
 18     int mam;
 19     int l, r;
 20     bool flag;
 21 }Tree[4*maxn];
 22 
 23 int gcd(int a, int b) {
 24     if (b == 0) return a;
 25     return gcd(b, a % b);
 26 }
 27 
 28 void Pushup(int root) {
 29     Tree[root].mam = max(Tree[lson].mam, Tree[rson].mam);
 30 }
 31 
 32 void Pushdown(int root) {
 33     Tree[lson].mam = Tree[root].mam;
 34     Tree[rson].mam = Tree[root].mam;
 35     Tree[lson].flag = Tree[root].flag;
 36     Tree[rson].flag = Tree[root].flag;
 37     Tree[root].flag = 0;
 38 }
 39 
 40 void Build(int l, int r, int root) {
 41     Tree[root].l = l;
 42     Tree[root].r = r;
 43     Tree[root].flag = 0;
 44     if (l == r) { scanf("%d", &Tree[root].mam); Tree[root].flag = 1; return; }
 45     int mid = (l + r) >> 1;
 46     Build(l, mid, lson);
 47     Build(mid+1,r,rson);
 48     Pushup(root);
 49 }
 50 
 51 void Update1(int l, int r, int root, int x) {
 52     if (r <  Tree[root].l || l >  Tree[root].r) return;
 53     if (l <= Tree[root].l && r >= Tree[root].r) {
 54         Tree[root].flag = 1;
 55         Tree[root].mam = x;
 56         return;
 57     }
 58     if (Tree[root].flag) Pushdown(root);
 59     Update1(l, r, lson, x);
 60     Update1(l, r, rson, x);
 61     Pushup(root);
 62 }
 63 
 64 void Update2(int l, int r, int root, int x) {
 65     if (Tree[root].mam < x) return;
 66     if (r <  Tree[root].l || l >  Tree[root].r) return;
 67     if (l <= Tree[root].l && r >= Tree[root].r && Tree[root].mam > x && Tree[root].flag) {   //保证更新的是被标记的结点!!!!
 68         Tree[root].mam = gcd(Tree[root].mam, x);
 69         return;
 70     }
 71     if (Tree[root].flag) Pushdown(root);
 72     Update2(l, r, lson, x);
 73     Update2(l, r, rson, x);
 74     Pushup(root);
 75 }
 76 
 77 void Print(int l, int r, int root) {
 78     if (l == r) {
 79         printf("%d ", Tree[root].mam);
 80         return;
 81     }
 82     if (Tree[root].flag) Pushdown(root);
 83     int mid = (l + r) >> 1;
 84     Print(l, mid, lson);
 85     Print(mid+1,r,rson);
 86 }
 87 
 88 int main()
 89 {    
 90     int T; scanf("%d", &T);
 91     while (T--) {
 92         int n; scanf("%d", &n);
 93         Build(1, n, 1);
 94         int q; scanf("%d", &q);
 95         while (q--) {
 96             int op; scanf("%d", &op);
 97             if (op == 1) {
 98                 int x, y, z;
 99                 scanf("%d%d%d", &x, &y, &z);
100                 Update1(x, y, 1, z);
101             }
102             else {
103                 int x, y, z;
104                 scanf("%d%d%d", &x, &y, &z);
105                 Update2(x, y, 1, z);
106             }
107         }
108         Print(1, n, 1);
109         printf("\n");
110     }
111     return 0;
112 }

 

posted @ 2018-03-19 13:37  天之道,利而不害  阅读(671)  评论(0编辑  收藏  举报