70分求调,#8,#9,#10,wa

P3372 【模板】线段树 1

with_my_moon @ 2024-08-20 18:03:40


#include <iostream>
#include <math.h>
#include <algorithm>
#include <cstdio>
using namespace std;

#define maxn 1000005
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1

int n,m;
int val,nowl,nowr;
int a[16*maxn];
long long sum[16*maxn],col[16*maxn];

void update(int rt){
    sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}

void build(int l,int r,int rt){
    if(l==r){
        sum[rt]=a[l];
        return;
    }
    int mid=(l+r)>>1;
    build(lson);
    build(rson);
    update(rt);
}

void color(int l,int r,int rt,int c){
    sum[rt]+=c*(r-l+1);
    col[rt]+=c;
}

void push_col(int l,int r,int rt){
    if(col[rt]){
        int mid=(r+l)>>1;
        color(lson,col[rt]);
        color(rson,col[rt]);
        col[rt]=0;
    }
}

int query(int l,int r,int rt){
    if(nowl<=l&&r<=nowr) return sum[rt];
    push_col(l,r,rt);
    int mid=(r+l)>>1,ans=0;
    if(nowl<=mid) ans+=query(lson);
    if(mid+1<=nowr) ans+=query(rson);
    return ans;
}

void modify(int l,int r,int rt,int c){
    if(nowl<=l&&r<=nowr){color(l,r,rt,c);return;}
    push_col(l,r,rt);
    int mid=(r+l)>>1;
    if(nowl<=mid) modify(lson,c);
    if(mid+1<=nowr) modify(rson,c);
    update(rt);
}

int main(){
    scanf("%d %d",&n,&m);
    for(int i=1;i<=n;++i) scanf("%d",&a[i]);
    build(1,n,1);
    for(int i=1;i<=m;++i){
        int op;
        scanf("%d %d %d",&op,&nowl,&nowr);
        if(op==1){
            scanf("%d",&val);
            modify(1,n,1,val);
        }
        else{
            printf("%d\n",query(1,n,1));
        }
    }
}

by Starlit_Moon @ 2024-08-20 18:06:54

@sza15147979889 查询里的ans需要开long long,求关注


by Starlit_Moon @ 2024-08-20 18:08:57

哎等等还是不行


by Starlit_Moon @ 2024-08-20 18:10:35

好了,输出要%lld


by Starlit_Moon @ 2024-08-20 18:11:10

@sza15147979889


by zhouzihang1 @ 2024-08-20 18:11:12

@sza15147979889 输入,a,color 里的 c,query及其里面的 ans,modify 的 c 都要开 long long


by Starlit_Moon @ 2024-08-20 18:12:26

我直接帮你#define int long long 了

#include <iostream>
#include <math.h>
#include <algorithm>
#include <cstdio>
using namespace std;
#define int long long 
#define maxn 1000005
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
int n,m;
int val,nowl,nowr;
int a[16*maxn];
long long sum[16*maxn],col[16*maxn];

void update(int rt){
    sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}

void build(int l,int r,int rt){
    if(l==r){
        sum[rt]=a[l];
        return;
    }
    int mid=(l+r)>>1;
    build(lson);
    build(rson);
    update(rt);
}

void color(int l,int r,int rt,int c){
    sum[rt]+=c*(r-l+1);
    col[rt]+=c;
}

void push_col(int l,int r,int rt){
    if(col[rt]){
        int mid=(r+l)>>1;
        color(lson,col[rt]);
        color(rson,col[rt]);
        col[rt]=0;
    }
}

long long query(int l,int r,int rt){
    if(nowl<=l&&r<=nowr) return sum[rt];
    push_col(l,r,rt);
    long long mid=(r+l)>>1,ans=0;
    if(nowl<=mid) ans+=query(lson);
    if(mid+1<=nowr) ans+=query(rson);
    return ans;
}

void modify(int l,int r,int rt,int c){
    if(nowl<=l&&r<=nowr){color(l,r,rt,c);return;}
    push_col(l,r,rt);
    int mid=(r+l)>>1;
    if(nowl<=mid) modify(lson,c);
    if(mid+1<=nowr) modify(rson,c);
    update(rt);
}

signed main(){
    scanf("%d %d",&n,&m);
    for(int i=1;i<=n;++i) scanf("%d",&a[i]);
    build(1,n,1);
    for(int i=1;i<=m;++i){
        int op;
        scanf("%d %d %d",&op,&nowl,&nowr);
        if(op==1){
            scanf("%d",&val);
            modify(1,n,1,val);
        }
        else{
            printf("%lld\n",query(1,n,1));
        }
    }
}

by with_my_moon @ 2024-08-20 18:12:39

@Starlit_Moon 啊……大佬,好像还是不行啊(已关


by with_my_moon @ 2024-08-20 18:14:21

@Starlit_Moon 好的好的,谢谢大佬,过了


by Starlit_Moon @ 2024-08-20 18:14:25

@sza15147979889 看我的代码吧


by with_my_moon @ 2024-08-20 18:14:44

@zhouzihang1 好的,已过,谢谢


| 下一页