Rabbit Kingdom

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

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 Wi 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, Wi <= 200000, 1 <= L <= R <= n)
  The input ends with n = 0 and m = 0.
 
Output
  For every query, output one line indicating the answer.
/*
hdu 4777 树状数组+合数分解 给你n个数,然后是q个询问,每次[l,r]中与区间内其它所有数互质的数的个数 感觉有点像查找区间内不同数的个数,于是用了l[i]表示左边最近与它不互质的数的
位置,本来想到是只要有互质的 就表为1,但是区间查询出现了问题(与i不互质的
l[i]如果不在区间内,i仍然被标记为1,- -2了)。 当遇到i时,只有当l[i]也在区间中的时候才能算,所以在l[i]上加1。当遇到r[i]时,就算没有l[i]
i也有r[i],所有l[i]减1,i上加1.
至于r[i]本身,已经被加到l[r[i]]上去了 所以大致思路就是:
先处理出l[i]和r[i](可以考虑求出质因子然后判断).
然后把查询按照r的从小到大排序,有点像往后递推的感觉
最后按照上面的思路求出区间[l,r]中不与其它互质的个数,再减去即可 //表示一直没看懂别人的报告(主要是不理解为什么这样插入删除),这还是偶然间想通的QAQ hhh-2016-03-06 14:01:51
*/
#include <algorithm>
#include <cmath>
#include <queue>
#include <iostream>
#include <cstring>
#include <map>
#include <cstdio>
#include <vector>
#include <functional>
#define lson (i<<1)
#define rson ((i<<1)|1)
using namespace std;
typedef long long ll;
const int maxn = 200050;
int vis[maxn];
const int inf = 0x3f3f3f3f;
int prime[maxn+1],factor[maxn],s[maxn];
int n,q;
void get_prime()
{
memset(prime,0,sizeof(prime));
for(int i =2 ; i <= maxn; i++)
{
if(!prime[i])
prime[++prime[0]] = i;
for(int j = 1; j <= prime[0] && prime[j] <= maxn/i; j++)
{
prime[i*prime[j]] = 1;
if(i % prime[j] == 0)break;
}
}
}
int facnt;
int getFactor(int x)
{
facnt = 0;
int tmp = x;
for(int i = 1; prime[i] <= tmp/prime[i]; i++)
{
if(tmp % prime[i] == 0)
{
factor[facnt]= prime[i];
while(tmp%prime[i] == 0)
{
tmp/=prime[i];
}
facnt ++;
}
}
if(tmp != 1)
{
factor[facnt++] = tmp;
}
} int lowbit(int x)
{
return x&(-x);
} void add(int x,int val)
{
if(x == 0) return ;
while(x <= n)
{
s[x] += val;
x += lowbit(x);
}
} int sum(int x)
{
int cnt = 0;
while(x)
{
cnt += s[x];
x -= lowbit(x);
}
return cnt;
} int t[maxn];
int l[maxn],r[maxn];
int ans[maxn],a[maxn]; struct node
{
int l,r;
int id;
} opr[maxn]; bool cmp(node a,node b)
{
return a.r < b.r;
}
vector<int>c[maxn];
int main()
{
int T;
int cas = 1;
get_prime();
while(scanf("%d%d",&n,&q) != EOF)
{
memset(s,0,sizeof(s)); if(n == 0 && q == 0)
break;
for(int i =1; i <= n; i++)
scanf("%d",&a[i]); for(int i =1; i <= q; i++)
{
scanf("%d%d",&opr[i].l,&opr[i].r);
opr[i].id = i;
} memset(t,0,sizeof(t));
for(int i =1; i <= n; i++)
{
l[i] = 0;
getFactor(a[i]);
for(int j = 0; j < facnt; j++)
{
l[i] = max(l[i],t[factor[j]]);
t[factor[j]] = i;
}
}
for(int i =0 ; i < maxn; i++) t[i] = n+1;
for(int i =n; i >= 1 ; i--)
{
r[i] = n+1;
getFactor(a[i]);
for(int j = 0; j < facnt; j++)
{
r[i] = min(r[i],t[factor[j]]);
t[factor[j]] = i;
}
}
for(int i = 0; i <= n+2; i++)
{
c[i].clear();
}
for(int i = 1; i <= n; i++)
c[r[i]].push_back(i);
//memset(vis,0,sizeof(vis)); sort(opr+1,opr+q+1,cmp);
for(int i = 1,k = 1; i <= q; i++)
{
while(k <= n && k <= opr[i].r)
{
add(l[k],1);
for(int o = 0; o < c[k].size(); o++)
{
int v = c[k][o];
add(v,1);
add(l[v],-1);
}
k++;
}
ans[opr[i].id] = opr[i].r-opr[i].l+1-(sum(opr[i].r)-sum(opr[i].l-1));
}
for(int i =1; i <= q; i++)
{
printf("%d\n",ans[i]);
}
}
return 0;
} /*
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 output:
2
1
1
3
1
2
*/

  

