DLUTOJ #1306 Segment Tree?
Description
有一个N个整数的序列(每个数的初值为0)。每个数都是整数。你有M次操作。操作有两种类型:
——Add Di Xi 从第一个数开始每隔Di 个位置增加Xi
——Query Li Ri 回答当前序列Li项到Ri项的和
Input
两个数N和M,输入到文件结尾。以下M行每行的输入两种操作形式的一种。(1 <= N, M, Di, Xi, Li, Ri <= 100000, Li <= Ri )
Output
对于每组数据,输出每组的询问的结果。
Sample Input
Sample Output
HINT
Source
这道题出自2013年国家队候选队员罗剑桥的论文《浅谈分块思想在一类数据处理问题中的应用》,是罗原创的一道题。
这道题的解法是分块:
将整个区间从左往右每$\lceil\sqrt{n}\rceil$个分成一块。
更新:
将$ADD\quad D \ X$操作分成两类
- $D \ge \lceil\sqrt{n} \rceil$ 的$ADD$操作,直接更新序列相应位置上元素,并更新各元素所属块由这类$ADD$操作所贡献的和,复杂度是$O(\sqrt{n})$。
- $D < \lceil \sqrt{n} \rceil$ 的$ADD$操作,我们将它记录在数组$sum[1\dots\lceil \sqrt{n} \rceil -1]$上:即对于$ADD \quad D \ X$,将$X$累加在$sum[D]$上,复杂度是$O(1)$。
查询:
对于查询区间$[L, R]$,分别查询上述两类$ADD$操作对$[L, R]$的贡献,相加即是答案。
Implementation:
这题我调了很长时间,先贴上第一版(有bug)代码
#include <bits/stdc++.h>
using namespace std; typedef long long LL;
const int N(1e5+);
int n, m, b;
LL bucket[N], sum[N], a[N];
char op[]; inline int ID(int x, int b){ //x>=0
return x? (x-)/b+: ;
} int main(){
for(int T=; ~scanf("%d%d", &n, &m); T++){
if(T==) for(;;);
memset(a, , sizeof(a));
memset(bucket, , sizeof(bucket));
memset(sum, , sizeof(sum));
b=sqrt(n);
for(int d, x, l, r; m--; ){
scanf("%s", op);
if(*op=='A'){
scanf("%d%d", &d, &x);
if(d>=b){
for(int i=; i<=n; i+=d)
a[i]+=x, bucket[ID(i, b)]+=x;
}
else sum[d]+=x;
}
else{
scanf("%d%d", &l, &r);
LL res=;
int L=ID(l-, b)+, R=ID(r+, b)-; //error-prone
////////////////////////////////////////
for(int i=l; i<=b*(L-); i++) res+=a[i];
for(int i=b*R+; i<=r; i++) res+=a[i];
////////////////////////////////////////
for(int i=L; i<=R; i++) res+=bucket[i];
for(int i=; i<b; i++){
res+=(ID(r, i)-ID(l-, i))*sum[i];
}
printf("%lld\n", res);
}
}
}
return ;
}
bug就在分离出来的那两行,坑暂时留着,以后填。
bug-free version
#include <bits/stdc++.h>
using namespace std; typedef long long LL;
const int N(1e5+);
int n, m, b, id[N];
LL bucket[N], sum[N], a[N];
char op[]; LL SUM(int x){
LL res=;
int R=id[x+]-;
for(int i=; i<=R; i++) res+=bucket[i];
for(int i=R*b+; i<=x; i++) res+=a[i];
for(int i=; i<b; i++) res+=((x-)/i+)*sum[i];
return res;
} int main(){
for(; ~scanf("%d%d", &n, &m); ){
memset(a, , sizeof(a));
memset(bucket, , sizeof(bucket));
memset(sum, , sizeof(sum));
b=sqrt(n);
for(int i=; i<=n+; i++) id[i]=(i-)/b+;
for(int d, x, l, r; m--; ){
scanf("%s", op);
if(*op=='A'){
scanf("%d%d", &d, &x);
if(d>=b) for(int i=; i<=n; i+=d) a[i]+=x, bucket[id[i]]+=x;
else sum[d]+=x;
}
else scanf("%d%d", &l, &r), printf("%lld\n", SUM(r)-SUM(l-));
}
}
return ;
}
DLUTOJ #1306 Segment Tree?的更多相关文章
- BestCoder#16 A-Revenge of Segment Tree
Revenge of Segment Tree Problem Description In computer science, a segment tree is a tree data struc ...
- [LintCode] Segment Tree Build II 建立线段树之二
The structure of Segment Tree is a binary tree which each node has two attributes startand end denot ...
- [LintCode] Segment Tree Build 建立线段树
The structure of Segment Tree is a binary tree which each node has two attributes start and end deno ...
- Segment Tree Modify
For a Maximum Segment Tree, which each node has an extra value max to store the maximum value in thi ...
- Segment Tree Query I & II
Segment Tree Query I For an integer array (index from 0 to n-1, where n is the size of this array), ...
- Segment Tree Build I & II
Segment Tree Build I The structure of Segment Tree is a binary tree which each node has two attribut ...
- Lintcode: Segment Tree Query II
For an array, we can build a SegmentTree for it, each node stores an extra attribute count to denote ...
- Lintcode: Segment Tree Modify
For a Maximum Segment Tree, which each node has an extra value max to store the maximum value in thi ...
- Lintcode: Segment Tree Query
For an integer array (index from 0 to n-1, where n is the size of this array), in the corresponding ...
随机推荐
- 斯坦福大学 iOS 7应用开发 ppt
上网的找了很久都不全,最后发现原来网易那个视频下面就有完整的PPT..
- Android中Intent传递对象的两种方法(Serializable,Parcelable)
今天要给大家讲一下Android中 Intent中如何传递对象,就我目前所知道的有两种方法,一种是Bundle.putSerializable(Key,Object);另一种是 Bundle.putP ...
- BFGS方法
今天看了 Nocedal 写的Numerical Optimization 中关于BFGS方法的介绍. BFGS方法有个近亲,叫做DFP方法.下面先介绍DFP方法. 这个方法的意图是找一种方法对Hes ...
- js常用宽高属性
document.body.clientWidth //body对象的宽度 document.body.clientHeight //body对象的高度 document.documentElemen ...
- 【转】【PNG压缩工具】PNG 图像的优化及压缩工具介绍
图像格式有许多种不同类型,在互联网上最常见的有JPEG.GIF.BMP.TIFF和PNG.每一种图像格式都有它自己的用途,比如GIF是用于动画的,JPEG是用于高清图片的,这种图片在保存或者调整大小后 ...
- [教程]Oracle 11g Express 安装和使用教程
使用工具的第一步就是安装工具,配置环境!下面就Oracle 11g Express的安装和简单实用做一简介. 一.下载安装过程 去oracle的官网下载Oracle 11g express,大概300 ...
- [CareerCup] 10.1 Client-facing Service 面向客户服务器
10.1 Imagine you are building some sort of service that will be called by up to 1000 client applicat ...
- 高层次综合(HLS)-简介
本文是我近段时间的学习总结,主要参考了Xilinx的技术文档以及部分网上其他资料.文档主要包括ug998<Introduction to FPGA Design Using High-Level ...
- libusb(.NET)开源项目使用小结
更多细节请参考官方帮助文档 1,修改设备类型为自己的标识 InfWizard项目里,改掉资源文件LibUsb-Win32-LUDN.Driver.Resources. 原来的三处libusb-win3 ...
- 千万不要在JS中使用连等赋值操作
前言 文章标题这句话原本是在国外某JavaScript规范里看到的,当时并没有引起足够的重视,直到最近一次出现了bug发现JS里的连等赋值操作的特色(坑). 网上搜索一番发现一个非常好的连等赋值的(来 ...