【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\)的 ...
随机推荐
- windows命令快捷启动应用-----window小技巧
前言 装逼的道路总是这么漫长 而又充满激情.对于崇尚技术的男儿,了解计算机的世界,是我一辈子都是在追寻的.看着各种黑客电影,有那个大牛还需要鼠标的辅助,想想都是那么的令人兴奋 为了有那么一天的到来,我 ...
- bzoj千题计划272:bzoj4557: [JLoi2016]侦察守卫
http://www.lydsy.com/JudgeOnline/problem.php?id=4557 假设当前到了x的子树,现在是合并 x的第k个子树 f[x][j] 表示x的前k-1个子树该覆盖 ...
- 5个经典的javascript面试问题
问题1:Scope作用范围 考虑下面的代码: (function() { var a = b = 5;})(); console.log(b); 什么会被打印在控制台上? 回答 上面的代码会打印 ...
- Linux - ssh 连接问题
SSH 连接方式 ssh -p 22 user@192.168.1.209 # 从linux ssh登录另一台linux ssh -p 22 root@192.168.1.209 CMD # 利用ss ...
- HDU 4627 The Unsolvable Problem 杭电多校联赛第三场1009 数学题
题意描述:给出一个n,要求在所有满足n = a+b的a和b里面求a和b的最小公倍数最大的两个数的最小公倍数. 解题报告:比赛的时候看到这个题的第一反应就是寻找这两个数一定是在a和b比较接近的地方找,这 ...
- CF258D Little Elephant and Broken Sorting (带技巧的DP)
题面 \(solution:\) 这道题主要难在考场上能否想到这个思路(即如何设置状态)(像我这样的蒟蒻就想不到呀QAQ)不过这一题确实很神奇! \(f[i][j]:\)表示第 \(a_i\) 个数比 ...
- 【干货】SIFT-Workstation 下载与安装 不跳过每一个细节部分
SIFT-Workstation.ova 下载地址https://digital-forensics.sans.org/community/download-sift-kit ov ...
- 第一篇:初始Golang
Golang简介 编程语言已经非常多,偏性能敏感的编译型语言有 C.C++.Java.C#.Delphi和Objective-C 等,偏快速业务开发的动态解析型语言有PHP.Python.Perl.R ...
- urb传输的代码分析【转】
转自:http://blog.csdn.net/zkami/article/details/2503829 urb传输的代码分析 如需引用,请注明出处blog.csdn.net/zkami 作者Zhe ...
- 使用java如何操作elasticsearch?简单示例。
在线API:https://www.elastic.co/guide/en/elasticsearch/client/java-api/2.4/transport-client.html教程:http ...