DWQA QuestionsCategory: ProgramError building line tree
VDON asked 1 month ago
#include <iostream>
using namespace std;
struct Node
{
    int left;
    int right;
    int delta;
    int sum;
    Node *lc;
    Node *rc;
};

void build(Node *cur,int l,int r){
    cur->left=l;
    cur->right=r;
    cur->sum=0;
    if(l+1<r){
        cur->lc = new Node;
        cur->rc = new Node;
        build(cur->lc,l,(l+r)/2);
        build(cur->rc,(l+r)/2,r);
    }
    else cur->lc = cur->rc = NULL;
}
void update(Node *cur){
    cur->lc->sum+=cur->delta*(cur->lc->right - cur->lc->left);
    cur->rc->sum+=cur->delta*(cur->rc->right - cur->rc->left);
    cur->lc->delta+=cur->delta;
    cur->rc->delta+=cur->delta;
    cur->delta=0;
}
void change(Node *cur,int l,int r,int delta){
    if(l<=cur->left&&r>=cur->right){
        cur->sum+=delta*(cur->right - cur->left);
        cur->delta+=delta;
    }
    else{
        if(cur->delta!=0) update(cur);
        if(l<(cur->left+cur->right)/2) change(cur->lc,l,r,delta);
        if(r>(cur->left+cur->right)/2) change(cur->rc,l,r,delta);
        cur->sum=cur->lc->sum+cur->rc->sum;
    }
}
int query(Node *cur,int l,int r){
    if(l<=cur->left&&r>=cur->right)
        return cur->sum;
    else{
        if(cur->delta!=0)
            update(cur);
        int ans=0;
        int t=(cur->left+cur->right)/2;
        if(l<t) ans+=query(cur->lc,l,r);
        if(r>t) ans+=query(cur->rc,l,r);
        return ans;
    }
}
int n,g,m;
int a,b,c,d;
int main(){
    Node *cur;
    cin>>n;
    build(cur,1,n);
    for(int i=1;i<=n;i++){
        cin>>g;
        change(cur,i,i+1,g);
    }
    cin>>m;
    for(int i=1;i<=m;i++){
        cin>>a;
        if(a==1){
            cin>>b>>c>>d;
            change(cur,b,c+1,d);
        }
        else{
            cin>>b>>c;
            cout<<query(cur,b,c+1);
        }
    }
    return 0;
}

Error building.
Program received signal SIGSEGV,Segmentation fault.
The problem of build has been solved. Just change node * cur to node * cur = new node
But the third time change went wrong, it was an instance of Wiki

3 Answers
Best Answer
VDON answered 1 month ago

We have found the answer, we should build (cur, 1, N + 1);
Because what I built is left closed and right open. To save to N, you have to go to N + 1

Cattle farm answered 1 month ago

Take a look with the naked eye. Cur in the main function is a nullptr. Call cur – > left and cur – > right in the build function to confirm the error.

The building owner is engaged in ACM. He has started to learn the line tree. He should have learned it for a long time. You still need to learn the basic debug. GDB will know why

VDON replied 1 month ago

High school, senior one. Do you want node * cur = new node? Let me have a try.

someone answered 1 month ago

May be int out of bounds, pay attention to usinglong long

struct node
{
    int l,r;
    long long v,lazy;
}a[600000];
int b[200003];
void build(int i,int l,int r)
{
    a[i].l=l,a[i].r=r,a[i].lazy=0;
    if(l==r)
    {
        a[i].v=b[l];
        return;
    }
    int m=(l+r)/2;
    build(2*i,l,m),build(2*i+1,m+1,r);
    a[i].v=a[2*i].v+a[2*i+1].v;
}
void update(int i,int l,int r,long long v)
{
    a[i].v+=((r-l+1)*v);
    if(a[i].l==l && a[i].r==r)
    {
        a[i].lazy+=v;
        return;
    }
    int m=(a[i].l+a[i].r)/2;
    if(r<=m)
        update(2*i,l,r,v);
    else if(l>m)
        update(2*i+1,l,r,v);
    else
        update(2*i,l,m,v),update(2*i+1,m+1,r,v);
}
int query(int i,int l,int r)
{
    if(a[i].l==l && a[i].r==r)
        return a[i].v;
    int m=(a[i].l+a[i].r)/2;
    if(r<=m)
        return (r-l+1)*a[i].lazy+query(2*i,l,r);
    else if(l>m)
        return (r-l+1)*a[i].lazy+query(2*i+1,l,r);
    else
        return (r-l+1)*a[i].lazy+query(2*i,l,m)+query(2*i+1,m+1,r);
}

When callingbuild(1,1,n), indicating that the root subscript of the tree is 1