chef and churu 分块 好题
题目大意
有一个长度为n的数组A
有n个函数,第i个函数 $$f(l[i],r[i])=\sum_{k=l[i]}^{r[i]}A_k$$
有两种操作:
1)修改A[i]
2)询问第x-y个函数值的和。
数据范围:n<=100000
分析1
考虑询问时x=y的情况
如何用尽可能快的速度回答询问?
维护\(sum1[i]\)表示前i块的前缀和
维护\(sum2[i][j]\)表示第i块中的前j个数的前缀和
修改时暴力维护\(sum2\),接着暴力维护\(sum1\)
复杂度\(O(2*\sqrt n)\)
询问就可以\(O(1)\)了
分析2
如何结局区间函数询问呢
我们对函数也分块
维护\(all[i]\)表示第i块函数的值
维护\(cov[i][j]\)表示第\(i\)块中,有多少个函数包含\(A[j]\)
对于\(cov\)数组,它是不会改变的
预处理时对于每块扫一次
用差分+前缀和的方法做到\(O(n\sqrt n)\)
对于\(all\)
每次修改时扫一下\(\sqrt n\)个块,用\(cov\)数组看下修改的那个单点对这个块的\(all\)的影响
注意
别再把BL,BR写成x,y了好嘛?
solution
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <algorithm>
using namespace std;
const int M=100007;
const int N=320;
typedef unsigned long long LL;
inline int rd(){
int x=0;bool f=1;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=0;
for(;isdigit(c);c=getchar()) x=x*10+c-48;
return f?x:-x;
}
int n,m,sn,MX;
LL val[M];
struct node{int l,r;}q[M];
LL sum1[N];
LL sum2[N][N];
LL cov[N][M];
LL all[N];
int loc(int x){
return x/sn+1;
}
int getL(int x){
return max((x-1)*sn,1);
}
int getR(int x){
return min(x*sn-1,n);
}
int getP(int x){
int L=getL(loc(x));
return x-L+1;
}
void init_sum(int x){
int i,L=getL(x),R=getR(x);
LL nw=0;
for(i=L;i<=R;i++){
nw+=val[i];
sum2[x][getP(i)]=nw;
}
sum2[x][sn]=nw;//这样方便
for(int i=x;i<=MX;i++) sum1[i]=sum1[i-1]+sum2[i][sn];
}
LL get_sum(int x,int y){
int L,R,BL,BR;
BL=loc(x); BR=loc(y);
if(BL+1>=BR){
if(BL==BR) return sum2[BL][getP(y)]-sum2[BL][getP(x)-1];
return sum2[BR][getP(y)]-sum2[BL][getP(x)-1]+sum2[BL][sn];
}
else{
if(getL(BL)!=x) BL++;
if(getR(BR)!=y) BR--;
L=getL(BL); R=getR(BR);
LL res=sum1[BR]-sum1[BL-1];
if(R!=y) res+=sum2[BR+1][getP(y)];
if(L!=x) res+=sum2[BL-1][sn]-sum2[BL-1][getP(x)-1];
return res;
}
}
void init_cov(int x){
int L=getL(x),R=getR(x);
for(int i=L;i<=R;i++){
cov[x][q[i].l]++;
cov[x][q[i].r+1]--;
all[x]+=get_sum(q[i].l,q[i].r);
}
for(int i=1;i<=n;i++){
cov[x][i]+=cov[x][i-1];
}
}
int main(){
int i,kd,x,y,BL,BR,L,R;
n=rd();
sn=(int)sqrt(n);
MX=loc(n);
for(i=1;i<=n;i++) val[i]=rd();
for(i=1;i<=n;i++) q[i].l=rd(),q[i].r=rd();
for(i=1;i<=MX;i++) init_sum(i);
for(i=1;i<=MX;i++)
init_cov(i);
m=rd();
while(m--){
kd=rd();
x=rd(),y=rd();
if(kd==1){
y-=val[x];
val[x]+=y;
init_sum(loc(x));
for(i=1;i<=MX;i++) all[i]+=cov[i][x]*y;
}
else{
LL ans=0;
BL=loc(x); BR=loc(y);
if(BL+1>=BR){
for(i=x;i<=y;i++) ans+=get_sum(q[i].l,q[i].r);
}
else{
if(getL(BL)!=x) BL++;
if(getR(BR)!=y) BR--;
L=getL(BL); R=getR(BR);
for(i=BL;i<=BR;i++) ans+=all[i];
for(i=x;i<L;i++) ans+=get_sum(q[i].l,q[i].r);
for(i=y;i>R;i--) ans+=get_sum(q[i].l,q[i].r);
}
printf("%llu\n",ans);
}
}
return 0;
}
chef and churu 分块 好题的更多相关文章
- CodeChef Chef and Churu [分块]
题意: 单点修改$a$ 询问$a$的区间和$f$的区间和 原来普通计算机是这道题改编的吧... 对$f$分块,预处理$c[i][j]$为块i中$a_j$出现几次,$O(NH(N))$,只要每个块差分加 ...
- 【Codechef-Hard】Chef and Churu 分块
题目链接: https://www.codechef.com/problems/FNCS Solution 大力分块.. 对序列分块,维护块内前缀和.块的前缀和,修改时暴力维护两个前缀和,询问单点答案 ...
- [CC-FNCS]Chef and Churu
[CC-FNCS]Chef and Churu 题目大意: 一个长度为\(n(n\le10^5)\)的数列\(A_{1\sim n}\),另有\(n\)个函数,第\(i\)个函数会返回数组中标号在\( ...
- Codechef FNCS Chef and Churu
Disciption Chef has recently learnt Function and Addition. He is too exited to teach this to his fri ...
- BZOJ 2724 蒲公英 | 分块模板题
题意 给出一个序列,在线询问区间众数.如果众数有多个,输出最小的那个. 题解 这是一道分块模板题. 一个询问的区间的众数,可能是中间"整块"区间的众数,也可能是左右两侧零散的数中的 ...
- Luogu 2801 教主的魔法 | 分块模板题
Luogu 2801 教主的魔法 | 分块模板题 我犯的错误: 有一处l打成了1,还看不出来-- 缩小块大小De完bug后忘了把块大小改回去就提交--还以为自己一定能A了-- #include < ...
- hzwer分块九题(暂时持续更新)
hzwer分块9题 分块1:区间加法,单点查询 Code #include<bits/stdc++.h> #define in(i) (i=read()) using namespace ...
- CodeChef - FNCS Chef and Churu(分块)
https://vjudge.net/problem/CodeChef-FNCS 题意: 思路: 用分块的方法,对每个函数进行分块,计算出该分块里每个数的个数,这样的话也就能很方便的计算出这个分块里所 ...
- 【分块+树状数组】codechef November Challenge 2014 .Chef and Churu
https://www.codechef.com/problems/FNCS [题意] [思路] 把n个函数分成√n块,预处理出每块中各个点(n个)被块中函数(√n个)覆盖的次数 查询时求前缀和,对于 ...
随机推荐
- Nodejs:npm run build之后,dist\index.html页面在火狐中可以正常显示登录页面并登录成功,在Chrome中可以正常显示登录页面,登录失败
问题描述:Nodejs:npm run build之后,dist\index.html页面在火狐中可以正常显示登录页面并登录成功,在Chrome中可以正常显示登录页面,登录失败 解决方法:将打包后的d ...
- Java基础操作面试题:Map集合排序 需要TreeMap 构造方法参数有比较器 输入字符串,统计A、B、C、D、出现次数,由高到低输出字母和出现次数,使用Map集合完成此题
Map和Collections是同级别的,不能像List排序那样直接用Collections.sort(new Comparator<?>(){ 复写compara方法}); HashMa ...
- 更改BootStrap popover的默认样式
.popover { position: absolute; top: 0; left: 0; z-index: 1060; display: none; max-width: 276px; padd ...
- 创建自定义 Estimator
ref 本文档介绍了自定义 Estimator.具体而言,本文档介绍了如何创建自定义 Estimator 来模拟预创建的 Estimator DNNClassifier 在解决鸢尾花问题时的行为.要详 ...
- java--String、StringBuilder、StringBuffer的解析和比较?
一.String的解析 1.String的含义 ①String是不可以被继承的,String类是final类,String类是由char[]数组来存储字符串. ②String是不可变的字符序列,如果存 ...
- 02 Django框架基础(APP的创建访问)
一.创建项目 1.命令:django-admin startproject sitename 2.IDLE环境:本质上都是执行上述命令 常用命令: python manage.py runserver ...
- windows使用批处理bat文件批量打开程序
windows命令行官网教程: https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/wind ...
- init_bootmem_node
初始化pg_data_t->bdtat结构体, /* * node_bootmem_map is a map pointer - the bits represent all physical ...
- CentOS 7 安装 配置 Nginx + PHP
. CentOS 7 下配置 yum 安装 Nginx. 进入/etc/yum.repos.d/目录下创建一个源配置文件nginx.repo: cd /etc/yum.repos.d/ vim ngi ...
- 函数名&函数名取地址
有时看到如下的代码: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 /*****************************/ #includ ...