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. codeforces 830 B Cards Sorting

    B. Cards Sorting  http://codeforces.com/problemset/problem/830/B Vasily has a deck of cards consisti ...

  2. Java代码风格和在idea中的一些设置

    源文件基本设置 1. 文件名 驼峰标识,.java结尾 2. 编码 统一为UTF-8 Transport...可以解决property文件不能正常显示为中文的问题 3. 特殊字符 尽量使用转义字符(\ ...

  3. 老板怎么办,我们网站遭到DDoS攻击又挂了?

    相信现在正在阅读此文的你,一定听说过发生在上个月的史上最大的DDoS攻击. 美国东部时间2月28日,GitHub在一瞬间遭到高达1.35Tbps的带宽攻击.这次DDoS攻击几乎可以堪称是互联网有史以来 ...

  4. IdentityServer4实战 - 基于角色的权限控制及Claim详解

    一.前言 大家好,许久没有更新博客了,最近从重庆来到了成都,换了个工作环境,前面都比较忙没有什么时间,这次趁着清明假期有时间,又可以分享一些知识给大家.在QQ群里有许多人都问过IdentityServ ...

  5. 微信qq,新浪等第三方授权登录的理解

    偶们常说的第三方是指的微信,qq,新浪这些第三方,因为现在基本每个人都有qq或者微信,那么我们就可以通过这些第三方进行登录.而这些网站比如慕课网是通过第三方获取用户的基本信息 它会有个勾选按钮,提示是 ...

  6. 服务器Windows Server 2008 远程控制安全设置技巧

    为了保障服务器远程控制操作的安全性,Windows Server 2008系统特意在这方面进行了强化,新推出了许多安全防范功能,不过有的功能在默认状态下并没有启用,这需要我们自行动手,对该系统进行合适 ...

  7. Oracle 使用pl/sql将表中的数据读出到文件中

    (1)在服务器上创建文件路径及文件 [oracle@redhat errormsg]$ touch test01.txt (2)在数据库中创建路径及授权 (3)创建存储过程 CREATE OR REP ...

  8. 深度学习之 cnn 进行 CIFAR10 分类

    深度学习之 cnn 进行 CIFAR10 分类 import torchvision as tv import torchvision.transforms as transforms from to ...

  9. Hazelcast分布式

    一般的应用正式环境中都不止一台服务器(也就是说是集群的),那么如果只是简单的将数据预加载到内存,那么就会有数据不同步的现象. (更新了其中一台JVM,另一台JVM并不会收到通知从而保持数据同步). 这 ...

  10. SpringBoot的RestController vs @ResponseBody + @Controller

    @Controller和@RestController的区别?官方文档:@RestController is a stereotype annotation that combines @Respon ...