跳到正文

个人记录

qt累计帮我调出来9道题并说出原因附送zorrS号

lazybug_on_tree约 7 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 7 分钟

版本

  1. v1当前

P3373 【模板】线段树 2

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,q,m;
map<int,bool> mp;
struct Tree{
    int l,r;
    int sum;
    int lazy1,lazy2;
}tree[900001];
int a[900001];
int s[900001];
int ans;
void push_up(int p){
	tree[p].sum=tree[p*2].sum+tree[p*2+1].sum;
}
void pushdown1(int p){
	if(tree[p].lazy1==0)return ;
    tree[p*2].sum*=tree[p].lazy1;
    tree[p*2].sum%=m;
    tree[p*2].lazy1+=tree[p].lazy1;
    if(tree[p*2].lazy1%m==0&&tree[p*2].lazy1!=0)tree[p*2].lazy1=m;
	else tree[p*2].lazy1%=m;
    tree[p*2+1].sum*=tree[p].lazy1;
    tree[p*2+1].sum%=m;
    tree[p*2+1].lazy1+=tree[p].lazy1;
    if(tree[p*2+1].lazy1%m==0&&tree[p*2].lazy1!=0)tree[p*2].lazy1=m;
	else tree[p*2+1].lazy1%=m;
    tree[p].lazy1=0;
}
void pushdown2(int p){
    int x=tree[p*2].l,y=tree[p*2].r;
    int X=tree[p*2+1].l,Y=tree[p*2+1].r;
    tree[p*2].sum+=(y-x+1)*(tree[p].lazy2)%m;
    tree[p*2].sum%=m;
    tree[p*2].lazy2+=tree[p].lazy2;
    tree[p*2].lazy2%=m;
    tree[p*2+1].sum+=(Y-X+1)*(tree[p].lazy2)%m;
    tree[p*2+1].sum%=m;
    tree[p*2+1].lazy2+=tree[p].lazy2;
    tree[p*2+1].lazy2%=m;
    tree[p].lazy2=0;
}
void mul(int p,int l,int r,int num){
    int x=tree[p].l,y=tree[p].r;
    if(x>=l&&y<=r){
        tree[p].lazy1+=num;
        if(tree[p].lazy1%m==0&&tree[p].lazy1!=0)tree[p].lazy1=m;
		else tree[p].lazy1%=m;
        tree[p].sum*=num;
        tree[p].sum%=m;
        return ;
    }
    pushdown1(p);
    pushdown2(p);
    int mid=x+y>>1;
    if(mid>=l)mul(p*2,l,r,num);
    if(mid+1<=r)mul(p*2+1,l,r,num);
    push_up(p);
}
void add(int p,int l,int r,int num){
    int x=tree[p].l,y=tree[p].r;
    if(x>=l&&y<=r){
//        cout<<"线段的编号是:"<<p<<"左边界"<<tree[p].l<<"右边界"<<tree[p].r<<endl;
//        cout<<"原先蓝标鸡:"<<tree[p].lazy2;
        tree[p].lazy2+=num;
//        cout<<"蓝标鸡:"<<tree[p].lazy2<<endl;
        tree[p].lazy2%=m;
//        cout<<"原先和:"<<tree[p].sum<<endl;
        tree[p].sum+=(y-x+1)*num;
//        cout<<"和:"<<tree[p].sum<<endl;
        tree[p].sum%=m;
        return ;
    }
    int mid=x+y>>1;
    pushdown1(p);
    pushdown2(p);
    if(mid>=l)add(p*2,l,r,num);
    if(mid+1<=r)add(p*2+1,l,r,num);
    push_up(p);
}
void que(int p,int l,int r){
    int x=tree[p].l,y=tree[p].r;
    if(x>=l&&y<=r){
//        cout<<"ans之前是:"<<ans<<endl;
        ans+=tree[p].sum;
//        cout<<"ans采纳了:"<<p<<"的和:"<<tree[p].sum<<"ans变成了:"<<ans<<endl;
        ans%=m;
        return ;
    }
    pushdown1(p);
    pushdown2(p);
    int mid=x+y>>1;
    if(mid>=l)que(p*2,l,r);
    if(mid+1<=r)que(p*2+1,l,r);
}
void buildtree(int p,int l,int r){
    tree[p].l=l;
    tree[p].r=r;
    tree[p].sum=s[r]-s[l-1];
    tree[p].sum%=m;
//    cout<<"线段编号:"<<p<<"左边界:"<<l<<"右边界:"<<r<<"和:"<<tree[p].sum<<endl;
    if(l==r){
        return ;
    }
    int mid=l+r>>1;
    buildtree(p*2,l,mid);
    buildtree(p*2+1,mid+1,r);
}
signed main(){
    cin>>n>>q>>m;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        s[i]=s[i-1]+a[i];
    }
//    for(int i=1;i<=n;i++)cout<<s[i]<<" ";
//    cout<<endl;
    buildtree(1,1,n);
    for(int i=1;i<=q;i++){
        int op;
        cin>>op;
        if(op==1){
            int x,y,k;
            cin>>x>>y>>k;
            mul(1,x,y,k);
        }
        if(op==2){
            int x,y,k;
            cin>>x>>y>>k;
            add(1,x,y,k);
        }
        if(op==3){
            int x,y;
            ans=0;
            cin>>x>>y;
            que(1,x,y);
            cout<<ans<<endl;
        }
    }
    return 0;
}
原文