hdu4777 Rabbit Kingdom (离线处理、互斥)

13 篇文章 0 订阅
4 篇文章 0 订阅

F.A.Q
Hand In Hand
Online Acmers
Forum | Discuss
Statistical Charts
Problem Archive
Realtime Judge Status
Authors Ranklist
 
      C/C++/Java Exams     
ACM Steps
Go to Job
Contest LiveCast
ICPC@China
Best Coder beta
VIP | STD Contests
Virtual Contests 
    DIY | Web-DIY beta
Recent Contests
Author ID 
Password 
  Register new ID

Rabbit Kingdom

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2615    Accepted Submission(s): 884


Problem Description
  Long long ago, there was an ancient rabbit kingdom in the forest. Every rabbit in this kingdom was not cute but totally pugnacious, so the kingdom was in chaos in season and out of season.
  n rabbits were numbered form 1 to n. All rabbits' weight is an integer. For some unknown reason, two rabbits would fight each other if and only if their weight is NOT co-prime.
  Now the king had arranged the n rabbits in a line ordered by their numbers. The king planned to send some rabbits into prison. He wanted to know that, if he sent all rabbits between the i-th one and the j-th one(including the i-th one and the j-th one) into prison, how many rabbits in the prison would not fight with others.
  Please note that a rabbit would not fight with himself.
 

Input
  The input consists of several test cases.
  The first line of each test case contains two integer n, m, indicating the number of rabbits and the queries.
  The following line contains n integers, and the i-th integer W i indicates the weight of the i-th rabbit.
  Then m lines follow. Each line represents a query. It contains two integers L and R, meaning the king wanted to ask about the situation that if he sent all rabbits from the L-th one to the R-th one into prison.
  (1 <= n, m, W i <= 200000, 1 <= L <= R <= n)
  The input ends with n = 0 and m = 0.
 

Output
  For every query, output one line indicating the answer.
 

Sample Input
      
      
3 2 2 1 4 1 2 1 3 6 4 3 6 1 2 5 3 1 3 4 6 4 4 2 6 0 0
 

Sample Output
      
      
2 1 1 3 1 2
Hint
  In the second case, the answer of the 4-th query is 2, because only 1 and 5 is co-prime with other numbers in the interval [2,6] .
 

Source
 

Recommend
 

Statistic |  Submit |  Discuss |  Note
Hangzhou Dianzi University Online Judge 3.0
Copyright © 2005-2017 HDU ACM Team. All Rights Reserved.
Designer & Developer Wang Rongtao LinLe GaoJie GanLu
Total 0.000000(s) query 1, Server time : 2017-09-28 08:45:39, Gzip enabled

题目大意:有n个数,m个询问,询问求的是一个区间内与其他数都互质的数有多少个,比如一个序列:1 3 6 7 5 15 答案为2个(1、7)。

题目思路:我们可以预处理一下每个数能到达的最左的位置和最右的位置,对于每个询问,离线一下,按右端点排序。



我们考虑不满足的情况:

①对于每个点p<=qr,我们只需要知道其左区间是否在询问的区间内即可(该点不满足)。我们先执行L++;

②对于每个点p.r<=qr,那么他的右区间肯定在询问的区间左边,使得不满足,但是要抵消左边的情况,否则会多算一次,所以我们执行,p.pos++,L--;

然后对于每个询问用树状数组维护单点更新区间和即可。


include<bits/stdc++.h> 
using namespace std;
const int maxn=2e5+10;
#define lowbit(x) x&-x
int prime[maxn],tot,pp[maxn],arr[maxn],L[maxn],R[maxn],facnum[maxn],sum[maxn],res[maxn];
bool vis[maxn];
vector<int>vec[maxn];
vector<int>factor[maxn];
struct Node
{
	int index,l,r; 
	Node(int _index=0,int _l=0,int _r=0):index(_index),l(_l),r(_r){}
}ans[maxn];
bool cmp(const Node& a,const Node& b)
{
	return a.r<b.r;
}
void Getprime()
{
	tot=0;
	memset(vis,false,sizeof(vis));
	for(int i=2;i<maxn;i++)
	{
		if(!vis[i]) prime[tot++]=i;
		for(int j=0;j<tot&&i*prime[j]<maxn;j++)
		{
			vis[i*prime[j]]=true;
			if(i%prime[j]==0) break;
		}
	}
}
void Init(int n)
{
	memset(pp,0,sizeof(pp));
	memset(sum,0,sizeof(sum));
	for(int i=0;i<=n;i++) L[i]=0,R[i]=n+1,factor[i].clear(),vec[i].clear();
}
void Divide(int p,int x)
{
	facnum[p]=0;
	for(int i=0;prime[i]*prime[i]<=x;i++) 
	{
		if(x%prime[i]==0)
		{
			factor[p].push_back(prime[i]);
			facnum[p]++;
			while(x%prime[i]==0) x/=prime[i];
		}
	}
	if(x>1) factor[p].push_back(x),facnum[p]++;
}
void Update(int p,int val)
{
	if(p<=0) return;
	while(p<maxn)
	{
		sum[p]+=val;
		p+=lowbit(p);
	}
}
int Query(int p)
{
	int ans=0;
	while(p>0)
	{
		ans+=sum[p];
		p-=lowbit(p);
	}
	return ans;
}
int main()
{
	int n,m;
	Getprime();
	while(~scanf("%d%d",&n,&m)&&(n||m))
	{
		Init(n);
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&arr[i]);
			Divide(i,arr[i]);
			for(int j=0;j<facnum[i];j++)
			{
				L[i]=max(L[i],pp[factor[i][j]]);//求一个数最左能到达哪 
				pp[factor[i][j]]=i;
			}
		}
		for(int i=0;i<maxn;i++) pp[i]=n+1;
		for(int i=n;i>=1;i--)
		{
			for(int j=0;j<facnum[i];j++)
			{
				R[i]=min(R[i],pp[factor[i][j]]);//同理 
				pp[factor[i][j]]=i;
			}
		}
		for(int i=1;i<=n;i++) vec[R[i]].push_back(i);//用vector存取右端点到R【i】结束的点集 
		for(int i=1;i<=m;i++)
		{
			scanf("%d%d",&ans[i].l,&ans[i].r);
			ans[i].index=i;
		}
		sort(ans+1,ans+1+m,cmp);
		int now=1;
		for(int i=1;i<=m;i++) 
		{
			while(now<=n&&now<=ans[i].r)
			{
				Update(L[now],1);
				for(int j=0;j<vec[now].size();j++)
				{
					int id=vec[now][j];
					Update(L[id],-1);
					Update(id,1);
				}
				now++;
			}
			res[ans[i].index]=Query(ans[i].r)-Query(ans[i].l-1);
			res[ans[i].index]=ans[i].r-ans[i].l+1-res[ans[i].index];
		}
		for(int i=1;i<=m;i++) printf("%d\n",res[i]);
	}
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值