题目:https://www.codechef.com/problems/FNCS

题解:

  我们知道要求区间和的时候,我们用前缀和去优化。这里也是一样,我们要求第 l 个函数到第 r 个函数 [l, r] 的函数和,那么我们可以用 sum[r] - sum[l-1] 来求得。

  由于这个数据量有点大,所以我们将函数分块。

  例如样例:

    1 3    有5个函数,那么我们分成3块。{ [1 3] , [2 5] }, { [4 5], [3 5] }, { [1 2] }。每一块对应都有一个sum ,这时如果我们要求前3个函数的和,就可以 (第一块的sum + 第3个函数的和)
    2 5    在一个块内暴力的复杂度是O(sqrt(n))
    4 5
    3 5    还有就是在处理块内函数总sum 记录下每一个a[i] 对应的权值。比如在第一块中 1 2 2 1 1 分别是a1~a5对这一块sum 的贡献。 
    1 2

  更新的时候就维护一下就可以了

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <set>
using namespace std;
typedef long long LL;
typedef unsigned long long uLL;
#define ms(a, b) memset(a, b, sizeof(a))
#define pb push_back
#define mp make_pair
const LL INF = 0x7fffffff;
const int inf = 0x3f3f3f3f;
const int mod = 1e9+;
const int maxn = +;
const int maxm = +;
int a[maxn], l[maxn], r[maxn], Add[maxn];
int unit, num, n;
int cnt[][maxn];//记录在第i块,j值的权
uLL sum[];//块的sum
uLL BIT_SUM[maxn];
int low_bit(int x)
{
return x&-x;
}
uLL BIT_sum(int x)
{
uLL ret = ;
while(x>){
ret += 1uLL*BIT_SUM[x]; x -= low_bit(x);
}
return ret;
}
void add(int x, int d)
{
while(x<=n){
BIT_SUM[x] += d; x+=low_bit(x);
}
}
uLL query(int x)
{
return 1uLL*BIT_sum(x);
}
uLL Query(int x)
{
int end_unit = x/unit + ;
LL ans = ;
for(int i = ;i<end_unit;i++)
ans += sum[i];
for(int i = (end_unit-)*unit+;i<=x;i++)
ans += query(r[i]) - query(l[i]-);
return ans;
}
void init()
{
ms(BIT_SUM, );
ms(sum, );
ms(cnt, );
}
int main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
// ios::sync_with_stdio(0);
// cin.tie(0);
init();
scanf("%d", &n);
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);//块的大小
num = (n-)/unit+;//块的数量
for(int i = ;i<=num;i++){
int begin_f = (i-)*unit+;//这一块的开始函数
int end_f = begin_f + unit - ;//结束函数
ms(Add,);
for(int j = begin_f;j<=end_f&&j<=n;j++){
Add[l[j]]++;Add[r[j]+]--;
}
int add = ;
for(int j = ;j<=n;j++){
add += Add[j];
cnt[i][j] += add;
}
for(int j = ;j<=n;j++)
sum[i] += 1uLL * a[j] * cnt[i][j];
// for(int j = 1;j<=n;j++)
// cout << cnt[i][j] <<" ";cout << endl;
// cout << sum[i] << endl;
}
int q;scanf("%d", &q);
while(q--){
int op;scanf("%d", &op);
if(op==){
int x, y;scanf("%d%d", &x, &y);
for(int i = ;i<=num;i++)
sum[i] -= 1uLL * a[x] * cnt[i][x];
add(x, y-a[x]);
a[x] = y;
for(int i = ;i<=num;i++)
sum[i] += 1uLL * a[x] * cnt[i][x];
}else{
int x, y;
scanf("%d%d", &x, &y);
uLL ans = Query(y) - Query(x-);
printf("%llu\n", ans);
}
}
return ;
}

