Link: https://ac.nowcoder.com/acm/contest/5203/C
Source: Niuke.com
space restrictions: C / C ++ 131072K, other languages 262144K
64bit the IO the Format: LLD%
Title description
Query the number of perfect squares in the range of [l, r] multiple times
Define the integer x as a perfect square if and only if the integer y can be found such that y * y = x
Enter description:
A number n in the first line indicates the number
of queries after the number of queries.
Output description:
For each query, output a number to indicate the answer
Input
5 1 3 1 4 2 4 4 4 1 1000000000
Output
1 2 1 1 31622
Remarks:
n <= 100000
0<= l <= r <= 1000000000
First pre- process all the total square numbers in [0,1000000000] , a total of 31,623 (0 2 ~ 31622 2 ).
Binary search finds the position index of the first perfect square greater than or equal to l . Binary search finds the position index of the first perfect square greater than r . Subtracting it is the answer.
You can use lower_bound () and upper_bound () respectively
1 #include <bits/stdc++.h> 2 typedef long long LL; 3 #define pb push_back 4 const int INF = 0x3f3f3f3f; 5 const double eps = 1e-8; 6 const int mod = 1e9+7; 7 const int maxn = 1e5+10; 8 using namespace std; 9 10 vector<int> vt; 11 12 int main() 13 { 14 #ifdef DEBUG 15 freopen("sample.txt","r",stdin); //freopen("data.out", "w", stdout); 16 #endif 17 18 for(int i=0;i*i<=1000000000;i++) 19 vt.pb(i*i); 20 21 int T; 22 scanf("%d",&T); 23 while(T--) 24 { 25 int l,r; 26 scanf("%d %d",&l,&r); 27 int pos1=lower_bound(vt.begin(),vt.end(),l)-vt.begin(); 28 int pos2=upper_bound(vt.begin(),vt.end(),r)-vt.begin(); 29 printf("%d\n",pos2-pos1); 30 } 31 32 return 0; 33 }
-