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 ...
随机推荐
- 【题解】Luogu P1204 [USACO1.2]挤牛奶Milking Cows
原题传送门:P1204 [USACO1.2]挤牛奶Milking Cows 实际是道很弱智的题目qaq 但窝还是觉得用珂朵莉树写会++rp(窝都初二了,还要考pj) 前置芝士:珂朵莉树 窝博客里对珂朵 ...
- 详解重定向(HTTP状态码301/302/303/307/408)附例子
本文为原创文章,转载请注明出处. 今天打算好好把状态码301.302.303.307.308好好撸一遍,并会测试下一些例子. 状态码的解释 我们都知道重定向与这几种状态码有关,来看下这几种HTTP状态 ...
- 牛客网校招全国统一模拟笔试(二月场)- Java方向
1.请问下面关于与wait()一起使用的方法notify()的各种陈述,哪个正确? A 多个线程同时等待某个条件,则只有等待时间最长的哪个线程被通知 B 多个线程同时等待某条件,则没有办法预测哪个线程 ...
- 封装QML能访问的类
一.常用宏 1.信号与槽 C++类中的信号与槽都可以在QML中访问 2.C++类的成员函数,Q_INVOKABLE Q_INVOKABLE void function(); 3.C++类的枚举,Q_E ...
- 使用JBarcode生成一维码
需要的jar包,只有jbarcode.jar 链接: https://pan.baidu.com/s/1o9oDPB8 密码: x367 public class Main { //设置条形码高度 p ...
- Spring 学习——Bean容器
Bean容器初始化 基础 org.springframework.beans org.springframework.context BeanFactory提供配置结构和基本功能,加载并初始化Bean ...
- linux内核中的dquot是什么?
答:这个与磁盘配额管理(disk quota)有关,内核配置选项为CONFIG_QUOTA,使能此选项意味着可以设置每个用户的硬盘使用限制.
- hihoCoder week19 RMQ问题再临-线段树 单点更新 区间查询
单点更新 区间查询 #include <bits/stdc++.h> using namespace std; #define m ((l+r)/2) #define ls (rt< ...
- vs 附加进程 iis进程显示
- BZOJ5018: [Snoi2017]英雄联盟
Description 正在上大学的小皮球热爱英雄联盟这款游戏,而且打的很菜,被网友们戏称为「小学生」.现在,小皮球终于受不 了网友们的嘲讽,决定变强了,他变强的方法就是:买皮肤!小皮球只会玩N个英雄 ...