【Codechef-Hard】Chef and Churu 分块
题目链接:
https://www.codechef.com/problems/FNCS
Solution
大力分块..
对序列分块,维护块内前缀和、块的前缀和,修改时暴力维护两个前缀和,询问单点答案就可以$O(1)$得到。
再对函数分块,维护每块函数的答案、每个位置对每块函数的贡献次数,贡献次数并不会发生改变,修改时只需要暴力修改$\sqrt N$块函数答案。
要开unsigned long long!!!
Code
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
inline int read()
{
int x=0,f=1; char ch=getchar();
while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();}
while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();}
return x*f;
} #define MAXN 100010
#define LL unsigned long long int N,M,val[MAXN],pl[MAXN],pr[MAXN]; struct Block{
int l,r,num[MAXN]; LL ans,sum[510];
}b[510];
int belong[MAXN],rk[MAXN],Bn,Bs;
LL Sum[MAXN]; inline void Getsum(int x)
{
LL t=0;
for (int i=b[x].l; i<=b[x].r; i++) {
t+=val[i];
b[x].sum[rk[i]]=t;
}
b[x].sum[Bs]=t;
for (int i=x; i<=Bn; i++) Sum[i]=Sum[i-1]+b[i].sum[Bs];
} inline LL Query(int x,int y)
{
int bx=belong[x],by=belong[y];
if (bx==by)
return b[bx].sum[rk[y]]-b[bx].sum[rk[x]-1];
else
if (bx+1==by)
return b[by].sum[rk[y]]-b[bx].sum[rk[x]-1]+b[bx].sum[Bs];
else
return b[by].sum[rk[y]]+Sum[by-1]-Sum[bx-1]-b[bx].sum[rk[x]-1];
} inline void Getnum(int x)
{
for (int i=b[x].l; i<=b[x].r; i++) {
b[x].num[pl[i]]++; b[x].num[pr[i]+1]--;
b[x].ans+=Query(pl[i],pr[i]);
}
for (int i=1; i<=N; i++) b[x].num[i]+=b[x].num[i-1];
} int main()
{
N=read();
for (int i=1; i<=N; i++) val[i]=read();
for (int i=1; i<=N; i++) pl[i]=read(),pr[i]=read(); Bs=int(sqrt(N));
for (int i=1; i<=N; i++) {
if ((i-1)%Bs==0) Bn++,b[Bn].l=i;
belong[i]=Bn; rk[i]=(i-1)%Bs+1;
if ((i-1)%Bs==Bs-1 || i==N) b[Bn].r=i;
} for (int i=1; i<=Bn; i++) Getsum(i);
for (int i=1; i<=Bn; i++) Getnum(i); M=read();
while (M--) {
int opt=read(),x=read(),y=read(),z;
if (opt==1) {
z=y-val[x];
val[x]=y;
Getsum(belong[x]);
for (int i=1; i<=Bn; i++) b[i].ans+=(LL)b[i].num[x]*z;
} else {
int bx=belong[x],by=belong[y];
LL ans=0;
if (bx==by || bx+1==by)
for (int i=x; i<=y; i++) ans+=Query(pl[i],pr[i]);
else {
for (int i=bx+1; i<=by-1; i++) ans+=b[i].ans;
for (int i=x; i<b[bx+1].l; i++) ans+=Query(pl[i],pr[i]);
for (int i=b[by-1].r+1; i<=y; i++) ans+=Query(pl[i],pr[i]);
}
printf("%llu\n",ans);
}
} return 0;
}
【Codechef-Hard】Chef and Churu 分块的更多相关文章
- Codechef FNCS Chef and Churu
Disciption Chef has recently learnt Function and Addition. He is too exited to teach this to his fri ...
- CodeChef Chef and Churu [分块]
题意: 单点修改$a$ 询问$a$的区间和$f$的区间和 原来普通计算机是这道题改编的吧... 对$f$分块,预处理$c[i][j]$为块i中$a_j$出现几次,$O(NH(N))$,只要每个块差分加 ...
- CodeChef - FNCS Chef and Churu(分块)
https://vjudge.net/problem/CodeChef-FNCS 题意: 思路: 用分块的方法,对每个函数进行分块,计算出该分块里每个数的个数,这样的话也就能很方便的计算出这个分块里所 ...
- 【xsy2111】 【CODECHEF】Chef and Churus 分块+树状数组
题目大意:给你一个长度为$n$的数列$a_i$,定义$f_i=\sum_{j=l_i}^{r_i} num_j$. 有$m$个操作: 操作1:询问一个区间$l,r$请你求出$\sum_{i=l}^{r ...
- 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个函数值的和 ...
- [CC-FNCS]Chef and Churu
[CC-FNCS]Chef and Churu 题目大意: 一个长度为\(n(n\le10^5)\)的数列\(A_{1\sim n}\),另有\(n\)个函数,第\(i\)个函数会返回数组中标号在\( ...
- [Codechef CHSTR] Chef and String - 后缀数组
[Codechef CHSTR] Chef and String Description 每次询问 \(S\) 的子串中,选出 \(k\) 个相同子串的方案有多少种. Solution 本题要求不是很 ...
- 【分块+树状数组】codechef November Challenge 2014 .Chef and Churu
https://www.codechef.com/problems/FNCS [题意] [思路] 把n个函数分成√n块,预处理出每块中各个点(n个)被块中函数(√n个)覆盖的次数 查询时求前缀和,对于 ...
- Codechef SEAARC Sereja and Arcs (分块、组合计数)
我现在真的什么都不会了呢...... 题目链接: https://www.codechef.com/problems/SEAARC 好吧,这题其实考察的是枚举的功力-- 题目要求的是\(ABAB\)的 ...
随机推荐
- python BeautifulSoup
之前解析LXML,用的是XPath,现在临时被抓取写爬虫,接人家的代码,看到用的是BeautifulSoup,稍微学了下,也挺好用的,简单记录下用法,有机会做下和Xpath的对比测试 初始化 from ...
- 流媒体技术学习笔记之(六)FFmpeg官方文档先进音频编码(AAC)
先进音频编码(AAC)的后继格式到MP3,和以MPEG-4部分3(ISO / IEC 14496-3)被定义.它通常用于MP4容器格式; 对于音乐,通常使用.m4a扩展名.第二最常见的用途是在MKV( ...
- [原]JUnit 自定义扩展思路
1. 理解Annotation,http://www.cnblogs.com/mandroid/archive/2011/07/18/2109829.html 2. JUNIT整体执行过程分析,htt ...
- mysql 1045 access denied for user 解决方法
提示:1045 access denied for user 'root'@'localhost' using password yes方法一: # /etc/init.d/mysql stop # ...
- Android的layout_weight和weightSum
先看一下weightSum属性的功能描述:定义weight总和的最大值.如果未指定该值,以所有子视图的layout_weight属性的累加值作为总和的最大值.把weightSum的定义搁在这里,先去看 ...
- [Alg::Trick]小白鼠找毒酒
题目来源:牛客网 https://www.nowcoder.com/questionTerminal/c26c4e43c77440ee9497b20118871bf1 8瓶酒一瓶有毒,用人测试.每次测 ...
- 安装.NET Framework返回1603错误的解决办法
昨天正在忙其它事情,实然同事向我反馈TFS上的文档无法浏览查看.第一反映是他的机器环境问题,让他试了下其它项目的文档也无法查看,后来在我电脑上也尝试了一下,果然无法查看项目文档,看来是TFS出了问题. ...
- 数组的splice方法
splice 该方法向或者从数组中添加或者删除项目,返回被删除的项目,同时也会改变原数组. splice(index,howmany,item1,...itemX) index参数:必须,整数,规定添 ...
- Python api认证
本节内容: 基本的api 升级的api 终极版api 环境:Djanao, 项目名:api_auto, app:api 角色:api端,客户端,黑客端 1.基本的api [api端] #api_aut ...
- 几个比较实用的CSS
1.filter:chroma(color:#FFFFFF); 让指定的背景色透明,例: <table cellspacing = "0" cellpadding = ...