[HDU 4417] Super Mario (树状数组)
2014-11-07 22:40
387 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4417
题目大意:给你n个数,下标为0到n-1,m个查询,问查询区间[l,r]之间小于等于x的数有多少个。
写的时候逗比了。。。还是写的太少了。。
我们按照x从小到大排序来查询,然后找区间上的点,如果小于等于它就插入,然后看这个区间内插入了多少个点。
点也是可以排序的。。
详见代码:
题目大意:给你n个数,下标为0到n-1,m个查询,问查询区间[l,r]之间小于等于x的数有多少个。
写的时候逗比了。。。还是写的太少了。。
我们按照x从小到大排序来查询,然后找区间上的点,如果小于等于它就插入,然后看这个区间内插入了多少个点。
点也是可以排序的。。
详见代码:
#include <cstdio> #include <cmath> #include <algorithm> #include <cstring> #include <vector> using namespace std; typedef pair<int,int> PII; const int MAX_N = 100100; int bit[MAX_N]; PII a[MAX_N]; int ans[MAX_N]; struct Node{ int ql,qr,qh,idx; bool operator<(const Node& a) const{ return qh<a.qh; } }; Node qq[MAX_N]; int T,n,m; int ub; void add(int i,int x){ while( i<=n ){ bit[i] += x; i+=i&-i; } } int sum(int i){ int res = 0; while( i>0 ){ res += bit[i]; i -= i&-i; } return res; } int main(){ scanf("%d",&T); int kase = 1; while( T-- ){ memset(bit,0,sizeof(bit)); printf("Case %d:\n",kase++); scanf("%d%d",&n,&m); int ptr = 0; for(int i=0;i<n;i++){ scanf("%d",&a[i].first); a[i].second = i+1; } for(int i=0;i<m;i++){ scanf("%d%d%d",&qq[i].ql,&qq[i].qr,&qq[i].qh); qq[i].idx = i; } sort(qq,qq+m); sort(a,a+n); int j=0; for(int i=0;i<m;i++){ while(j<n && a[j].first<=qq[i].qh ){ add(a[j].second,1); j++; } ans[qq[i].idx] = sum( qq[i].qr+1 ) - sum(qq[i].ql); } for(int i=0;i<m;i++){ printf("%d\n",ans[i]); } } return 0; }
相关文章推荐
- HDU 4417 Super Mario ( 离线树状数组 )
- HDU 4417 Super Mario--离线树状数组、划分树、线段树
- hdu 4417 Super Mario (树状数组)
- HDU 4417 Super Mario(树状数组离线处理 or 主席树)
- hdu 4417 Super Mario 树状数组||主席树
- HDU 4417 Super Mario (树状数组、离线处理)
- HDU 4417 Super Mario(离线线段树or树状数组)
- HDU 4417 Super Mario (树状数组 + 离线)
- HDU 4417 Super Mario (树状数组)
- hdu 4417 Super Mario(划分树或树状数组)
- HDU 4417 Super Mario 第37届ACM/ICPC 杭州赛区网络赛1008题(树状数组或者线段树)
- HDU 4417 —— Super Mario(树状数组,离散化,离线处理)
- hdu-4417 Super Mario(树状数组 + 划分树)
- hdu 4417 树状数组 离线处理
- HDU 4417-树状数组求区间rank查询/主席树区间rank查询
- hdu 4417 离线树状数组 胡乱搞搞
- HDU 4417 Super Mario(线段树||树状数组+离线操作 之树状数组篇)
- hdoj 4417 Super Mario 【树状数组 + 思维】
- hdu 4417 离线树状数组
- 树状数组 or 归并树 —— HDU 4417