随笔- 181  文章- 2  评论- 0  阅读- 6806 

细节不少

//根据更相减损法gcd(x,y) = gcd(x,y-x)
//推广到三项为gcd(x,y,z) = gcd(x,y-x,z-y)
//可以推广到n项
#include<bits/stdc++.h>
using namespace std;
#define x first
#define y second
typedef pair<int,int> PII;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<string> VS;
typedef vector<int> VI;
typedef vector<vector<int>> VVI;
const int N = 500010;
int n,q;
template <class T>
struct BIT
{
	T c[N];
	int sz;
	void init(int s)
	{
		sz = s;
		for(int i=1;i<=sz;++i) c[i] = 0;
	}
	T lowbit(int x)
	{
		return x&-x;
	}
	T sum(int x) 
	{
		assert(x <= sz);
		T res=0;
		while(x) res+=c[x],x-=lowbit(x);
		return res;
	}
	T query(int L, int R)
    {
        return sum(R) - sum(L-1);
    }
	void update(int x,T y) 
	{
		assert(x != 0);
		while(x<=sz) c[x]+=y,x+=lowbit(x);
	}
};
BIT<ll> A;
struct info
{
	ll gcd;
};
info operator + (const info& l,const info& r)
{
	return {__gcd(l.gcd,r.gcd)};
}
struct node
{
	int l,r;
	info val;
}tr[N<<2];
void pushup(int p)
{
	tr[p].val = tr[p<<1].val + tr[p<<1|1].val;
}
void build(int l,int r,int p)
{
	tr[p].l = l,tr[p].r = r;
	if(l==r)
	{
		tr[p].val = {A.sum(r) - A.sum(r-1)};
		return ;
	}
	int m = (l+r)/2;
	build(l,m,p<<1);
	build(m+1,r,p<<1|1);
	pushup(p);
}
void update(int x,ll d,int p)
{
	int l = tr[p].l, r = tr[p].r;
	if(l==r)
	{
		tr[p].val.gcd += d;
		return ;
	}
	int m = (l+r)/2;
	if(x<=m) update(x,d,p<<1);
	else update(x,d,p<<1|1);
	pushup(p);
}
info query(int x,int y,int p)
{
	int l = tr[p].l,r = tr[p].r;
	if(x==l&&r==y) return tr[p].val;
	info ans;
	int m = (l+r)/2;
	if(y<=m) return query(x,y,p<<1);
	else if(x>m) return query(x,y,p<<1|1);
	else return query(x,m,p<<1)+query(m+1,y,p<<1|1);
}
void solve()
{
	//用线段树维护B[i]为A[i]的差分数组
	//用树状数组维护A[i]
	cin>>n>>q;
	//注意使用封住BIT需要init
	A.init(n);
	for(int i=1;i<=n;++i) 
	{
		ll a;
		cin>>a;
		A.update(i,a);
		A.update(i+1,-a);
	}
	build(1,n,1);
	
	for(int i=1;i<=q;++i)
	{
		char op;
		cin>>op;
		if(op == 'C')
		{
			int l,r;
			ll d;
			cin>>l>>r>>d;
			A.update(l,d);
			A.update(r+1,-d);
			update(l,d,1);
			//特判一下,以免干扰和n+1相连接的区块
			if(r<n) update(r+1,-d,1);
		}
		else
		{
			int l,r;
			cin>>l>>r;
			if(l==r) cout<<A.sum(l)<<'\n';
			else
			{
				auto res = query(l+1,r,1).gcd;
				cout<<abs(__gcd(A.sum(l),res))<<"\n";
			}
			// for(int i=1;i<=n;++i) cout<<A.sum(i)<<" \n"[i==n];
			// for(int i=1;i<=n;++i) cout<<query(i,i,1).gcd<<" \n"[i==n];
		}
	}
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	int T=1;
	//cin>>T;
	while(T--)
	{
		solve();
	}
}
 posted on   ruoye123456  阅读(12)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
点击右上角即可分享
微信分享提示