Codeforces 444 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的更多相关文章
- Codeforces 444 C. DZY Loves Colors (线段树+剪枝)
题目链接:http://codeforces.com/contest/444/problem/C 给定一个长度为n的序列,初始时ai=i,vali=0(1≤i≤n).有两种操作: 将区间[L,R]的值 ...
- codeforces 444 C. DZY Loves Colors(线段树)
题目大意: 1 l r x操作 讲 [l,r]上的节点涂成x颜色,而且每一个节点的值都加上 |y-x| y为涂之前的颜色 2 l r 操作,求出[l,r]上的和. 思路分析: 假设一个区间为同样的颜 ...
- Codeforces Round #254 DZY Loves Colors
题意:输入n, m ; 有n给位置, 初始时第i个位置的color为i, colorfulness为0. 有m次操作,一种是把成段的区域color更新为x, 对于更新的区域,每个位置(令第i ...
- 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 ...
- Codeforces Round #254 (Div. 1) C. DZY Loves Colors 分块
C. DZY Loves Colors 题目连接: http://codeforces.com/contest/444/problem/C Description DZY loves colors, ...
- Codeforces 444C DZY Loves Colors(线段树)
题目大意:Codeforces 444C DZY Loves Colors 题目大意:两种操作,1是改动区间上l到r上面德值为x,2是询问l到r区间总的改动值. 解题思路:线段树模板题. #inclu ...
- CodeForces 445E DZY Loves Colors
DZY Loves Colors Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces ...
- Codeforces444C DZY Loves Colors(线段树)
题目 Source http://codeforces.com/problemset/problem/444/C Description DZY loves colors, and he enjoys ...
- CF444C. DZY Loves Colors[线段树 区间]
C. DZY Loves Colors time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
随机推荐
- 安装python3.7和PyCharm专业版
安装python3.7 安装PyCharm专业版 1.下载地址http://www.jetbrains.com/pycharm/download/,选择windows下面的Professional,点 ...
- HihoCoder 1236 Scores - bitset - 分块
Kyle is a student of Programming Monkey Elementary School. Just as others, he is deeply concerned wi ...
- Python3 tkinter基础 Frame bind 捕捉多键同时按
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- Nginx 配置 Jenkins 反向代理
安装 Nginx 参考之前的一篇文章 Nginx 安装配置 安装 Jenkins 参考之前的一篇文章 Linux 搭建 Jenkins Nginx 配置 Jenkins 的反向代理 # /etc/ng ...
- linux内核中的hisi_sas是什么?
答: 是一个HISILICON SAS 控制器驱动(HISILICON SAS controller driver)
- P4097 [HEOI2013]Segment(李超树)
链接 https://www.luogu.org/problemnew/show/P4097 https://www.lydsy.com/JudgeOnline/problem.php?id=3165 ...
- 【做题】spoj4060 A game with probability——dp
赛前做题时忽然发现自己概率博弈类dp很弱,心好慌.(获胜概率或最优解期望) 于是就做了这道题,续了特别久. 一开始列dp式子的时候就花了很长时间,首先搞错了两次,然后忘记了根据上一轮dp值直接确定选什 ...
- (转)MongoDB学习
(二期)25.分布式文件存储数据库MongoDB [课程25]mongod...命令.xmind96.9KB [课程25]MongoD...概念.xmind0.5MB [课程25]MongoDB简介. ...
- C#DataTable 使用GroupBy方法的lamda 表达式和Linq语句写法
https://www.cnblogs.com/johnblogs/p/6006867.html DataTable ds = new DataTable(); //1.lamda 表达式写法(推荐) ...
- 【ASP.Net】 http请求中get,put,post,delete的区别与使用总结
在web api的设计上, 需要设计这个每个action对应的资源的请求方法是什么. Get方法是对服务器资源的请求获取, 一般get方法的参数都放在URL当中的. 所以通常情况下这种请求方式都是不安 ...