Codeforces444C DZY Loves Colors(线段树)
题目
Source
http://codeforces.com/problemset/problem/444/C
Description
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?
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.
Sample Input
3 3
1 1 2 4
1 2 3 5
2 1 3
3 4
1 1 3 4
2 1 1
2 2 2
2 3 3
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
Sample Output
8
3
2
1
129
分析
有点不明觉厉。。
http://blog.csdn.net/kyleyoung_ymj/article/details/51768532
代码
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define MAXN 111111
int col[MAXN<<2],col_tag[MAXN<<2];
long long sum[MAXN<<2],tag[MAXN<<2];
int N,x,y,z;
void update(int i,int j,int k){
if(x<=i && j<=y){
if(col[k]){
tag[k]+=abs(col[k]-z);
sum[k]+=(j-i+1LL)*abs(col[k]-z);
col[k]=z;
col_tag[k]=z;
return;
}
}
int mid=i+j>>1;
if(tag[k]){
tag[k<<1]+=tag[k];
sum[k<<1]+=(mid-i+1LL)*tag[k];
tag[k<<1|1]+=tag[k];
sum[k<<1|1]+=(j-mid)*tag[k];
tag[k]=0;
}
if(col_tag[k]){
col[k<<1]=col[k<<1|1]=col_tag[k<<1]=col_tag[k<<1|1]=col_tag[k];
col_tag[k]=0;
}
if(x<=mid) update(i,mid,k<<1);
if(y>mid) update(mid+1,j,k<<1|1);
sum[k]=sum[k<<1]+sum[k<<1|1];
if(col[k<<1] && col[k<<1]==col[k<<1|1]) col[k]=col[k<<1];
else col[k]=0;
}
long long query(int i,int j,int k){
if(x<=i && j<=y){
return sum[k];
}
int mid=i+j>>1;
if(tag[k]){
tag[k<<1]+=tag[k];
sum[k<<1]+=(mid-i+1LL)*tag[k];
tag[k<<1|1]+=tag[k];
sum[k<<1|1]+=(j-mid)*tag[k];
tag[k]=0;
}
if(col_tag[k]){
col[k<<1]=col[k<<1|1]=col_tag[k<<1]=col_tag[k<<1|1]=col_tag[k];
col_tag[k]=0;
}
long long ret=0;
if(x<=mid) ret+=query(i,mid,k<<1);
if(y>mid) ret+=query(mid+1,j,k<<1|1);
return ret;
}
void init(int i,int j,int k){
if(i==j){
col[k]=x;
return;
}
int mid=i+j>>1;
if(x<=mid) init(i,mid,k<<1);
else init(mid+1,j,k<<1|1);
}
int main(){
int n,m;
scanf("%d%d",&n,&m);
for(N=1; N<n; N<<=1);
for(x=1; x<=n; ++x) init(1,N,1);
int a;
while(m--){
scanf("%d",&a);
if(a==1){
scanf("%d%d%d",&x,&y,&z);
update(1,N,1);
}else{
scanf("%d%d",&x,&y);
printf("%lld\n",query(1,N,1));
}
}
return 0;
}

浙公网安备 33010602011771号