CodeForces - 444C
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:
- 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.
Output
For each operation 2, print a line containing the answer — sum of colorfulness.
Examples
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
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的更多相关文章
- Codeforces 444C DZY Loves Colors(线段树)
题目大意:Codeforces 444C DZY Loves Colors 题目大意:两种操作,1是改动区间上l到r上面德值为x,2是询问l到r区间总的改动值. 解题思路:线段树模板题. #inclu ...
- CodeForces 444C 分块
题目链接:http://codeforces.com/problemset/problem/444/C 题意:给定一个长度为n的序列a[].起初a[i]=i,然后还有一个色度的序列b[],起初b[i] ...
- CodeForces 444C. DZY Loves Physics(枚举+水题)
转载请注明出处:http://blog.csdn.net/u012860063/article/details/37509207 题目链接:http://codeforces.com/contest/ ...
- DZY Loves Colors CodeForces - 444C (线段树势能分析)
大意:有$n$个格子, 初始$i$位置的颜色为$i$, 美丽值为0, 有两种操作 将区间$[l,r]$内的元素全部改为$x$, 每个元素的美丽值增加$|x-y|$, $y$为未改动时的值 询问区间$[ ...
- CodeForces 444C 线段树
想分块想了很久一点思路都没有,结果一看都是写的线段树= = ...完全忘记了还有线段树这种操作 题意:给一个数组,一种操作是改变l到r为c,还有一种操作是查询l到r的总和差 线段树记得+lazy标记 ...
- CodeForces 444C 节点更新求变化值的和
http://vjudge.net/problem/viewProblem.action?id=51622 题目大意: 给定一列n个数字,最初赋予值1到n 两个操作:1.将区间[l,r]内的数改为x, ...
- Codeforces 444C 线段树 懒惰标记
前天晚上的CF比赛div2的E题,很明显一个线段树,当时还在犹豫复杂度的问题,因为他是区间修改和区间查询,肯定是要用到懒惰标记. 然后昨天真的是给这道题跪了,写了好久好久,...我本来是写了个add标 ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
随机推荐
- Codeforces Round #495 (Div. 2) C. Sonya and Robots
http://codeforces.com/contest/1004/problem/C 题意: 在一行上有n个数字,现在在最左边和最右边各放置一个机器人,左右机器人各有一个数字p和q.现在这两个机器 ...
- 使用Rancher和私有仓库快速搭建Kubernetes集群
来来来,先出题:Rancher可以快速部署Kubernetes,但其使用的gcr.io上的镜像无法下载怎么办?使用Rancher可以快速部署Kubernetes,但我们需要下载哪些镜像?Rancher ...
- png转tif
发国外的文章要求图片是tif,cmyk色彩空间的. 大小尺寸还有要求. 比如 网上大神多,找到了一段代码,感谢! https://www.jianshu.com/p/ec2af4311f56 http ...
- acm
给定一组数字,一组有9个数字,将这9个数字填写到3*3的九宫格内:使得横,竖,斜对角一条线上的三个数字之和相等:如果无解则打印无解: #include <iostream> #includ ...
- Docker Image管理学习笔记,ZT
Docker Image管理学习笔记 http://blog.csdn.net/junjun16818/article/details/38423391
- CentOS卸载通过yum安装的软件
以erlang为例:rpa -qa|grep erlang 使用:yum -y remove erlang-* 扩展--查看yum安装软件的路径:rpm -ql erlang-cosFileTrans ...
- 2015-10-06 认识jQuery1
jQue ...
- Excel中针对IP地址的排序方法
新建一个辅助排序列,用辅助列来扩展,辅助列公式如下: =TRIM(TEXT(LEFT(SUBSTITUTE(A1,".",REPT(" ",99)),100), ...
- SpringCloud注册中心环境搭建euraka
- 【Jenkins】testng+testNgXslt+ant优化测试报告
步骤: 准备: testng-results.xsl saxon-8.7.jar 下载地址:http://download.csdn.net/download/a804229570/10210509 ...