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 with_my_moon @ 2024-08-20 18:14:58

已过,谢谢


by zhizhenyaohanyu @ 2024-08-20 20:28:45

@sza15147979889

#include<cstdio>
#include<cstring> 
#include<iostream>
#include<algorithm>
#include<cmath>

using namespace std;

struct Tree
{
   long long left,right;
   long long sum,lazy;
}; 

Tree tree[900001];
long long a[900001];

void build(long long id,long long l,long long r)
{
        tree[id].left=l; tree[id].right=r;
        if (l==r) tree[id].sum=a[l];
        else
        {
            long long mid=(l+r)/2;
            build(id*2,l,mid);
            build(id*2+1,mid+1,r);
            tree[id].sum=tree[id*2].sum+tree[id*2+1].sum;
        }
}

void vlazy(long long id)
{
    long long mid=(tree[id].left+tree[id].right)/2;
    tree[id*2].sum+=(mid-tree[id].left+1)*tree[id].lazy;
    tree[id*2].lazy+=tree[id].lazy;
    tree[id*2+1].sum+=(tree[id].right-mid)*tree[id].lazy;
    tree[id*2+1].lazy+=tree[id].lazy;
    tree[id].lazy=0;
}

long long update(long long id,long long l,long long r){
        if (tree[id].left==l && tree[id].right==r)return tree[id].sum;
        else
        {
            if(tree[id].lazy!=0) vlazy(id);
            long long mid=(tree[id].left+tree[id].right)/2; 
            if (r<=mid) return update(id*2,l,r);
            else if (l>mid) return update(id*2+1,l,r);
            else
               return update(id*2,l,mid)+update(id*2+1,mid+1,r);
        }
}

void query(long long id,long long l,long long r,long long val){
        if (tree[id].left==l && r==tree[id].right)
        {
             tree[id].sum+=val*(r-l+1);
             tree[id].lazy+=val;
        } 
        else
        {
            if(tree[id].lazy!=0) vlazy(id);
            long long mid=(tree[id].left+tree[id].right)/2; 
            if (r<=mid) query(id*2,l,r,val);
            else if (l>mid) query(id*2+1,l,r,val);
            else
            {
                query(id*2,l,mid,val);
                query(id*2+1,mid+1,r,val);
            }
            tree[id].sum=tree[id*2].sum+tree[id*2+1].sum;
        }
}

int main()
{
    long long d,x,c;
    int m,n,z;
    scanf("%d",&n);
    scanf("%d",&m);
    for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
    build(1,1,n); 
    for(int i=1;i<=m;i++){
        scanf("%d",&z);
        if(z==1)
        {
             scanf("%lld%lld%lld",&d,&x,&c);
             query(1,d,x,c); 
        }
        else{
            scanf("%lld%lld",&x,&c);
            printf("%lld\n",update(1,x,c));
        }
    }
    return 0;
}

求关qwq


上一页 |