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

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


明天省选了。写一题和明天ROUND1难度差不多的题练手【其实就是去年ROUND1抄过去的题,然后去年ROUND1之前我就写过这题】

这题有+和*两个操作。那么我们用两个lazytag就可以解决了。【标记传递见代码push操作】

#include<cstdio>
using namespace std;
struct tree
{
     int l,r;
     long long x;
     long long tag1,tag2;
}tr[800001];
long long mod;
long long a[100001];
inline void up(int p)
{
     tr[p].x=(tr[p*2].x+tr[p*2+1].x)%mod;
}
inline void down(int p)
{
	 int l=tr[p].r-tr[p].l+1;
     tr[p*2].x*=tr[p].tag2;
     tr[p*2].x+=(l-l/2)*tr[p].tag1;
     tr[p*2].x%=mod;
     tr[p*2].tag1=(tr[p*2].tag1*tr[p].tag2+tr[p].tag1)%mod;
     tr[p*2].tag2=(tr[p*2].tag2*tr[p].tag2)%mod;
     
     tr[p*2+1].x*=tr[p].tag2;
     tr[p*2+1].x+=l/2*tr[p].tag1;
     tr[p*2+1].x%=mod;
     tr[p*2+1].tag1=(tr[p*2+1].tag1*tr[p].tag2+tr[p].tag1)%mod;
     tr[p*2+1].tag2=(tr[p*2+1].tag2*tr[p].tag2)%mod;
     
     tr[p].tag1=0;
     tr[p].tag2=1;
}
inline void build(int p,int l,int r)
{
     tr[p].l=l;
     tr[p].r=r;
     if(l!=r)
     {
          int mid=(l+r)/2;
          build(p*2,l,mid);
          build(p*2+1,mid+1,r);
          up(p);
          tr[p].tag2=1;
     }
     else
     {
          tr[p].x=a[l]%mod;
          tr[p].tag2=1;
     }
}
inline void add(int p,int l,int r,long long x)
{
     if(l<=tr[p].l&&tr[p].r<=r)
     {
          tr[p].x+=(tr[p].r-tr[p].l+1)*x;
          tr[p].x%=mod;
          tr[p].tag1=(tr[p].tag1+x)%mod;
     }
     else
     {
     	  down(p);
          int mid=(tr[p].l+tr[p].r)/2;
          if(l<=mid)
               add(p*2,l,r,x);
          if(r>mid)
               add(p*2+1,l,r,x);
          up(p);
     }
}
inline void change(int p,int l,int r,long long x)
{
     if(l<=tr[p].l&&tr[p].r<=r)
     {
     	  down(p);
          tr[p].x*=x;
          tr[p].x%=mod;
          tr[p].tag2=(tr[p].tag2*x)%mod;
     }
     else
     {
     	  down(p);
          int mid=(tr[p].l+tr[p].r)/2;
          if(l<=mid)
               change(p*2,l,r,x);
          if(r>mid)
               change(p*2+1,l,r,x);
          up(p);
     }
}
inline long long ask(int p,int l,int r)
{
     if(l<=tr[p].l&&tr[p].r<=r)
          return tr[p].x;
     else
     {
     	  down(p);
          int mid=(tr[p].l+tr[p].r)/2;
          long long ans=0;
          if(l<=mid)
               ans+=ask(p*2,l,r);
          if(r>mid)
               ans+=ask(p*2+1,l,r);
          ans%=mod;
          //up(p);
          return ans;
     }
}
int main()
{
     int n;
     scanf("%d%lld",&n,&mod);
     int i;
     for(i=1;i<=n;i++)
          scanf("%lld",&a[i]);
     build(1,1,n);
     int m;
     scanf("%d",&m);
     int s,t;
     long long x;
     for(i=1;i<=m;i++)
     {
          scanf("%lld",&x);
          if(x==2)
          {
          	   scanf("%d%d%lld",&s,&t,&x);
               add(1,s,t,x);
          }
          else if(x==1)
          {
          	   scanf("%d%d%lld",&s,&t,&x);
               change(1,s,t,x);
          }
          else
          {
               scanf("%d%d",&s,&t);
               printf("%lld\n",ask(1,s,t));
          }
     }
     return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值