# 【bzoj1798】[Ahoi2009]Seq 维护序列seq 线段树

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

#include <cstdio>
#include <cstring>
#define lson l , mid , x << 1
#define rson mid + 1 , r , x << 1 | 1
typedef long long lint;
lint mod , sum[400010] , add[400010] , mul[400010];
void pushup(int x)
{
sum[x] = (sum[x << 1] + sum[x << 1 | 1]) % mod;
}
void pushdown(int l , int r , int x)
{
int mid = (l + r) >> 1;
if(mul[x] != 1)
{
sum[x << 1] = sum[x << 1] * mul[x] % mod;
sum[x << 1 | 1] = sum[x << 1 | 1] * mul[x] % mod;
add[x << 1 | 1] = add[x << 1 | 1] * mul[x] % mod;
mul[x << 1] = mul[x << 1] * mul[x] % mod;
mul[x << 1 | 1] = mul[x << 1 | 1] * mul[x] % mod;
mul[x] = 1;
}
{
sum[x << 1] = (sum[x << 1] + add[x] * (mid - l + 1)) % mod;
sum[x << 1 | 1] = (sum[x << 1 | 1] + add[x] * (r - mid)) % mod;
}
}
void build(int l , int r , int x)
{
mul[x] = 1;
if(l == r)
{
scanf("%lld" , &sum[x]);
sum[x] %= mod;
return;
}
int mid = (l + r) >> 1;
build(lson);
build(rson);
pushup(x);
}
void updatemul(int b , int e , lint m , int l , int r , int x)
{
if(b <= l && r <= e)
{
sum[x] = sum[x] * m % mod;
mul[x] = mul[x] * m % mod;
return;
}
pushdown(l , r , x);
int mid = (l + r) >> 1;
if(b <= mid) updatemul(b , e , m , lson);
if(e > mid) updatemul(b , e , m , rson);
pushup(x);
}
void updateadd(int b , int e , lint a , int l , int r , int x)
{
if(b <= l && r <= e)
{
sum[x] = (sum[x] + a * (r - l + 1)) % mod;
return;
}
pushdown(l , r , x);
int mid = (l + r) >> 1;
if(b <= mid) updateadd(b , e , a , lson);
if(e > mid) updateadd(b , e , a , rson);
pushup(x);
}
lint query(int b , int e , int l , int r , int x)
{
if(b <= l && r <= e)
return sum[x];
pushdown(l , r , x);
int mid = (l + r) >> 1;
lint ans = 0;
if(b <= mid) ans = (ans + query(b , e , lson)) % mod;
if(e > mid) ans = (ans + query(b , e , rson)) % mod;
return ans;
}
int main()
{
int n , m , p , l , r;
lint k;
scanf("%d%lld" , &n , &mod);
build(1 , n , 1);
scanf("%d" , &m);
while(m -- )
{
scanf("%d%d%d" , &p , &l , &r);
switch(p)
{
case 1: scanf("%lld" , &k); updatemul(l , r , k , 1 , n , 1); break;
case 2: scanf("%lld" , &k); updateadd(l , r , k , 1 , n , 1); break;
default: printf("%lld\n" , query(l , r , 1 , n , 1));
}
}
return 0;
}
posted @ 2017-02-16 16:39  GXZlegend  阅读(248)  评论(0编辑  收藏  举报