Disciption

Chef has recently learnt Function and Addition. He is too exited to teach this to his friend Churu. Chef and Churu are very fast friends, they share their knowledge whenever they meet. Chef use to give a lot of exercises after he teaches some concept to Churu.

Chef has an array of N numbers. He also has N functions. Each function will return the sum of all numbers in the array from Li to Ri. So Chef asks churu a lot of queries which are of two types.

  • Type 1: Change the xth element of the array to y.
  • Type 2: Return the sum of all functions from m to n.

Now Churu has started to solve, but Chef realize that it is tough for him to decide whether Churu is correct or not. So he needs your help , will you help him out ?

Input Format

First Line is the size of the array i.e. N 
Next Line contains N space separated numbers Ai denoting the array 
Next N line follows denoting Li and Ri for each functions. 
Next Line contains an integer Q , number of queries to follow. 
Next Q line follows , each line containing a query of Type 1 or Type 2. 
x y : denotes a type 1 query,where x and y are integers 
m n : denotes a type 2 query where m and n are integers

Output Format

For each query of type 2 , output as asked above.

Constraints

1 ≤ N ≤ 10 5 
1 ≤ A i ≤ 10 9 
1 ≤ L i ≤ N 
i ≤ R i ≤ N 
1 ≤ Q ≤ 10 5 
1 ≤ x ≤ N 
1 ≤ y ≤ 10 9 
1 ≤ m ≤ N 
m ≤ n ≤ N

Subtask

  • Subtask 1: N ≤ 1000 , Q ≤ 1000 , 10 points
  • Subtask 2: R-L ≤ 10 , all x will be distinct ,10 points
  • Subtask 3: Refer to constraints above , 80 points

Sample Input


1 2 3 4 5 
1 3 
2 5 
4 5 
3 5 
1 2 

2 1 4 
1 3 7 
2 1 4 
2 3 5

Sample Output

41 
53 
28

Explanation

Functions values initially : 
F[1] = 1+ 2 + 3 = 6 
F[2] = 2 + 3 + 4 + 5 = 14 
F[3] = 4+5 = 9 
F[4] = 3+4+5 = 12 
F[5] = 1+2 = 3 
Query 1: F[1] + F[2] + F[3] + F[4] = 41 
After Update , the Functions are : 
F[1] = 10 , F[2] = 18 , F[3] = 9 , F[4] = 16 , F[5] = 3 
Query 3: F[1] + F[2] + F[3] + F[4] = 53 
Query 4: F[3]+F[4]+F[5] = 28

本来想用分块看看能不能水过的,,,结果怎么就水过了23333

我们设每个块内的元素有M个,那么就有(N/M)个块。

我们对函数分块了之后,可以用数组tag[i][j]表示第i个块内的函数总共加了几次第j个元素。

这个数组的预处理差分之后前缀和一下就好了。

然后有了这个数组之后我们就可以很方便的维护每个块内函数的和了。

对于整块的话,修改的复杂度O(N/M)  [考虑这个元素对每个块的总和的影响] ,查询的复杂度也是 O(N/M) ,因为最多要查询N/M个块。

而零散的块用树状数组维护一下元素数组的前缀和就好了,修改复杂度 O(log N),查询复杂度 O(M * log N)。

可以解出 M = sqrt(N/ log N) 的时候程序的效果应该是最好的,但是限于我们开不出这么大的数组,所以M只能取sqrt(N)稍小一点。

我取的是sqrt(N)/1.414 ,然后就A了。

#include<bits/stdc++.h>
#define ll unsigned long long
#define maxn 100005
using namespace std;
int n,a[maxn],sz,opt,le,ri,mx,m;
int tag[505][maxn],bl[maxn];
ll tot[505],f[maxn],ans;
int l[maxn],r[maxn]; inline void update(int x,int y){
for(;x<=n;x+=x&-x) f[x]+=(ll)y;
} inline ll query(int x){
ll an=0;
for(;x;x-=x&-x) an+=f[x];
return an;
} inline void input(){
scanf("%d",&n),sz=sqrt(n/2+1);
for(int i=1;i<=n;i++){
scanf("%d",a+i);
update(i,a[i]);
} for(int i=1;i<=n;i++){
bl[i]=(i-1)/sz+1;
scanf("%d%d",l+i,r+i);
tag[bl[i]][l[i]]++;
tag[bl[i]][r[i]+1]--;
}
} inline void prework(){
mx=bl[n];
for(int i=1;i<=mx;i++)
for(int j=1;j<=n;j++){
tag[i][j]+=tag[i][j-1];
tot[i]+=a[j]*(ll)tag[i][j];
}
} inline void TOL(){
int derta=ri-a[le];
update(le,derta);
for(int i=1;i<=mx;i++) tot[i]+=tag[i][le]*(ll)derta; a[le]=ri;
} inline void REQ(){
ans=0; if(bl[le]==bl[ri]){
for(;le<=ri;le++) ans+=query(r[le])-query(l[le]-1);
}
else{
for(;bl[le]==bl[le-1];le++) ans+=query(r[le])-query(l[le]-1);
for(;bl[ri]==bl[ri+1];ri--) ans+=query(r[ri])-query(l[ri]-1);
for(int j=bl[le];j<=bl[ri];j++) ans+=tot[j];
} printf("%llu\n",ans);
} int main(){
input();
prework();
scanf("%d",&m);
while(m--){
scanf("%d%d%d",&opt,&le,&ri);
if(opt==1) TOL();
else REQ();
}
return 0;
}

  

