Nice boat
Time Limit: 30000/15000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 2910 Accepted Submission(s): 1295
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
Author
WJMZBMR
Source
链接:http://acm.hdu.edu.cn/showproblem.php?pid=4902
题意:
操作1:[l,r]全部修改为v
操作2:[l,r]大于v的数a[i]取gcd(a[i],v)
最后打印区间1~n的每个值。
题解:操作1的话,直接加懒标记往下推;操作2分两种情况:
情况一:当前区间[l,r]被操作1浏览过,说明[l,r]每个值都相同,所以直接修改当前节点a[i].maxn就好。
情况二:当前区间[l,r]没有被操作1浏览过,每个值之间可能不同,满足gcd(maxn,v)修改要求,就要一直更新到叶子节点。
(但是总感觉怪怪的,如果q次询问只有操作2,每次都要更新到叶子节点,会不会超时呢?)
#include <iostream>
using namespace std;
struct node
{
int maxn;
int flag;
}tree[100005*4];
int gcd(int a,int b)
{
return b==0?a:gcd(b,a%b);
}
void pushup(int i)
{
tree[i].maxn=max(tree[i+i].maxn,tree[i+i+1].maxn);
}
void pushdown(int i)
{
if(tree[i].flag)
{
tree[i+i].maxn=tree[i+i+1].maxn=tree[i].maxn;
tree[i+i].flag=tree[i+i+1].flag=1;
tree[i].flag=0;
}
}
void build(int l,int r,int i)
{
tree[i].flag=0;
if(l==r)
{
scanf("%d",&tree[i].maxn);
tree[i].flag=1;
return;
}
int mid=(l+r)/2;
build(l,mid,i+i);
build(mid+1,r,i+i+1);
pushup(i);
}
void updata1(int i,int l,int r,int ll,int rr,int v)
{
if(ll<=l&&r<=rr)
{
tree[i].maxn=v;
tree[i].flag=1;
return;
}
pushdown(i);
int mid=(l+r)/2;
if(ll<=mid)
updata1(i+i,l,mid,ll,rr,v);
if(mid<rr)
updata1(i+i+1,mid+1,r,ll,rr,v);
pushup(i);
}
void updata2(int i,int l,int r,int ll,int rr,int v)
{
if(tree[i].maxn<=v)
return;
if(ll<=l&&r<=rr)
{
if(tree[i].flag)
{
tree[i].maxn=gcd(tree[i].maxn,v);
return;
}
}
pushdown(i);
int mid=(l+r)/2;
if(ll<=mid)
updata2(i+i,l,mid,ll,rr,v);
if(mid<rr)
updata2(i+i+1,mid+1,r,ll,rr,v);
pushup(i);
}
void print(int l,int r,int i)
{
if(l==r)
{
printf("%d ",tree[i].maxn);
return;
}
pushdown(i);
int mid=(l+r)/2;
print(l,mid,i+i);
print(mid+1,r,i+i+1);
}
int main()
{
int n,t,q,op,l,r,v;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
build(1,n,1);
scanf("%d",&q);
for(int i=1;i<=q;i++)
{
scanf("%d%d%d%d",&op,&l,&r,&v);
if(op==1)
updata1(1,1,n,l,r,v);
else
updata2(1,1,n,l,r,v);
}
print(1,n,1);
cout<<endl;
}
return 0;
}