Hdu 4777 Rabbit Kingdom (offline tree array), hdu4777
Question:
A m query is provided to query the number of [l-r] numbers in which all numbers in this range are mutually qualitative.
Train of Thought Analysis:
First, we can process each position, the first one on the left and the right, which is not in mutual quality. Record it under pre and next. This method can be used to decompose the prime factor.
The answer in an interval is equal to the number of all numbers in this interval minus the number of numbers with mutual quality.
What we want to calculate now is
1. for a given Query [l, r] interval, count the number of I (l <= I <= r) pre [I] Or next [I] in [l, r.
2. for a given Query [l, r] interval, the pre [I] with multiple I (l <= I <= r) and next [I] in [l, r.
Then the answer to the interval is r-l + 1-(the answer of 1 in statistics) + (the answer of 2 in statistics ).
For statistics 2, we determine that there are multiple [pre [I], next [I] in [l, r.
For Statistics 1, we can replace it with [pre [I], I] or [I, next [I.
# Include <iostream> # include <cstdio> # include <cstring> # include <algorithm> # include <cmath> # include <vector> # define lowbit (x) (x & (-x) # define maxn 200005 # define lson num <1, s, mid # define rson num <1 | 1, mid + 1, eusing namespace std; bool vis [200005]; int prime [50000], top_prime; int n, m; struct foo {int s, e; int index, ans; foo () {} foo (int st, int ed, int id): s (st), e (ed), index (id) {} bool operator <(const foo & cmp) co Nst {return e <cmp. e ;}} Q [200005]; bool cmp_id (foo a, foo B) {return. index <B. index;} int bit [maxn]; void update (int pos) {if (pos = 0 | pos = n + 1) return; for (int x = pos; x <= n; x + = lowbit (x) bit [x] ++;} int query (int l, int r) {int ans = 0; for (int x = r; x> = 1; x-= lowbit (x) ans + = bit [x]; for (int x = L-1; x> = 1; x-= lowbit (x) ans-= bit [x]; return ans;} void sieve (int n) {int m = (int) sqrt (n + 0.5 ); memset (vis, 0, sizeof (vis); f Or (int I = 2; I <= m; I ++) {if (! Vis [I]) {for (int j = I * I; j <= n; j + = I) vis [j] = 1 ;}} for (int I = 2; I <= m; I ++) {if (vis [I] = 0) prime [top_prime ++] = I ;}} int pre [200005], next [200005], now [200005], a [200005], tnext [200005]; vector <int> p [200005]; vector <foo> ret [3]; int ans [3] [maxn]; void getans (int key) {memset (bit, 0, sizeof bit); int ind = 0; for (int I = 1; I <= m; I ++) {while (ind <n & ret [key] [ind]. e <= Q [I]. e) {update (ret [key] [ind]. s); ind ++;} ans [key] [Q [I]. index] + = query (Q [I]. s, Q [I]. e) ;}} int num [maxn]; int main () {sieve (200000); while (~ Scanf ("% d", & n, & m) {if (! N &&! M) break; memset (pre, 0, sizeof pre); memset (next, 0, sizeof next); memset (tnext, 0, sizeof tnext ); for (int I = 1; I <= n; I ++) {scanf ("% d", & a [I]); p [a [I]. clear () ;}for (int I = 1; I <= n; I ++) {if (p [a [I]. size () continue; int tmp = a [I]; for (int j = 0; prime [j] * prime [j] <= tmp & j <top_prime; j ++) {if (tmp % prime [j] = 0) {p [a [I]. push_back (prime [j]); while (tmp % prime [j] = 0) tmp/= prime [j] ;}} if (tmp> 1) p [a [I]. push_back (tmp);} memset (now, 0, sizeof (now); for (int I = 1; I <= n; I ++) {for (int j = 0; j <p [a [I]. size (); j ++) {int tmp = p [a [I] [j]; pre [I] = max (now [tmp], pre [I]); now [tmp] = I ;}} reverse (a + 1, a + 1 + n); memset (now, 0, sizeof (now )); for (int I = 1; I <= n; I ++) {for (int j = 0; j <p [a [I]. size (); j ++) {int tmp = p [a [I] [j]; tnext [I] = max (now [tmp], tnext [I]); now [tmp] = I ;}for (int I = 1; I <= n; I ++) {if (tnext [n-I + 1] = 0) next [I] = 0; else next [I] = n-tnext [n-I + 1] + 1 ;}for (int I = 1; I <= n; I ++) {if (next [I] = 0) next [I] = n + 1; // The next Initialization is 0} // The pre and next for (int I = 1; I <= m; I ++) {int st, ed; scanf ("% d", & st, & ed); Q [I] = foo (st, ed, I); Q [I]. ans = 0; num [I] = ed-st + 1;} for (int I = 0; I <3; I ++) ret [I]. clear (); for (int I = 1; I <= n; I ++) {ret [0]. push_back (foo (pre [I], I, 0); ret [1]. push_back (foo (I, next [I], 0); ret [2]. push_back (foo (pre [I], next [I], 0);} sort (Q + 1, Q + 1 + m); for (int I = 0; I <3; I ++) sort (ret [I]. begin (), ret [I]. end (); memset (ans, 0, sizeof ans); for (int I = 0; I <3; I ++) getans (I ); for (int I = 1; I <= m; I ++) printf ("% d \ n ", num [I]-ans [0] [I]-ans [1] [I] + ans [2] [I]);} return 0 ;}