HDU 4325 Flowers(树状数组+离散化)
2017-09-08 00:06
363 查看
#include <iostream> #include <cstdio> #include <fstream> #include <algorithm> #include <cmath> #include <deque> #include <vector> #include <queue> #include <string> #include <cstring> #include <map> #include <stack> #include <set> #include <list> #define INF 0x3f3f3f #define maxn 100000 + 50 #define juzheng 300 #define ll long long using namespace std; //Date:2017-9-4 //Author:HarryBlackCat struct xx{ int s; int e; }time[maxn]; int arr[maxn],tree[maxn],que[maxn],n,m,counter; int lowbit(int x){ return x & (-x); } void add(int index,int val,int n){ for(int i = index;i <= n;i += lowbit(i)) tree[i] += val; } int get_sum(int index){ int ans = 0; for(int i = index ;i > 0; i -=lowbit(i)) ans += tree[i]; return ans; } void init(){ memset(arr,0,sizeof(arr)); memset(tree,0,sizeof(tree)); memset(que,0,sizeof(que)); counter = 0; } int main(){ int t; while(cin >> t){ for(int p = 1;p <= t;p++){ init(); cin >> n >> m; for(int i = 1;i <= n;i++){ cin >> time[i].s >> time[i].e; //将时间段存入待处理的离散化数组 arr[++counter] = time[i].s; arr[++counter] = time[i].e; } for(int i = 1;i <= m;i++){ cin >> que[i]; //将询问时间点存入待处理的离散化数组 arr[++counter] = que[i]; } //离散化处理 sort(arr + 1,arr + 1 + counter);//排序 int len = unique(arr + 1,arr + 1 + counter) - arr - 1;//去重,并记录去重后的长度 for(int i = 1;i <= n;i++){ int s = lower_bound(arr + 1,arr + 1 + len,time[i].s) - arr - 1 + 1;//找到开始点在离散化数组的下标(+1是表示从1开始算起) int e = lower_bound(arr + 1,arr + 1 + len,time[i].e) - arr - 1 + 1;//找到结束点在离散化数组的下标 //利用 8c45 树状数组储存离散化的区间 add(s,1,len); add(e + 1,-1,len); } printf("Case #%d:\n",p); for(int i = 1;i <= m;i++){ int index = lower_bound(arr + 1,arr + 1 + len,que[i]) - arr - 1 + 1;//找到询问点的离散化数组中的下标 printf("%d\n",get_sum(index));//输出ans } } } return 0; }
相关文章推荐
- HDU 4325 Flowers(树状数组+离散化)
- hdu 4325 Flowers (树状数组+离散化)
- hdu 4325 Flowers 2012多校1006题 树状数组解法
- [HDOJ4325]Flowers(树状数组 离散化)
- HDU OJ 4325 Flowers 【树状数组离散化】
- HDU 4325 Flowers(树状数组)
- Flowers--(树状数组,离散化)
- hdu 4325 Flowers(区间离散化)
- HDU 5542 The Battle of Chibi (离散化+树状数组优化DP)
- HDU 5714 树状数组 + 离散化
- hdu_2227_Find the nondecreasing subsequences_树状数组,离散化
- hdu 4605(树状数组+离散化)
- 反向树状数组 + 离散化 HDU 5372
- HDU 4911 Inversion(树状数组求逆序对数 + 数据离散化)
- HDU4325--Flowers--树状数组,离散化
- HDU 4417 —— Super Mario(树状数组,离散化,离线处理)
- hdu_2227_Find the nondecreasing subsequences_树状数组,离散化
- 树状数组 + 离散化 + dfs HDU 5877
- HDU 5542 The Battle of Chibi (ccpc 南阳 C)(DP 树状数组 离散化)
- hdu 5489 Removed Interval 2015合肥网络赛 树状数组 dp 离散化/dp