首页 > 其他分享 >CF438D(The Child and Sequence-线段树mod x)

CF438D(The Child and Sequence-线段树mod x)

时间:2022-10-24 18:06:36浏览次数:59  
标签:include return Sequence int ll maxv CF438D mod define


维护一个区间的和,支持单点修改,区间mod x

考虑a>x,时,amodx<a/2,否则a=x

所以暴力维护就行了

#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <functional>
#include <cstdlib>
#include <queue>
#include <stack>
using namespace std;
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
For(j,m-1) cout<<a[i][j]<<' ';\
cout<<a[i][m]<<endl; \
}
#pragma
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
const int maxn =110000;
ll sum[maxn<<2];
int a[maxn],maxv[maxn<<2];
void pushup(int o) {
sum[o]=sum[Lson]+sum[Rson];
maxv[o]=max(maxv[Lson],maxv[Rson]);
}
void build(int l,int r,int o) {
if (l==r) {
sum[o]=maxv[o]=a[l]; return ;
}
int m=(l+r)>>1;
build(l,m,Lson),build(m+1,r,Rson);
pushup(o);
}
void update(int l,int r,int o,int p,int v) {
if (l==r) {
sum[o]=maxv[o]=v; return;
}
int m=(l+r)>>1;
if (p<=m) update(l,m,Lson,p,v);
else update(m+1,r,Rson,p,v);
pushup(o);
}
bool check(int o,int x) {
return maxv[o]<x;
}
void updamod(int l,int r,int o,int L,int R,int x) {
if (maxv[o]<x) return ;
if (l==r) {
sum[o]%=x; maxv[o]=sum[o]; return;
}
int m=(l+r)>>1;
if (L<=m) updamod(l,m,Lson,L,R,x);
if (m<R) updamod(m+1,r,Rson,L,R,x);
pushup(o);
}
ll query(int l,int r,int o,int L,int R) {
if(L<=l && r<=R ) return sum[o];
int m=(l+r)>>1;
ll ret=0;
if(L<=m) ret+=query(l,m,Lson,L,R);
if(m<R) ret+=query(m+1,r,Rson,L,R);
return ret;
}
int main()
{
// freopen("CF438D.in","r",stdin);
// freopen(".out","w",stdout);
ios_base::sync_with_stdio(0);//cin.tie(0);
int n,m;cin>>n>>m;
For(i,n) cin>>a[i];
build(1,n,1);
while(m--) {
int p,p1,p2;
cin>>p>>p1>>p2;
switch(p){
case 1:{
cout<<query(1,n,1,p1,p2)<<endl;
break;
}
case 2:{
int x;cin>>x;
updamod(1,n,1,p1,p2,x);
break;
}
case 3:{
update(1,n,1,p1,p2);
}
}
}
return 0;
}


标签:include,return,Sequence,int,ll,maxv,CF438D,mod,define
From: https://blog.51cto.com/u_15724837/5790744

相关文章