CodeForces 444C 分块
题目链接:http://codeforces.com/problemset/problem/444/C
题意:给定一个长度为n的序列a[]。起初a[i]=i,然后还有一个色度的序列b[],起初b[i]=0。现在有2种操作:
1 l r x:区间染色,a[l],a[l+1]...a[r]变成x.同时b[l],b[l+1]...b[r]加上对应的|x-a[i]|值。
2 l r:统计区间和,统计区间b[l],b[l+1]...b[r]的和。
思路:线段树是比较常见的做法。考虑分块。每块维护一个lazy表示是否整块颜色都相同,Bsum表示块的b[i]总和.lazyb表示块的b[i]累加值的和。然后暴力维护即可。
#define _CRT_SECURE_NO_DEPRECATE
#include<stdio.h>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<queue>
#include<math.h>
#include<time.h>
#include<vector>
#include<iostream>
#include<map>
using namespace std;
typedef long long int LL;
const int MAXN = + ;
int belong[MAXN], block, num, L[MAXN], R[MAXN];
int n, q;
LL a[MAXN],b[MAXN];
struct Node{
LL lazy, Bsum,lazyb;
}Bval[];
void build(){
block = (int)sqrt(n + 0.5);
num = n / block; if (n%block){ num++; }
for (int i = ; i <= num; i++){
Bval[i].Bsum = ; Bval[i].lazy = ; Bval[i].lazyb=;
L[i] = (i - )*block + ; R[i] = i*block;
}
R[num] = n;
for (int i = ; i <= n; i++){
belong[i] = ((i - ) / block) + ;
}
}
void modify(int st, int ed,int val){
if (belong[st] == belong[ed]){
if(Bval[belong[st]].lazy){
for(int i=L[belong[st]];i<=R[belong[st]];i++){
a[i]=Bval[belong[st]].lazy;
}
Bval[belong[st]].lazy=;
}
for(int i=st;i<=ed;i++){
Bval[belong[st]].Bsum+=abs(a[i]-val);
b[i]+=abs(a[i]-val);
a[i]=val;
}
return;
}
if(Bval[belong[st]].lazy){
for(int i=L[belong[st]];i<=R[belong[st]];i++){
a[i]=Bval[belong[st]].lazy;
}
Bval[belong[st]].lazy=;
}
for (int i = st; i <= R[belong[st]]; i++){
Bval[belong[st]].Bsum+=abs(val-a[i]);
b[i]+=abs(val-a[i]);
a[i]=val;
}
for (int i = belong[st] + ; i < belong[ed]; i++){
if(Bval[i].lazy){
Bval[i].lazyb+=abs(val-Bval[i].lazy);
Bval[i].Bsum+=(1LL*abs(Bval[i].lazy-val)*(R[i]-L[i]+));
Bval[i].lazy=val;
}
else{
for(int j=L[i];j<=R[i];j++){
b[j]+=abs(a[j]-val);
Bval[i].Bsum+=abs(a[j]-val);
a[j]=val;
}
Bval[i].lazy=val;
}
}
if(Bval[belong[ed]].lazy){
for(int i=L[belong[ed]];i<=R[belong[ed]];i++){
a[i]=Bval[belong[ed]].lazy;
}
Bval[belong[ed]].lazy=;
}
for (int i = L[belong[ed]]; i <= ed; i++){
Bval[belong[ed]].Bsum+=abs(val-a[i]);
b[i]+=abs(val-a[i]);
a[i]=val;
}
}
LL query(int st, int ed){
LL ans = ;
if (belong[st] == belong[ed]){
for (int i = st; i <= ed; i++){
ans += (b[i]+Bval[belong[st]].lazyb);
}
return ans;
}
for (int i = st; i <= R[belong[st]]; i++){
ans += (b[i]+Bval[belong[st]].lazyb);
}
for (int i = belong[st] + ; i < belong[ed]; i++){
ans += Bval[i].Bsum;
}
for (int i = L[belong[ed]]; i <= ed; i++){
ans += (b[i]+Bval[belong[ed]].lazyb);
}
return ans;
}
int main(){
//#ifdef kirito
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
//#endif
// int start = clock();
while (~scanf("%d%d", &n,&q)){
for (int i = ; i <= n; i++){ a[i]=i; b[i]=;}
build();
int type, l, r, v;
for (int i = ; i <= q; i++){
scanf("%d", &type);
if (type == ){
scanf("%d%d%d", &l, &r,&v);
modify(l, r,v);
}
else{
scanf("%d%d", &l, &r);
printf("%lld\n", query(l, r));
}
}
}
//#ifdef LOCAL_TIME
// cout << "[Finished in " << clock() - start << " ms]" << endl;
//#endif
return ;
}
CodeForces 444C 分块的更多相关文章
- Codeforces 444C DZY Loves Colors(线段树)
题目大意:Codeforces 444C DZY Loves Colors 题目大意:两种操作,1是改动区间上l到r上面德值为x,2是询问l到r区间总的改动值. 解题思路:线段树模板题. #inclu ...
- CodeForces 455D 分块
题目链接:http://codeforces.com/problemset/problem/455/D 题意:给定一个长度为n的序列a[]. m次操作.共有两种操作 1 l r:将序列的a[l].a[ ...
- CodeForces 551E 分块
题目链接:http://codeforces.com/problemset/problem/551/E 题意:给定一个长度为N的序列. 有2个操作 1 l r v:序列第l项到第r项加v(区间加), ...
- CodeForces 103D 分块处理
题目链接:http://codeforces.com/problemset/problem/103/D 题意:给定一个长度为n的序列.然后q个询问.每个询问为(a,b),表示从序列第a项开始每b项的加 ...
- Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) Problem E (Codeforces 828E) - 分块
Everyone knows that DNA strands consist of nucleotides. There are four types of nucleotides: "A ...
- CodeForces 13E 分块
题目链接:http://codeforces.com/problemset/problem/13/E 题意:给定n个弹簧和每个弹簧初始的弹力a[].当球落在第i个位置.则球会被弹到i+a[i]的位置. ...
- Serega and Fun CodeForces - 455D (分块 或 splay)
大意:给定n元素序列, 2种操作 将区间$[l,r]$循环右移1位 询问$[l,r]$中有多少个等于k的元素 现在给定q个操作, 输出操作2的询问结果, 强制在线 思路1: 分块 每个块内维护一个链表 ...
- CodeForces 444C 线段树
想分块想了很久一点思路都没有,结果一看都是写的线段树= = ...完全忘记了还有线段树这种操作 题意:给一个数组,一种操作是改变l到r为c,还有一种操作是查询l到r的总和差 线段树记得+lazy标记 ...
- CodeForces 444C. DZY Loves Physics(枚举+水题)
转载请注明出处:http://blog.csdn.net/u012860063/article/details/37509207 题目链接:http://codeforces.com/contest/ ...
随机推荐
- miniui后台无法接收到input传值
出错原因:在miniui中,此处应写成<input textName="current_unit",在php中才可以使用$_POST['current_unit']获取到值, ...
- phpexcel导入数据部分数据有误
数据在excel中是这样的: 插入数据库后是这样的: 很难发现,出错的那几条数据中的单元格中都有英文','符号,而phpexcel又是以','来拼接读取到的数据的. 解决办法:修改代码中的','为不常 ...
- codevs 1036 商务旅行(Targin求LCA)
传送门 Description 某首都城市的商人要经常到各城镇去做生意,他们按自己的路线去做,目的是为了更好的节约时间. 假设有N个城镇,首都编号为1,商人从首都出发,其他各城镇之间都有道路连接,任意 ...
- 常见input输入框 点击 发光白色外阴影 focus
先看看具体实现的效果 第一就是点击input 实现的效果 默认谷歌点击input是蓝色边框 去掉用outline:0; 实现效果用focus 默认状态的边框颜色一般较重 如border:1px s ...
- [Kerberos] Kerberos 认证过程整理
Kerberos是一种安全认证协议,意在提供 more secure authentication simplified management of password convenience of s ...
- URL、URN、URI的区别?
URL.URN.URI区别 既然Web应用程序的文件等资源是放在服务器上,而服务器是因特网(Internet)上的主机,当然必须要有个方法,告诉浏览器到哪里取得文件等资源.通常会听到有人这么说:“你要 ...
- css 强制 中文、英文 换行
.livechat-text a { display: block; word-break:break-all; /* 英文换行 */ white-space:normal; /* 中文换行 */ } ...
- PHP大数(浮点数)取余
一般我们进行取余运算第一个想到的就是用百分号%,但当除数是个很大的数值,超出了int范围时,这样取余就不准确了. php大数(浮点数)取余函数 /** * php大数取余 * * @param int ...
- 初学者在ubuntu下安装使用git(上)
一 git的安装测试 在Ubuntu系统下的bash中输入git,如果提示没有安装的话,用命令 sudo apt-get install git 安装git,安装完成之后通过 git –versi ...
- PYTHON 函数的动态参数
#普通参数示例 def func(args): print(args) func(123456) #执行结果为123456 如果给多个参数,就会报错 #动态参数示例 def func(*args): ...