# [BZOJ] 1798: [Ahoi2009]Seq 维护序列seq #线段树：区间加+区间乘+区间求和

## 1798: [Ahoi2009]Seq 维护序列seq

Time Limit: 30 Sec  Memory Limit: 64 MB
Submit: 7136  Solved: 2582
[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

Day1

## Analysis

= =退役前最后一天补掉了，，，从学线段树那天开始就没有做出来的题

（没开在luogu只过了3个点= =）

## Code

#include<stdio.h>
#include<iostream>
#define maxn 200005
#define mid (L+R)/2
#define lc (rt<<1)
#define rc (rt<<1|1)
#define LL long long
using namespace std;

int n,m; LL mod;

int ret = 0,t = 1; char ctr = getchar();
while(ctr < '0' || ctr > '9') (ctr == '-' && (t = -1)),ctr = getchar();
while(ctr >= '0' && ctr <= '9') ret = ret*10+ctr-'0',ctr = getchar();
return ret;
}

void maintain(int rt){ T[rt].sum = (T[lc].sum+T[rc].sum)%mod; }

void mul_single(int rt,LL val){
T[rt].sum = T[rt].sum*val%mod;
T[rt].mul_lazy = 1LL*T[rt].mul_lazy*val %mod;
}

void add_single(int rt,int L,int R,LL val){
T[rt].sum = (T[rt].sum+val*(R-L+1))%mod;
}

void pushdown(int rt,int L,int R){
if(T[rt].mul_lazy != 1){
mul_single(lc, T[rt].mul_lazy);
mul_single(rc, T[rt].mul_lazy);
T[rt].mul_lazy = 1;
}

}
}

void build(int rt,int L,int R){
T[rt].add_lazy = 0; T[rt].mul_lazy = 1;
if(L == R) T[rt].sum = read();
else{
build(lc,L,mid); build(rc,mid+1,R);
maintain(rt);
}
}

void mul_modify(int rt,int L,int R,int qL,int qR,LL val){
pushdown(rt,L,R);
if(qL <= L && R <= qR) mul_single(rt,val);
else{
if(qL <= mid) mul_modify(lc, L, mid, qL, qR, val);
if(qR > mid) mul_modify(rc, mid+1, R, qL, qR, val);
maintain(rt);
}
}

void add_modify(int rt, int L, int R, int qL,int qR,LL val){
pushdown(rt,L,R);
if(qL <= L && R <= qR) add_single(rt,L,R,val);
else{
maintain(rt);
}
}

LL query(int rt,int L,int R,int qL,int qR){
pushdown(rt,L,R);
if(qL <= L && R <= qR) return T[rt].sum;
else{
LL ret = 0;
if(qL <= mid) ret += query(lc,L,mid,qL,qR);
if(qR > mid) ret += query(rc,mid+1,R,qL,qR);
return ret%mod;
}
}

int main(){
scanf("%d%lld",&n,&mod);
build(1,1,n);
scanf("%d",&m);

for(int i = 1;i <= m;i++){
int a,b,c; LL d; scanf("%d",&a);
switch(a){
scanf("%d%d%lld",&b,&c,&d);
mul_modify(1,1,n,b,c,d); break;
scanf("%d%d%lld",&b,&c,&d);
scanf("%d%d",&b,&c);
printf("%lld\n",query(1,1,n,b,c)%mod);
}
//		printf("Now: "); for(int j = 1;j <= n;j++) printf("%d ",query(1,1,n,j,j)); cout << endl;
}

return 0;
}


posted @ 2017-11-09 14:31  Leviaton  阅读(201)  评论(0编辑  收藏  举报