【Codechef-Hard】Chef and Churu 分块
题目链接:
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 分块的更多相关文章
- 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 - FNCS Chef and Churu(分块)
https://vjudge.net/problem/CodeChef-FNCS 题意: 思路: 用分块的方法,对每个函数进行分块,计算出该分块里每个数的个数,这样的话也就能很方便的计算出这个分块里所 ...
- 【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 SEAARC Sereja and Arcs (分块、组合计数)
我现在真的什么都不会了呢...... 题目链接: https://www.codechef.com/problems/SEAARC 好吧,这题其实考察的是枚举的功力-- 题目要求的是\(ABAB\)的 ...
随机推荐
- JAVA记录-Servlet RequestDispatcher请求转发
RequestDispatcher接口提供将请求转发送到另一个资源的功能,它可能是html,servlet或jsp等. 此接口也可用于包括另一资源的内容.它是servlet协作的一种方式. 在Requ ...
- JS面向对象编程之对象(简化版)
上次网上看了一篇这个文章,然后乱七八糟晕头转向把我晕的够呛.看了半天没找到错的地方但是浏览器Hello world就是没有定义...我也是醉了,最后发现我认为是废话的话一句话竟然有用!!!所以我还是简 ...
- IOS 与 PHP 通信加密,使用AES 128 CBC no padding
这个网上的资料真实浩如烟海,但是真正有价值的屈指可数 自己尝试了一天多,终于还是搞定了. 再次要感谢网上的前辈么. 比如下面这个关于php和java端的实现: http://my.oschina.ne ...
- Jquery 较好的效果
仿google图片效果图片展示相册(jquery)的演示页面 产品相册展示插件slideshow多图可翻页 懒人建站 Jquery分享A Jquery分享B Jquery分享C Jquery分享D
- AngularJS入门基础——表单验证
<form name="form" novalidata> <label name="email">your email</l ...
- struts的理解
1.struts是一个按MVC模式设计的Web层框架,其实他就是一个大大的servlet,这个Servlet名为ActionServlet,或是ActionServlet的子类.我们可以在web.xm ...
- webp实践的javascript检测方案
function hasWebp () { // 查看Cookie,如果没有则进行以下逻辑 var img = new Image(); img.onload = handleSupport; img ...
- c++刷题(39/100)笔试题3
题目1: 现在你需要用一台奇怪的打字机书写一封书信.信的每行只能容纳宽度为100的字符,也就是说如果写下某个字符会导致行宽超过100,那么就要另起一行书写 信的内容由a-z的26个小写字母构成,而每个 ...
- 解决centos7下tomcat启动正常,无法访问项目的问题
centos7防火墙不再采用iptables命令,改用firewalld 禁用防火墙命令: # systemctl stop firewalld.service # systemctl disable ...
- Entity Framework 6.1.0 Tools for Visual Studio 2012 & 2013
http://www.microsoft.com/en-us/download/confirmation.aspx?id=40762