Codeforces 301D (顺序统计+树状数组)

D. Yaroslav and Divisors
time limit per test:2 seconds
memory limit per test:256 megabytes
input:standard input
output:standard output

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
1
2
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],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[N],q2[N];

int n,m,a[N],pos[N];
LL ans[N];

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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值