社区讨论

求大神帮忙指点错误,

P3373【模板】线段树 2参与者 4已保存回复 5

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
5 条
当前快照
1 份
快照标识符
@mi6ggahs
此快照首次捕获于
2025/11/20 04:28
4 个月前
此快照最后确认于
2025/11/20 04:28
4 个月前
查看原帖
CPP
#include<bits/stdc++.h>
#define maxn 1000007
#define LL long long
using namespace std;
LL n,m;
LL c[4*maxn],a[maxn];
LL add[maxn];
LL P;
LL addplus[maxn];
void PushUp(LL rt)
{
     c[rt]=c[rt<<1]+c[rt<<1|1];
     c[rt]%=P;
}
void Build(LL l,LL r,LL rt)
{
     if(l==r)
     {
         c[rt]=a[l];
         return ;
     }
     LL m=(l+r)>>1;
     Build(l,m,rt<<1);
     Build(m+1,r,rt<<1|1);
     PushUp(rt);
}
void Pushdown(LL rt,LL ln,LL rn)
{
   if(addplus[rt])
   {
       addplus[rt<<1]*=addplus[rt]%P;
       addplus[rt<<1|1]*=addplus[rt]%P;
       c[rt<<1]*=addplus[rt]%P;
       c[rt<<1|1]*=addplus[rt]%P;
       addplus[rt]=0;
   }
   if(add[rt])
   {
      add[rt<<1]+=add[rt]%P;
      add[rt<<1|1]+=add[rt]%P;
      c[rt<<1]+=(add[rt]*ln)%P;
      c[rt<<1|1]+=(add[rt]*rn)%P;
      add[rt]=0;
   }
}
void Update_interval(LL L,LL R,LL C,LL l,LL r,LL  rt)
{
    if(L<=l&&r<=R)
    {
      c[rt]+=C*(r-l+1);
      add[rt]+=C;
      return ;
   }
   LL m=(l+r)>>1;
   Pushdown(rt,m-l+1,r-m);
   if(L<=m) Update_interval(L,R,C,l,m,rt<<1);
   if(R>m) Update_interval(L,R,C,m+1,r,rt<<1|1);
   PushUp(rt);
}
LL Query(LL L,LL R,LL l,LL r,LL rt)
{
       if(L<=l&&r<=R)
          return c[rt];
      LL m=(l+r)>>1;
      LL ans=0;
      Pushdown(rt,m-l+1,r-m);
      if(L<=m) ans+=Query(L,R,l,m,rt<<1)%P;
      if(R>m) ans+=Query(L,R,m+1,r,rt<<1|1)%P;
      return ans%P;
}
void Update_intervalplus(LL L,LL R,LL C,LL l,LL r,LL rt)
{
   if(L<=l&&r<=R)
    {
      c[rt]*=C;
      add[rt]*=C;
      addplus[rt]*=C;
      return ;
   }
   LL m=(l+r)>>1;
   Pushdown(rt,m-l+1,r-m);
   if(L<=m) Update_intervalplus(L,R,C,l,m,rt<<1);
   if(R>m) Update_intervalplus(L,R,C,m+1,r,rt<<1|1);
   PushUp(rt);
}
int main()
{
    ios::sync_with_stdio(false);
    cin>>n>>m;
    cin>>P;
    for(LL i=1;i<=n;i++)
      cin>>a[i];
      Build(1,n,1);
      LL t1,t2,t3,t4;
    for(LL  i=1;i<=m;i++)
    {
         cin>>t1;
         if(t1==1)
         {
            cin>>t2>>t3>>t4;
            Update_intervalplus(t2,t3,t4,1,n,1);
         }
         if(t1==2)
          { 
                  cin>>t2>>t3>>t4;
                  Update_interval(t2,t3,t4,1,n,1);
          }
          if(t1==3)
          {
             cin>>t2>>t3;
             cout<<(Query(t2,t3,1,n,1))%P<<endl;
          }
    }  
    return 0;
}

回复

5 条回复,欢迎继续交流。

正在加载回复...