Codeforces Round #254 (Div. 1)C. DZY Loves Colors(线段树经典操作/分块)

C. DZY Loves Colors
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

DZY loves colors, and he enjoys painting.

On a colorful day, DZY gets a colorful ribbon, which consists of n units (they are numbered from 1 to n from left to right). The color of the i-th unit of the ribbon is i at first. It is colorful enough, but we still consider that the colorfulness of each unit is 0 at first.

DZY loves painting, we know. He takes up a paintbrush with color x and uses it to draw a line on the ribbon. In such a case some contiguous units are painted. Imagine that the color of unit i currently is y. When it is painted by this paintbrush, the color of the unit becomes x, and the colorfulness of the unit increases by |x - y|.

DZY wants to perform m operations, each operation can be one of the following:

  1. Paint all the units with numbers between l and r (both inclusive) with color x
  2. Ask the sum of colorfulness of the units between l and r (both inclusive). 

Can you help DZY?

Input

The first line contains two space-separated integers n, m (1 ≤ n, m ≤ 105).

Each of the next m lines begins with a integer type (1 ≤ type ≤ 2), which represents the type of this operation.

If type = 1, there will be 3 more integers l, r, x (1 ≤ l ≤ r ≤ n; 1 ≤ x ≤ 108) in this line, describing an operation 1.

If type = 2, there will be 2 more integers l, r (1 ≤ l ≤ r ≤ n) in this line, describing an operation 2.

Output

For each operation 2, print a line containing the answer — sum of colorfulness.

Examples
input
3 3
1 1 2 4
1 2 3 5
2 1 3
output
8
input
3 4
1 1 3 4
2 1 1
2 2 2
2 3 3
output
3
2
1
input
10 6
1 1 5 3
1 2 7 9
1 10 10 11
1 3 8 12
1 1 10 3
2 1 10
output
129
Note

In the first sample, the color of each unit is initially [1, 2, 3], and the colorfulness is [0, 0, 0].

After the first operation, colors become [4, 4, 3], colorfulness become [3, 2, 0].

After the second operation, colors become [4, 5, 5], colorfulness become [3, 3, 2].

So the answer to the only operation of type 2 is 8.


题意

一开始,a[i]=i,b[i]=0

然后两个操作

1.使得[l,r]的b[i]+=fabs(x-a[i]),a[i]=x

2.查询[l,r]的b[i]和


题解:

这是属于线段树比较经典的操作,可以设置一个量记录区间是否相等,若一个结点左右儿子区间分别都是相等的,而且两个区间也是相等的,那么可以合并这个区间。

也可以分块做。


线段树代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define fi first
#define se second
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const int inf=0x3fffffff;
const ll mod=1000000007;
const int maxn=1e5+100;
struct node
{
    int l,r,c,ctag;
    ll atag,sum;
}seg[maxn*4];
void build(int i,int l,int r)
{
    seg[i].l=l,seg[i].r=r,seg[i].ctag=0,seg[i].sum=0,seg[i].atag=0,seg[i].c=-1;
    if(l==r)
    {
        seg[i].c=l;
        return;
    }
    int m=(l+r)/2;
    build(i*2,l,m),build(i*2+1,m+1,r);
}
void pushdown(int i)
{
    if(seg[i].l!=seg[i].r)
    {
        seg[i*2].c=seg[i*2+1].c=seg[i].c;
        seg[i*2].ctag=seg[i*2+1].ctag=seg[i].c;  //
        if(seg[i].atag)
        {
            seg[i*2].atag+=seg[i].atag;
            seg[i*2+1].atag+=seg[i].atag;
            seg[i*2].sum+=seg[i].atag*(seg[i*2].r-seg[i*2].l+1);
            seg[i*2+1].sum+=seg[i].atag*(seg[i*2+1].r-seg[i*2+1].l+1);
        }
        seg[i].atag=seg[i].ctag=0;
    }
}
void pushup(int i)
{
    if(seg[i*2].c==seg[i*2+1].c)
        seg[i].c=seg[i*2].c;
    else seg[i].c=-1;          //
    seg[i].sum=seg[i*2].sum+seg[i*2+1].sum;
}
void update(int i,int l,int r,int v)
{
    if(seg[i].l==l&&seg[i].r==r&&seg[i].c!=-1)
    {
        seg[i].sum+=1ll*abs(seg[i].c-v)*(seg[i].r-seg[i].l+1);
        seg[i].atag+=abs(seg[i].c-v);
        seg[i].c=seg[i].ctag=v;
        return;
    }
    if(seg[i].ctag)
        pushdown(i);
    int m=(seg[i].l+seg[i].r)/2;
    if(r<=m) update(i*2,l,r,v);
    else if(l>m) update(i*2+1,l,r,v);
    else
    {
        update(i*2,l,m,v),update(i*2+1,m+1,r,v);
    }
    pushup(i);
}
ll query(int i,int l,int r)
{
    if(seg[i].l==l&&seg[i].r==r)
    {
        return seg[i].sum;
    }
    if(seg[i].ctag)
        pushdown(i);
    int m=(seg[i].l+seg[i].r)/2;
    if(r<=m) return query(i*2,l,r);
    else if(l>m) return query(i*2+1,l,r);
    else return query(i*2,l,m)+query(i*2+1,m+1,r);
}
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    build(1,1,n);
    while(m--)
    {
        int op,l,r,v;
        scanf("%d%d%d",&op,&l,&r);
        if(op==1)
        {
            scanf("%d",&v);
            update(1,l,r,v);
        }
        else if(op==2)
        {
            printf("%I64d\n",query(1,l,r));
        }
    }
    return 0;
}