Codechef FNCS Chef and Churu的更多相关文章

  1. CodeChef - FNCS Chef and Churu(分块)

    https://vjudge.net/problem/CodeChef-FNCS 题意: 思路: 用分块的方法,对每个函数进行分块,计算出该分块里每个数的个数,这样的话也就能很方便的计算出这个分块里所 ...

  2. [CC-FNCS]Chef and Churu

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

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

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

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

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

  5. Chef and Problems(from Code-Chef FNCS) ( 回 滚 )

    题目: 题意:给定序列,求[l,r]区间内数字相同的数的最远距离. 链接:https://www.codechef.com/problems/QCHEF #include<bits/stdc++ ...

  6. CodeChef Chef and Churu [分块]

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

  7. CodeChef FNCS

    题面:https://www.codechef.com/problems/FNCS 题解: 我们考虑对 n 个函数进行分块,设块的大小为S. 每个块内我们维护当前其所有函数值的和,以及数组中每个元素对 ...

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

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

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

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

随机推荐

  1. SQL Server ALwayson 正在解析

    原因:把主库切换到辅助副本以后,集群全部出现正在解析的情况,数据库显示“恢复挂起” 过程:把服务器重启,原以为正在解析会恢复正常.结果失败. 解决方法:出现“正在解析”的情况跟故障转移群集有关,进故障 ...

  2. linux系统装载ELF过程

    参考:程序员的自我修养 fork -->execve() //----kenerl space--------------- sys_execve() /*arch\i386\kernel\pr ...

  3. ACM 深度优化搜索算法小总结

    深度优化搜索算法的本质:就是从一状态不断转移,如果无法转移了就需要返回上一个状态,知道找到解为止. 其核心:递归函数 基本模型: dfs(int i, int j) { //控制结束条件 //进行状态 ...

  4. Java学习笔记1---JVM、JRE、JDK

    jdk包含jre,jre包含jvm. 用java语言进行开发时,必须先装jdk: 只运行java程序,不进行开发时,可以只装jre. JVM 即Java Virtual machine,Java虚拟机 ...

  5. java中,为什么char类型数组可以直接用数组名打印,打印结果居然不是地址值!

    char类型的数组就相当于一个字符串. 因为输出流System.out是PrintStream对象,PrintStream有多个重载的println方法,其中一个就是public void print ...

  6. rocketmq源码分析2-broker的消息接收

    broker消息接收,假设接收的是一个普通消息(即没有事务),此处分析也只分析master上动作逻辑,不涉及ha. 1. 如何找到消息接收处理入口 可以通过broker的监听端口10911顺藤摸瓜式的 ...

  7. 浅析win32 Win64 x86 x64 区别 及Eclipse启动报Java was started but returned exit code=13 错误

    win32.x86_64是64位 X86就是  32位系统 X64 就是64位系统 最好记得方法就是带有64的就是64位,其余都是32位 为什么要讲这个呢? 如果是绿色版本的eclipse,在打开ec ...

  8. python安装和eclipse安装及环境变量配置

    非常好的技术文档,链接如下: http://jingyan.baidu.com/article/eb9f7b6da950c4869364e8f5.html 感谢作者的分享 http://python. ...

  9. [linux time命令学习篇] time 统计命令执行的时间

    注意: 命令后面一定要有分号; http://codingstandards.iteye.com/blog/798788 用途说明 time命令常用于测量一个命令的运行时间,注意不是用来显示和修改系统 ...

  10. [整理]linux中颜色的含义

    蓝色(Blue): Directory  目录 绿色(Green): Executable or recognized data file  可执行文件,可执行的程序 天蓝(Sky Blue): Sy ...