[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 ...
随机推荐
- 玩转Hook——Android权限管理功能探讨(二)
距离我上一篇研究ptrace的随笔http://www.cnblogs.com/zealotrouge/p/3544147.html已经过去半年了,最近不忙的时候抽空继续研究了下.同样,参考了Prad ...
- Jenkins mac pkg安装 后默认配置文件/启动路径
自启动文件路径 /Library/LaunchDaemons/org.jenkins-ci.plist jenkins.war 执行文件路径 /Applications/Jenkins/jenkins ...
- 关于cookie和session
在设置cookie的时候,它会保留在本地,无论你有没有退出浏览器都是.但是session只能在登录状态有效.退出浏览器过后就会消除掉.同时设置也是有问题的. @app.route('/login',m ...
- Python学习笔记之逻辑回归
# -*- coding: utf-8 -*- """ Created on Wed Apr 22 17:39:19 2015 @author: 90Zeng " ...
- vs中如何统计整个项目的代码行数
在一个大工程中有很多的源文件和头文件,如何快速统计总行数? ------解决方案--------------------b*[^:b#/]+.*$^b*[^:b#/]+.*$ ctrl + shift ...
- Project Euler Problem4
Largest palindrome product Problem 4 A palindromic number reads the same both ways. The largest pali ...
- Jmeter遇到Connection reset by peer的解决方法
解决方案如下: 1.修改HTTP请求下面的Impementation选项,改成HttpClient4 2.修改了/bin/jmeter.bat文件:找到这2行 set HEAP=-Xms256m -X ...
- 谈谈如何查看Android项目方法数
我们都知道,Android App的方法数是有天花板的,在方法数达到65536时,就会出现打包异常,这个时候,我们需要去除一些不需要的三方工具包,或者采用多Dex技术分包,都能达到正常打包的效果. 可 ...
- java实战
1.http://learning.happymmall.com/ http://www.happymmall.com/index.html 前台官网 http://test.happymmall. ...
- VMWare 虚拟机 安装 Mac OS X
VMWare安装Mac OS X 随着iPhone.iPad.Mac等苹果产品越来越火爆,越来越多的初学者想要了解和尝试苹果平台,包括苹果操作系统Mac OS X.苹果演示软件Keynote.苹果开发 ...