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, ...
随机推荐
- 栈(stack)和堆(heap)
栈(stack)和堆(heap), Java程序在运行时都要开辟空间,任何软件在运行时都要在内存中开辟空间,Java虚拟机运行时也是要开辟空间的.JVM运行时在内存中开辟一片内存区域,启动时在自己的内 ...
- linux 安装python3 date更新
http://linux.51yip.com/ ntpdate -u ntp.aliyun.com 更新时间 centos 默认是有 python的,是2.7.5的 重启网络的命令 -- sys ...
- PLSQL 问题小记
问题1:在一个拼写长字符串的函数中,若出现ora-06502或者ora-06512的错误,则有可能是在拼串的过程中,数据类型的隐式转换出了问题,如:字符类型转为数字,此时会出现错误. 解决方案:在拼串 ...
- 初学delphi
今天女朋友的一门课,要求用delphi 软件编程,内容是一个计算器.当然,这个工作肯定是落在我的头上了. 这个软件是我第一次使用,边自学边进行代码编写,在n多次修改完善之后,终于成形.功能不是很多,跟 ...
- Eloquent JavaScript #05# higher-order functions
索引 Notes 高阶函数 forEach filter map reduce some findIndex 重写课本示例代码 Excercises Flattening Your own loop ...
- m3u8文件下载合并的一种方法
# -*- coding: utf-8 -*- """ Created on Wed Mar 14 15:09:14 2018 @author: Y "&quo ...
- oracle 12c AUTO_SAMPLE_SIZE动态采用工作机制
The ESTIMATE_PERCENT parameter in DBMS_STATS.GATHER_*_STATS procedures controls the percentage of ro ...
- hibernate validator自定义校验注解以及基于服务(服务组)的校验
hibernate validator是Bean Validation 1.1 (JSR 349) Reference Implementation,其广泛的应用在mvc的参数校验中,尤其是使用服务端 ...
- 纯手写SpringMVC到SpringBoot框架项目实战
引言 Spring Boot其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置. 通过这种方式,springboot ...
- Qone 自动删除说说脚本
请看图 script var delay = 1000; function del() { document.querySelector('.app_canvas_frame').contentDoc ...