Nice boat
Time Limit: 30000/15000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
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
题意:给出n个数,然后对这n个数进行两种操作:
如果是 1 l r x,则把 [l, r] 区间里面的每一个数都变为x;
如果是 2 l r x,则 比较 [l, r]区间里的数a_i和x的大小,如果a_i > x,把a_i变为a_i和x的最大公约数。
最后输出这n个数最终的值。
看大神们都是用线段树过的这个题,但是我这种弱菜不会写线段树,只好用暴力来求了。
分析:如果按照题目直接模拟求解的话,肯定会超时。所以我们逆向求解,也就是说,对每一个数来讲,只有后面的操作影响最终的结果,而最后一个1操作之前的操作全都不影响最终结果。这样,我们求每一个数的最终结果时,就可以用栈存储影响这个数的操作,直到遇到1操作,或者没有遇到1操作,则初值就为输入的a_i。然后再按照题意模拟求解即可。
#include<cstdio>
#include<stack>
using namespace std;
typedef __int64 LL;
const int N = 1e5 + 10;
struct opertion
{
int t, l, r;
LL x;
}o[N];
LL a[N];
LL gcd(LL x, LL y)
{
while(y) {
LL r = x % y;
x = y;
y = r;
}
return x;
}
int main()
{
int T, n, i, j, Q;
scanf("%d",&T);
while(T--) {
scanf("%d",&n);
for(i = 1; i <= n; i++)
scanf("%I64d",&a[i]);
scanf("%d",&Q);
for(i = 0; i < Q; i++)
scanf("%d%d%d%I64d",&o[i].t, &o[i].l, &o[i].r, &o[i].x);
for(i = 1; i <= n; i++) {
stack<LL> s;
int flag = 0;
for(j = Q - 1; j >= 0; j--) {
if(i >= o[j].l && i <= o[j].r) {
s.push(o[j].x);
if(o[j].t == 1) {
flag = 1;
break;
}
}
}
if(!flag) //没有遇到1操作
s.push(a[i]);
while(s.size() > 1) {
LL ans = s.top(); s.pop();
LL tmp = s.top(); s.pop();
if(ans > tmp)
ans = gcd(ans, tmp);
s.push(ans);
}
printf("%I64d ", s.top());
}
printf("\n");
}
return 0;
}