您的位置:首页 > 其它

HDU 4777 - Rabbit Kingdom (树状数组 区间互素数)

2014-09-23 15:11 441 查看


Rabbit Kingdom


Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)


Total Submission(s): 1037 Accepted Submission(s): 343

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.

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] .


Source

2013 Asia Hangzhou Regional Contest

Recommend

We have carefully selected several similar problems for you: 5041 5040 5039 5038 5037

Statistic | Submit | Discuss | Note

题意:

给定n个数的序列A和m个询问,对于每一个询问L,R,输出序列A的区间[L, R]里面X数的个数,X数为和[L, R]中除了它本身的其他数互素。

首先用nlogn预处理出Ai左边/右边第一个不互素的位置FL[i], FR[i]:

先预处理出每个数的质因子,然后从左至右扫描一遍,维护每个素数因子出现的最右位置

对于Ai,FL[i] = max(vis[j]),其中j为FL[i]的质因子,vis[j]是维护的j这个因子出现的最大位置

FR[i]同理

然后离线处理询问,按照询问区间的做端点L从小到大排序,然后依次处理

处理过程中维护一个序列B,满足:

对于当前询问Qi [Li, Ri]

SB[j] 为 B[j]的前缀和。

SB[Ri] - SB[Li-1]就是Qi的答案

可以这有处理B,对于一个数Ak,B[Ak]+1, B[FR[Ak]] - 1;

#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <queue>
#include <set>

using namespace std;

//#define WIN
#ifdef WIN
typedef __int64 LL;
#define iform "%I64d"
#define oform "%I64d\n"
#define oform1 "%I64d"
#else
typedef long long LL;
#define iform "%lld"
#define oform "%lld\n"
#define oform1 "%lld"
#endif

#define S64I(a) scanf(iform, &(a))
#define P64I(a) printf(oform, (a))
#define P64I1(a) printf(oform1, (a))
#define REP(i, n) for(int (i)=0; (i)<n; (i)++)
#define REP1(i, n) for(int (i)=1; (i)<=(n); (i)++)
#define FOR(i, s, t) for(int (i)=(s); (i)<=(t); (i)++)
#define lowbit(x) (x&-x)

const int INF = 0x3f3f3f3f;
const double eps = 1e-9;
const double PI = (4.0*atan(1.0));

const int maxn = 200000 + 20;

vector<int> factor[maxn];
int vis[maxn];
int A[maxn];
int FL[maxn], FR[maxn];
vector<int> has[maxn];
int n, m;

int C[maxn];
int sum(int x) {
int tx = x;
int ret = 0;
while(x > 0) {
ret += C[x];
x -= lowbit(x);
}
//printf("sum(%d) = %d\n", tx, ret);
return ret;
}

void add(int x, int v) {
//printf("add : %d %d\n", x, v);
while(x <= n) {
C[x] += v;
x += lowbit(x);
}
}

struct Query {
int id, L, R;
int ans;
bool operator < (const Query & b) const {
return L < b.L;
}
} querys[maxn];

bool cmp(Query a, Query b) {
return a.id < b.id;
}

void init(int n) {
memset(vis, 0, sizeof(vis));
for(int i=2; i<=n; i++) if(!vis[i]) {
for(int j=i; j<=n; j+=i) factor[j].push_back(i), vis[i] = 1;
}
}

int main() {

init(maxn-1);
while(scanf("%d%d", &n, &m) != EOF && n+m) {
for(int i=1; i<=n; i++) scanf("%d", &A[i]), has[i].clear();
memset(vis, 0, sizeof(vis));
memset(C, 0, sizeof(C));
for(int i=1; i<=n; i++) {
int sz = factor[A[i]].size();
int maxp = 0;
for(int j=0; j<sz; j++) {
int x = factor[A[i]][j];
maxp = max(maxp, vis[x]);
vis[x] = i;
}
FL[i] = maxp;
}
memset(vis, 0x3f, sizeof(vis));
for(int i=n; i>0; i--) {
int sz = factor[A[i]].size();
int minp = n+1;
for(int j=0; j<sz; j++) {
int x = factor[A[i]][j];
minp = min(minp, vis[x]);
vis[x] = i;
}
FR[i] = minp;
}
for(int i=1; i<=n; i++) {
has[FL[i]+1].push_back(i);
}
for(int i=0; i<m; i++) {
scanf("%d%d", &querys[i].L, &querys[i].R);
querys[i].id = i;
}
sort(querys, querys+m);
int p = 1;
int pp = 1;
for(int i=0; i<m; i++) {
//printf("------------[%d, %d] p=%d--------------\n", querys[i].L, querys[i].R, p);
while(pp < querys[i].L) {
add(pp, -1);
add(FR[pp], 1);
pp++;
}
while(p <= querys[i].L) {
int sz = has[p].size();
for(int j=0; j<sz; j++) {
int v = has[p][j];
add(v, 1);
add(FR[v], -1);
}
p++;
}
querys[i].ans = sum(querys[i].R) - sum(querys[i].L-1);
}
sort(querys, querys+m, cmp);
for(int i=0; i<m; i++) {
printf("%d\n", querys[i].ans);
}
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: