Note that the length of the ant that survive in each interval is equal to the GCD of the interval.
Therefore, you can pre-process The GCD in the interval first.
Then you can perform binary search.
Preprocessing gcd I use the multiplication method here
Total time complexity O (nlogn)
/* Cf 271f multiplier evaluate the complexity of the interval GCD for the subscript binary time O (nlogn) */# include # include # include # include using namespace STD; const int maxn = 100009; int st [maxn] [20]; int N, T; Map Pos; vector F [maxn]; inline int gcd (int x, int y) {return y = 0? X: gcd (Y, X % Y);} inline void ST () {for (INT I = n-1; I> 0; -- I) for (Int J = 1; I + (1 N; int Tol = 0; For (INT I = 1; I <= N; I ++) {CIN> st [I] [0]; If (POS. find (ST [I] [0]) = POS. end () tol ++, POS [st [I] [0] = tol; F [POS [st [I] [0]. push_back (I);} ST (); CIN> T; For (INT I = 1, L, R; I <= T; I ++) {CIN> L> r; int key = getgcd (L, R), k = POS [Key]; int d = upper_bound (F [K]. begin (), f [K]. end (), R)-lower_bound (F [K]. begin (), f [K]. end (), L); cout
View code
Codeforces 474f-ant colony