Nice boat
Time Limit: 30000/15000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 146 Accepted Submission(s): 75
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.
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++)
Please output a single more space after end of the sequence
题意:给定一个数字序列,对其进行两种操作:1、将序列中连续的一段变成一样的数x;2、给序列中连续的一段和一个数x,对于这个连续一段中的每个数,如果小于x则取x与这个数的最大公约数,否则不操作。
题解:线段树+懒人标记
代码:
#include <cstdio>
#include <cstring> const int LEN = ; struct line
{
int left;
int right;
int value;
}line[LEN*];
int point[LEN*]; //记录线段树底层节点的位置 inline int gcd(int a,int b) //求最大公约数
{
return a % b ? gcd(b, a % b) : b;
} void buildt(int left, int right, int step) //建树同时初始化懒惰标记
{
line[step].value = -;
line[step].left = left;
line[step].right = right;
if (left == right){
point[left] = step;
return;
}
int mid = (left + right) / ;
buildt(left, mid, step*);
buildt(mid+, right, step*+);
} void query_1(int left, int right, int x, int step) //操作一
{
if (left == line[step].left && right == line[step].right){
line[step].value = x;
return;
}
if (line[step].value != -){
line[step*].value = line[step*+].value = line[step].value;
line[step].value = -;
}
int mid = (line[step].left + line[step].right) / ;
if (right <= mid)
query_1(left, right, x, step*);
else if (left > mid)
query_1(left, right, x, step*+);
else{
query_1(left, mid, x, step*);
query_1(mid+, right, x, step*+);
}
} void query_2(int left, int right, int x, int step) //操作函数二
{
if (line[step].left == left && line[step].right == right){
if (line[step].value != -){ //如果找到对应区间,且有标记,则进行条件判断,否则继续
if (line[step].value > x)
line[step].value = gcd(line[step].value, x);
return;
}
}
if (line[step].value != -){ //如果改点被标记,则下移一层
line[step*].value = line[step*+].value = line[step].value;
line[step].value = -;
}
int mid = (line[step].left + line[step].right) / ;
if (right <= mid)
query_2(left, right, x, step*);
else if (left > mid)
query_2(left, right, x, step*+);
else{
query_2(left, mid, x, step*);
query_2(mid+, right, x, step*+);
}
} void findans(int left, int right, int step) //dfs输出结果
{
if (line[step].left == left && line[step].right == right){
if (line[step].value != -){
for(int i = ; i < line[step].right - line[step].left + ; i++)
printf("%d ", line[step].value);
return;
}
}
if (line[step].value != -){
line[step*].value = line[step*+].value = line[step].value;
line[step].value = -;
}
int mid = (line[step].left + line[step].right) / ;
if (right <= mid)
findans(left, right, step*);
else if (left > mid)
findans(left, right, step*+);
else{
findans(left, mid, step*);
findans(mid+, right, step*+);
}
} int main()
{
int T;
//freopen("in.txt", "r", stdin);
scanf("%d", &T);
while(T--){
int n;
memset(point, , sizeof(point));
scanf("%d", &n);
buildt(, n, );
for(int i = ; i <= n; i++){
int t;
scanf("%d", &t);
line[point[i]].value = t;
}
int m;
scanf("%d", &m);
for(int i = ; i < m; i++){
int t, l, r, x;
scanf("%d %d %d %d", &t, &l, &r, &x);
if (t == ){
query_1(l, r, x, );
}
else if (t == ){
query_2(l, r, x, );
}
}
findans(, n, );
printf("\n");
}
return ;
}