hdu4325-Flowers-樹狀陣列+離散化
阿新 • • 發佈:2018-10-31
Flowers
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3687 Accepted Submission(s): 1821
For each case, the first line contains two integer N and M, where N (1 <= N <= 10^5) is the number of flowers, and M (1 <= M <= 10^5) is the query times.
In the next N lines, each line contains two integer S i
In the next M lines, each line contains an integer T i, means the time of i-th query.
Output For each case, output the case number as shown and then print M lines. Each line contains an integer, meaning the number of blooming flowers.
Sample outputs are available for more details.
1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #include<string> 5 #include<cmath> 6 #include<algorithm> 7 #include<stack> 8 #include<climits> 9 #include<map> 10 #include<queue> 11 #define eps 1e-7 12 #define ll long long 13 #define inf 0x3f3f3f3f 14 #define pi 3.141592653589793238462643383279 15 using namespace std; 16 const int MAXN = 3e5 + 21; 17 18 int getNum[MAXN],li[MAXN/3],ri[MAXN/3],c[MAXN]; 19 20 int lowBit(int x) 21 { 22 return x&(-x); 23 } 24 25 void add(int x,int num,int len) //區間更新 26 { 27 for(int i=x; i<=len; i+=lowBit(i)) 28 { 29 c[i] += num; 30 } 31 } 32 33 ll query(int x) //單點查詢 34 { 35 ll ans = 0; 36 for(int i=x; i>0; i-=lowBit(i)) 37 { 38 ans += c[i]; 39 } 40 return ans; 41 } 42 43 int bSerach(int num,int len) //找出某個數在陣列中的下標,並返回下標+1 44 { 45 return lower_bound(getNum,getNum+len,num) - getNum + 1; 46 } 47 48 int main() 49 { 50 int t,n,m,cnt = 0; 51 cin>>t; 52 while(t--) 53 { 54 memset(c,0,sizeof(c)); 55 memset(getNum,0,sizeof(getNum)); 56 57 scanf("%d%d",&n,&m); 58 59 int cnt1 = 0; 60 61 for(int i=0; i<n; ++i) 62 { 63 scanf("%d%d",&li[i],&ri[i]); 64 getNum[cnt1++] = li[i]; //將花開放範圍出現的資料存入陣列 65 getNum[cnt1++] = ri[i]; 66 } 67 68 int ques[MAXN/3]; 69 for(int i=0; i<m; ++i) 70 { 71 scanf("%d",&ques[i]); 72 getNum[cnt1++] = ques[i]; //將查詢出現的資料存入陣列 73 } 74 75 sort(getNum,getNum+cnt1); //給所有出現的數排序 76 77 int cnt2 = 1; 78 for(int i=1; i<cnt1; ++i) 79 { 80 if(getNum[i] != getNum[i-1]) //去除重複的數 81 getNum[cnt2++] = getNum[i]; 82 } 83 84 85 for(int i=0; i<n; ++i) //樹狀陣列區間更新 86 { 87 add( bSerach(li[i],cnt2), 1,cnt2); 88 add( bSerach(ri[i],cnt2)+1, -1,cnt2); 89 } 90 91 printf("Case #%d:\n",++cnt); 92 for(int i=0; i<m; ++i) 93 { 94 cout<<query( bSerach(ques[i], cnt2) )<<endl; //樹狀陣列單點查詢 95 } 96 } 97 return 0; 98 }