hdu 4777 树状数组+合数分解的更多相关文章

  1. hdu 4638 树状数组 区间内连续区间的个数(尽可能长)

    Group Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

  2. HDU 2852 (树状数组+无序第K小)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2852 题目大意:操作①:往盒子里放一个数.操作②:从盒子里扔掉一个数.操作③:查询盒子里大于a的第K小 ...

  3. HDU 4911 (树状数组+逆序数)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4911 题目大意:最多可以交换K次,就最小逆序对数 解题思路: 逆序数定理,当逆序对数大于0时,若ak ...

  4. hdu 5792(树状数组,容斥) World is Exploding

    hdu 5792 要找的无非就是一个上升的仅有两个的序列和一个下降的仅有两个的序列,按照容斥的思想,肯定就是所有的上升的乘以所有的下降的,然后再减去重复的情况. 先用树状数组求出lx[i](在第 i ...

  5. HDU 1934 树状数组 也可以用线段树

    http://acm.hdu.edu.cn/showproblem.php?pid=1394 或者是我自己挂的专题http://acm.hust.edu.cn/vjudge/contest/view. ...

  6. 2018 CCPC网络赛 1010 hdu 6447 ( 树状数组优化dp)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=6447 思路:很容易推得dp转移公式:dp[i][j] = max(dp[i][j-1],dp[i-1][j ...

  7. 【模板】HDU 1541 树状数组

    http://acm.hdu.edu.cn/showproblem.php?pid=1541 题意:给你一堆点,每个点右一个level,为其右下方所有点的数量之和,求各个level包含的点数. 题解: ...

  8. hdu 5147 树状数组

    题意:求满足a<b<c<d,A[a]<A[b],A[c]<A[d]的所有四元组(a,b,c,d)的个数 看到逆序对顺序对之类的问题一开始想到了曾经用归并排序求逆序对,结果 ...

  9. HDU 3584 树状数组

    Cube Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submi ...

随机推荐

  1. iOS开发-FFmpeg深入分析

    FFmpeg是相当强大的多媒体编解码框架,在深入分析其源代码之前必须要有基本的多媒体基础知识,否则其源代码会非常晦涩难懂.本文将从介绍一些基本的多媒体只是,主要是为研读ffmpeg源代码做准备,比如一 ...

  2. idea 导eclipse项目

    https://www.cnblogs.com/xiaoBlog2016/archive/2017/05/08/6825014.html

  3. 22.C++- 继承与组合,protected访问级别

    在C++里,通过继承和组合实现了代码复用,使得开发效率提高,并且能够通过代码看到事物的关系 组合比继承简单,所以在写代码时先考虑能否组合,再来考虑继承. 组合的特点 将其它类的对象作为当前类的成员使用 ...

  4. 2017 清北济南考前刷题Day 3 afternoon

    期望得分:100+40+100=240 实际得分:100+40+100=240 将每个联通块的贡献乘起来就是答案 如果一个联通块的边数>点数 ,那么无解 如果边数=点数,那么贡献是 2 如果边数 ...

  5. 2017 国庆湖南 Day5

    期望得分:76+80+30=186 实际得分:72+10+0=82 先看第一问: 本题不是求方案数,所以我们不关心 选的数是什么以及的选的顺序 只关心选了某个数后,对当前gcd的影响 预处理 cnt[ ...

  6. python之路--day13-模块

    1,什么是模块 模块就是系统功能的集合体,在python中,一个py文件就是一个模块, 例如:module.py 其中module叫做模块名 2,使用模块 2.1 import导入模块 首次带入模块发 ...

  7. thinkphp中ajax技术

    thinkphp可以直接返回json数据,json数据事可以跟前端的js通用的

  8. 作业三:模拟 mysql 进行增删改查

    # !/usr/bin/env python3 # _*_coding:utf-8_*_ def help_sql(cmd): if cmd in func_dic.keys(): print('{} ...

  9. 上传视频使用ffmpeg自动截取缩略图

    上传视频之后,有的需要显示缩略图,而不是仅仅显示视频名称的列表,这时候就需要对上传的视频截取缩略图. 简单粗暴点,将以下代码作为工具类复制粘贴即可: package com.util; import ...

  10. HTNL表单详解

    HTML表单 表单的结构 表单的标签:<form> </form> 常用属性 Name , method(get,post), action(服务器的接收的页面如:reg.ph ...