[CC-FNCS]Chef and Churu
[CC-FNCS]Chef and Churu
题目大意:
一个长度为\(n(n\le10^5)\)的数列\(A_{1\sim n}\),另有\(n\)个函数,第\(i\)个函数会返回数组中标号在\(l_i\sim r_i\)之间的元素的和。\(q(q\le10^5)\)次询问,询问包含以下两种:
- 将数组的第\(x\)个元素修改为\(y\);
- 询问标号在\(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的更多相关文章
- Codechef FNCS Chef and Churu
Disciption Chef has recently learnt Function and Addition. He is too exited to teach this to his fri ...
- CodeChef - FNCS Chef and Churu(分块)
https://vjudge.net/problem/CodeChef-FNCS 题意: 思路: 用分块的方法,对每个函数进行分块,计算出该分块里每个数的个数,这样的话也就能很方便的计算出这个分块里所 ...
- 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 大力分块.. 对序列分块,维护块内前缀和.块的前缀和,修改时暴力维护两个前缀和,询问单点答案 ...
- 【分块+树状数组】codechef November Challenge 2014 .Chef and Churu
https://www.codechef.com/problems/FNCS [题意] [思路] 把n个函数分成√n块,预处理出每块中各个点(n个)被块中函数(√n个)覆盖的次数 查询时求前缀和,对于 ...
- 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个函数值的和 ...
- CodeChef November Challenge 2014
重点回忆下我觉得比较有意义的题目吧.水题就只贴代码了. Distinct Characters Subsequence 水. 代码: #include <cstdio> #include ...
- 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] 表示第 ...
- 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 ...
随机推荐
- 第一篇:打造专属开发工具Eclipse篇
第一篇:打造专属开发工具Eclipse篇 eclipse 优化 1.动画很酷,但如果可以的话,我总是在所有的工具中禁用动画.所以classic或者window classic主题是我最常用的主题 , ...
- 设置 Linux 服务器中 MySQL 允许远程访问
开启 MySQL 远程访问权限: 在linux系统上登陆mysql服务. -- root 是用户名 [root@localhost ~]# mysql -u root -p Enter passwor ...
- SQL记录-PLSQL函数
PL/SQL函数 PL/SQL函数与过程相同,不同之处在于函数有一个返回值.因此,前面的章节中的所有讨论都适用于函数. 创建函数 建立一个独立函数可以使用CREATE FUNCTION语句创建.CRE ...
- 【DS】排序算法之冒泡排序(Bubble Sort)
一.算法思想 冒泡排序是排序算法中比较有意思的一种排序方法,也很简单.其算法思想如下: 1)比较相邻的元素.如果第一个比第二个大,就交换他们两个. 2)对每一对相邻元素作同样的工作,从开始第一对到结尾 ...
- SQL语句(七)简单查询
--简单信息查询 --例1 查询所有学生的信息 --学生 -- Student --所有学生 -- 不限定班级.性别.年龄等条件 --所有信息 -- 所有字段,* select * from stud ...
- Spring RedisTemplate操作-哈希操作(3)
@Autowired @Resource(name="redisTemplate") private RedisTemplate<String, String> rt; ...
- 五个案例让你明白GCD死锁(转)
转自:http://ios.jobbole.com/82622/ 死锁一直都是在使用多线程时,需要注意的一个问题.以前对同步.异步,串行.并行只有一个模糊的概念,想想也是时候整理一下了.再看看之前的博 ...
- Centos6.5下升级Python版本
Cenos6.5升级Python2.6到2.7 1.下载源码包 wget https://www.python.org/ftp/python/2.7.12/Python-2.7.12.tgz 2.进行 ...
- 五大常见的MySQL高可用方案【转】
1. 概述 我们在考虑MySQL数据库的高可用的架构时,主要要考虑如下几方面: 如果数据库发生了宕机或者意外中断等故障,能尽快恢复数据库的可用性,尽可能的减少停机时间,保证业务不会因为数据库的故障而中 ...
- 解决getJSON跨域登录Session丢失的问题
最近在做项目中发现,我用下面的代码异步请求到login.ashx: var memberUrl = rooturl + 'member.ashx?r=' + Math.random() + '& ...