社区讨论
WA on #2神秘封装
P10463Interval GCD参与者 2已保存回复 3
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 3 条
- 当前快照
- 1 份
- 快照标识符
- @mircjd3d
- 此快照首次捕获于
- 2025/12/04 19:22 3 个月前
- 此快照最后确认于
- 2025/12/06 17:40 3 个月前
CPP
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxn=3e6+5;
#define lson (u<<1)
#define rson (u<<1|1)
#define mid ((l+r)>>1)
int n,q;
int a[maxn],d[maxn];
struct segtree{ //差分线段树:单点修改,区间查询
struct treenode{
int l,r,val;
}tree[maxn<<2];
void pushup(int u){
tree[u].val=__gcd(tree[lson].val,tree[rson].val);
}
void build(int u,int l,int r){
tree[u].l=l,tree[u].r=r,tree[u].val=0;
if(l==r){
tree[u].val=d[l];
return;
}
build(lson,l,mid);
build(rson,mid+1,r);
pushup(u);
}
void update(int u,int p,int x){
int l=tree[u].l,r=tree[u].r;
if(l>p||r<p)return;
if(l==r){
tree[u].val+=x;
return;
}
if(p<=mid)update(lson,p,x);
else update(rson,p,x);
pushup(u);
}
int query(int u,int L,int R){
int l=tree[u].l,r=tree[u].r;
if(l>R||r<L)return 0;
if(L<=l&&r<=R)return tree[u].val;
return __gcd(query(lson,L,R),query(rson,L,R));
}
}t;
struct seg_tree{ //线段树:区间修改,单点查询
struct treenode{
int l,r,val,lzy;
}tree[maxn<<2];
void pushup(int u){
tree[u].val=tree[lson].val+tree[rson].val;
}
void build(int u,int l,int r){
tree[u].l=l,tree[u].r=r,tree[u].val=tree[u].lzy=0;
if(l==r){
tree[u].val=a[l];
return;
}
build(lson,l,mid);
build(rson,mid+1,r);
pushup(u);
}
void pushdown(int u){
int l=tree[u].l,r=tree[u].r;
tree[lson].lzy=tree[rson].lzy=tree[u].lzy;
tree[lson].val+=tree[u].lzy*(mid-l+1);
tree[rson].val+=tree[u].lzy*(r-mid);
tree[u].lzy=0;
}
void update(int u,int L,int R,int x){
int l=tree[u].l,r=tree[u].r;
if(l>R||r<L)return;
pushdown(u);
if(L<=l&&r<=R){
tree[u].val+=x*(r-l+1);
tree[u].lzy+=x;
return;
}
update(lson,L,R,x);
update(rson,L,R,x);
pushup(u);
}
int query(int u,int p){
int l=tree[u].l,r=tree[u].r;
if(l>p||r<p)return 0;
pushdown(u);
if(l==r){
return tree[u].val;
}
return query(lson,p)+query(rson,p);
}
}s;
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin>>n>>q;
for(int i=1;i<=n;i++){
cin>>a[i];
d[i]=a[i]-a[i-1];
}
t.build(1,1,n);
s.build(1,1,n);
while(q--){
char op;
cin>>op;
if(op=='C'){
int l,r,x;
cin>>l>>r>>x;
t.update(1,l,x);
if(r<n)t.update(1,r+1,-x);
s.update(1,l,r,x);
}else{
int l,r;
cin>>l>>r;
cout<<llabs(__gcd(s.query(1,l),t.query(1,l+1,r)))<<endl;
}
}
return 0;
}
回复
共 3 条回复,欢迎继续交流。
正在加载回复...