F - DZY Loves Colors

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.

 #include <cstdio>
#include <stack>
#include <cmath>
#include <queue>
#include <string>
#include <queue>
#include <cstring>
#include <iostream>
#include <algorithm> #define lid id<<1
#define rid id<<1|1
#define closein cin.tie(0)
#define scac(a) scanf("%c",&a)
#define scad(a) scanf("%d",&a)
//#define print(a) printf("%d\n",a)
#define debug printf("hello world")
#define form(i,n,m) for(int i=n;i<m;i++)
#define mfor(i,n,m) for(int i=n;i>m;i--)
#define nfor(i,n,m) for(int i=n;i>=m;i--)
#define forn(i,n,m) for(int i=n;i<=m;i++)
#define scadd(a,b) scanf("%d%d",&a,&b)
#define memset0(a) memset(a,0,sizeof(a))
#define scaddd(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define scadddd(a,b,c,d) scanf("%d%d%d%d",&a,&b,&c,&d) #define INF 0x3f3f3f3f
#define maxn 100005
typedef long long ll;
using namespace std;
//---------AC(^-^)AC---------\\ int n,m,block,num;
int blo[maxn],l[maxn],r[maxn];
ll color[maxn],sum[maxn],allsum[maxn],flag2[maxn],flag[maxn]; void print()
{
forn(i,,num)
{
printf("%lld %lld\n",allsum[i],flag2[i]);
}
debug;
printf("\n");
forn(i,,n)
printf("%lld %lld\n",color[i],sum[i]);
}
void push_down(int id)
{
forn(i,l[id],r[id]) color[i]=flag[id];
flag[id]=-;
}
void update(int ld,int rd,int add)
{
if(blo[ld]==blo[rd])
{
if(flag[blo[ld]]!=-) push_down(blo[ld]);
forn(i,ld,rd) {
sum[i]+=abs(color[i]-add);
allsum[blo[ld]]+=abs(color[i]-add);
color[i]=add;
}
}else {
if(flag[blo[ld]]!=-) push_down(blo[ld]);
forn(i,ld,r[blo[ld]]) {
sum[i]+=abs(color[i]-add);
allsum[blo[ld]]+=abs(color[i]-add);
color[i]=add;
}
forn(i,blo[ld]+,blo[rd]-) {
if(flag[i]!=-) {
allsum[i]+=abs(flag[i]-add)*(r[i]-l[i]+);
flag2[i]+=abs(flag[i]-add);
flag[i]=add;
}else {
forn(j,l[i],r[i]) {
sum[j]+=abs(color[j]-add);
allsum[i]+=abs(color[j]-add);
color[j]=add;
}
flag[i]=add;
}
}
if(flag[blo[rd]]!=-) push_down(blo[rd]);
forn(i,l[blo[rd]],rd) {
sum[i]+=abs(color[i]-add);
allsum[blo[rd]]+=abs(color[i]-add);
color[i]=add;
}
}
//print();
}
void query(int ld,int rd)
{
ll ans=;
if(blo[ld]==blo[rd]) {
forn(i,ld,rd) ans+=sum[i]+flag2[blo[i]];
}else {
forn(i,ld,r[blo[ld]]) {
ans+=sum[i]+flag2[blo[i]];
}
forn(i,blo[ld]+,blo[rd]-) {
ans+=allsum[i];
}
forn(i,l[blo[rd]],rd) {
ans+=sum[i]+flag2[blo[i]];
}
}
printf("%lld\n",ans);
} int main()
{
scadd(n,m);
block=sqrt(n);
num=(n-)/block+;
forn(i,,n) {
color[i]=i;
blo[i]=(i-)/block+;
}
forn(i,,num) {
l[i]=(i-)*block+;
r[i]=i*block;
}
r[num]=n;
forn(i,,num) flag[i]=-;
while(m--)
{
int op,l,r;
scaddd(op,l,r);
if(op==)
{
int x;
scad(x);
update(l,r,x);
}
else
{
query(l,r);
//print();
}
}
return ;
}

