Title Link: Hdu 4777 Rabbit Kingdom
The main topic: a rabbit Kingdom, there are n rabbits, each rabbit has a weight, if the weight of two rabbits is not coprime, then will dry, and now the king wants to put the rabbit between the L R Prison, it wants to know how many rabbits will not and other rabbit dry rack.
Problem-solving ideas: pre-l,r each rabbit to the left and right the rabbit will have a recent conflict with the rabbit, pre-treatment as long as the weight of each rabbit into a mass factor after the traversal two times.
For inquiries, the query will be asked according to the right interval sort, hit I, then L position +1, hit R, then I position +1,l position-1. (If L≤l && r≤r, then the rabbit will not clash with other rabbits in this l,r inquiry.)
So the L~r interval is counted as the rabbits that will fight.
#include <cstdio>#include <cstring>#include <cmath>#include <vector>#include <algorithm>using namespace STD;Const intMAXN =200005;intNP, PRI[MAXN], VIS[MAXN];voidPrime_table (intN) {np =0; for(inti =2; I <= N; i++) {if(Vis[i])Continue; pri[np++] = i; for(intj = i *2; J <= N; J + = i) vis[j] =1; } vis[1] =1;}#Define lowbit (x) ((x) & ( ×))structSeg {intL, R, id; Seg (intL =0,intR =0,intID =0) { This->l = l; This->r = R; This->id = ID; }friend BOOL operator< (Constseg& A,Constseg& b) {returnA.R < B.R; }};intN, M, L[MAXN], P[MAXN];intFENW[MAXN], ANS[MAXN]; vector<int>G[MAXN]; vector<Seg>Vec, que;inline voidAdd (intXintD) {if(x <=0)return; while(x <= N) {Fenw[x] + = D; x + = Lowbit (x); }}inline intSUM (intx) {intRET =0; while(x) {ret + = fenw[x]; X-= Lowbit (x); }returnRET;}voidInit () {vec.clear (); Que.clear ();intX for(inti =1; I <= N; i++) {scanf("%d", &x); G[i].clear (); for(intj =0; J < NP; J + +) {if(x% pri[j] = =0) {G[i].push_back (pri[j]); while(x% pri[j] = =0) x/= pri[j]; }if(Vis[x] = =0) {g[i].push_back (x); Break; } } } for(inti =0; i < MAXN; i++) P[i] =0; for(inti =1; I <= N; i++) {intTMP =0; for(intj =0; J < G[i].size (); J + +) {TMP = MAX (TMP, P[G[I][J]]); P[G[I][J]] = i; } L[i] = tmp; Vec.push_back (SEG (TMP, I,0)); } for(inti =0; i < MAXN; i++) P[i] = N +1; for(inti = N; I i--) {intTMP = N +1; for(intj =0; J < G[i].size (); J + +) {tmp = MIN (tmp, P[G[I][J]]); P[G[I][J]] = i; } vec.push_back (Seg (i, TMP,1)); }intL, R; for(inti =1; I <= M; i++) {scanf("%d%d", &l, &r); Que.push_back (Seg (L, R, I)); }}voidSolve () {sort (Que.begin (), Que.end ()); Sort (Vec.begin (), Vec.end ());memset(Fenw,0,sizeof(FENW));intMV =0; for(inti =0; I < que.size (); i++) { while(MV < Vec.size () && VEC[MV].R <= QUE[I].R) {Add (VEC[MV].L,1);if(vec[mv].id) Add (L[VEC[MV].L],-1); mv++; }intTMP = SUM (QUE[I].R)-SUM (QUE[I].L-1); Ans[que[i].id] = que[i].r-que[i].l +1-TMP; }}intMain () {prime_table (MAXN); while(scanf("%d%d", &n, &m) = =2&& N + M) {init (); Solve (); for(inti =1; I <= M; i++)printf("%d\n", Ans[i]); }return 0;}
HDU 4777 Rabbit Kingdom (tree-like array)