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. Django 博客

    blogproject/blogproject/settings.py ## 其它配置代码... # 把英文改为中文 LANGUAGE_CODE = 'zh-hans' # 把国际时区改为中国时区 T ...

  2. SpaceVim - 让你的vim变得更加高效和强大

    SpaceVim 中文手册 项 目 主 页: https://spacevim.org Github 地址 : https://github.com/SpaceVim/SpaceVim SpaceVi ...

  3. Vue.js自己从官网整理的东东

    1.采用简洁的模板语法来声明渲染数据: <div id="app"> {{ message }} </div> var app = new Vue({ el ...

  4. php中函数和方法的区别

    php的方法就是定义在类里面的方法,一般不建议在方法内部定义方法,但是这种也可以这种叫做内部方法,一般只能本方法调用. 如果定义在同一个类中的方法,在同类的其他方法中调用是$this->方法名就 ...

  5. zTree根据json选中节点,并且设置其他节点不可选

    首先,在适配目录树时,使用checkbox形式,配置代码如下: var settingCatalog = { check:{ enable: true }, data:{ simpleData:{ e ...

  6. jsp连接书库DatabaseUtil类

    public class DatabaseUtil { private static String driver = ConfigManager.getProperties("driver& ...

  7. Qt编译oci教程

    Qt编译OCI教程 上图oci.dll 不是oci.lib 我很奇怪网上大部分教程都是写的oci.lib,其实大家可以去oracle目录找一下这个文件,看看是不是在这个目录,我找了一下没发现.而lib ...

  8. Java面试题——中级(上)

    List和Set比较,各自的子类比较 对比一:Arraylist与LinkedList的比较 1.ArrayList是实现了基于动态数组的数据结构,因为地址连续,一旦数据存储好了,查询操作效率会比较高 ...

  9. Python的字典和JSON

    Python的字典和JSON在表现形式上非常相似 #这是Python中的一个字典 dic = { 'str': 'this is a string', 'list': [1, 2, 'a', 'b'] ...

  10. 类似吸顶功能解决ios不能实时监听onscroll的触发问题

    问题:近期项目需要一个类似西东功能,当页面向上滚动160px后div固定在顶部 解决方法:首先,想到的是window.onscroll方法 .fixed{position:fixed;-webkit- ...