약수

http://codeforces.com/problemset/problem/359/D
http://codeforces.com/problemset/problem/265/D
http://acm.hdu.edu.cn/showproblem.php?pid=4630
38 계 ACM 항주 H 문제;
이상 의 이 문제 들 은 모두 유지 보수 인수 입 니 다. 제 가 잘 생각해 보고 정리 하 겠 습 니 다. (CF 359 sqrt (n) 매 거 진 약수 회 T)
HDU 4777
#include <cstdio>
#include <cstring>
#include <algorithm>
#define N 200100
using namespace std;
int last[N],val[N];
int ans[N];
struct Point{
	int l,r,pos;
}point[N];

struct Query{
	int l,r,id;
}query[N];
int s[N];

bool cmp(struct Query a,struct Query b){
	return a.r<b.r;
}
bool cmp2(struct Point a,struct Point b){
	return a.r<b.r;
}
int pn=0,factor[N],prime[N];

void init(){
	for(int i=1;i<=200000;i++) factor[i]=i;
	for(int i=2;i<=200000;i++){
		if(i==factor[i]) prime[pn++]=i;
		for(int j=0;j<pn && prime[j]*i<=200000;j++){
			factor[i*prime[j]]=prime[j];
			if(i%prime[j]==0) break;
		}
	}
}

struct Tree{
	int l,r,sum;
}tree[N<<2];

void build(int l,int r,int id){
	tree[id].l=l;
	tree[id].r=r;
	tree[id].sum=0;
	if(l!=r){
		int mid=(l+r)>>1;
		build(l,mid,id<<1);
		build(mid+1,r,id<<1|1);
	}
}

void insert(int pos,int val,int id){
	tree[id].sum+=val;
	if(tree[id].l==tree[id].r) return ;
	int mid=(tree[id].l+tree[id].r)>>1;
	if(mid>=pos) insert(pos,val,id<<1);
	else insert(pos,val,id<<1|1);
}

int querys(int l,int r,int id){
	if(tree[id].l==l && tree[id].r==r) return tree[id].sum;
	int mid=(tree[id].l+tree[id].r)>>1;
	if(mid>=r) return querys(l,r,id<<1);
	else if(mid<l) return querys(l,r,id<<1|1);
	else return querys(l,mid,id<<1)+querys(mid+1,r,id<<1|1);
}

int main(){
	int n,m;
	init();
	while(scanf("%d %d",&n,&m)){
		if(n==0 && m==0) break;
		for(int i=1;i<=n;i++) scanf("%d",&val[i]);
		for(int i=0;i<m;i++){
			scanf("%d %d",&query[i].l,&query[i].r);
			query[i].id=i;
		}
		sort(query,query+m,cmp);
		memset(last,0,sizeof(last));
		for(int i=1;i<=n;i++){
			int tmpn=val[i];
			point[i].l=1;
			point[i].pos=i;
			while(tmpn!=factor[tmpn]){
				if(last[factor[tmpn]]) point[i].l=max(point[i].l,last[factor[tmpn]]+1);
				last[factor[tmpn]]=i;
				int tmp=factor[tmpn];
				while(tmpn%tmp==0) tmpn/=tmp;
			}
			if(tmpn>1){
				if(last[factor[tmpn]]) point[i].l=max(point[i].l,last[factor[tmpn]]+1);
				last[factor[tmpn]]=i;
			}
			s[i]=point[i].l;
		}
		memset(last,0,sizeof(last));
		for(int i=n;i>=1;i--){
			int tmpn=val[i];
			point[i].r=n;
			while(tmpn!=factor[tmpn]){
				if(last[factor[tmpn]]) point[i].r=min(point[i].r,last[factor[tmpn]]-1);
				last[factor[tmpn]]=i;
				int tmp=factor[tmpn];
				while(tmpn%tmp==0) tmpn/=tmp;
			}
			if(tmpn>1){
				if(last[factor[tmpn]]) point[i].r=min(point[i].r,last[factor[tmpn]]-1);
				last[factor[tmpn]]=i;
			}
		}
		sort(point+1,point+1+n,cmp2);
		build(1,n,1);
		for(int i=1,j=0,k=1;j<m && i<=n;i++){
			insert(i,1,1);
			//printf("%d %d
",i,s[i]); if(s[i]!=1) insert(s[i]-1,-1,1); while(j<m && query[j].r==i){ ans[query[j].id]=querys(query[j].l,query[j].r,1); j++; } while(k<=n && point[k].r==i){ insert(point[k].pos,-1,1); if(point[k].l!=1) insert(point[k].l-1,1,1); k++; } } for(int i=0;i<m;i++) printf("%d
",ans[i]); } return 0; }

좋은 웹페이지 즐겨찾기