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 ...
随机推荐
- Bumped!【最短路】(神坑
问题 B: Bumped! 时间限制: 1 Sec 内存限制: 128 MB 提交: 351 解决: 44 [提交] [状态] [命题人:admin] 题目描述 Peter returned fr ...
- Win32汇编学习(6):键盘输入消息
这次,我们将要学习WINDOWS程序是如何处理键盘消息的. 理论: 因为大多数的PC只有一个键盘,所以所有运行中的WINDOWS程序必须共用它.WINDOWS 将负责把击键消息送到具有输入焦点的那个应 ...
- 最新版的Chrome如何始终开启flash而不是先询问?
链接:https://www.zhihu.com/question/266170237/answer/342137190 设置Chrome启用Flash,修改配置之前先看Chrome的版本,不同版 ...
- CSS【05】:CSS三大特性
继承性 作用:给父元素设置一些属性,子元素也可以使用,这个我们就称之为继承性 示例代码: <style> div { color: red; } </style> <di ...
- Zedboard初体验
前言 这是我学习Zedboard时做的笔记 Zedboard是什么 Zedboard是Xilinx公司推出的搭载了Zynq芯片的开发板,其中Zynq芯片采用全新的设计理念,将ARM处理器嵌入FPGA可 ...
- 机器学习实战1-2.1 KNN改进约会网站的配对效果 datingTestSet2.txt 下载方法
今天读<机器学习实战>读到了使用k-临近算法改进约会网站的配对效果,道理我都懂,但是看到代码里面的数据样本集 datingTestSet2.txt 有点懵,这个样本集在哪里,只给了我一个文 ...
- html5画心
- Confluence 6 服务器硬件要求指南
服务器管理员可以通过本页面的指南来对在运行 Confluence 评估版本的最小服务器硬件需求进行评估.应为实际的服务器负载是很难进行预测的,所以最好的办法是通过实际运行一个 Confluence 实 ...
- 20165309 2017-2018-2《Java程序设计》课程总结
20165309 2017-2018-2<Java程序设计>课程总结 一.每周作业链接汇总 预备作业1:我期望的师生关系 预备作业2:技能学习经验与C语言 预备作业3:Linux安装及学习 ...
- EL函数库及用法
导入标签库: <%@ tablib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%&g ...