题目链接:

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

Solution

大力分块..

对序列分块,维护块内前缀和、块的前缀和,修改时暴力维护两个前缀和,询问单点答案就可以$O(1)$得到。

再对函数分块,维护每块函数的答案、每个位置对每块函数的贡献次数,贡献次数并不会发生改变,修改时只需要暴力修改$\sqrt N$块函数答案。

要开unsigned long long!!!

Code

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
inline int read()
{
int x=0,f=1; char ch=getchar();
while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();}
while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();}
return x*f;
} #define MAXN 100010
#define LL unsigned long long int N,M,val[MAXN],pl[MAXN],pr[MAXN]; struct Block{
int l,r,num[MAXN]; LL ans,sum[510];
}b[510];
int belong[MAXN],rk[MAXN],Bn,Bs;
LL Sum[MAXN]; inline void Getsum(int x)
{
LL t=0;
for (int i=b[x].l; i<=b[x].r; i++) {
t+=val[i];
b[x].sum[rk[i]]=t;
}
b[x].sum[Bs]=t;
for (int i=x; i<=Bn; i++) Sum[i]=Sum[i-1]+b[i].sum[Bs];
} inline LL Query(int x,int y)
{
int bx=belong[x],by=belong[y];
if (bx==by)
return b[bx].sum[rk[y]]-b[bx].sum[rk[x]-1];
else
if (bx+1==by)
return b[by].sum[rk[y]]-b[bx].sum[rk[x]-1]+b[bx].sum[Bs];
else
return b[by].sum[rk[y]]+Sum[by-1]-Sum[bx-1]-b[bx].sum[rk[x]-1];
} inline void Getnum(int x)
{
for (int i=b[x].l; i<=b[x].r; i++) {
b[x].num[pl[i]]++; b[x].num[pr[i]+1]--;
b[x].ans+=Query(pl[i],pr[i]);
}
for (int i=1; i<=N; i++) b[x].num[i]+=b[x].num[i-1];
} int main()
{
N=read();
for (int i=1; i<=N; i++) val[i]=read();
for (int i=1; i<=N; i++) pl[i]=read(),pr[i]=read(); Bs=int(sqrt(N));
for (int i=1; i<=N; i++) {
if ((i-1)%Bs==0) Bn++,b[Bn].l=i;
belong[i]=Bn; rk[i]=(i-1)%Bs+1;
if ((i-1)%Bs==Bs-1 || i==N) b[Bn].r=i;
} for (int i=1; i<=Bn; i++) Getsum(i);
for (int i=1; i<=Bn; i++) Getnum(i); M=read();
while (M--) {
int opt=read(),x=read(),y=read(),z;
if (opt==1) {
z=y-val[x];
val[x]=y;
Getsum(belong[x]);
for (int i=1; i<=Bn; i++) b[i].ans+=(LL)b[i].num[x]*z;
} else {
int bx=belong[x],by=belong[y];
LL ans=0;
if (bx==by || bx+1==by)
for (int i=x; i<=y; i++) ans+=Query(pl[i],pr[i]);
else {
for (int i=bx+1; i<=by-1; i++) ans+=b[i].ans;
for (int i=x; i<b[bx+1].l; i++) ans+=Query(pl[i],pr[i]);
for (int i=b[by-1].r+1; i<=y; i++) ans+=Query(pl[i],pr[i]);
}
printf("%llu\n",ans);
}
} return 0;
}

  

【Codechef-Hard】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 - FNCS Chef and Churu(分块)

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

  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 SEAARC Sereja and Arcs (分块、组合计数)

    我现在真的什么都不会了呢...... 题目链接: https://www.codechef.com/problems/SEAARC 好吧,这题其实考察的是枚举的功力-- 题目要求的是\(ABAB\)的 ...

随机推荐

  1. myBatis 3.2.7 如何打印 sql

    该文中使用的log框架为logback myBatis3.0.6左右的版本时 打印sql的时候只需要配置如下属性: <logger name="java.sql.Connection& ...

  2. 如何使用ASP.NET开发基于推技术的聊天室?

    public class Content : System.Web.UI.Page{private void Page_Load(object sender, System.EventArgs e){ ...

  3. HDU 1308 What Day Is It?(模拟题)

    解题报告:输入一个年月日,让你求出那一天是星期几,但是做这题之前必须先了解一点历史.首先在1582年之前,判断是否是闰年的标准是只要能被四整除就是闰年, 然后在1752年9月2号的后的11天被抹去了, ...

  4. Maven私服安装及配置——(十二)

    0.私服实际是B/S架构的,需要通过浏览器访问.访问地址在 nexus-2.12.0-01\conf\nexus.properties中查看.

  5. 运动规划——PT and PVT 模式

    最近项目成员在做机器人上层开发时,经常说到PT模式和PVT模式.一直没搞懂这两种模式的实际差别.上网检索进行学习...... ref link:https://blog.csdn.net/m0_376 ...

  6. shell函数中eof报错(warning: here-document at line 9 delimited by end-of-file (wanted `EOF'))

    在shell编写函数时,函数中有eof和EOF,如果是在sublime编写按照格式tab缩进会有以下报错 解决办法: 取消函数中的tab缩进,在运行即可

  7. Gitlab & Github

    windwos上Git的使用 软件下载地址:https://github.com/git-for-windows/git/releases/download/v2.15.1.windows.2/Git ...

  8. sublime text 3 使用简介

    2014年1月22日 09:47:50 2用了一段时间感觉不错,就是自带的高亮显示匹配标签或者代码块儿时有点儿不清楚,所以一直是sublime 开PHP,notepad++开html 现在想只用一个编 ...

  9. 深入理解java虚拟机-00

    这本书买了有两年了,只有买回来翻了两页...今天电脑有点卡,游戏玩不了了,就来看看这本书. 首先看了序言,这本书是第二版,讲解的jdk版本是1.7,现在公司用的1.8,而且1.8的改动也挺大的,不过在 ...

  10. 基于bootstrap的上传插件fileinput实现ajax异步上传功能(支持多文件上传预览拖拽)

    首先需要导入一些js和css文件 ? 1 2 3 4 5 6 <link href="__PUBLIC__/CSS/bootstrap.css" rel="exte ...