[HDU 4417] Super Mario (树状数组)

时间:2023-03-09 16:42:57
[HDU 4417] Super Mario (树状数组)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4417

题目大意:给你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 = ;
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 = ;
while( i> ){
res += bit[i];
i -= i&-i;
}
return res;
} int main(){
scanf("%d",&T);
int kase = ;
while( T-- ){
memset(bit,,sizeof(bit));
printf("Case %d:\n",kase++);
scanf("%d%d",&n,&m);
int ptr = ;
for(int i=;i<n;i++){
scanf("%d",&a[i].first);
a[i].second = i+;
}
for(int i=;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=;
for(int i=;i<m;i++){
while(j<n && a[j].first<=qq[i].qh ){
add(a[j].second,);
j++;
}
ans[qq[i].idx] = sum( qq[i].qr+ ) - sum(qq[i].ql);
} for(int i=;i<m;i++){
printf("%d\n",ans[i]);
} }
return ;
}