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(分块)的更多相关文章

  1. Codechef FNCS Chef and Churu

    Disciption Chef has recently learnt Function and Addition. He is too exited to teach this to his fri ...

  2. CodeChef Chef and Churu [分块]

    题意: 单点修改$a$ 询问$a$的区间和$f$的区间和 原来普通计算机是这道题改编的吧... 对$f$分块,预处理$c[i][j]$为块i中$a_j$出现几次,$O(NH(N))$,只要每个块差分加 ...

  3. 【Codechef-Hard】Chef and Churu 分块

    题目链接: https://www.codechef.com/problems/FNCS Solution 大力分块.. 对序列分块,维护块内前缀和.块的前缀和,修改时暴力维护两个前缀和,询问单点答案 ...

  4. 【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 ...

  5. 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个函数值的和 ...

  6. [CC-FNCS]Chef and Churu

    [CC-FNCS]Chef and Churu 题目大意: 一个长度为\(n(n\le10^5)\)的数列\(A_{1\sim n}\),另有\(n\)个函数,第\(i\)个函数会返回数组中标号在\( ...

  7. [Codechef CHSTR] Chef and String - 后缀数组

    [Codechef CHSTR] Chef and String Description 每次询问 \(S\) 的子串中,选出 \(k\) 个相同子串的方案有多少种. Solution 本题要求不是很 ...

  8. 【分块+树状数组】codechef November Challenge 2014 .Chef and Churu

    https://www.codechef.com/problems/FNCS [题意] [思路] 把n个函数分成√n块,预处理出每块中各个点(n个)被块中函数(√n个)覆盖的次数 查询时求前缀和,对于 ...

  9. CodeChef FNCS (分块+树状数组)

    题目:https://www.codechef.com/problems/FNCS 题解: 我们知道要求区间和的时候,我们用前缀和去优化.这里也是一样,我们要求第 l 个函数到第 r 个函数 [l, ...

随机推荐

  1. python type的用法

    目录 描述 语法 用法 type和isinstance Type和Object 描述 python的 type 函数有两个用法,当只有一个参数的时候,返回对象的类型.当有三个参数的时候返回一个类对象. ...

  2. 网站图标 favicon.ico

    默认情况下,浏览器访问一个网站的时候,同时还会向服务器请求“/favicon.ico”这个URL,目的是获取网站的图标. 若没有配置的话,Django就会返回一个404错误,并且浏览器接收到这个404 ...

  3. xshell中出现的绿色背景的文件夹

    这种文件夹表示权限为777的文件夹 可以使用chmod 777 fileName进行权限修改 如果需要将文件夹以及其子文件夹的权限全部置为777 chmod 777 -R directoryName/ ...

  4. javanio2

    package com.lanhuigu.nio.selector; import java.net.InetSocketAddress; import java.nio.ByteBuffer; im ...

  5. tcpdump 命令

    tcpdump命令高级网络 tcpdump命令是一款sniffer工具,它可以打印所有经过网络接口的数据包的头信息,也可以使用-w选项将数据包保存到文件中,方便以后分析. 选项 -a:尝试将网络和广播 ...

  6. 利用yum搭建lamp环境并进一步创建博客

    用yum搭建lamp环境 第一.安装apache yum -y install httpd 第二.安装mariadb Yum -y mariadb mariadb-server 第三.安装php Yu ...

  7. Logstash 安装和使用

    下载地址 https://artifacts.elastic.co/downloads/logstash/logstash-5.6.8.zip 下载后解压,测试 #将键盘内容输出到控制台 logsta ...

  8. Golang中map的三种声明方式和简单实现增删改查

    package main import ( "fmt" ) func main() { test3 := map[string]string{ "one": & ...

  9. 电影编码JPEG2000与H.264

    电影的第三次革命是数字电影的诞生,数字电影取代了胶片,那么数字电影就一定有其独特的封装(压缩)格式.在网络上,我们经常见到许多视频格式,诸如mp4.mkv.flv.rmvb等,这些都是在通用计算机上播 ...

  10. neutron full stack

    1.  通读一下 neutron的那个文档.  里面介绍了, db怎么隔离的, amqp怎么隔离的. 2.  记住文档中,那个full stack的图. 3.  走读代码      从TestOvsC ...