摘自:https://www.cnblogs.com/fzl194/p/9034201.html
题意:
给定一个长度为n的序列m次操作,操作的种类一共有三种:
查询
L :查询一个区间的所有的数的最小公倍数modp
G :查询一个区间的所有的数的最大公约数modp
修改
C :将给定位置的值修改成x
思路:
上面的博客讲的很清楚,可以去看。
其实是一个单点更新的线段树,有波折的是这个gcd,lcm。因为1-100的lcm会存不下,所以需要用到质因式分解,用位压缩来实现。
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
#define lson root<<1, l, mid
#define rson root<<1 | 1, mid+1, r
typedef long long LL;
const int INF = 0x3f3f3f3f;
const int maxn = 1e6+5;
using namespace std;
struct Node{
int L, G;
}Tree[maxn];
int prime[] = {2, 3, 5, 7, 11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97};
int pos[] = {28,25,23,21,20,19,18,17,16,15,14,13,12,11,10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0}; //pos数组就存的是这些素数的指数具体存在哪一位
int tmp;
inline int Min(int x, int y){
return min(x&0x70000000, y&0x70000000) | min(x&0x0e000000, y&0x0e000000) | min(x&0x01800000, y&0x01800000)
| min(x&0x00600000, y&0x00600000) | ((x&0x001fffff)&(y&0x001fffff));
}
inline int Max(int x, int y){
return max(x&0x70000000, y&0x70000000) | max(x&0x0e000000, y&0x0e000000) | max(x&0x01800000, y&0x01800000)
| max(x&0x00600000, y&0x00600000) | ((x&0x001fffff)|(y&0x001fffff));
}
// 将 x 质因数分解,并且将指数存在y的每一个bit上
inline int Turn(int x){
int y = 0;
for(int i = 0; i < 25&&x > 1; ++i){ // 枚举25个质因子
int cnt = 0;
while(x % prime[i] == 0){
x /= prime[i];
++cnt;
}
y |= cnt << pos[i];
}
return y;
}
//将所存的指数转化成原来的数字,并且模上c
int two[] = {1, 2, 4, 8, 16, 32, 64};
int three[] = {1, 3, 9, 27, 81};
int five[] = {1, 5, 25};
int seven[] = {1, 7, 49};
inline int Back(int x, int c){
int y = 1;
int k = x >> pos[0]; // 2的指数
x ^= k << pos[0]; // 从x中去掉2的指数
y = y * two[k] % c;
//while(k--) y = y * prime[0] % c; // 累乘到 y
k = x >> pos[1]; x ^= k << pos[1]; y = y * three[k] % c; //while(k--) y = y * prime[1] % c;
k = x >> pos[2]; x ^= k << pos[2]; y = y * five[k] % c; //while(k--) y = y * prime[2] % c;
k = x >> pos[3]; x ^= k << pos[3]; y = y * seven[k] % c; //while(k--) y = y * prime[3] % c;
for(int i = 4; i < 25; ++i)
if(x & (1<<pos[i])) y = y * prime[i] % c;
return y%c;
}
void push_up(int root){
Tree[root].G = Min(Tree[root<<1].G, Tree[root<<1 | 1].G);
Tree[root].L = Max(Tree[root<<1].L, Tree[root<<1 | 1].L);
}
void Stree_build(int root, int l, int r){
if(l == r){
scanf("%d", &tmp); Tree[root].L = Tree[root].G = Turn(tmp);
return;
}
int mid = (l+r) >> 1;
Stree_build(lson);
Stree_build(rson);
push_up(root);
}
void update(int k, int l, int r, int root, int val){
if(l == r){
Tree[root].L = Tree[root].G = Turn(val);
return;
}
int mid = (l+r) >> 1;
if(k <= mid)
update(k, l, mid, root<<1, val);
if(k > mid)
update(k, mid+1, r, root<<1 | 1, val);
push_up(root);
}
// kind: 0--gcd, 1--lcm
void Query(int la, int rb, int l, int r, int root,int& ans, int kind){
if(la > r||l > rb) return;
if(la <= l&&rb >= r){
if(kind == 0) ans = (ans == -1? Tree[root].G : Min(ans, Tree[root].G));
else ans = (ans == -1? Tree[root].L : Max(ans, Tree[root].L));
return;
}
int mid = (l+r) >> 1;
if(la <= mid)
Query(la, rb, l, mid, root<<1, ans, kind);
if(rb > mid)
Query(la, rb, mid+1, r, root<<1 | 1, ans, kind);
}
int main()
{
freopen("in.txt","r",stdin);
char str[5];
int n,m;
int a,b,c, ans;
while(scanf("%d%d",&n,&m) == 2){
Stree_build(1, 1, n);
while(m--){
scanf("%s", str);
scanf("%d%d",&a,&b);
if(str[0] == 'C') update(a, 1, n, 1, b);
else{
scanf("%d", &c);
ans = -1;
if(str[0] == 'G') Query(a, b, 1, n, 1, ans, 0);
else Query(a, b, 1, n, 1, ans, 1);
ans = Back(ans, c);
printf("%d\n", ans);
}
}
}
fclose(stdin);
return 0;
}