悬关,0分样例不过求调

P3372 【模板】线段树 1

rnf5114 @ 2023-12-04 23:26:16

#include <bits/stdc++.h>
using namespace std;
#define int long long
int n,m,a[100010],s,awa=1,sum,ans;
int op,x,y,k;
struct node{
    int l,r,he,zuo,you,add;
}tree[400010];
int maketree(int l,int r,int dian){
    int mid=(l+r)/2;
    if(l>r)
        return 0;
    sum++;
    if(l==r){
        tree[dian].l=l;
        tree[dian].r=r;
        tree[dian].he=a[l];
        return a[l];
    }
    tree[dian].l=l;
    tree[dian].r=r;
    tree[dian].he=maketree(l,mid,2*dian)+maketree(mid+1,r,2*dian+1);
    return tree[dian].he;
}
void cao1(int dian){
    if(dian==0)
        return ;
    if(tree[dian].r<x||tree[dian].l>y)
        return ;
    else if(tree[dian].l>=x&&tree[dian].r<=y){
        tree[dian].add+=k;
        return ;
    }
    else{
        cao1(tree[dian].zuo);
        cao1(tree[dian].you);
    }
}
void cao2(int dian,int jia){
    if(dian==0||tree[dian].r<x||tree[dian].l>y)
        return ;
    else if(tree[dian].l>=x&&tree[dian].r<=y){
        ans+=tree[dian].he+jia;
        return ;
    }
    else{
        cao2(tree[dian].zuo,jia+tree[dian].add);
        cao2(tree[dian].you,jia+tree[dian].add);
    }
}
signed main(){
    cin>>n>>m;
    for(int i=1;i<=n;i++)
        cin>>a[i];
    maketree(awa,n,awa);
    for(int i=1;i<=sum;i++){
        if(tree[2*i].he)
            tree[i].zuo=2*i;
        if(tree[2*i+1].he)
            tree[i].you=2*i+1;
    }
    while(m--){
        cin>>op;
        if(op==1){
            cin>>x>>y>>k;
            cao1(1);
        }
        else{
            ans=0;
            cin>>x>>y;
            cao2(1,0);
            cout<<ans<<endl;
        }
    }
    return 0;
}

/*
       ┏┓   ┏┓
     ┏┛┻━━━┛┻┓
     ┃       ┃
     ┃   ━   ┃
     ┃ ┳┛ ┗┳ ┃
     ┃       ┃
     ┃   ┻   ┃
     ┃       ┃
     ┗━┓   ┏━┛Codes are far away from bugs with the animal protecting
         ┃   ┃    神兽保佑,代码无bug
         ┃   ┃
         ┃   ┗━━━┓
         ┃      ┣┓
         ┃     ┏┛
         ┗┓┓┏━┳┓┏┛
           ┃┫┫ ┃┫┫
           ┗┻┛ ┗┻┛     ○| ̄|_

*/

by rnf5114 @ 2023-12-04 23:31:58

下线睡觉了,明天晚上再上线查回复


by IOI_AK_TLR @ 2023-12-05 04:36:47

@rnfmabj5114 调好了

另外建议lz看看这个

#include <bits/stdc++.h>
using namespace std;
#define int long long
int n, m, a[100010], s, awa = 1, ans;
int op, x, y, k;
struct node {
    int l, r, he, zuo, you, add;
} tree[800010];
int maketree(int l, int r, int dian) {
    int mid = (l + r) / 2;
    if (l > r)
        return 0;
    tree[dian].zuo = dian * 2;
    tree[dian].you = dian * 2 + 1; //直接放递归函数里
    if (l == r) {
        tree[dian].l = l;
        tree[dian].r = r;
        tree[dian].he = a[l];
        return a[l];
    }
    tree[dian].l = l;
    tree[dian].r = r;
    tree[dian].he = maketree(l, mid, 2 * dian) + maketree(mid + 1, r, 2 * dian + 1);
    return tree[dian].he;
}
void cao1(int dian) {
    if (dian == 0)
        return ;
    if (tree[dian].r < x || tree[dian].l > y)
        return ;

    //下传标记给左右儿子
    if (tree[dian].add && tree[dian].l != tree[dian].r) {
        tree[tree[dian].zuo].add += tree[dian].add;
        tree[tree[dian].you].add += tree[dian].add;
        tree[tree[dian].zuo].he += (tree[tree[dian].zuo].r - tree[tree[dian].zuo].l + 1) * tree[dian].add;
        tree[tree[dian].you].he += (tree[tree[dian].you].r - tree[tree[dian].you].l + 1) * tree[dian].add;
        tree[dian].add = 0;
    }
    if (tree[dian].l >= x && tree[dian].r <= y) {
        tree[dian].add += k;
        //he为什么没更新
        tree[dian].he += (tree[dian].r - tree[dian].l + 1) * k;
        return ;
    } else {
        cao1(tree[dian].zuo);
        cao1(tree[dian].you);
        if (tree[dian].l != tree[dian].r) {
            tree[dian].he = tree[tree[dian].zuo].he + tree[tree[dian].you].he;
        }//操作完某一小段(比如[1,2]这一段)以后,它的祖先的he也应当更新(比如[1,4]的he)
    }
}
void cao2(int dian) {
    if (dian == 0 || tree[dian].r < x || tree[dian].l > y)
        return ;
    //下传标记给左右儿子
    if (tree[dian].add && tree[dian].l != tree[dian].r) {
        tree[tree[dian].zuo].add += tree[dian].add;
        tree[tree[dian].you].add += tree[dian].add;
        tree[tree[dian].zuo].he += (tree[tree[dian].zuo].r - tree[tree[dian].zuo].l + 1) * tree[dian].add;
        tree[tree[dian].you].he += (tree[tree[dian].you].r - tree[tree[dian].you].l + 1) * tree[dian].add;
        tree[dian].add = 0;
    }
    if (tree[dian].l >= x && tree[dian].r <= y) {
        ans += tree[dian].he;
        return ;
    } else {
        cao2(tree[dian].zuo);
        cao2(tree[dian].you);
    }
}
signed main() {
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    maketree(awa, n, awa);
    while (m--) {
        cin >> op;
        if (op == 1) {
            cin >> x >> y >> k;
            cao1(1);
        } else {
            ans = 0;
            cin >> x >> y;
            cao2(1);
            cout << ans << endl;
        }
    }
    return 0;
}

/*
   _____    __      __    _____   
  /  _  \  /  \    /  \  /  _  \  
 /  /_\  \ \   \/\/   / /  /_\  \ 
/    |    \ \        / /    |    \
\____|__  /  \__/\  /  \____|__  /
        \/        \/           \/ 
*/

by rnf5114 @ 2023-12-05 18:30:56

@IOI_AK_TLR 感谢dalao%%%


|