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

Time Limit: 1 Sec  Memory Limit: 256 MB

## 题目连接

http://www.lydsy.com/JudgeOnline/problem.php?id=1798

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

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << (#x) << " = " << (x) << endl
#define error(x) (!(x)?puts("error"):0)
#define rdm(x, i) for(int i=ihead[x]; i; i=e[i].next)
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }

const int N=100005;
#define lc x<<1
#define rc x<<1|1
#define MID (l+r)>>1
#define lson l, mid, lc
#define rson mid+1, r, rc

int n, MD;

struct node {
int sum, add, mul;
void upd(int a, int m, int len) {
add=((ll)add*m+a)%MD;
mul=((ll)mul*m)%MD;
sum=((ll)sum*m+(ll)a*len)%MD;
}
}t[N<<2];
void pushdown(int x, int len) {
if(t[x].add!=0 || t[x].mul!=1)
t[lc].upd(t[x].add, t[x].mul, (len-(len>>1))),
t[rc].upd(t[x].add, t[x].mul, len>>1),
t[x].add=0, t[x].mul=1;
}
void pushup(int x) { t[x].sum=(t[lc].sum+t[rc].sum)%MD; }
void build(int l, int r, int x) {
t[x].add=0;
t[x].mul=1;
if(l==r) { t[x].sum=getint(); return; }
int mid=MID;
build(lson); build(rson);
pushup(x);
}
void update(int l, int r, int x, int L, int R, int add, int mul) {
if(L<=l && r<=R) { t[x].upd(add, mul, r-l+1); return; }
pushdown(x, r-l+1);
int mid=MID;
if(L<=mid) update(lson, L, R, add, mul);
if(mid<R) update(rson, L, R, add, mul);
pushup(x);
}
int query(int l, int r, int x, int L, int R) {
if(L<=l && r<=R) return t[x].sum;
pushdown(x, r-l+1);
int mid=MID, ret=0;
if(L<=mid) ret+=query(lson, L, R);
if(mid<R) ret+=query(rson, L, R);
ret%=MD;
return ret;
}
int main() {
read(n); read(MD);
build(1, n, 1);
int m=getint();
while(m--) {
int c=getint();
if(c==1) { int l=getint(), r=getint(), x=getint(); update(1, n, 1, l, r, 0, x); }
else if(c==2) { int l=getint(), r=getint(), x=getint(); update(1, n, 1, l, r, x, 1); }
else if(c==3) { int l=getint(), r=getint(); printf("%d\n", query(1, n, 1, l, r)); }
}
return 0;
}

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << (#x) << " = " << (x) << endl
#define error(x) (!(x)?puts("error"):0)
#define rdm(x, i) for(int i=ihead[x]; i; i=e[i].next)
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }

const int N=100005;
#define lc x<<1
#define rc x<<1|1
#define MID (l+r)>>1
#define lson l, mid, lc
#define rson mid+1, r, rc

int n, MD;

struct node {
int sum, add, mul;
void upd(int a, int m, int len) {
add=((ll)add*m+a)%MD;
mul=((ll)mul*m)%MD;
sum=((ll)sum*m+(ll)a*len)%MD;
}
}t[N<<2];
void pushdown(int x, int len) {
if(t[x].add!=0 || t[x].mul!=1)
t[lc].upd(t[x].add, t[x].mul, (len-(len>>1))),
t[rc].upd(t[x].add, t[x].mul, len>>1),
t[x].add=0, t[x].mul=1;
}
void pushup(int x) { t[x].sum=(t[lc].sum+t[rc].sum)%MD; }
void build(int l, int r, int x) {
t[x].add=0;
t[x].mul=1;
if(l==r) { t[x].sum=getint(); return; }
int mid=MID;
build(lson); build(rson);
pushup(x);
}
void update(int l, int r, int x, int L, int R, int add, int mul) {
if(L<=l && r<=R) { t[x].upd(add, mul, r-l+1); return; }
pushdown(x, r-l+1);
int mid=MID;
if(L<=mid) update(lson, L, R, add, mul);
if(mid<R) update(rson, L, R, add, mul);
pushup(x);
}
int query(int l, int r, int x, int L, int R) {
if(L<=l && r<=R) return t[x].sum;
pushdown(x, r-l+1);
int mid=MID, ret=0;
if(L<=mid) ret+=query(lson, L, R);
if(mid<R) ret+=query(rson, L, R);
ret%=MD;
return ret;
}
int main() {
read(n); read(MD);
build(1, n, 1);
int m=getint();
while(m--) {
int c=getint();
if(c==1) { int l=getint(), r=getint(), x=getint(); update(1, n, 1, l, r, 0, x); }
else if(c==2) { int l=getint(), r=getint(), x=getint(); update(1, n, 1, l, r, x, 1); }
else if(c==3) { int l=getint(), r=getint(); printf("%d\n", query(1, n, 1, l, r)); }
}
return 0;
}

posted @ 2015-04-09 18:26  qscqesze  阅读(482)  评论(0编辑  收藏  举报