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

4 4
Query 2 3
Add 1 1
Query 2 3
Query 1 4

Sample Output

0
2
4

HINT

Source


这道题出自2013年国家队候选队员罗剑桥的论文《浅谈分块思想在一类数据处理问题中的应用》,是罗原创的一道题。

这道题的解法是分块:

将整个区间从左往右每$\lceil\sqrt{n}\rceil$个分成一块。

更新:

将$ADD\quad D \ X$操作分成两类

  1.   $D \ge \lceil\sqrt{n} \rceil$ 的$ADD$操作,直接更新序列相应位置上元素,并更新各元素所属块由这类$ADD$操作所贡献的和,复杂度是$O(\sqrt{n})$。
  2. $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?的更多相关文章

  1. BestCoder#16 A-Revenge of Segment Tree

    Revenge of Segment Tree Problem Description In computer science, a segment tree is a tree data struc ...

  2. [LintCode] Segment Tree Build II 建立线段树之二

    The structure of Segment Tree is a binary tree which each node has two attributes startand end denot ...

  3. [LintCode] Segment Tree Build 建立线段树

    The structure of Segment Tree is a binary tree which each node has two attributes start and end deno ...

  4. Segment Tree Modify

    For a Maximum Segment Tree, which each node has an extra value max to store the maximum value in thi ...

  5. 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), ...

  6. Segment Tree Build I & II

    Segment Tree Build I The structure of Segment Tree is a binary tree which each node has two attribut ...

  7. Lintcode: Segment Tree Query II

    For an array, we can build a SegmentTree for it, each node stores an extra attribute count to denote ...

  8. Lintcode: Segment Tree Modify

    For a Maximum Segment Tree, which each node has an extra value max to store the maximum value in thi ...

  9. 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 ...

随机推荐

  1. Web Storage中的sessionStorage和localStorage

    html5中的Web Storage包括了两种存储方式:sessionStorage和localStorage. sessionStorage用于本地存储一个会话(session)中的数据,这些数据只 ...

  2. swift代理使用

    代理声明: //oc调用代理 @objc(NurseListCellDelegate) protocol NurseListCellDelegate : NSObjectProtocol{ func ...

  3. 实战:ADFS3.0单点登录系列-总览

    本系列将以一个实际项目为背景,介绍如何使用ADFS3.0实现SSO.其中包括SharePoint,MVC,Exchange等应用程序的SSO集成. 整个系列将会由如下几个部分构成: 实战:ADFS3. ...

  4. Elasticsearch 相关名词理解

    Cluster包含多个node,Indices不应该理解成动词索引,Indices可理解成关系数据库中的databases,Indices可包含多个Index,Index对应关系数据库中的databa ...

  5. C++ c++与C语言的区别(实用性增强,register关键字增强,全局变量检测增强)

    //区别①:实用性增强 #include<iostream> using namespace std; //C语言中的变量都必须在作用域开始的位置定义!! //C++中更强调语言的“实用性 ...

  6. DWZ(JUI)的lookupGroup增加回调函数

    DWZ 是一个很好的富客户端框架 lookupGroup也是一个必用到的东东,但没有回调函数,后期处理相当的不便. 修改其dwz.database.js 增加几行,就能实行一个很好的回调. 使用就方便 ...

  7. XML CDATA的作用

    操作XML文件时,如果允许用户输入内容,例如∶"< ".">"."/".""等,当生成XML时,会破坏了XM ...

  8. listview的头布局把我的ACTION_DOWN事件给吃了.....

    因为头布局的viewpager自己处理点击事件 public boolean dispatchTouchEvent(MotionEvent ev) { switch (ev.getAction()) ...

  9. Android中对JSONArray数组的指定项进行删除,更新。

    首先假设有这么一个JSONArray JSONArray Array1;JSONArray ITEM = new JSONArray(); name为你获取要删除的字段名称,IETM就是你删除后得到的 ...

  10. java系列: 在eclipse中调试时,输入的jsp或者servlet页面的地址要区分大小写

    比如在当前web工程中有一个jsp页面的名字是: Welcome.jsp 在eclipse中调试时,如果在浏览器中输入: http://localhost:8080/MavenWeb/welcome. ...