题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4902
题目的意思很是简单,一系列的数。
两种操作:1.[l,r]内变为x。2.[l,r]内大于x的变为gcd(a[i],x)。时限15000mm,很有暴力的诱惑力。
不过纯暴力肯定会TLE的。但是,也并不是说,不能不暴力。暴力的姿势够美,过还是没有问题的。
不过,该题作者是意思可能是线段树。但是,还是有待学习啊。
分析:
如果某一个元素最后进行了1操作,那么,它与前面所涉及到它的操作就无关了,值与最后这一次1操作有关。。
如果某元素最后进行了n次2操作,那就和最后一次进行的1操作有关了。把n次进行的2操作压入栈,然后再统一进行2操作就好了。很是巧妙。
Code:
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <stack>
using namespace std;
#define LL long long
const int N = 1e5 + 4;
struct operation{
int t, l ,r;
LL x;
}op[N];
LL gcd(LL a, LL b){
if(b == 0) return a;
else return gcd(b, a % b);
}
int main(){
int T;
scanf("%d", &T);
while(T --){
LL arr[N];
int n, q;
scanf("%d", &n);
for(int i = 1; i <= n; i ++){
scanf("%I64d", &arr[i]);
}
scanf("%d", &q);
for(int i = 1; i <= q; i ++){
scanf("%d %d %d %I64d", &op[i].t, &op[i].l, &op[i].r, &op[i].x);
}
for(int i = 1; i <= n; i ++){
stack<LL> st;
for(int j = q; j >= 1; j --){
if(i >= op[j].l && op[j].r >= i){
if(op[j].t == 1){
arr[i] = op[j].x;
break;
}
else {
st.push(op[j].x);
}
}
}
while(!st.empty()){
if(st.top() < arr[i])
arr[i] = gcd(arr[i], st.top());
st.pop();
}
}
for(int i = 1; i <= n; i ++){
printf("%I64d ", arr[i]);
}
printf("\n");
}
return 0;
}
思路很是巧妙。这给人很大的启发。
遇到不会的问题的时候,要分析其特性。也许从这些特性进行着手的时候,你就会发现一种巧妙的私立。