分块代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define fi first
#define se second
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const int maxn=1e6+100;

int belong[maxn],l[maxn],r[maxn];
ll a[maxn],b[maxn];
ll lazy[1000],sum[1000],lazyb[1000];
int num,block;
int n,m;
void build()
{
    block=sqrt(n);    //
    num=n/block;
    if(n%block) num++;
    for(int i=1;i<=num;i++)
        l[i]=(i-1)*block+1,r[i]=i*block,lazy[i]=-1,sum[i]=0,lazyb[i]=0;
    r[num]=n;
    for(int i=1;i<=n;i++) belong[i]=(i-1)/block+1;
}
void update(int ld,int rd,ll v)
{
    int bl=belong[ld],br=belong[rd];
    if(bl==br)
    {
        if(lazy[bl]!=-1)
        {
            rep(i,l[bl],r[bl]+1)
            {
                a[i]=lazy[bl];
            }
            lazy[bl]=-1;
        }
        rep(i,ld,rd+1)
        {
            b[i]+=abs(a[i]-v);
            sum[bl]+=abs(a[i]-v);
            a[i]=v;
        }
    }
    else
    {
        if(lazy[bl]!=-1)
        {
            rep(i,l[bl],r[bl]+1)
            a[i]=lazy[bl];
            lazy[bl]=-1;
        }
        rep(i,ld,r[bl]+1)
        {
            b[i]+=abs(v-a[i]);
            sum[bl]+=abs(v-a[i]);
            a[i]=v;
        }
        rep(i,bl+1,br)
        {
            if(lazy[i]!=-1)
            {
                lazyb[i]+=abs(lazy[i]-v);
                sum[i]+=abs(lazy[i]-v)*(r[i]-l[i]+1);
                lazy[i]=v;
            }
            else
            {
                rep(j,l[i],r[i]+1)
                {
                    b[j]+=abs(v-a[j]);
                    sum[i]+=abs(v-a[j]);
                    a[j]=v;
                }
                lazy[i]=v;
            }
        }
        if(lazy[br]!=-1)
        {
            rep(i,l[br],r[br]+1)
            a[i]=lazy[br];
            lazy[br]=-1;
        }
        rep(i,l[br],rd+1)
        {
            b[i]+=abs(a[i]-v);
            sum[br]+=abs(a[i]-v);
            a[i]=v;
        }
    }
}
ll query(int ld,int rd)
{
    ll ans=0;
    int bl=belong[ld],br=belong[rd];
    if(bl==br)
    {
        rep(i,ld,rd+1)
        ans+=b[i]+lazyb[bl];
    }
    else
    {
        rep(i,ld,r[bl]+1) ans+=b[i]+lazyb[bl];
        rep(i,bl+1,br)
        ans+=sum[i];
        rep(i,l[br],rd+1) ans+=b[i]+lazyb[br];
    }
    return ans;
}
int main()
{
    scanf("%d%d",&n,&m);
    memset(l,0,sizeof(l));
    memset(r,0,sizeof(r));
    memset(belong,0,sizeof(belong));
    memset(sum,0,sizeof(sum));
    memset(lazy,0,sizeof(lazy));
    memset(lazyb,0,sizeof(lazyb));
    build();
    rep(i,1,n+1) a[i]=i,b[i]=0;
    while(m--)
    {
        int op,l,r;
        scanf("%d%d%d",&op,&l,&r);
        if(op==1)
        {
            ll v;
            scanf("%I64d",&v);
            update(l,r,v);
        }
        else
        {
            printf("%I64d\n",query(l,r));
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值