Fork me on GitHub

「BZOJ5028」小Z的加油店 差分 线段树单点修改区间求gcd

Links there:BZOJ5028

题意

参见传送门(懒得写了)

思路

考试时的我:”md怎么搞啊 应该是求个区间gcd吧 那这样的话加标记对求gcd有影响怎么算啊 不会不是线段树这个东西吧 会不会是分块啊 难道还有其他姿势吗…”

诶 那么差分一下不久完事了!多开个线段树或者树状数组记录原序列就好了啊!

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//my vegetable has exploded. :(
#include<bits/stdc++.h>
#define max(x,y) (x>y?x:y)
#define min(x,y) (x<y?x:y)
#define MM(x,y) memset(x,y,sizeof(x))
#define MCPY(a,b) memcpy(a,b,sizeof(b))
#define pb push_back
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,b,a) for(int i=b;i>=a;i--)
#define fi first
#define se second
using namespace std;
#define int long long
#define lowbit(x) (x&-x)

inline int quickpow(int m,int n,int p){int b=1;while(n){if(n&1)b=b*m%p;n=n>>1;m=m*m%p;}return b;}
inline int getinv(int x,int p){return quickpow(x,p-2,p);}
inline int read(void){
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)){f=ch=='-'?-1:1;ch=getchar();}
while(isdigit(ch)){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
return x * f;
}
#define lc o<<1
#define rc o<<1|1

const int N = 1e5+100;
int n,m,B[N],ans[4*N],C[4*N];
inline void add(int p,int v){for(int i = p;i < N;i += lowbit(i)) C[i] += v;}
inline int sum(int p){int ret = 0;for(int i = p;i;i -= lowbit(i)) ret += C[i];return ret;}
inline int _gcd(int a,int b){a=abs(a);b=abs(b);return __gcd(a,b);}
inline void build(int o,int l,int r){
if(l == r){ans[o] = B[l] - B[l - 1];return;}
int mid = (l + r) >> 1;
build(lc,l,mid);build(rc,mid + 1,r);
ans[o] = _gcd(ans[lc],ans[rc]);
}
inline void modify(int o,int l,int r,int p,int v){
if(l == r){ans[o] += v;return;}
int mid = (l + r) >> 1;
if(p <= mid) modify(lc,l,mid,p,v);
else modify(rc,mid + 1,r,p,v);
ans[o] = _gcd(ans[lc],ans[rc]);
}
inline int query(int o,int l,int r,int L,int R){
if(l == L && r == R) return ans[o];
int mid = (l + r) >> 1;
if(R <= mid) return query(lc,l,mid,L,R);
if(L > mid) return query(rc,mid + 1,r,L,R);
return _gcd(query(lc,l,mid,L,mid),query(rc,mid + 1,r,mid + 1,R));
}
signed main(signed argc, char *argv[]){
// freopen("bucket.in","r",stdin);
// freopen("bucket.out","w",stdout);
n = read(),m = read();
MM(B,0);MM(C,0);
for(int i = 1;i <= n;i++){
B[i] = read();
add(i,B[i]);
add(i + 1,-B[i]);
}
build(1,1,n);
while(m--){
int opt = read(),l = read(),r = read(),v;
if(opt == 1){
v = read();
modify(1,1,n,l,v);
if(r < n)
modify(1,1,n,r + 1,-v);
add(l,v);
add(r + 1,-v);
}
else{
if(l == r){
printf("%lld\n",sum(l));
continue;
}
printf("%lld\n",_gcd(query(1,1,n,l + 1,r),sum(r)));
}
}
// fclose(stdin); fclose(stdout);
return 0;
}