[CC-FNCS]Chef and Churu

题目大意:

一个长度为\(n(n\le10^5)\)的数列\(A_{1\sim n}\),另有\(n\)个函数,第\(i\)个函数会返回数组中标号在\(l_i\sim r_i\)之间的元素的和。\(q(q\le10^5)\)次询问,询问包含以下两种:

  1. 将数组的第\(x\)个元素修改为\(y\);
  2. 询问标号在\(m\)和\(n\)之间的函数的值的和。

思路:

对函数分块,树状数组维护\(A\)的前缀和。

时间复杂度\(\mathcal O(n\sqrt n\log n)\)。

源代码:

#include<cmath>
#include<cstdio>
#include<cctype>
#include<algorithm>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
typedef unsigned long long uint64;
const int N=1e5+1,B=317;
int n,a[N],block,cnt[N][B],bel[N],beg[B],end[B];
uint64 sum[B];
struct Func {
int l,r;
};
Func f[N];
class FenwickTree {
private:
uint64 val[N];
int lowbit(const int &x) const {
return x&-x;
}
public:
void modify(int p,const int &x) {
for(;p<=n;p+=lowbit(p)) {
val[p]+=x;
}
}
uint64 query(int p) const {
uint64 ret=0;
for(;p;p-=lowbit(p)) {
ret+=val[p];
}
return ret;
}
uint64 query(const int &l,const int &r) const {
return query(r)-query(l-1);
}
};
FenwickTree bit;
class SegmentTree {
#define _left <<1
#define _right <<1|1
#define mid ((b+e)>>1)
private:
int val[N<<2];
public:
void build(const int &p,const int &b,const int &e) {
val[p]=0;
if(b==e) return;
build(p _left,b,mid);
build(p _right,mid+1,e);
}
void modify(const int &p,const int &b,const int &e,const int &l,const int &r) {
if(b==l&&e==r) {
val[p]++;
return;
}
if(l<=mid) modify(p _left,b,mid,l,std::min(mid,r));
if(r>mid) modify(p _right,mid+1,e,std::max(mid+1,l),r);
}
int query(const int &p,const int &b,const int &e,const int &x) const {
int ret=val[p];
if(b==e) return ret;
if(x<=mid) ret+=query(p _left,b,mid,x);
if(x>mid) ret+=query(p _right,mid+1,e,x);
return ret;
}
#undef _left
#undef _right
#undef mid
};
SegmentTree sgt;
inline void init() {
for(register int i=bel[1];i<=bel[n];i++) {
sgt.build(1,1,n);
for(register int j=beg[i];j<=end[i];j++) {
sgt.modify(1,1,n,f[j].l,f[j].r);
sum[i]+=bit.query(f[j].l,f[j].r);
}
for(register int j=1;j<=n;j++) {
cnt[j][i]=sgt.query(1,1,n,j);
}
}
}
inline uint64 query(const int &l,const int &r) {
uint64 ret=0;
if(bel[l]==bel[r]) {
for(register int i=l;i<=r;i++) {
ret+=bit.query(f[i].l,f[i].r);
}
return ret;
}
for(register int i=l;i<=end[bel[l]];i++) {
ret+=bit.query(f[i].l,f[i].r);
}
for(register int i=bel[l]+1;i<bel[r];i++) {
ret+=sum[i];
}
for(register int i=beg[bel[r]];i<=r;i++) {
ret+=bit.query(f[i].l,f[i].r);
}
return ret;
}
int main() {
n=getint(),block=sqrt(n)*2;
for(register int i=1;i<=n;i++) {
a[i]=getint();
bit.modify(i,a[i]);
bel[i]=i/block;
if(!beg[bel[i]]) beg[bel[i]]=i;
end[bel[i]]=i;
}
for(register int i=1;i<=n;i++) {
f[i].l=getint();
f[i].r=getint();
}
init();
const int q=getint();
for(register int i=0;i<q;i++) {
const int opt=getint(),x=getint(),y=getint();
if(opt==1) {
bit.modify(x,y-a[x]);
for(register int i=bel[1];i<=bel[n];i++) {
sum[i]+=(uint64)(y-a[x])*cnt[x][i];
}
a[x]=y;
}
if(opt==2) {
printf("%llu\n",query(x,y));
}
}
return 0;
}

