题意:

单点修改$a$

询问$a$的区间和$f$的区间和


原来普通计算机是这道题改编的吧...

对$f$分块,预处理$c[i][j]$为块i中$a_j$出现几次,$O(NH(N))$,只要每个块差分加上然后扫一遍就行了不用树状数组之类的

修改,整块直接改,还要单点修改$a$

查询,整块直接查,两边暴力查询$a$的区间和

对$a$的操作可以用树状数组,也可以再分块维护前缀和实现$O(N)-O(1)$

这样不用带一个log总复杂度$O(NH(N))$

实测快了0.4s

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int N=1e5+,M=;
typedef unsigned long long ll;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
int n,Q,op,x,y;
ll a[N],s[N];
int pos[N],m,block;
struct _Block{int l,r;} b[M], f[N];
inline void iniBlock(){
block=sqrt(n); m=(n-)/block+;
for(int i=;i<=n;i++) pos[i]=(i-)/block+;
for(int i=;i<=m;i++) b[i].l=(i-)*block+,b[i].r=i*block;
b[m].r=n;
}
struct BlockA{
ll add[M];
void Add(int x,ll v){
for(int i=x;i<=b[pos[x]].r;i++) s[i]+=v;
for(int i=pos[x]+;i<=m;i++) add[i]+=v;
}
ll fun(_Block &x){
return s[x.r]+add[ pos[x.r] ] - (s[x.l-]+add[ pos[x.l-] ]);
}
}A;
struct Block{
int c[M][N],s[N];
ll sum[N];
void ini(){
for(int i=;i<=m;i++){
for(int j=b[i].l ; j<=b[i].r ; j++)
s[ f[j].l ]++,s[ f[j].r+ ]--;
int *cc=c[i];
for(int j=;j<=n;j++)
cc[j]=cc[j-]+s[j],sum[i]+=cc[j]*a[j],s[j]=;
}
}
void cha(int x,ll v){
ll t=v-a[x]; A.Add(x,t);
for(int i=;i<=m;i++) sum[i]+=c[i][x]*t;
a[x]=v;
}
ll que(int l,int r){
ll re=;
if(pos[l]==pos[r])
for(int i=l;i<=r;i++) re+=A.fun(f[i]);
else{
for(int i=pos[l]+;i<=pos[r]-;i++) re+=sum[i];
for(int i=l;i<=b[pos[l]].r;i++) re+=A.fun(f[i]);
for(int i=b[pos[r]].l;i<=r;i++) re+=A.fun(f[i]);
}
return re;
}
}B;
int main(){
freopen("in","r",stdin);
n=read();
for(int i=;i<=n;i++) a[i]=read(),s[i]=s[i-]+a[i];
for(int i=;i<=n;i++) f[i].l=read(),f[i].r=read();
iniBlock();B.ini();
Q=read();
while(Q--){
op=read();x=read();y=read();
if(op==) B.cha(x,y);
else printf("%llu\n",B.que(x,y));
}
}
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define pii pair<int,int>
#define MP make_pair
#define fir first
#define sec second
const int N=1e5+,M=;
typedef unsigned long long ll;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
int n,Q,op,x,y;
ll a[N];
pii f[N];
struct BIT{
ll c[N];
inline void add(int p,ll v){for(;p<=n;p+=(p&-p)) c[p]+=v;}
inline ll sum(int p){ll re=;for(;p;p-=(p&-p)) re+=c[p];return re;}
}C;
inline ll fun(int x){return C.sum(f[x].sec)-C.sum(f[x].fir-);}
struct _Block{int l,r;ll sum;};
struct Block{
int block,m,pos[N],c[M][N],s[N];
_Block b[M];
void ini(){
block=sqrt(n); m=(n-)/block+;
for(int i=;i<=n;i++) pos[i]=(i-)/block+; for(int i=;i<=m;i++){
b[i].l=(i-)*block+; b[i].r= i==m ? n : i*block;
for(int j=b[i].l ; j<=b[i].r ; j++)
s[ f[j].fir ]++,s[ f[j].sec+ ]--;
int *cc=c[i];
for(int j=;j<=n;j++) cc[j]=cc[j-]+s[j],b[i].sum+=cc[j]*a[j],s[j]=;
}
}
void cha(int x,ll v){
v-=a[x];
for(int i=;i<=m;i++) b[i].sum+=c[i][x]*v;
C.add(x,v); a[x]+=v;
}
ll que(int l,int r){
ll re=;
if(pos[l]==pos[r])
for(int i=l;i<=r;i++) re+=fun(i);
else{
for(int i=pos[l]+;i<=pos[r]-;i++) re+=b[i].sum;
for(int i=l;i<=b[pos[l]].r;i++) re+=fun(i);
for(int i=b[pos[r]].l;i<=r;i++) re+=fun(i);
}
return re;
}
}B;
int main(){
freopen("in","r",stdin);
n=read();
for(int i=;i<=n;i++) a[i]=read(),C.add(i,a[i]);
for(int i=;i<=n;i++) f[i].fir=read(),f[i].sec=read();
B.ini();
Q=read();
while(Q--){
op=read();x=read();y=read();
if(op==) B.cha(x,y);
else printf("%llu\n",B.que(x,y));
}
}