CodeChef FNCS (分块+树状数组)的更多相关文章

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

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

  2. 【BZOJ 3295】动态逆序对 - 分块+树状数组

    题目描述 给定一个1~n的序列,然后m次删除元素,每次删除之前询问逆序对的个数. 分析:分块+树状数组 (PS:本题的CDQ分治解法见下一篇) 首先将序列分成T块,每一块开一个树状数组,并且先把最初的 ...

  3. 【bzoj2141】排队 分块+树状数组

    题目描述 排排坐,吃果果,生果甜嗦嗦,大家笑呵呵.你一个,我一个,大的分给你,小的留给我,吃完果果唱支歌,大家乐和和.红星幼儿园的小朋友们排起了长长地队伍,准备吃果果.不过因为小朋友们的身高有所区别, ...

  4. 【bzoj3744】Gty的妹子序列 分块+树状数组+主席树

    题目描述 我早已习惯你不在身边, 人间四月天 寂寞断了弦. 回望身后蓝天, 跟再见说再见…… 某天,蒟蒻Autumn发现了从 Gty的妹子树(bzoj3720) 上掉落下来了许多妹子,他发现 她们排成 ...

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

  6. Bzoj 3295: [Cqoi2011]动态逆序对 分块,树状数组,逆序对

    3295: [Cqoi2011]动态逆序对 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2886  Solved: 924[Submit][Stat ...

  7. 【XSY2111】Chef and Churus 分块 树状数组

    题目描述 有一个长度为\(n\)的数组\(A\)和\(n\)个区间\([l_i,r_i]\),有\(q\)次操作: \(1~x~y\):把\(a_x\)改成\(y\) \(2~x~y\):求第\(l\ ...

  8. BZOJ3787:Gty的文艺妹子序列(分块,树状数组)

    Description Autumn终于会求区间逆序对了!Bakser神犇决定再考验一下他,他说道: “在Gty的妹子序列里,某个妹子的美丽度可也是会变化的呢.你还能求出某个区间中妹子们美丽度的逆序对 ...

  9. 2018.06.30 BZOJ4765: 普通计算姬(dfs序+分块+树状数组)

    4765: 普通计算姬 Time Limit: 30 Sec Memory Limit: 256 MB Description "奋战三星期,造台计算机".小G响应号召,花了三小时 ...

  10. [P3759][TJOI2017]不勤劳的图书管理员(分块+树状数组)

    题目描述 加里敦大学有个帝国图书馆,小豆是图书馆阅览室的一个书籍管理员.他的任务是把书排成有序的,所以无序的书让他产生厌烦,两本乱序的书会让小豆产生 这两本书页数的和的厌烦度.现在有n本被打乱顺序的书 ...

随机推荐

  1. LeetCode 94. Binary Tree Inorder Traversal 动态演示

    非递归的中序遍历,要用到一个stack class Solution { public: vector<int> inorderTraversal(TreeNode* root) { ve ...

  2. java8 语言特性

    Lamda 表达式 使用内部类也可以实现相关的功能, 但使用lamda更简短 lamda 的参数类型可以省略 如果是单条语句, lamda 的花括号可以省略 如果是单条语句, lamda 的 retu ...

  3. Navicat Premium 12.1.20.0安装与激活

    一.Navicat Premium 12下载 链接: https://pan.baidu.com/s/1GgNbCPGahN-Z91f4dnQkBQ 提取码: 3q8f 复制这段内容后打开百度网盘手机 ...

  4. OpenCV-----图像的加载与保存

    OpenCV中的图像: 定义:在opencv中图像就是结构化存储数据的信息. 属性:1.宽.高和通道数目 1 print(image.shape) #形状:行(长).列(宽).通道数(深度) 2.像素 ...

  5. 重磅 | Elasticsearch7.X学习路线图

    原文:重磅 | Elasticsearch7.X学习路线图 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.c ...

  6. Tomcat 8.5 apr 模式配置

    tomcat APR模式配置 一.环境 操作系统:Ubutnu 14 ubuntu@ubuntu:~$ uname -a Linux ubuntu 4.4.0-31-generic #50~14.04 ...

  7. service worker介绍

    原文:Service workers explained 译者:neal1991 welcome to star my articles-translator, providing you advan ...

  8. Linux压缩、解压

    gzip压缩: 归档,压缩,yourFloder文件夹生成yourName.tar.gz: - tar -zcvf yourName.tar.gz yourFloder 解压yourName.tar. ...

  9. 记录cobbler报错

    出现下面这个错误解决方法 httpd does not appear to be running and proxying cobbler, or SELinux is in the way. Ori ...

  10. Linux架构之Rsync守护进程推和拉

    第三十三章 Rsync服务 33.1)Rsync基本概述 rsync是一款开源.快速.多功能.可实现全量及增量的本地或远程数据同步备份的优秀工具.rsync软件适用于Unix/linux/Window ...