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 ...
随机推荐
- 框架frame
使用框架切分网页 效果: 代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"& ...
- postman 带变量参数使用方法
设置代码组变量后就可以在这个组内所有接口使用这些变量了,代码用的是js语法: 代码编写的快捷方法在这里: 然后就可以在接口中使用这些变量了,当然也可以直接在接口中写这些变量
- PHP 将大量数据导出到 Excel 的方法
原文链接:https://blog.csdn.net/bkcnl/article/details/76152505 public function getxiajiueser(){ $id = req ...
- Windows Media Player添加播放插件
- UVa 11019 Matrix Matcher - Hash
题目传送门 快速的vjudge传送门 快速的UVa传送门 题目大意 给定两个矩阵S和T,问T在S中出现了多少次. 不会AC自动机做法. 考虑一维的字符串Hash怎么做. 对于一个长度为$l$的字符串$ ...
- 【MonkeyRunner环境搭建】
一.配置MonkeyRunner环境变量 1.首先下载一个AndroidSDK,在SDK的目录中的tools文件夹中,直接带有MonkeyRunner 2.打开MonkeyRunner的方式: |-- ...
- Item的anchors属性
1.anchors group: anchors.top : AnchorLine anchors.bottom : AnchorLine anchors.left : AnchorLine anch ...
- linux --- 3 vim 网络 用户 权限 软连接 压缩 定时任务 yum源
一.vi 和vim vi 是老式的字处理器,不过功能已经很齐全了,但是还是有可以进步的地方. vim 则可以说是程序开发者的一项很好用的工具 ①命令模式 移动光标 w(e) 移动光标到下一个单词 b ...
- 2.Android硬件访问服务编写系统代码【转】
本文转载自:https://blog.csdn.net/qq_33443989/article/details/76696772 版权声明:本文为博主(Tower)自学笔记,欢迎转载! :-) ...
- 虚拟机极简配置manjaro gnome
一.安装gnome启动到桌面后,卸载所有不必要的软件,减少系统大小 打开[增加/删除软件],找到要删除的软件卸载 ms-office ---微软在线办公软件 Thunderbird ---邮箱 kon ...