题目大意:有编号1到n的连续n个格子。每个格子有两种属性,color和colorfulness。两种操作
1 l r x :把[l,r]的格子染色为x
2 l r :查询[l,r]格子的colorfulness的和。
每个格子colorfulness初始为0,每改变一次color,colorfulness累加k。其中,k
== abs(以前的color - 现在的color)。
思路:乱搞(话说这样真的靠谱吗o(╯□╰)o )。利用线段树,区间修改时候,一直查找到纯色区间即可。
//#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<cmath>
#include<cctype>
#include<string>
#include<algorithm>
#include<iostream>
#include<ctime>
#include<map>
#include<set>
using namespace std;
#define MP(x,y) make_pair((x),(y))
#define PB(x) push_back(x)
typedef __int64 LL;
//typedef unsigned __int64 ULL;
/* ****************** */
const int INF=100011122;
const double INFF=1e100;
const double eps=1e-8;
const int mod=9999991;
const int NN=100005;
const int MM=5010;
/* ****************** */
struct TR
{
int l,r,col,fg;
LL ad;
LL sum_colful;
int mid()
{
return (l+r)>>1;
}
int getlen()
{
return r-l+1;
}
}tr[NN*4];
LL ans;
void push_up(int R)
{
tr[R].sum_colful=tr[R<<1].sum_colful+tr[R<<1|1].sum_colful;
if(tr[R<<1].col!=-1 && tr[R<<1].col==tr[R<<1|1].col)
tr[R].col=tr[R<<1].col;
else
tr[R].col=-1;
}
void down(int R,int c,LL jia)
{
tr[R].sum_colful+=jia*tr[R].getlen();
tr[R].fg=c;
tr[R].col=c;
tr[R].ad+=jia;
}
void push_down(int R)
{
if(tr[R].fg!=-1)
{
down(R<<1,tr[R].fg,tr[R].ad);
down(R<<1|1,tr[R].fg,tr[R].ad);
tr[R].fg=-1;
tr[R].ad=0;
}
}
void build(int l,int r,int R)
{
tr[R].l=l;
tr[R].r=r;
tr[R].fg=-1;
tr[R].ad=0;
if(l==r)
{
tr[R].col=l;
tr[R].sum_colful=0;
return;
}
int mid=tr[R].mid();
build(l,mid,R<<1);
build(mid+1,r,R<<1|1);
push_up(R);
}
void update(int l,int r,int R,int c)
{
if(l<=tr[R].l && tr[R].r<=r)
{
if(tr[R].col!=-1)
{
down(R,c, abs(tr[R].col-c) );
return;
}
}
push_down(R);
int mid=tr[R].mid();
if(l<=mid)
update(l,r,R<<1,c);
if(r>=mid+1)
update(l,r,R<<1|1,c);
push_up(R);
}
void query(int l,int r,int R)
{
if(l<=tr[R].l && tr[R].r<=r)
{
ans+=tr[R].sum_colful;
return;
}
push_down(R);
int mid=tr[R].mid();
if(l<=mid)
query(l,r,R<<1);
if(r>=mid+1)
query(l,r,R<<1|1);
}
int main()
{
int n,m;
int op,l,r,x;
scanf("%d%d",&n,&m);
build(1,n,1);
while(m--)
{
scanf("%d",&op);
if(op==1)
{
scanf("%d%d%d",&l,&r,&x);
// puts("go to update");
update(l,r,1,x);
// puts("end of update");
// printf("sum==%I64d\n",tr[1].sum_colful);
}
else
{
scanf("%d%d",&l,&r);
ans=0;
query(l,r,1);
printf("%I64d\n",ans);
}
}
return 0;
}