您的位置:首页 > 产品设计 > UI/UE

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

2017-03-28 20:57 417 查看
维护一个区间的和,支持单点修改,区间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 For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p])
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,0x3f,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define MEMx(a,b) memset(a,b,sizeof(a));
#define INF (0x3f3f3f3f)
#define F (1000000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int>
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase) printf("Case %d:\n",kcase);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a
<<endl;
#define PRi2D(a,n,m) For(i,n) { \
For(j,m-1) cout<<a[i][j]<<' ';\
cout<<a[i][m]<<endl; \
}
#pragma comment(linker, "/STACK:102400000,102400000")
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: