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.

【题意】

给定一个长度为n的序列,初始时ai=i,vali=0(1in).有两种操作:

  1. 将区间[L,R]的值改为x,并且当一个数从y改成x时它的权值vali会增加|xy|.
  2. 询问区间[L,R]的权值和.
n105,1x106.
【解题方法】 不明觉厉
【代码君】
//
//Created by just_sort 2016/10/3 14:10
//Copyright (c) 2016 just_sort.All Rights Reserved
//

#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 1e5+7;
typedef long long LL;
struct node{
    int l,r,same;
    LL tag,sum;
    int len(){
        return r-l+1;
    }
}T[maxn*4];
void pushup(int rt)
{
    T[rt].sum = T[rt*2].sum + T[rt*2+1].sum;
    if(T[rt*2].same != T[rt*2+1].same) T[rt].same = -1;
    else T[rt].same = T[rt*2].same;
}
void update(LL val,int col,int rt)
{
    T[rt].tag += val;
    T[rt].sum += 1LL*val*T[rt].len();
    T[rt].same = col;
}
void pushdown(int rt)
{
    if(T[rt].tag)
    {
        update(T[rt].tag,T[rt].same,rt*2);
        update(T[rt].tag,T[rt].same,rt*2+1);
        T[rt].tag = 0;
    }
}
void Build(int l,int r,int rt)
{
    T[rt].l = l,T[rt].r = r,T[rt].same = -1;
    T[rt].sum = T[rt].tag = 0;
    if(l == r){
        T[rt].same = l;
    }
    else{
        int m = (l + r)/2;
        Build(l,m,rt*2);
        Build(m+1,r,rt*2+1);
    }
}
void modify(int L,int R,int val,int rt)
{
    if(L == T[rt].l && T[rt].r == R && ~T[rt].same)
    {
        update(abs(val-T[rt].same),val,rt);
    }
    else{
        pushdown(rt);
        int mid = (T[rt].l + T[rt].r)/2;
        if(R <= mid) modify(L,R,val,rt*2);
        else if(L > mid) modify(L,R,val,rt*2+1);
        else{
            modify(L,mid,val,rt*2);
            modify(mid+1,R,val,rt*2+1);
        }
        pushup(rt);
    }
}
LL queryans(int L,int R,int rt)
{
    if(T[rt].l == L && T[rt].r == R)
    {
        return T[rt].sum;
    }
    else{
        pushdown(rt);
        int mid = (T[rt].l + T[rt].r)/2;
        if(R <= mid) return queryans(L,R,rt*2);
        else if(L > mid) return queryans(L,R,rt*2+1);
        else{
            LL ans = 0;
            ans += queryans(L,mid,rt*2);
            ans += queryans(mid+1,R,rt*2+1);
            return ans;
        }
    }
}
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    Build(1,n,1);
    int op,l,r,x;
    for(int i = 1; i <= m; i++)
    {
        scanf("%d",&op);
        if(op == 1)
        {
            scanf("%d%d%d",&l,&r,&x);
            modify(l,r,x,1);
        }
        else{
            scanf("%d%d",&l,&r);
            printf("%lld\n",queryans(l,r,1));
        }
    }
    return 0;
}