CodeChef - FNCS Chef and Churu(分块)
https://vjudge.net/problem/CodeChef-FNCS
题意:

思路:
用分块的方法,对每个函数进行分块,计算出该分块里每个数的个数,这样的话也就能很方便的计算出这个分块里所有数的和。
用树状数组维护数组的话可以很方便的计算出某个区间内所有数的和以及修改某个数。
每次查询时,如果在中间块的函数,我们直接加上sum[i](sum[i]为预处理的每一块的和),对于两边的函数,就用树状数组快速求一下和即可。
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<int,int> pll;
const int INF = 0x3f3f3f3f;
const int maxn = 1e5+; int n , m, unit, num;
int a[maxn],l[maxn],r[maxn];
int cnt[][maxn];
ll c[maxn],sum[maxn]; int lowbit(int x)
{
return x&-x;
} ll getsum(int x)
{
ll ret = ;
while(x>)
{
ret+=c[x];
x-=lowbit(x);
}
return ret;
} int add(int x, int d)
{
while(x<=n)
{
c[x]+=d;
x+=lowbit(x);
}
} void update(int x, int y)
{
add(x,y-a[x]);
for(int i=;i<=num;i++) sum[i]+=(ll)cnt[i][x]*(y-a[x]);
a[x]=y;
} ll query(int left, int right)
{
ll ans = ;
int l_unit=(left-)/unit+,r_unit=(right-)/unit+;
if(l_unit==r_unit)
{
for(int i=left;i<=right;i++)
ans+=getsum(r[i])-getsum(l[i]-);
}
else
{
for(int i=l_unit+;i<r_unit;i++) ans+=sum[i];
for(int i=left;i<=(l_unit)*unit;i++) ans+=getsum(r[i])-getsum(l[i]-);
for(int i=(r_unit-)*unit+;i<=right;i++) ans+=getsum(r[i])-getsum(l[i]-);
}
return ans;
} int main()
{
//freopen("in.txt","r",stdin);
scanf("%d",&n);
memset(c,,sizeof(c));
for(int i=;i<=n;i++) {scanf("%d",&a[i]);add(i,a[i]);}
for(int i=;i<=n;i++) scanf("%d%d",&l[i],&r[i]); unit = (int)sqrt(n+0.5);
num = unit + (unit*unit!=n);
int now = ;
for(int i=;i<=n;i++)
{
if(i%unit==) now++;
cnt[now][l[i]]++;
cnt[now][r[i]+]--;
} for(int i=;i<=num;i++)
{
for(int j=;j<=n;j++)
{
cnt[i][j]+=cnt[i][j-];
sum[i]=sum[i]+(ll)cnt[i][j]*a[j];
}
} scanf("%d",&m);
while(m--)
{
int op, x, y;
scanf("%d%d%d",&op,&x,&y);
if(op==) update(x,y);
else printf("%lld\n",query(x, y));
}
return ;
}
CodeChef - 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 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 大力分块.. 对序列分块,维护块内前缀和.块的前缀和,修改时暴力维护两个前缀和,询问单点答案 ...
- 【xsy2111】 【CODECHEF】Chef and Churus 分块+树状数组
题目大意:给你一个长度为$n$的数列$a_i$,定义$f_i=\sum_{j=l_i}^{r_i} num_j$. 有$m$个操作: 操作1:询问一个区间$l,r$请你求出$\sum_{i=l}^{r ...
- 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个函数值的和 ...
- [CC-FNCS]Chef and Churu
[CC-FNCS]Chef and Churu 题目大意: 一个长度为\(n(n\le10^5)\)的数列\(A_{1\sim n}\),另有\(n\)个函数,第\(i\)个函数会返回数组中标号在\( ...
- [Codechef CHSTR] Chef and String - 后缀数组
[Codechef CHSTR] Chef and String Description 每次询问 \(S\) 的子串中,选出 \(k\) 个相同子串的方案有多少种. Solution 本题要求不是很 ...
- 【分块+树状数组】codechef November Challenge 2014 .Chef and Churu
https://www.codechef.com/problems/FNCS [题意] [思路] 把n个函数分成√n块,预处理出每块中各个点(n个)被块中函数(√n个)覆盖的次数 查询时求前缀和,对于 ...
- CodeChef FNCS (分块+树状数组)
题目:https://www.codechef.com/problems/FNCS 题解: 我们知道要求区间和的时候,我们用前缀和去优化.这里也是一样,我们要求第 l 个函数到第 r 个函数 [l, ...
随机推荐
- Hive批量删除历史分区
批量删除历史分区和数据可以采用如下操作: -- 删除20180101之前的所有分区 alter table example_table_name drop if exists partition (d ...
- Linux基础命令---ifdown、ifup
ifup ifup指令用来启动网络接口设备,设备必须是定义在“/etc/sysconfig/network-scripts/ifcfg-ethX”或者“/etc/sysconfig/network”的 ...
- python 图片
- bzoj4445 小凸想跑步
题目链接 半平面交,注意直线方向!!! 对于凸包上任意一条边$LINE(p_i,p_{i+1})$都有$S_{\Delta{p_i} {p_{i + 1}}p} < S_{\Delta{p_0} ...
- Zsh和oh my zsh的安装和使用
Zsh 兼容 Bash,据传说 99% 的 Bash 操作 和 Zsh 是相同的,默认 CentOS / Ubuntu / Mac 系统用的是 Bash,倒也不是说 Bash 不好,而是说我们有更好的 ...
- 前端框架VUE----babel
这个是解析我们es6的代码的,为什么要用它呢,因为对于一些ie浏览器,甚至FF浏览器,低版本的还不能识别我们的es6代码,那么vue里面好多还让我们去写es6的代码,这个时候我们就可以用babel这个 ...
- FFMPEG结构体分析:AVFrame(解码后的数据)
https://blog.csdn.net/jxcr1984/article/details/52766524 本文转自: http://blog.csdn.net/leixiaohua1020/ar ...
- 使用pytorch完成kaggle猫狗图像识别
kaggle是一个为开发商和数据科学家提供举办机器学习竞赛.托管数据库.编写和分享代码的平台,在这上面有非常多的好项目.好资源可供机器学习.深度学习爱好者学习之用.碰巧最近入门了一门非常的深度学习框架 ...
- mysqldump 使用小结
语法: 备份某个数据库: mysqldump -uroot -p*** [options] –-databases DB_name > back_db_name.sql --databases: ...
- Prometheus监控学习笔记之PromQL简单示例
0x00 简单的时间序列选择 返回度量指标 http_requests_total 的所有时间序列样本数据: http_requests_total 返回度量指标名称为 http_requests_t ...