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:
- Paint all the units with numbers between l and r (both inclusive) with color x.
- Ask the sum of colorfulness of the units between l and r (both inclusive).
Can you help DZY?
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.
For each operation 2, print a line containing the answer — sum of colorfulness.
3 3 1 1 2 4 1 2 3 5 2 1 3
8
3 4 1 1 3 4 2 1 1 2 2 2 2 3 3
3 2 1
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
129
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;
}