Sysu 1686 line segment tree lazy

Source: Internet
Author: User

Interval Update + lazy


I operation l-R interval + c

Operate the maximum value of the L-r range in C and delete the maximum value.


The line segment tree can be added with the maximum value of the record.



#include "stdio.h"#include "string.h"int ans,mark;struct node{    int l,r,Max,mark,lazy;}data[400010];void Pushdown(int k){    if (data[k].l==data[k].r) return ;    if (data[k].lazy==0) return ;    data[k*2].Max+=data[k].lazy;    data[k*2].lazy+=data[k].lazy;    data[k*2+1].Max+=data[k].lazy;    data[k*2+1].lazy+=data[k].lazy;    data[k].lazy=0;}void build(int l,int r,int k){    int mid;    data[k].l=l;    data[k].r=r;    data[k].lazy=0;    data[k].Max=0;    data[k].mark=l;    if (l==r) return ;    mid=(l+r)/2;    build(l,mid,k*2);    build(mid+1,r,k*2+1);}void updata(int l,int r,int k,int op){    int mid;    if (data[k].l==l && data[k].r==r)    {        data[k].lazy+=op;        data[k].Max+=op;        return ;    }    Pushdown(k);    mid=(data[k].l+data[k].r)/2;    if (r<=mid) updata(l,r,k*2,op);    else        if (l>mid) updata(l,r,k*2+1,op);    else    {        updata(l,mid,k*2,op);        updata(mid+1,r,k*2+1,op);    }    if (data[k*2].Max>=data[k*2+1].Max)    {        data[k].Max=data[k*2].Max;        data[k].mark=data[k*2].mark;    }    else    {        data[k].Max=data[k*2+1].Max;        data[k].mark=data[k*2+1].mark;    }}void query(int l,int r,int k){    int mid;    if (data[k].l==l &&data[k].r==r)    {        if (data[k].Max>ans)        {            ans=data[k].Max;            mark=data[k].mark;        }        return ;    }    mid=(data[k].l+data[k].r)/2;    Pushdown(k);    if (r<=mid) query(l,r,k*2);    else        if (l>mid) query(l,r,k*2+1);    else    {        query(l,mid,k*2);        query(mid+1,r,k*2+1);    }}int main(){    int n,m,l,r,c;    char ch;    while (scanf("%d%d",&n,&m)!=EOF)    {        if (n==0 && m==0) break;        build(1,n,1);        while (m--)        {            getchar();            scanf("%c",&ch);            if (ch=='I')            {                scanf("%d%d%d",&l,&r,&c);                updata(l,r,1,c);            }            else            {                ans=0;                scanf("%d%d",&l,&r);                query(l,r,1);                printf("%d\n",ans);                updata(mark,mark,1,-ans);            }        }    }    return 0;}


Sysu 1686 line segment tree lazy

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.