Codeforces Round #182 (Div. 1)D. Yaroslav and Divisors

64 篇文章 0 订阅
5 篇文章 0 订阅
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 liri (1 ≤ li ≤ ri ≤ n).
  • The answer to the query li, ri is the number of pairs of integers qw (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 cincout streams or the %I64dspecifier.

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

题意:给一个1-n,n个数的序列,然后查询一个区间[l,r],问这个区间内有多少对:一个数是另外一个数的约数
解题思路:这种询问什么的尤其加上修改,遇到基本GG,没有什么特别好的操作,由于这道题是1~n所以可以将每个数字的位置存在下来,然后对于每个数字来说,找到每个数字所有约数的区间长度
这样我们就可以利用树状数组进行存储,然后将询问存起来,对于每个询问,我们只需要那些区间完全存在于询问区间的个数,所以,需要用树状数组在遍历的同时,进行维护,值得注意的是,在操作
的时候,要将询问和约数区间按照区间左半边进行排序,这样可以降低时间复杂度

#include<iostream>  
#include<cstdio>
#include<stdio.h>
#include<cstring>
#include<cstdio>
#include<climits>   
#include<cmath> 
#include<vector>  
#include <bitset>  
#include<algorithm>    
#include <queue>  
#include<map> 
#define inf 9999999; 
using namespace std;


int pos[200005],a[200005],p[200005];
int n,m;
struct point
{
	int l,r,k,sum;
}check[5000005],q[200005];
int lowbit(int x) {
	return x&(-x);
}
void adds(int x,int y)
{
	while(x<=n)
	{
		p[x]+=y;
		x+=lowbit(x);
	}
}
int query(int x)
{
	int ans=0;
	while(x>0)
	{
		ans+=p[x];
		x-=lowbit(x);
	}
	return ans;
}
bool cmp(point x,point y)
{
	if(x.l==y.l)
		return x.r<y.r;
	return x.l<y.l;
}
bool cmp1(point x,point y)
{
	return x.k<y.k;
}
int main()
{
	int i,cnt,j,head,now;
	scanf("%d%d",&n,&m);
	//cin>>n>>m;
	for(i=1;i<=n;i++)
	{
		cin>>a[i];
		pos[a[i]]=i;
	}
	cnt=0;
	for(i=1;i<=n;i++)
	{
		for(j=1;i*j<=n;j++)
		{
			check[++cnt].l=min(pos[i],pos[i*j]);
			check[cnt].r=max(pos[i],pos[i*j]);
			adds(check[cnt].r,1);
		}
	}
	sort(check+1,check+1+cnt,cmp);
	for(i=1;i<=m;i++)
	{
		scanf("%d%d",&q[i].l,&q[i].r);
		//cin>>q[i].l>>q[i].r;
		q[i].k=i;
	}
	sort(q+1,q+1+m,cmp);
	head=now=1;
	for(i=1;i<=m;i++)
	{
		while(head<q[i].l) head++;
		while(check[now].l<head)
		{
			adds(check[now].r,-1);
			now++;
		}
		q[i].sum=query(q[i].r);
	}
	sort(q+1,q+1+m,cmp1);
	for(i=1;i<=m;i++)
	{	printf("%d\n",q[i].sum);
		//cout<<q[i].sum<<endl;
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值