1798: [Ahoi2009]Seq 维护序列seq
Time Limit: 30 Sec Memory Limit: 64 MB
Submit: 5229 Solved: 1861
Description
老师交给小可可一个维护数列的任务,现在小可可希望你来帮他完成。 有长为N的数列,不妨设为a1,a2,…,aN 。有如下三种操作形式: (1)把数列中的一段数全部乘一个值; (2)把数列中的一段数全部加一个值; (3)询问数列中的一段数的和,由于答案可能很大,你只需输出这个数模P的值。
Input
第一行两个整数N和P(1≤P≤1000000000)。第二行含有N个非负整数,从左到右依次为a1,a2,…,aN, (0≤ai≤1000000000,1≤i≤N)。第三行有一个整数M,表示操作总数。从第四行开始每行描述一个操作,输入的操作有以下三种形式: 操作1:“1 t g c”(不含双引号)。表示把所有满足t≤i≤g的ai改为ai×c (1≤t≤g≤N,0≤c≤1000000000)。 操作2:“2 t g c”(不含双引号)。表示把所有满足t≤i≤g的ai改为ai+c (1≤t≤g≤N,0≤c≤1000000000)。 操作3:“3 t g”(不含双引号)。询问所有满足t≤i≤g的ai的和模P的值 (1≤t≤g≤N)。 同一行相邻两数之间用一个空格隔开,每行开头和末尾没有多余空格。
Output
对每个操作3,按照它在输入中出现的顺序,依次输出一行一个整数表示询问结果。
Sample Input
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
Sample Output
2
35
8
HINT
【样例说明】
初始时数列为(1,2,3,4,5,6,7)。
经过第1次操作后,数列为(1,10,15,20,25,6,7)。
对第2次操作,和为10+15+20=45,模43的结果是2。
经过第3次操作后,数列为(1,10,24,29,34,15,16}
对第4次操作,和为1+10+24=35,模43的结果是35。
对第5次操作,和为29+34+15+16=94,模43的结果是8。
测试数据规模如下表所示
数据编号 1 2 3 4 5 6 7 8 9 10
N= 10 1000 1000 10000 60000 70000 80000 90000 100000 100000
M= 10 1000 1000 10000 60000 70000 80000 90000 100000 100000
同时带两个延迟状态的线段树。。
pushdown和包含区间的更新时,要把add标记调整到mult标记作用之后,也就是要用分配律展开。
pushdown可以在判断了要进行下一步递归才更新。。。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<string>
#include<iomanip>
#include<ctime>
#include<climits>
#include<cctype>
#include<algorithm>
#ifdef WIN32
#define AUTO "%I64d"
#else
#define AUTO "%lld"
#endif
using namespace std;
#define smax(x,tmp) x=max((x),(tmp))
#define smin(x,tmp) x=min((x),(tmp))
#define maxx(x1,x2,x3) max(max(x1,x2),x3)
#define minn(x1,x2,x3) min(min(x1,x2),x3)
const int INF=0x3f3f3f3f;
const int maxn = 100005;
typedef long long LL;
struct Node
{
int mult,add;
int sum;
}node[maxn<<2];
#define mult(root) node[root].mult
#define add(root) node[root].add
#define sum(root) node[root].sum
int a[maxn];
int n,mod;
inline void update(int root)
{
sum(root) = ((LL)sum(root<<1) + sum(root<<1|1)) % mod;
}
void build(int root,int l,int r)
{
mult(root)=1; add(root)=0;
if(l==r)
{
sum(root)=a[l]%mod;
return ;
}
int m=(l+r)>>1;
build(root<<1,l,m);
build(root<<1|1,m+1,r);
update(root);
}
inline void pushdown(int root,int l,int r)
{
int m=(l+r)>>1;
sum(root<<1) = ((LL)sum(root<<1)*mult(root) + (LL)add(root)*(m-l+1)) % mod;
mult(root<<1) = (LL)mult(root<<1)*mult(root) % mod;
add(root<<1) = ((LL)add(root<<1)*mult(root)+add(root)) % mod;
sum(root<<1|1) = ((LL)sum(root<<1|1)*mult(root) + (LL)add(root)*(r-m)) % mod;
mult(root<<1|1) = (LL)mult(root<<1|1)*mult(root) % mod;
add(root<<1|1) = ((LL)add(root<<1|1)*mult(root)+add(root)) % mod;
mult(root)=1; add(root)=0;
}
void modify(int root,int l,int r,int x,int y,int ad,int mul)
{
if(x<=l && r<=y)
{
sum(root) = ((LL)sum(root)*mul + (LL)ad*(r-l+1)) % mod;
mult(root) = (LL)mult(root)*mul % mod;
add(root) = ((LL)add(root)*mul + ad) % mod;
return;
}
pushdown(root,l,r);
int m=(l+r)>>1;
if(x<=m && l<=y) modify(root<<1,l,m,x,y,ad,mul);
if(y>=m+1 && r>=x) modify(root<<1|1,m+1,r,x,y,ad,mul);
update(root);
}
int query(int root,int l,int r,int x,int y)
{
if(x<=l && r<=y) return sum(root);
pushdown(root,l,r);
int m=(l+r)>>1;
LL ret = 0;
if(x<=m && l<=y) ret += query(root<<1,l,m,x,y);
if(y>=m+1 && r>=x) ret += query(root<<1|1,m+1,r,x,y);
return ret % mod;
}
void init()
{
scanf("%d%d",&n,&mod);
for(int i=1;i<=n;i++) scanf("%d",a+i);
build(1,1,n);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("seq.in","r",stdin);
freopen("seq.out","w",stdout);
#endif
init();
int q;
scanf("%d",&q);
int opt,x,y,z;
while(q--)
{
scanf("%d%d%d",&opt,&x,&y);
if(opt==3)
{
int ans = query(1,1,n,x,y);
printf("%d\n",ans);
continue;
}
scanf("%d",&z);
if(opt==1) modify(1,1,n,x,y,0,z);
else modify(1,1,n,x,y,z,1);
}
return 0;
}