[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. Spark记录-Scala异常与处理

    Scala try-catch语句 Scala提供try和catch块来处理异常.try块用于包含可疑代码.catch块用于处理try块中发生的异常.可以根据需要在程序中有任意数量的try...cat ...

  2. Java SSM框架之MyBatis3(七)MyBatis之参数取值

    在mybatis中,参数取值方式有两种:#{ } 和 ${ } 一.#{ } select * from student where name=#{name} 编译后执行的sql语句: select ...

  3. Java技术体系总结

    PC前端:Javascript.JQuery.Angularjs.Reactjs.TypeScript 移动前端:Vue.js.Zepto WebJars spring:spring mvc.spri ...

  4. 利用fiddler来模拟低速环境

    为了让我们的站点拥有更好的用户体验,更短的加载时间,我们会“按需加载”页面的资源. 在调试程序的时候,我们希望能有一个低速率的网络环境来模拟真实线上的环境,这个时候fiddler(下载fiddler请 ...

  5. HttpClient与HttpUrlConnection下载速度比较

    Android有两套http的API,刚开始使用网络编程时多少有些迷惑到底用哪个好呢?其实孰优孰劣无需再争论,google已经指出HttpUrlConnection是Android更优的选择,并在SD ...

  6. 01:MFC应用程序编程

    一 MFC的发展 VC 1.0->VC 5.0->VC 6.0->VC2008 SP1)->VS2010 二 MFC基础 1 MFC 微软基础类库 采用类的方式,将Win32 ...

  7. kali更新失败

    今天更新kali时失败,出现如下问题: root@kali:~# apt-get update Get: http://mirrors.aliyun.com/kali kali-rolling InR ...

  8. js 加alert后才能执行方法

    原因是:访问页面时,某些js方法还没初始化(或者还没有加载出来)此时调用肯定不执行.alert起到了延迟的功能,当用户点击确定此时要执行的js恰好初始化完成,能正常执行. 解决方法是 加setTime ...

  9. springboot整合Thymeleaf模板引擎

    引入依赖 需要引入Spring Boot的Thymeleaf启动器依赖. <dependency> <groupId>org.springframework.boot</ ...

  10. js和css实现内容超过边框,就自动省略,自动添加title

    在项目汇总,我们有这样的需求,如果内容多了,就自动省略,自动添加title 这个需要判断判断俩个值,一个是width(),一个是scrollWidth, 在div中,如果内容没有超过边框,这俩个值是一 ...