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. P1357 花园

    洛咕原题 题解 状压dp+矩乘 首先看到题目说M<=5,这么小的数据明显可以用状压保存相邻状态,于是可以得到一个80分的dp: 先筛出所有可用的状态,然后建立一个矩阵保存可转移的状态,再然后把每 ...

  2. win7安装mysql-8.0.13-winx64

    这里展示一下,由于需要安装一个版本测试一下数据,其实就是超简单的啦. 下包 注:https://dev.mysql.com/downloads/mysql/ 解压与配置 [mysqld] basedi ...

  3. answerOpenCV轮廓类问题解析

    contour在opencv中是一个基础的数据结构,灵活运用的话,作用很大.以contour为关键字,在answerOpenCV中能够发现很多有趣的东西. 1.无法解决的问题 http://answe ...

  4. ODAC(V9.5.15) 学习笔记(三)TOraSession(3)

    3. 选项 TOraSession的Options有如下内容 名称 类型 说明 CharLength TCharLength 单个字符的长度,缺省0,表示从服务器获取对应的字符集中单个字符长度 Cha ...

  5. uniGUI试用笔记(六)

    uniGUI提供了一个文件上传控件TUniFileUpload,进行数据的导入就变得比较容易.首先将TUniFileUpload控件放置在窗体上,按下导入按钮后,执行TUniFileUpload的文件 ...

  6. uniGUI试用笔记(四)

    uniGUI下有专用的登录窗体类:TUniLoginForm,该类属于AppForm,构建代码为: function frmWebLogin: TfrmWebLogin; begin Result : ...

  7. word2010使用记录

    安装时提示: installer无法更新被保护的一个或多个文件, 解决方法是: 将三个dll文件,放在 c:\program files\common files\ms shared\web serv ...

  8. 牛客竞赛&&mjt的毒瘤赛

    题目链接 https://ac.nowcoder.com/acm/contest/368/F 思路 询问可以离线. 然后每个节点上建32个权值线段树(权值不大,其实只要20颗) 记录每一位权值为x(如 ...

  9. Windows FindFirstFile利用

    目前大多数程序都会对上传的文件名加入时间戳等字符再进行MD5,然后下载文件的时候通过保存在数据库里的文件ID读取文件路径,一样也实现了文件下载,这样我们就无法直接得到我们上传的webshell文件路径 ...

  10. Basic Mathematics You Should Mastered

    Basic Mathematics You Should Mastered 2017-08-17  21:22:40  1. Statistical distance  In statistics,  ...