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, ...
随机推荐
- Jmeter分布式压力测试
有时候,一台机器无法支持很多个虚拟用户并发,这时就会使用分布式测试来实现这个功能,jmeter是有提供这个功能的.要实现分布式测试,得在主从(agent和controler)机器的jmeter安装目录 ...
- 【转】求职面试-HR会问你什么问题?
前言 面试是程序员们经常探讨的话题,只要你通过前面的技术面,最后一面必然是HR面试,基本上到了这关你离Offer的距离应该不会太远了,但有的公司的HR是有刷入的权利,如果你并不能很好的应对HR的问题, ...
- 微信小程序制作家庭记账本之四
第四天,仍然对记账本代码进行研究,对按钮的大小设置,颜色,具体位置进行分析,但其中很多代码都不明白.
- 使用github管理Eclipse分布式项目开发
使用github管理Eclipse分布式项目开发 老关我在前面的博文(github管理iOS分布式项目开发)中介绍了github管理iOS分布式开发,今天老关将向大家介绍使用github管 理Ecli ...
- [转载] mysql 索引中的USING BTREE 的意义
索引是在存储引擎中实现的,因此每种存储引擎的索引都不一定完全相同,并且每种存储引擎也不一定支持所有索引类型. 根据存储引擎定义每个表的最大索引数和最大索引长度.所有存储引擎支持每个表至少16个索引,总 ...
- 学习Linux shell脚本中连接字符串的方法
这篇文章主要介绍了Linux shell脚本中连接字符串的方法,如果想要在变量后面添加一个字符,可以用一下方法: 代码如下: $value1=home $value2=${value1}"= ...
- The Little Prince-12/01
The Little Prince-12/01 The people have no imagination. They repeat whatever one says to them… On my ...
- 数据分析之pandas02
DataFrame 一.DataFrame DataFrame是一个[表格型]的数据结构.DataFrame又按一定顺序排列的多列数据组成,设计初衷是将Series的使用场景从一维扩展到多维.Date ...
- Java中的ASCII码与Unicode码
先上代码 后续更新 public class Unicode { public static void main(String[] args) { char ch1 = 'c'; char ch2 = ...
- kivy 小demo
from kivy.lang.builder import Builder from kivy.uix.boxlayout import BoxLayout from kivy.app import ...