[Line segment tree segment update] HDU - 1698 E - Just a Hook

            ** [Line Segment Renewal] HDU - 1698 E - Just a Hook**

Main idea of the title: There is a hook with length n. It consists of n small hooks. The initial value of each small hook is 1. By one operation, for intervals
[x,y], update the value of each small hook in the interval to X (1<=x<=3). Find the total value of the last hook.
Analysis: At the beginning of the line segment tree, when I first wrote, I simply updated the corresponding interval, without considering that when the interval is not exactly included, it has been wrong. The second time, I updated every update to the leaves, resolutely T. Then I used the most introductory paragraph update method (at first I thought it was unnecessary), and saw other blogs called lazy tags...

#include <stdio.h>
struct node
{
    int l,r;
    int sum;
    int lnc;//increment 
};
node tree[400005];
int mid(int root)
{
    return (tree[root].l+tree[root].r)/2;
}
void BuildTree(int root,int l,int r)//Tree-building initialization 
{
  tree[root].l=l;
  tree[root].r=r;
  tree[root].sum=r-l+1;
  tree[root].lnc=0;
  if(l!=r)
  {
    BuildTree(2*root+1,l,mid(root));
    BuildTree(2*root+2,mid(root)+1,r);
  } 
}
void updata(int root,int a,int b,int v)
{
    if(tree[root].l==a&&tree[root].r==b)//The current interval coincides with the operation interval completely. The sum is updated directly and marked. 
    {
        tree[root].sum=(b-a+1)*v;
        tree[root].lnc=v;
        return;
    }
    if(tree[root].lnc!=0)//When incomplete coincidence occurs, if there are tags, the tags are updated left and right 
    {
     updata(2*root+1,tree[root].l,mid(root),tree[root].lnc);
     updata(2*root+2,mid(root)+1,tree[root].r,tree[root].lnc);  
     tree[root].lnc=0;//Elimination mark 
    }
    //Recursive Updating of Judgment Interval Conditions 
    if(b<=mid(root))
    {
      updata(2*root+1,a,b,v);
    }
    else if(a>mid(root))
    {
      updata(2*root+2,a,b,v);   
    }
    else
    {
      updata(2*root+1,a,mid(root),v);
      updata(2*root+2,mid(root)+1,b,v); 
    }
    //Update the sum of the current node directly when function backtracking 
    tree[root].sum=tree[2*root+1].sum+tree[2*root+2].sum;   
}
int main(int argc, char *argv[])
{
    int T;
    scanf("%d",&T);
    int ans=0;
    while(T--)
    {
        int n;
        scanf("%d",&n);
        BuildTree(0,1,n);
        int q;
        scanf("%d",&q);
        int x,y,z;
        for(int i=0;i<q;i++)
        {
          scanf("%d %d %d",&x,&y,&z);
          updata(0,x,y,z);
        }
        printf("Case %d: The total value of the hook is %d.\n",++ans,tree[0].sum);
    }
    return 0;
}

Posted by jennifer_ann on Wed, 13 Feb 2019 15:15:18 -0800