C - DZY Loves Colors

思路:

分块,复杂度有点玄学,和普通分块不同的是在这个块被一次染色的时候暴力染整个块。

代码:

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize(4)
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pi acos(-1.0)
#define LL long long
//#define mp make_pair
#define pb push_back
#define ls rt<<1, l, m
#define rs rt<<1|1, m+1, r
#define ULL unsigned LL
#define pll pair<LL, LL>
#define pii pair<int, int>
#define piii pair<pii, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout);
//head const int N = 1e5 + , M = ;
int a[N], lazy[M], bl[N], blo, n;
LL v[N], bv[M], sm[M];
void push_down(int x) {
for (int i = (x-)*blo + ; i <= min(x*blo, n); i++) a[i] = lazy[x];
lazy[x] = ;
}
void first_color(int x, int value) {
for (int i = (x-)*blo + ; i <= min(x*blo, n); i++) v[i] += abs(value - a[i]), sm[x] += abs(value - a[i]), a[i] = value;
}
void update(int l, int r, int x) {
if(bl[l] == bl[r]) {
if(lazy[bl[l]]) push_down(bl[l]);
for (int i = l; i <= r; i++) v[i] += abs(a[i] - x), sm[bl[i]] += abs(a[i] - x), a[i] = x;
return ;
}
else {
if(lazy[bl[l]]) push_down(bl[l]);
for (int i = l; i <= bl[l]*blo; i++) {
sm[bl[i]] += abs(a[i] - x);
v[i] += abs(a[i] - x);
a[i] = x;
} for (int i = bl[l]+; i <= bl[r]-; i++) {
if(lazy[i]) {
bv[i] += abs(x - lazy[i]);
sm[i] += 1LL * blo * abs(x - lazy[i]);
lazy[i] = x;
}
else {
first_color(i, x);
lazy[i] = x;
}
} if(lazy[bl[r]]) push_down(bl[r]);
for (int i = (bl[r]-)*blo + ; i <= r; i++) {
sm[bl[i]] += abs(a[i] - x);
v[i] += abs(a[i] - x);
a[i] = x;
}
}
}
LL query(int l, int r) {
LL ans = ;
if(bl[l] == bl[r]){
for (int i = l; i <= r; i++) {
ans += v[i] + bv[bl[i]];
}
return ans;
}
for (int i = l; i <= bl[l]*blo; i++) {
ans += v[i] + bv[bl[i]];
}
for (int i = bl[l]+; i <= bl[r]-; i++) {
ans += sm[i];
}
for (int i = (bl[r]-)*blo+; i <= r; i++) {
ans += v[i] + bv[bl[i]];
}
return ans;
}
int main() {
int m, ty, l, r, x;
scanf("%d %d", &n, &m);
blo = sqrt(n);
for (int i = ; i <= n; i++) {
bl[i] = (i-)/blo + ;
a[i] = i;
v[i] = ;
}
while(m--) {
scanf("%d", &ty);
if(ty == ) {
scanf("%d %d %d", &l, &r, &x);
update(l, r, x);
}
else {
scanf("%d %d", &l, &r);
printf("%lld\n", query(l, r));
}
}
return ;
}

Codeforces 444 C - DZY Loves Colors的更多相关文章

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

    题目链接:http://codeforces.com/contest/444/problem/C 给定一个长度为n的序列,初始时ai=i,vali=0(1≤i≤n).有两种操作: 将区间[L,R]的值 ...

  2. codeforces 444 C. DZY Loves Colors(线段树)

    题目大意: 1 l r x操作 讲 [l,r]上的节点涂成x颜色,而且每一个节点的值都加上 |y-x| y为涂之前的颜色 2 l r  操作,求出[l,r]上的和. 思路分析: 假设一个区间为同样的颜 ...

  3. Codeforces Round #254 DZY Loves Colors

    题意:输入n, m ; 有n给位置, 初始时第i个位置的color为i, colorfulness为0.      有m次操作,一种是把成段的区域color更新为x, 对于更新的区域,每个位置(令第i ...

  4. Codeforces Round #254 (Div. 1) C. DZY Loves Colors 线段树

    题目链接: http://codeforces.com/problemset/problem/444/C J. DZY Loves Colors time limit per test:2 secon ...

  5. Codeforces Round #254 (Div. 1) C. DZY Loves Colors 分块

    C. DZY Loves Colors 题目连接: http://codeforces.com/contest/444/problem/C Description DZY loves colors, ...

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

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

  7. CodeForces 445E DZY Loves Colors

    DZY Loves Colors Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces ...

  8. Codeforces444C DZY Loves Colors(线段树)

    题目 Source http://codeforces.com/problemset/problem/444/C Description DZY loves colors, and he enjoys ...

  9. CF444C. DZY Loves Colors[线段树 区间]

    C. DZY Loves Colors time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

随机推荐

  1. VMware Workstation Pro14安装

    1. 下载VMware Workstation Pro14,注意,这个链接支持win7 64及以上系统 2.  点击进入安装 3. 接受许可协议 4. 选择安装目录,是否选择增强型键盘驱动程序 5. ...

  2. 利用JQuery Mobile开发web app

    什么是web app web app 是基于web的应用程序,是针对移动设备优化后的web站点,它具有 开发成本低——采用web开发技术,不需要考虑跨平台以及底层适配问题: 升级简单——不需要通知用户 ...

  3. openstack cloud init set password

    设置代理和password #!/bin/bash cd /home/ubuntu wget otcloud-gateway.bj.intel.com/script.tar.gz ]; then cu ...

  4. 批量telnet端口通不通

    每读一行就运行一次telnet IP port #!/bin/bash port="22" cat *.txt | while read line do telnet ${line ...

  5. kali meterpreter中mimikatz模块获取密码

    kali这方面不说了, meterpreter也略过, 做个关于mimikatz的笔记. mimikatz模块, 能获取对方机器的密码(包括哈希和明文). 渗透模块怎么进的也不说了, 方式太多, 我用 ...

  6. RPMB分区介绍【转】

    本文转载自:https://blog.csdn.net/xiezhi123456/article/details/81479793 RPMB(Replay Protected Memory Block ...

  7. tp剩余未验证内容-2

    如何设置一个 "资源" (文件/图片/zip/视频等)在点击时, 自动开始下载? 通常只要在这些地方, 设置 一个链接a, 让href等于这个资源就行了. 这样当点击这个资源时, ...

  8. [nginx] - 使用nginx实现反向代理,动静分离,负载均衡,session共享

    反向代理概念 先说正向代理,比如要访问youtube,但是不能直接访问,只能先找个FQ软件,通过FQ软件才能访问youtube. FQ软件就叫做正向代理.所谓的反向代理,指的是用户要访问youtube ...

  9. Google advertiser api开发概述

    对象.方法和服务 AdWords API 主要供 AdWords 的高级用户使用.如果您是 AdWords 新手,或需要复习 AdWords 基本概念,请查看 AdWords 基础知识页面. 对象层级 ...

  10. tomcat启动出现Preparing launch delegate,一直卡在100%

    本地启动项目时,Tomcat一直停留在, Starting Tomcat V8.0 Server at localhost   Preparing launch delegate...    百度可得 ...