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. 20145237《Java程序设计》实验报告一

    实验一 Java开发环境的熟悉(Windows + Eclipse) 实验内容 1.使用JDK编译.运行简单的Java程序: 2.使用Eclipse 编辑.编译.运行.调试Java程序. 实验要求 1 ...

  2. new malloc和delete free 的区别

    今天看了一个面试题:问new 和 malloc, delete 和 free 的区别,扭捏了半天,也没说完全:现总结如下: 1.先看看new 和 delete 看一个例子: <span styl ...

  3. 【iOS】Swift LAZY 修饰符和 LAZY 方法

    延时加载或者说延时初始化是很常用的优化方法,在构建和生成新的对象的时候,内存分配会在运行时耗费不少时间,如果有一些对象的属性和内容非常复杂的话,这个时间更是不可忽略.另外,有些情况下我们并不会立即用到 ...

  4. IE浏览器支持响应式网站设计

    目前响应式网站设计比较流行, 下面是摘自百度百科有关响应式设计的定义. 响应式网站设计是一种网络页面设计布局,其理念是:集中创建页面的图片排版大小,可以智能地根据用户行为以及使用的设备环境进行相对应的 ...

  5. 《javascript设计模式与开发实践》阅读笔记(12)—— 享元模式

    享元模式 享元(flyweight)模式是一种用于性能优化的模式,"fly"在这里是苍蝇的意思,意为蝇量级.享元模式的核心是运用共享技术来有效支持大量细粒度的对象. 享元模式的核心 ...

  6. MSIL实用指南-一维数组的操作

    本篇讲解怎么生成和操作一维数组.各种数组类型创建的步骤是一样的,但是加载和保存步骤有所不同. 一.创建数组所有类型的一维数组创建都是一样的,分三步.1.加载数组长度2.生成指令 Newarr < ...

  7. 关于APIcloud对应C#的 wcf框架作为后台,实现多库功能

    首先,我是使用ajax原来的请求方式,并没有使用apicloud中封装的请求方式. 前端代码: function makeRequest() { //alert("inside makeRe ...

  8. 判断一个字符串是不是一个合法的IP地址

    最近在笔试的时候遇到碰一道算法题, 要求判断一个字符串是不是合法的ip地址. 将我的思路发出来分享一下,不一定正确,也不一定是最优的方法.希望能分享一些交流 要求用java或者c来实现,我的java代 ...

  9. ActiveMQ学习系列(三)----下载github源码并编译

    前记:坚持使用官网的资源去学习是挺痛苦的一个过程,昨天瞎溜达了一天,也没看到有系统性的学习文章,倒是发现了github上的ActiveMq项目. 地址:https://github.com/apach ...

  10. SpringBoot2.x开发案例之整合Quartz任务管理系统

    基于spring-boot 2.x + quartz 的CRUD任务管理系统,适用于中小项目. 基于spring-boot +quartz 的CRUD任务管理系统: https://gitee.com ...