Codeforces 301D (顺序统计+树状数组)
2017-07-16 19:09
211 查看
[align=center]D. Yaroslav and Divisors[/align]
[align=center]time limit per test:2 seconds[/align]
[align=center]memory limit per test:256 megabytes[/align]
[align=center]input:standard input[/align]
[align=center]output:standard output[/align]
[align=center]
[/align]
Yaroslav has an array p = p1, p2, ..., pn
(1 ≤ pi ≤ n), consisting of
n distinct integers. Also, he has
m queries:
Query number i is represented as a pair of integers
li,
ri
(1 ≤ li ≤ ri ≤ n).
The answer to the query li, ri is the number of pairs
of integers q, w
(li ≤ q, w ≤ ri) such that
pq is the divisor of
pw.
Help Yaroslav, answer all his queries.
Input
The first line contains the integers n and
m (1 ≤ n, m ≤ 2·105). The second line contains
n distinct integers
p1, p2, ..., pn
(1 ≤ pi ≤ n). The following
m lines contain Yaroslav's queries. The
i-th line contains integers li, ri
(1 ≤ li ≤ ri ≤ n).
Output
Print m integers — the answers to Yaroslav's queries in the order they appear in the input.
Please, do not use the %lld specifier to read or write 64-bit integers in
C++. It is preferred to use the
cin, cout streams or the
%I64d specifier.
Examples
Input
Output
Input
Output
Codeforces还是有一些很难的题的……这道题就真的不简单……
给你一组n个数字,这些数字是1~n的一个排列。现在有很多个询问,询问给定一个区间,问区间内有多少个整除对。
说实话,根本想不到是用树状数组的题目……求整除对,怎么都像是数论的题目,为什么和树状数组有关。后来才知道统计的力量!
首先,我们设sum[i],表示区间[1,i]的整除对数量,自然而然的,我们就会认为对于区间[l,r],它的整除对数量可能是sum[r]-sum[l-1],我们不妨设这个数字为x。当然了,整除对数目肯定不是这么简单的,因为显然我们多算了一个数字在[1,l]区间,而另一个数字在[l,r]区间内的整除对。那么我们要做的就是要消除这个多余的,不妨设这个多余的部分为y,则最后结果就是x-y。
对于一个数字i,他在1~n内的倍数有n/i个,意味着对应i的倍数都可以与他组成整除对。那么我们便可以利用这个性质计算之前所说的y。顺序枚举这个i,对于q.l=i的询问,sum[q.r]-sum[q,l-1]就是y,然后再把所有i的倍数的sum值加1。这样子说可能不太好理解,自己用一组数据尝试就会知道,其实y就是在q.l和q.r之间的数字的倍数没有加进sum之前的值,然后每次把位置i的数字的倍数加入sum中。至于x,则是类似,当q.r=i的时候,即区间右端点前所有数字的倍数已经加进sum中之后,x=sum[q.r]-sum[q,l-1],因为再往后的值对该区间已经不会再产生影响了。
现在已经很明显了,需要以个支持单点修改,区间查询的数据结构,树状数组为上策。然后具体写的时候也有点讲究的,不然在枚举区间的时候容易把复杂度退化到O(N^2)具体代码如下:
[align=center]time limit per test:2 seconds[/align]
[align=center]memory limit per test:256 megabytes[/align]
[align=center]input:standard input[/align]
[align=center]output:standard output[/align]
[align=center]
[/align]
Yaroslav has an array p = p1, p2, ..., pn
(1 ≤ pi ≤ n), consisting of
n distinct integers. Also, he has
m queries:
Query number i is represented as a pair of integers
li,
ri
(1 ≤ li ≤ ri ≤ n).
The answer to the query li, ri is the number of pairs
of integers q, w
(li ≤ q, w ≤ ri) such that
pq is the divisor of
pw.
Help Yaroslav, answer all his queries.
Input
The first line contains the integers n and
m (1 ≤ n, m ≤ 2·105). The second line contains
n distinct integers
p1, p2, ..., pn
(1 ≤ pi ≤ n). The following
m lines contain Yaroslav's queries. The
i-th line contains integers li, ri
(1 ≤ li ≤ ri ≤ n).
Output
Print m integers — the answers to Yaroslav's queries in the order they appear in the input.
Please, do not use the %lld specifier to read or write 64-bit integers in
C++. It is preferred to use the
cin, cout streams or the
%I64d specifier.
Examples
Input
1 1 1 1 1
Output
1
Input
10 9 1 2 3 4 5 6 7 8 9 10 1 10 2 9 3 8 4 7 5 6 2 2 9 10 5 10 4 10
Output
27
14
8
4
2
12
7
9
Codeforces还是有一些很难的题的……这道题就真的不简单……
给你一组n个数字,这些数字是1~n的一个排列。现在有很多个询问,询问给定一个区间,问区间内有多少个整除对。
说实话,根本想不到是用树状数组的题目……求整除对,怎么都像是数论的题目,为什么和树状数组有关。后来才知道统计的力量!
首先,我们设sum[i],表示区间[1,i]的整除对数量,自然而然的,我们就会认为对于区间[l,r],它的整除对数量可能是sum[r]-sum[l-1],我们不妨设这个数字为x。当然了,整除对数目肯定不是这么简单的,因为显然我们多算了一个数字在[1,l]区间,而另一个数字在[l,r]区间内的整除对。那么我们要做的就是要消除这个多余的,不妨设这个多余的部分为y,则最后结果就是x-y。
对于一个数字i,他在1~n内的倍数有n/i个,意味着对应i的倍数都可以与他组成整除对。那么我们便可以利用这个性质计算之前所说的y。顺序枚举这个i,对于q.l=i的询问,sum[q.r]-sum[q,l-1]就是y,然后再把所有i的倍数的sum值加1。这样子说可能不太好理解,自己用一组数据尝试就会知道,其实y就是在q.l和q.r之间的数字的倍数没有加进sum之前的值,然后每次把位置i的数字的倍数加入sum中。至于x,则是类似,当q.r=i的时候,即区间右端点前所有数字的倍数已经加进sum中之后,x=sum[q.r]-sum[q,l-1],因为再往后的值对该区间已经不会再产生影响了。
现在已经很明显了,需要以个支持单点修改,区间查询的数据结构,树状数组为上策。然后具体写的时候也有点讲究的,不然在枚举区间的时候容易把复杂度退化到O(N^2)具体代码如下:
#include<bits/stdc++.h>
#define LL long long
#define N 210000
using namespace std;
struct BinaryIndexedTree
{
LL c
,n;
inline void init()
{
memset(c,0,sizeof(c));
}
inline int lowbit(int x)
{
return x&-x;
}
inline void update(int x,int k)
{
while (x<=n)
{
c[x]+=k;
x+=lowbit(x);
}
}
inline LL getsum(int x)
{
LL ans=0;
while (x>0)
{
ans+=c[x];
x-=lowbit(x);
}
return ans;
}
} BIT;
struct query
{
int l,r,id;
} q1,q2
;
int n,m,a
,pos
;
LL ans
;
bool cmp1(query a,query b)
{
return a.l==b.l ? a.r<b.r : a.l<b.l;
}
bool cmp2(query a,query b)
{
return a.r==b.r ? a.l<b.l : a.r<b.r;
}
int main()
{
while (~scanf("%d%d",&n,&m))
{
BIT.n=n; BIT.init();
memset(ans,0,sizeof(ans));
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
pos[a[i]]=i;
}
for(int i=1;i<=m;i++)
{
scanf("%d%d",&q1[i].l,&q1[i].r);
q2[i]=q1[i];q1[i].id=q2[i].id=i;
}
int i,j,k;
sort(q1+1,q1+1+m,cmp1); //提前把询问按照端点排序,使得找起来方便
sort(q2+1,q2+1+m,cmp2); //一个是左端点优先,一个是右端点优先
for(i=1,j=1,k=1;i<=n;i++)
{
for(;q1[j].l==i&&j<=m;j++) //找左端点为i的询问,由于排序了所以依次遍历即可
ans[q1[j].id]-=BIT.getsum(q1[j].r)-BIT.getsum(q1[j].l-1); //计算y
for(int p=1;a[i]*p<=n;p++)
BIT.update(pos[a[i]*p],1); //把位置i的数字的倍数加入sum中
for(;q2[k].r==i&&k<=m;k++) //找右端点为i的询问
ans[q2[k].id]+=BIT.getsum(q2[k].r)-BIT.getsum(q2[k].l-1); //计算x
}
for(int i=1;i<=m;i++)
printf("%I64d\n",ans[i]);
}
return 0;
}
相关文章推荐
- CodeForces 1209 B. Jury Size 树状数组处理区间统计问题
- CodeForces 1208 B. Physics Practical 区间统计,树状数组
- 如果四位数各位上的数字均是0或2或4或6或8,则统计出满足此条件的个数cnt,并把这些四位数按从大到小的顺序存入数组b中。(160514)
- 【codeforces】293E. Close Vertices 点分治+树状数组
- 统计一个字符串中每个字符出现的次数,并按自然顺序输出
- CodeForces - 668D Little Artem and Time Machine(线段树||树状数组)
- CodeForces - 830B Cards Sorting(贪心+离散化+树状数组)
- 最大最小值以及前驱后继操作最坏情况都为O(1)的顺序统计树
- 第二部分 排序与顺序统计
- (HDU 5792)2016 Multi-University Training Contest 5 World is Exploding (逆序数、顺序数、树状数组)
- Codeforces 369E Valera and Queries --树状数组+离线操作
- SQL Server 执行计划利用统计信息对数据行的预估原理二(为什么复合索引列顺序会影响到执行计划对数据行的预估)
- 算法之动态顺序统计
- 算法导论14.1动态顺序统计 练习总结
- 【算法导论】第六课 顺序统计,中值
- codeforces--707E. Garlands(二维树状数组)
- Codeforces 101B Buses 排序+树状数组
- Codeforces 869 E. The Untended Antiquity 二维树状数组
- Codeforces 570D Tree Requests(DFS重标号+树状数组)
- 《算法导论》第14章 数据结构的扩张 (1)动态顺序统计