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 ...
随机推荐
- 26Spring_的注解实际应用_关键整理一下之前的注解
写一个银行转账案例, 案例结构如下:
- js运动框架 step by step
开启setInterval定时器之前,请先清除之前的定时器 window.onload = function() { var btn = document.getElementById('btn'); ...
- Asp.net通过Jquery操作WebService进行Ajax读写
一说到开始,我们当然需要项目. 首先,创建一个Asp.net Web 应用,然后新增一个名称为“Web 服务”的文件,也就是后缀名为".asmx"的文件,然后需要进行配置一下,在W ...
- 抓包工具charles的使用
Charles是一款抓包修改工具,数据请求控制容易,操作简单. 下载和安装 首先是工具下载和安装 安装前需要先有Java的运行环境.下载到charles的破解版以后,正常安装.一般破解版里会有char ...
- JS replace()方法-字符串首字母大写
replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串. replace()方法有两个参数,第一个参数是正则表达式,正则表达式如果带全局标志/g,则是代表替换 ...
- caffe windows学习:第一个测试程序
caffe windows编译成功后,就可以开始进行测试了.如果还没有编译成功的,请参考:caffe windows 学习第一步:编译和安装(vs2012+win 64) 一般第一个测试都是建议对手写 ...
- 从0开始学Java——@override的作用
早上跟着<jsp&Servlet学习笔记>来学习jsp,在使用eclipse创建了一个servlet类之后,发现自动创建的类和书上相比,doGet方法的前面少了@override, ...
- [译]Canvas的基本用法
在本文章中 <canvas> 元素 替换内容 </canvas> 标签不可省 渲染上下文(The rendering context如何翻译) 检查支持性 一个模板骨架 一个简 ...
- 利用javascript和WebGL绘制地球 【翻译】
利用javascript和WebGL绘制地球 [翻译] 原翻译:利用javascript和WebGL绘制地球 [翻译] 在我们所有已知的HTML5API中,WebGL可能是最有意思的一个,利用这个AP ...
- Ubuntu安装uget和aria2下载工具
Windows下的下载工具有迅雷,快车等.Ubuntu下也有强大的下载工具uget和aria2. 一.安装.uget和aria2都可以在“软件中心”中安装,但是版本太老啦,所以最好还是在终端中添加pp ...