codeforces 438D 分类: codeforces 2015-03-28 10:18 38人阅读 评论(0) 收藏
分析:对于数
所以对
线段树维护区间最大值和区间和,
取模操作对
时间复杂度:
#include<map>
#include<string>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
const int MAXN = 100005;
int n , m;
int a[MAXN] = {0};
struct treenode
{
long long sum;int max;
}tree[MAXN<<2] = {0};
#define L(x) (x<<1)
#define R(x) ((x<<1)|1)
long long query(int l,int r,int ll,int rr,int si)
{
if(l == ll && r == rr)return tree[si].sum;
else
{
int mid = (ll+rr)>>1;long long ret = 0;
if(r <= mid) ret = query(l,r,ll,mid,L(si));
else if(l > mid)ret = query(l,r,mid+1,rr,R(si));
else
{
ret += query(l,mid,ll,mid,L(si));
ret += query(mid+1,r,mid+1,rr,R(si));
}
return ret;
}
}
void update(int x)
{
tree[x].sum = tree[L(x)].sum + tree[R(x)].sum;
tree[x].max = std::max(tree[L(x)].max, tree[R(x)].max);
}
void build(int ll,int rr,int si)
{
if(ll == rr){tree[si].sum = tree[si].max = a[ll];}
else
{
int mid = (ll+rr)>>1;
if(ll<=mid)build(ll,mid,L(si));
if(mid<rr)build(mid+1,rr,R(si));
update(si);
}
}
void modset(int l,int r,int ll,int rr,int si,int p)
{
if(tree[si].max < p)return;
if(ll == rr){tree[si].max %= p; tree[si].sum = tree[si].max;}
else
{
int mid = (ll+rr)>>1;
if(r <= mid) modset(l,r,ll,mid,L(si),p);
else if(l > mid)modset(l,r,mid+1,rr,R(si),p);
else
{
modset(l,mid,ll,mid,L(si),p);
modset(mid+1,r,mid+1,rr,R(si),p);
}
update(si);
}
}
void change(int k,int ll,int rr,int si,int val)
{
if(ll == rr){tree[si].sum = tree[si].max = val;}
else
{
int mid = (ll+rr)>>1;
if(k <= mid) change(k,ll,mid,L(si),val);
else change(k,mid+1,rr,R(si),val);
update(si);
}
}
#undef L
#undef R
int main()
{
#ifndef ONLINE_JUDGE
freopen("CF438D.in","r",stdin);
freopen("CF438D.out","w",stdout);
#endif
scanf("%d%d",&n,&m);
for(int i = 1 ; i <= n; i++)scanf("%d",&a[i]);
build(1,n,1);
while(m--)
{
int op;scanf("%d",&op);
int l , r ,v;
switch(op)
{
case 1:
scanf("%d%d",&l,&r);
printf("%I64d\n",query(l,r,1,n,1));
break;
case 2:
scanf("%d%d%d",&l,&r,&v);
modset(l,r,1,n,1,v);
break;
case 3:
scanf("%d%d",&l,&v);
change(l,1,n,1,v);
break;
}
}
#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
#endif
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。