[CC-FNCS]Chef and Churu的更多相关文章

  1. Codechef FNCS Chef and Churu

    Disciption Chef has recently learnt Function and Addition. He is too exited to teach this to his fri ...

  2. CodeChef - FNCS Chef and Churu(分块)

    https://vjudge.net/problem/CodeChef-FNCS 题意: 思路: 用分块的方法,对每个函数进行分块,计算出该分块里每个数的个数,这样的话也就能很方便的计算出这个分块里所 ...

  3. CodeChef Chef and Churu [分块]

    题意: 单点修改$a$ 询问$a$的区间和$f$的区间和 原来普通计算机是这道题改编的吧... 对$f$分块,预处理$c[i][j]$为块i中$a_j$出现几次,$O(NH(N))$,只要每个块差分加 ...

  4. 【Codechef-Hard】Chef and Churu 分块

    题目链接: https://www.codechef.com/problems/FNCS Solution 大力分块.. 对序列分块,维护块内前缀和.块的前缀和,修改时暴力维护两个前缀和,询问单点答案 ...

  5. 【分块+树状数组】codechef November Challenge 2014 .Chef and Churu

    https://www.codechef.com/problems/FNCS [题意] [思路] 把n个函数分成√n块,预处理出每块中各个点(n个)被块中函数(√n个)覆盖的次数 查询时求前缀和,对于 ...

  6. 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个函数值的和 ...

  7. CodeChef November Challenge 2014

    重点回忆下我觉得比较有意义的题目吧.水题就只贴代码了. Distinct Characters Subsequence 水. 代码: #include <cstdio> #include ...

  8. CF&&CC百套计划2 CodeChef December Challenge 2017 Chef And Easy Xor Queries

    https://www.codechef.com/DEC17/problems/CHEFEXQ 题意: 位置i的数改为k 询问区间[1,i]内有多少个前缀的异或和为k 分块 sum[i][j] 表示第 ...

  9. CF&&CC百套计划2 CodeChef December Challenge 2017 Chef and Hamming Distance of arrays

    https://www.codechef.com/DEC17/problems/CHEFHAM #include<cstdio> #include<cstring> #incl ...

随机推荐

  1. [应用篇]第六篇 JSTL之自定义函数标签库

      在之前的JSTL的总结中已经对函数标签库进行了一些说明,在这里我再一次重新整理一下! 自带函数标签库介绍 引入该标签库的方法为: <%@ taglib prefix="fn&quo ...

  2. SimpleRoundedImage-不使用mask实现圆角矩形图片

    1.一张图片是如何显示在屏幕上的 一张图片渲染到unity界面中的大致流程. 2.我们要做什么 我们要做的就是在CPU中将图片的矩形顶点数据修改成圆角矩形的顶点信息,之后Unity会将修改后的顶点数据 ...

  3. Liberty Mutual Property Inspection, Winner's Interview: Qingchen Wang

    Liberty Mutual Property Inspection, Winner's Interview: Qingchen Wang The hugely popular Liberty Mut ...

  4. .NET面试题系列(三)排序算法

    冒泡排序 , , , , , 7, 2, 4 }; //外层循环控制排序趟数 ; i < arr.Length - ; i++) { //内层循环控制每一趟排序多少次 ; j < arr. ...

  5. 第9月第3天 uilabel contentscale

    1. http://blog.csdn.net/u012703795/article/details/43706449

  6. 在xampp与phpstorm环境下安装xdebug[转]

    XDebug是什么 很多PHP程序员调试使用echo.print_r().var_dump().printf()等,虽然对于有较丰富开发经验的程序员来说这些也已经足够了,他们往往可以在程序执行的过程中 ...

  7. cas:覆盖安装

    1.首先到github上下载最新的模板代码 https://github.com/apereo/cas-overlay-template 下载完成后,导入该工程. 2.编译打包 cd cas-over ...

  8. jexus - 分析日志文件

    1.统计IP访问次数 awk '{print $3}' default |sort -n|uniq -c|sort -rn|head

  9. 测试开发之Django——No4.Django中前端框架的配置与添加

    我们在开发一个web项目的时候,虽然我们不是专业开发,但是我们也想要做出来一个美美的前端页面. 这种时候,百度上铺天盖地的前端框架就是我们的最好选择了. 当然,在网上直接下载的框架,我们是不能直接用的 ...

  10. 详解使用 Tarjan 求 LCA 问题(图解)

    LCA问题有多种求法,例如倍增,Tarjan. 本篇博文讲解如何使用Tarjan求LCA. 如果你还不知道什么是LCA,没关系,本文会详细解释. 在本文中,因为我懒为方便理解,使用二叉树进行示范. L ...