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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
| #include<bits/stdc++.h> #define int long long #define ull unsigned long long #define maxn 300005 #define put() putchar('\n') #define Tp template<typename T> #define Ts template<typename T,typename... Ar> using namespace std; Tp void read(T &x){ int f=1;x=0;char c=getchar(); while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();} while (c>='0'&&c<='9') {x=x*10+c-'0';c=getchar();} x*=f; } namespace Debug{ Tp void _debug(char* f,T t){cerr<<f<<'='<<t<<endl;} Ts void _debug(char* f,T x,Ar... y){while(*f!=',') cerr<<*f++;cerr<<'='<<x<<",";_debug(f+1,y...);} #define gdb(...) _debug((char*)#__VA_ARGS__,__VA_ARGS__) }using namespace Debug; #define fi first #define se second #define mk make_pair const int mod=1e9+7; int power(int x,int y=mod-2) { int sum=1; while (y) { if (y&1) sum=sum*x%mod; x=x*x%mod;y>>=1; } return sum; } int n,q; int gcd(int x,int y) {return !y?x:gcd(y,x%y);} struct node { int ls,rs,sum,g; }f[maxn*60]; int root[maxn]; int a[maxn],cnt,vis[maxn],Ans[maxn]; vector<pair<int,int> > O[maxn]; void pushup(int rt) { f[rt].sum=f[f[rt].ls].sum+f[f[rt].rs].sum; f[rt].g=gcd(f[f[rt].ls].g,f[f[rt].rs].g); } void Update(int l,int r,int &rt,int head,int k) { if (!rt) rt=++cnt; if (l==r) return f[rt].sum=f[rt].g=k,void(); int mid=l+r>>1; if (head<=mid) Update(l,mid,f[rt].ls,head,k); else Update(mid+1,r,f[rt].rs,head,k); pushup(rt); } int qsum(int l,int r,int &rt,int head,int tail) { if (!rt) return 0; if (head<=l&&r<=tail) return f[rt].sum; int mid=l+r>>1,sum=0; if (head<=mid) sum+=qsum(l,mid,f[rt].ls,head,tail); if (tail>mid) sum+=qsum(mid+1,r,f[rt].rs,head,tail); return sum; } int qgcd(int l,int r,int &rt,int head,int tail) { if (!rt) return 0; if (head<=l&&r<=tail) return f[rt].g; int mid=l+r>>1,tmp1=0,tmp2=0; if (head<=mid) tmp1=qgcd(l,mid,f[rt].ls,head,tail); if (tail>mid) tmp2=qgcd(mid+1,r,f[rt].rs,head,tail); return gcd(tmp1,tmp2); } int merge(int l,int r,int x,int y) { if (!x||!y) return x+y; if (l==r) { f[x].sum+=f[y].sum; f[x].g=f[x].sum; return x; } int mid=l+r>>1; f[x].ls=merge(l,mid,f[x].ls,f[y].ls); f[x].rs=merge(mid+1,r,f[x].rs,f[y].rs); pushup(x); return x; } signed main(void){ int i,op,l,r,x; read(n),read(q); for (i=1;i<=n;i++) read(a[i]),Update(0,q,root[i],0,a[i]-a[i-1]); for (i=1;i<=q;i++) { read(op); if (op==1) { read(l);read(r);read(x); Update(0,q,root[l],i,x); if (r<n) Update(0,q,root[r+1],i,-x); } else { read(l);read(x); O[l].push_back(mk(x,i)); vis[i]=1; } } for (i=1;i<=n;i++) { for (auto tmp:O[i]) { int tmp1=qsum(0,q,root[i],0,tmp.fi-1); int tmp2=qgcd(0,q,root[i],tmp.fi,tmp.se); Ans[tmp.se]=abs(gcd(tmp1,tmp2)); } if (i<n) root[i+1]=merge(0,q,root[i+1],root[i]); } for (i=1;i<=q;i++) if (vis[i]) printf("%lld\n",Ans[i]); return 0; }
|