Codeforces 444 C. dzy loves colors (line segment tree)

Source: Internet
Author: User

Question:

1 l r x operation description [L, R] nodes are painted in X color, and the value of each node is added | Y-x | Y is the color before painting.

2 L r operation, find the sum on [L, R.


Train of Thought Analysis:

Assume that an interval is of the same color. Then we can merge the operations.

So we can look for the same range before.

However, the question is how to merge operations.

Then we define a Val to indicate the value that should be added to each position in this range.

This value can be added during Pushdown.


#include <cstdio>#include <iostream>#include <cstring>#include <algorithm>#define maxn 100005#define lson num<<1,s,mid#define rson num<<1|1,mid+1,eusing namespace std;typedef long long LL;LL color[maxn<<2];LL sum[maxn<<2];LL val[maxn<<2];LL Abs(LL x){    return x>0?x:-x;}void pushdown(int num,int s,int e){    if(color[num]!=-1)    {        int mid=(s+e)>>1;        sum[num<<1]+=val[num]*(mid-s+1);        sum[num<<1|1]+=val[num]*(e-mid);        val[num<<1]+=val[num];        val[num<<1|1]+=val[num];        color[num<<1]=color[num<<1|1]=color[num];        color[num]=-1;        val[num]=0;    }}void pushup(int num){    if(color[num<<1]==color[num<<1|1])        color[num]=color[num<<1];    else color[num]=-1;    sum[num]=sum[num<<1]+sum[num<<1|1];}void build(int num,int s,int e){    sum[num]=0;    val[num]=0;    color[num]=-1;    if(s==e)    {        color[num]=s;        return;    }    int mid=(s+e)>>1;    build(lson);    build(rson);}void update(int num,int s,int e,int l,int r,LL v){    if(l<=s && r>=e)    {        if(color[num]!=-1)        {            sum[num]+=(LL)Abs(v-color[num])*(e-s+1);            val[num]+=Abs(color[num]-v);            color[num]=v;            return;        }    }    int mid=(s+e)>>1;    pushdown(num,s,e);    if(l<=mid)update(lson,l,r,v);    if(r>mid)update(rson,l,r,v);    pushup(num);}LL query(int num,int s,int e,int l,int r){    if(l<=s && r>=e)    {        return sum[num];    }    int mid=(s+e)>>1;    pushdown(num,s,e);    if(r<=mid)return query(lson,l,r);    else if(l>mid)return query(rson,l,r);    else return query(lson,l,mid)+query(rson,mid+1,r);}int main(){    int n,m;    scanf("%d%d",&n,&m);    build(1,1,n);    while(m--)    {        int type;        scanf("%d",&type);        if(type==1){            int l,r;            LL x;            scanf("%d%d%I64d",&l,&r,&x);            update(1,1,n,l,r,x);        }        else        {            int l,r;            scanf("%d%d",&l,&r);            printf("%I64d\n",query(1,1,n,l,r));        }    }    return 0;}/*10 101 5 9 62 6 101 1 9 31 3 10 52 4 6*/


Codeforces 444 C. dzy loves colors (line segment tree)

Related Article

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.