BIT

CodeChef Chef and Churu [分块]的更多相关文章

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

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

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

  3. Codechef FNCS Chef and Churu

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

  4. CodeChef:Chef and Problems(分块)

    CodeChef:Chef and Problems 题目大意 有一个长度为n的序列$a_1,a_2,……,a_n$,每次给出一个区间[l,r],求在区间内两个相等的数的最远距离($max(j-i,满 ...

  5. [CC-FNCS]Chef and Churu

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

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

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

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

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

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

  9. CODECHEF Chef and Churus 解题报告

    [CODECHEF]Chef and Churus Description 有一个长度为\(n\)的数组\(A\),有\(n\)个函数,第\(i\)个函数的值为\(\sum_{j=l_i}^{r_i} ...

随机推荐

  1. win10清除桌面快捷方式小箭头

    reg add /d "%systemroot%\system32\imageres.dll,197" /t reg_sz /f taskkill /f /im explorer. ...

  2. TI-RTOS 定时器的使用

    定时器 在TI-RTOS中属于内核的一部分,因此想了解它的使用还是要阅读Bios_User_Guide.pdf. 主要用到这么几个API, 加粗字体 更多的定义可以在 ..\packages\ti\s ...

  3. mybatis sql循环的使用

    foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合. foreach元素的属性主要有 item,index,collection,open,separator,close. ...

  4. logback的使用和logback.xml详解

    一.logback的介绍 Logback是由log4j创始人设计的另一个开源日志组件,官方网站: http://logback.qos.ch.它当前分为下面下个模块: logback-core:其它两 ...

  5. GitHub上传文件或项目的教程

    既然是往GitHub上传文件,那GitHub账号必须得有,这时候就会有同学问:妖怪吧,我没有GitHub账号怎么办? 别急别急,打开GitHub网站https://github.com/,然后注册就O ...

  6. 启动tomcat时,一直卡在Deploying web application directory这块的解决方案

    本来今天正常往服务器上扔一个tomcat 部署一个项目的, 最后再启动tomcat 的时候 发现项目一直都访问不了,看了一下日志: [root@iz8vbdzx7y7owm488t4d89z bin] ...

  7. Screen命令安装使用教程

    在安装lnmp之前,我们一般先运行一下Screen程序,因为screen好像一个容器一样,把lnmp的安装过程保护了起来.以CentOS中安装lnmp为例,程序下载.编译都需要比较长的时间,如果中途遇 ...

  8. 虚拟主机、VPS以及云主机的区别和对比

    对于很多需要建网站的朋友来说,虚拟主机是必须要了解的基础知识.虚拟主机相对于VPS与云主机来说出现的较早,也是被大多数站长所了解的主机.很多人容易将这三者混淆,弄不清楚三者的联系与区别.那么虚拟主机. ...

  9. LNMP一键安装包

    http://www.aliweihu.com/333.html LNMP一键安装包是什么? LNMP一键安装包是一个用Linux Shell编写的可以为CentOS/RadHat.Debian/Ub ...

  10. pthread_cond_wait的spurious wakeup问题

    最近在温习pthread的时候,忽然发现以前对pthread_cond_wait的了解太肤浅了.昨晚在看<Programming With POSIX Threads>的时候,看到了pth ...