bzoj1798: [Ahoi2009]Seq 维护序列seq(线段树)

1798: [Ahoi2009]Seq 维护序列seq

Time Limit: 30 Sec  Memory Limit: 64 MB
Submit: 6752  Solved: 2404
[Submit][Status][Discuss]

7 43
1 2 3 4 5 6 7
5
1 2 5 5
3 2 4
2 3 7 9
3 1 3
3 4 7

2
35
8

HINT

【样例说明】

N= 10 1000 1000 10000 60000 70000 80000 90000 100000 100000
M= 10 1000 1000 10000 60000 70000 80000 90000 100000 100000

Source

Day1

#include<iostream>
#include<cstdio>
#include<cstring>

#define ll long long
#define N 100010

using namespace std;
ll ans,mod;
int n,m;
struct tree
{
int l,r;
ll mul_,sum_;
ll sum;
}tr[N<<3];

void pushup(int k)
{
tr[k].sum=(tr[k<<1].sum+tr[k<<1|1].sum)%mod;
}

void build(int k,int l,int r)
{
tr[k].l=l;tr[k].r=r;tr[k].sum_=0;tr[k].mul_=1;
if(l==r)
{
scanf("%lld",&tr[k].sum);
return;
}
int mid=(l+r)>>1;
build(k<<1,l,mid);build(k<<1|1,mid+1,r);
pushup(k);
}

void down_sum(int k)
{
tr[k<<1].sum_=(tr[k].sum_+tr[k<<1].sum_)%mod;
tr[k<<1].sum =(tr[k<<1].sum + (tr[k<<1].r-tr[k<<1].l+1)*tr[k].sum_)%mod;
tr[k<<1|1].sum_=(tr[k].sum_+tr[k<<1|1].sum_)%mod;
tr[k<<1|1].sum =(tr[k<<1|1].sum + (tr[k<<1|1].r-tr[k<<1|1].l+1)*tr[k].sum_)%mod;
tr[k].sum_=0;
}

void down_mul(int k)
{
tr[k<<1].sum =(tr[k<<1].sum *tr[k].mul_)%mod;
tr[k<<1].sum_=(tr[k<<1].sum_*tr[k].mul_)%mod;
tr[k<<1].mul_=(tr[k<<1].mul_*tr[k].mul_)%mod;

tr[k<<1|1].sum =(tr[k<<1|1].sum *tr[k].mul_)%mod;
tr[k<<1|1].sum_=(tr[k<<1|1].sum_*tr[k].mul_)%mod;
tr[k<<1|1].mul_=(tr[k<<1|1].mul_*tr[k].mul_)%mod;
tr[k].mul_=1;
}

void changesum(int k,int l,int r,int c)
{
if(l>r||r<l) return;
if(tr[k].l==l && tr[k].r==r)
{
tr[k].sum_=(tr[k].sum_+c)%mod;
tr[k].sum=(tr[k].sum+(tr[k].r-tr[k].l+1)*c)%mod;
return;
}
if(tr[k].mul_!=1) down_mul(k);
if(tr[k].sum_) down_sum(k);
int mid=(tr[k].l+tr[k].r)>>1;
if(r<=mid) changesum(k<<1,l,r,c);
else if(l>mid) changesum(k<<1|1,l,r,c);
else changesum(k<<1,l,mid,c),changesum(k<<1|1,mid+1,r,c);
pushup(k);
}

void changemul(int k,int l,int r,int c)
{
if(l>r||r<l) return;
if(tr[k].l==l && tr[k].r==r)
{
tr[k].mul_=(tr[k].mul_*c)%mod;
tr[k].sum_=(tr[k].sum_*c)%mod;
tr[k].sum=(tr[k].sum*c)%mod;
return;
}
if(tr[k].mul_!=1) down_mul(k);
if(tr[k].sum_) down_sum(k);
int mid=(tr[k].l+tr[k].r)>>1;
if(r<=mid) changemul(k<<1,l,r,c);
else if(l>mid) changemul(k<<1|1,l,r,c);
else changemul(k<<1,l,mid,c),changemul(k<<1|1,mid+1,r,c);
pushup(k);
}

int query(int k,int l,int r)
{
if(l>r||r<l) return 0;
if(tr[k].l==l && tr[k].r==r)
return tr[k].sum%mod;
if(tr[k].mul_!=1) down_mul(k);
if(tr[k].sum_) down_sum(k);
pushup(k);
int mid=(tr[k].r+tr[k].l)>>1;
if(r<=mid) return query(k<<1,l,r)%mod;
else if(l>mid) return query(k<<1|1,l,r)%mod;
else return query(k<<1,l,mid)%mod+query(k<<1|1,mid+1,r)%mod;
}

int main()
{
scanf("%d%lld",&n,&mod);
build(1,1,n);
scanf("%d",&m);
int f, x, y;
long long c;
for(int i=1;i<=m;i++)
{
scanf("%d",&f);
if(f==1)
{
scanf("%d%d%lld",&x,&y,&c);
changemul(1,x,y,c);
}
else if(f==2)
{
scanf("%d%d%lld",&x,&y,&c);
changesum(1,x,y,c);
}
else
{
scanf("%d%d",&x,&y);
printf("%lld\n",query(1,x,y)%mod);
}
}
return 0;
}

posted @ 2017-08-14 09:24  安月冷  阅读(109)  评论(0编辑  收藏  举报