D - Just a Hook

题目链接:https://cn.vjudge.net/contest/269834#problem/D

题目大意:刚开始给你n个点,每个点的价值是1,然后会区间更新价值,询问最后总的价值是多少。

题解:lazy算法。

代码:

#include 
#include 
#include 
#include 
#include 

using namespace std;

struct node
{
    int l,r,num;
    int lazy;
} s[2000000];

int h[2000000];

void creat(int t,int l,int r)//建树
{
    int mid=(l+r)>>1;
    if(l==r)
    {
        s[t].l=l;
        s[t].r=r;
        s[t].lazy=1;
        return ;
    }
    s[t].l=l;
    s[t].r=r;
    s[t].lazy=1;
    creat(t*2,l,mid);
    creat(t*2+1,mid+1,r);
}

void addre(int t,int l,int r,int e)//区间更新
{
    int mid=(s[t].l+s[t].r)>>1;
    if(s[t].l==l&&s[t].r==r)
    {
        s[t].lazy=e;
        return ;
    }
    if(s[t].lazy!=0)
    {
        s[t*2+1].lazy=s[t].lazy;
        s[t*2].lazy=s[t].lazy;
        s[t].lazy=0;
    }
    if(mid>=r)
    {

        addre(t*2,l,r,e);
    }
    else if(mid

 

你可能感兴趣的:(线段树)