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/ ...
随机推荐
- php实现转换html格式为文本格式的方法
有时候需要转换html格式的字符串为文本,但又需要保持一定的格式,比如要求段落变成的分段格式就可以用下面这个函数 function html2text($str){ $str = preg_repl ...
- 我的SqlHelper类!
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...
- git版本控制管理实践-2
给网站设置一个 "根目录下的logo.ico", 还是很有必要的,比如赶集网,这时在 "历史"搜索时, 就可以根据 网站的 logo.ico 很轻松的就能够找到 ...
- VTK初学一,vtkDelaunay2D创建球冠曲面
#ifndef INITIAL_OPENGL #define INITIAL_OPENGL #include <vtkAutoInit.h> VTK_MODULE_INIT(vtkRend ...
- tyvj1098 任务安排
描述 N个任务排成一个序列在一台机器上等待完成(顺序不得改变),这N个任务被分成若干批,每批包含相邻的若干任务.从时刻0开始,这些任务被分批加工,第i个任务单独完成所需的时间是Ti.在每批任务开始前, ...
- [Recommendation System] 推荐系统之协同过滤(CF)算法详解和实现
1 集体智慧和协同过滤 1.1 什么是集体智慧(社会计算)? 集体智慧 (Collective Intelligence) 并不是 Web2.0 时代特有的,只是在 Web2.0 时代,大家在 Web ...
- Mac Pro 资源管理器 Double Commander“个性化设置” 备份
操作系统自带的资源管理器,总是有些别扭的地方,在 Windows 系统下,我一般用 Total Commander(破解版)来作为替代品.现在换为 Mac 系统,自带的 Finer 也不怎么好用,连最 ...
- 解决宿主机不能访问虚拟机CentOS中的站点 | 更新CentOS防火墙设置开启80端口访问
前阵子在虚拟机上装好了centos6.0,并配好了nginx+php+mysql,但是本机就是无法访问.一直就没去折腾了. 具体情况如下 1.本机能ping通虚拟机 2.虚拟机也能ping通本机 3. ...
- C和指针 第十二章 使用结构和指针
链表是一种常用的数据结构,每个节点通过链或者指针链接在一起,程序通过间接指针访问链表中的节点. typedef struct Node { //指向下一个节点的指针 struct Node *next ...
- php之获取程序源码的方法
文件hello.php和index.php在同一目录 hello.php <?php class hello{ public function __construct() { echo 'hel ...