【二分答案】Problem A:天堂_珍珠
阿新 • • 發佈:2018-08-04
二分答案 分答 wap iostream 魔法 name tro div clas 第二行有n個整數(代表每條項鏈晶的好看程度Xi,0<=Xi<=maxlongint。)
第三行為正整數m,表示總天數(也就是總詢問次數)。
以下m行,每行兩個整數Ai,Bi(1<=Ai,Bi<=maxlongint),詢問好看程度在Ai到Bi之間的項鏈條數(含等於Ai或Bi的,Ai與Bi大小關系不確定)。
Problem A:天堂_珍珠
Time Limit:5000MS Memory Limit:65536K
Total Submit:232 Accepted:106
Description
我有很多很多(n條)用魔法合成的珍珠項鏈……(其實神仙比凡人更愛美),每天起來我都要從中挑一條戴上……挑哪條很有講究,如果比情敵**的難看,那麽就會被**(-_-),如果比天後Hera的好看,那麽就完蛋了(-_-)。所以我希望你能幫幫我,解決這個令人頭疼的問題——每天幫我算算,那天我能戴的項鏈有多少條。
Input
第一行為正整數n(項鏈總條數)。
第三行為正整數m,表示總天數(也就是總詢問次數)。
以下m行,每行兩個整數Ai,Bi(1<=Ai,Bi<=maxlongint),詢問好看程度在Ai到Bi之間的項鏈條數(含等於Ai或Bi的,Ai與Bi大小關系不確定)。
Output
輸出m行,對於每次詢問輸出一行,從Ai到Bi(含Ai,Bi)好看程度在Ai到Bi之間的項鏈條數。
Sample Input
7 8 2 3 5 6 7 7 6 1 5 8 6 1 10 5 5 4 4 7 8
Sample Output
3 4 7 1 0 3
Hint
對於25%數據,有m,n<=1000。
對於100%數據,有m,n<=100000。
#include <algorithm> #include <cstdio> #include <iostream> using namespace std; int n,a1,b,b1; long long a[1000010]; int find(int a1) { int low=0,high=n+1; while(low+1<high) {int mid=(low+high)/2; if(a[mid]<a1) low=mid; else high=mid; } return low; } int find2(int a1) { int low=0,high=n+1; while(low+1<high) { int mid=(low+high)/2; if(a[mid]<=a1) low=mid; else high=mid; } return low; } int main() { cin>>n; for(int i=1; i<=n; i++) scanf("%d",&a[i]); sort(a+1,a+1+n); a[0]=-1; a[n+1]=1000001; cin>>b; for(int i=1;i<=b;i++) { cin>>a1>>b1; if(b1<a1) swap(b1,a1); cout<<find2(b1)-find(a1)<<endl; } return 0; }
【二分答案】Problem A:天堂_珍珠