[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. WebService和Http的POST和GET请求区别和示例

    web service(SOAP) Webservice的一个最基本的目的就是提供在各个不同平台的不同应用系统的协同工作能力.Web service 就是一个应用程序,它向外界暴露出一个能够通过Web ...

  2. 解决linux mysql命令 bash: mysql: command not found 的方法

    错误: root@DB-02 ~]# mysql -u root-bash: mysql: command not found 原因:这是由于系统默认会查找/usr/bin下的命令,如果这个命令不在这 ...

  3. CF876 D 树状数组

    大意就是放n个硬币,每次放一个计算下这种情况下的操作次数,一个操作为从左到右扫描,如果一个硬币右边是空的,就将硬币后移,否则该次操作停止. 显然发现对于一个情况,我们只要考虑最右边的空位的左侧有几个硬 ...

  4. inline-block的间距问题

    张鑫旭的博客有提到,解决的方法有很多,先贴下,回头再做整理. http://www.zhangxinxu.com/wordpress/2012/04/inline-block-space-remove ...

  5. javascript类式继承函数最优版

    直接上代码: klass函数 var klass = function (Parent, props) { var Child, F, i; //1.新构造函数 Child = function () ...

  6. 玩转Hook——Android权限管理功能探讨(一)

    随着Android设备上的隐私安全问题越来越被公众重视,恶意软件对用户隐私,尤其是对电话.短信等私密信息的威胁日益突出,各大主流安全软件均推出了自己的隐私行为监控功能,在root情况下能有效防止恶意软 ...

  7. Windows入侵问题排查

    1.深入分析,查找入侵原因 1.1 检查帐户和弱口令 1.查看服务器已有系统或应用帐户是否存在弱口令 检查说明:主要检查系统管理员帐户.网站后台帐户.数据库帐户以及其他应用程序(FTP.Tomcao. ...

  8. usb的一些网址

    一些关于usb的帖子.网址: usb gadget device g_ether.ko 做成usbnetwork http://bbs.csdn.net/topics/370120345 Linux ...

  9. Jenkins 安装及使用

    jenkins是基于Java开发的一种持续集成工具,用于监控持续重复的工作. 可以用它做网站代码提交,更新. 1,安装 首先确保目标机器上装有 java jdk 版本最好在 1.6 以上,小编使用的是 ...

  10. mysql-5.7.13-winx64 免安装版配置方法

    1.下载MySQL Community Server 5.7.13 解压MySQL压缩包 2.修改 my-default.ini skip-grant-tables 作用是安装好MySQL后 免密码登 ...