0
点赞
收藏
分享

微信扫一扫

HDU 4777 Rabbit Kingdom

承蒙不弃 2022-11-10 阅读 84


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 W  i 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, W  i <= 200000, 1 <= L <= R <= n) 
  The input ends with n = 0 and m = 0. 


Output

For every query, output one line indicating the answer.

Sample 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


Sample Output



2
1
1
3
1
2


Hint



  In the second case, the answer of the 4-th query is 2, because only 1 and 5 is co-prime with other numbers in the interval [2,6] .

静态区间循环一段区间内,与其他数互质的有几个。

先预处理出每个数字向左和向右最远能走多远,然后就相当于统计平面上的点被多少个矩形同时覆盖。

离线询问,从左到右加入矩形的边,树状数组统计答案。

#include<cstdio>
#include<algorithm>
#define rep(i,j,k) for (int i = j; i <= k; i++)
#define per(i,j,k) for (int i = j; i >= k; i--)
#define loop(i,j,k) for (int i = j;i != -1; i = k[i])
#define inone(x) scanf("%d", &x)
#define intwo(x,y) scanf("%d%d", &x, &y)
using namespace std;
const int low(int x) { return x&-x; }
const int N = 4e5 + 10;
int n, m, x, y, a[N];
int p[N], f[N], t;
int L[N], R[N], pre[N];
int ft[N], nt[N], u[N], v[N], sz;
int Ft[N], Nt[N], U[N], V[N], Sz;
int F[N], ans[N];

void init()
{
rep(i, 2, N)
{
if (!f[i]) p[f[i] = ++t] = i;
rep(j, 1, t)
{
if (i*p[j] >= N) break;
f[i*p[j]] = j;
if (i%p[j] == 0) break;
}
}
}

void add(int x, int y)
{
for (int i = x; i <= n; i += low(i)) F[i] += y;
}

int sum(int x)
{
int res = 0;
for (int i = x; i; i -= low(i)) res += F[i];
return res;
}

int main()
{
init();
while (intwo(n, m), n + m)
{
rep(i, 1, n) inone(a[i]), L[i] = 1, R[i] = n;
rep(i, 1, n) Ft[i] = ft[i] = -1, F[i] = 0;
Sz = sz = 0;
rep(i, 1, t) pre[i] = 0;
rep(i, 1, n)
{
for (int j = a[i]; f[j]; j /= p[f[j]]) L[i] = max(L[i], pre[f[j]] + 1);
for (int j = a[i]; f[j]; j /= p[f[j]]) pre[f[j]] = i;
}
rep(i, 1, t) pre[i] = n + 1;
per(i, n, 1)
{
for (int j = a[i]; f[j]; j /= p[f[j]]) R[i] = min(R[i], pre[f[j]] - 1);
for (int j = a[i]; f[j]; j /= p[f[j]]) pre[f[j]] = i;
}
rep(i, 1, n)
{
u[sz] = L[i]; v[sz] = i + 1; nt[sz] = ft[i]; ft[i] = sz++;
if (R[i] == n) continue;
u[sz] = i + 1; v[sz] = L[i]; nt[sz] = ft[R[i] + 1]; ft[R[i] + 1] = sz++;
}
rep(i, 1, m)
{
intwo(x, y);
U[Sz] = i; V[Sz] = x; Nt[Sz] = Ft[y]; Ft[y] = Sz++;
}
for (int i = 1, k = 1; i <= n; i++)
{
loop(j, ft[i], nt) add(u[j], 1), add(v[j], -1);
loop(j, Ft[i], Nt) ans[U[j]] = sum(V[j]);
}
rep(i, 1, m) printf("%d\n", ans[i]);
}
return 0;
}



举报

相关推荐

0 条评论