Codeforces 438D The Child and Sequence

时间:2024-12-21 23:33:32

题意:给定一个n个数的序列,完成以下3个操作:

  1.给定区间求和

  2.给定区间对x取模

  3.单点修改

对一个数取模,这个数至少折半。于是我们记一个最大值max,如果x>max则不做处理。

#include<stdio.h>
#include<algorithm>
using namespace std;
#define MAXN 1000000+10
typedef long long LL;
struct tree{LL mx,sum;}tr[MAXN<<];
int n,m;
LL a[MAXN];
void build(int k,int l,int r){
if(l==r){
tr[k].mx=tr[k].sum=a[l];
return;
}
int mid=(l+r)>>;
build(k<<,l,mid);
build(k<<|,mid+,r);
tr[k].mx=max(tr[k<<].mx,tr[k<<|].mx);
tr[k].sum=tr[k<<].sum+tr[k<<|].sum;
}
LL sum(int k,int l,int r,int L,int R){
if(l>=L&&r<=R)return tr[k].sum;
int mid=(l+r)>>;
if(R<=mid)return sum(k<<,l,mid,L,R);
else if(L>mid)return sum(k<<|,mid+,r,L,R);
else return sum(k<<,l,mid,L,R)+sum(k<<|,mid+,r,L,R);
tr[k].mx=max(tr[k<<].mx,tr[k<<|].mx);
tr[k].sum=tr[k<<].sum+tr[k<<|].sum;
}
void update(int k,int l,int r,int t,LL x){
if(l==t&&r==t){
tr[k].mx=tr[k].sum=x;
return;
}
int mid=(l+r)>>;
if(t<=mid)update(k<<,l,mid,t,x);
if(t>mid)update(k<<|,mid+,r,t,x);
tr[k].mx=max(tr[k<<].mx,tr[k<<|].mx);
tr[k].sum=tr[k<<].sum+tr[k<<|].sum;
}
void mod(int k,int l,int r,int L,int R,LL x){
if(tr[k].mx<x)return;
if(l==r){
tr[k].mx%=x;
tr[k].sum%=x;
return;
}
int mid=(l+r)>>;
if(R<=mid)mod(k<<,l,mid,L,R,x);
else if(L>mid)mod(k<<|,mid+,r,L,R,x);
else mod(k<<,l,mid,L,R,x),mod(k<<|,mid+,r,L,R,x);
tr[k].mx=max(tr[k<<].mx,tr[k<<|].mx);
tr[k].sum=tr[k<<].sum+tr[k<<|].sum;
}
int main(){
//freopen("mod.in","r",stdin);
//freopen("mod.out","w",stdout);
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)scanf("%I64d",&a[i]);
build(,,n);
while(m--){
int opt;
scanf("%d",&opt);
if(opt==){
int l,r;
scanf("%d%d",&l,&r);
printf("%I64d\n",sum(,,n,l,r));
}
if(opt==){
int l,r;LL x;
scanf("%d%d%I64d",&l,&r,&x);
mod(,,n,l,r,x);
}
if(opt==){
int k;LL y;
scanf("%d%I64d",&k,&y);
update(,,n,k,y);
}
}
return ;
}