CodeForces - 444C的更多相关文章

  1. Codeforces 444C DZY Loves Colors(线段树)

    题目大意:Codeforces 444C DZY Loves Colors 题目大意:两种操作,1是改动区间上l到r上面德值为x,2是询问l到r区间总的改动值. 解题思路:线段树模板题. #inclu ...

  2. CodeForces 444C 分块

    题目链接:http://codeforces.com/problemset/problem/444/C 题意:给定一个长度为n的序列a[].起初a[i]=i,然后还有一个色度的序列b[],起初b[i] ...

  3. CodeForces 444C. DZY Loves Physics(枚举+水题)

    转载请注明出处:http://blog.csdn.net/u012860063/article/details/37509207 题目链接:http://codeforces.com/contest/ ...

  4. DZY Loves Colors CodeForces - 444C (线段树势能分析)

    大意:有$n$个格子, 初始$i$位置的颜色为$i$, 美丽值为0, 有两种操作 将区间$[l,r]$内的元素全部改为$x$, 每个元素的美丽值增加$|x-y|$, $y$为未改动时的值 询问区间$[ ...

  5. CodeForces 444C 线段树

    想分块想了很久一点思路都没有,结果一看都是写的线段树= = ...完全忘记了还有线段树这种操作 题意:给一个数组,一种操作是改变l到r为c,还有一种操作是查询l到r的总和差 线段树记得+lazy标记 ...

  6. CodeForces 444C 节点更新求变化值的和

    http://vjudge.net/problem/viewProblem.action?id=51622 题目大意: 给定一列n个数字,最初赋予值1到n 两个操作:1.将区间[l,r]内的数改为x, ...

  7. Codeforces 444C 线段树 懒惰标记

    前天晚上的CF比赛div2的E题,很明显一个线段树,当时还在犹豫复杂度的问题,因为他是区间修改和区间查询,肯定是要用到懒惰标记. 然后昨天真的是给这道题跪了,写了好久好久,...我本来是写了个add标 ...

  8. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  9. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

随机推荐

  1. 【Git】git error记录之 "unpacking the sent packfile failed on the remote"

    错误信息: error: cannot open .git/FETCH_HEAD: Permission denied unpacking the sent packfile failed on th ...

  2. Linux md5sum 的用法

    MD5 算法常常被用来验证网络文件传输的完整性,防止文件被篡改.MD5 全称是报文摘要算法,此算法对任意长度 的信息逐位计算,产生一个二进制长度为 128 位(十六进制长度 32 位)的报文摘要,不同 ...

  3. hdu 6010 Daylight Saving Time 泰勒公式

    Daylight Saving Time Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  4. 使用Rancher和私有仓库快速搭建Kubernetes集群

    来来来,先出题:Rancher可以快速部署Kubernetes,但其使用的gcr.io上的镜像无法下载怎么办?使用Rancher可以快速部署Kubernetes,但我们需要下载哪些镜像?Rancher ...

  5. 简单粗暴的上传项目至 Github

    嗯,写了一个项目,觉得还OK,就想放在 Github 继续维护和方便使用,那么如何简单快速的将代码上传至 Github 上? 1.  你得有自己的 Github账号,如何创建账号这里就不说了.因为.. ...

  6. 几种优化方法的整理(SGD,Adagrad,Adadelta,Adam)

    参考自: https://zhuanlan.zhihu.com/p/22252270 常见的优化方法有如下几种:SGD,Adagrad,Adadelta,Adam,Adamax,Nadam 1. SG ...

  7. Kayleigh O'Connor - I Won't Be There

    Do you feel like you're about to drown The wave is rushing over you throw you onto now I remember th ...

  8. 不正常退出vim产生swp文件的解决方法

    vi -r README.txt恢复文件,这样上次意外退出没有保存的修改,就会覆盖文件. vi -r查看生成的交换文件 rm .README.txt.swp删除交换文件 搞定!下次打开文件就可以正常编 ...

  9. 『流畅的Python』第12章:继承的优缺点

  10. react-thunk的使用流程

    react-thunk作用:使我们可以在action中返回函数,而不是只能返回一个对象.然后我们可以在函数中做很多事情,比如发送异步的ajax请求. 这就是react-thunk的使用方法.接受一个d ...