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, ...
随机推荐
- flask 处理表单数据
处理表单数据 表单数据的处理涉及很多内容,从获取数据到保存数据大致有以下步骤: 1. 解析请求,获取表单数据 2. 对数据进行必要的转换,比如讲勾选框的值转换成python的布尔值 3. 验证数 ...
- Spring boot 问题总结
1. Spring boot 嵌入的tomcat不能启动: Unregistering JMX-exposed beans on shutdown 在官网(http://start.spring. ...
- 创建一个简单的WCF程序
1.创建WCF服务库 打开VS2010,选择文件→新建→项目菜单项,在打开的新建项目对话框中,依次选择Visual C#→WCF→WCF服务库,然后输入项目名称(Name),存放位置(Location ...
- html5水平方向重力感应
html5图片随手机重力感应而移动 <!DOCTYPE html> <html lang="zh-cn"><head><meta http ...
- asp.net热门框架
http://developer.51cto.com/art/201501/464292.htm
- 怎样从外网访问内网Lighttpd?
本地安装了一个Lighttpd,只能在局域网内访问,怎样从外网也能访问到本地的Lighttpd呢?本文将介绍具体的实现步骤. 准备工作 安装并启动Lighttpd 默认安装的Lighttpd端口是80 ...
- torchvision.datasets.ImageFolder数据加载
ImageFolder 一个通用的数据加载器,数据集中的数据以以下方式组织 root/dog/xxx.png root/dog/xxy.png root/dog/xxz.png root/cat/12 ...
- 通过经纬度获取所属城市信息-php
测试经纬度信息,37.863036,113.598909.通过地图查询,所在城市为:阳泉. <?php class test{ public static $test_key = 'dfgfdg ...
- 11: python递归
1.1 递归讲解 1.定义 1. 在函数内部,可以调用其他函数.如果一个函数在内部调用自身本身,这个函数就是递归函数. 2.递归特性 1. 必须有一个明确的结束条件 2. 每次进入更深一层递归时,问题 ...
- Codeforces 995F Cowmpany Cowmpensation - 组合数学
题目传送门 传送点I 传送点II 传送点III 题目大意 给定一个棵$n$个点的有根树和整数$D$,给这$n$个点标号,要求每个节点的标号是正整数,且不超过父节点的标号,根节点的标号不得超过D. 很容 ...