标签:hdu
http://acm.hdu.edu.cn/showproblem.php?pid=4417
1 10 10 0 5 2 7 5 4 3 8 7 7 2 8 6 3 5 0 1 3 1 1 9 4 0 1 0 3 5 5 5 5 1 4 6 3 1 5 7 5 7 3
Case 1: 4 0 0 3 1 2 0 1 5 1
题意:n个数,m次询问[l,r]区间比h小于等于的数的个数。
思路:本来是个划分树裸题的,不好划分树233。但是树状数组也可以搞。
离线存进所有询问。按h大小从小到大处理每个询问,对于每个询问,查询[l,r]区间比h小的,其实就是查询[1,r]区间的减去[1,l-1]区间的,我们把比h小的数先插入到树状数组中,这里是对其在原数组的位置的地方插入,然后再进行查询,就可以算出每一个询问。
/**
* @author neko01
*/
//#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <cstring>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath>
#include <set>
#include <map>
using namespace std;
typedef long long LL;
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define pb push_back
#define mp(a,b) make_pair(a,b)
#define clr(a) memset(a,0,sizeof a)
#define clr1(a) memset(a,-1,sizeof a)
#define dbg(a) printf("%d\n",a)
typedef pair<int,int> pp;
const double eps=1e-8;
const double pi=acos(-1.0);
const int INF=0x7fffffff;
const LL inf=(((LL)1)<<61)+5;
const int N=100005;
struct node{
int l,r,val;
int id;
}q[N];
int ans[N];
int bit[N];
struct hehe{
int x,id;
}a[N];
int n,m;
bool cmp1(hehe u,hehe v)
{
return u.x<v.x;
}
bool cmp2(node u,node v)
{
return u.val<v.val;
}
int sum(int x)
{
int s=0;
while(x>0)
{
s+=bit[x];
x-=x&-x;
}
return s;
}
void add(int x,int val)
{
while(x<=n)
{
bit[x]+=val;
x+=x&-x;
}
}
int main()
{
int t,cnt=0;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
clr(bit);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i].x);
a[i].id=i;
}
for(int i=0;i<m;i++)
{
scanf("%d%d%d",&q[i].l,&q[i].r,&q[i].val);
q[i].l++;
q[i].r++;
q[i].id=i;
}
sort(a+1,a+n+1,cmp1);
sort(q,q+m,cmp2);
int j=1;
for(int i=0;i<m;i++)
{
while(j<=n&&q[i].val>=a[j].x)
{
add(a[j].id,1);
j++;
}
ans[q[i].id]=sum(q[i].r)-sum(q[i].l-1);
}
printf("Case %d:\n",++cnt);
for(int i=0;i<m;i++)
printf("%d\n",ans[i]);
}
return 0;
}
hdu4417 Super Mario 树状数组离线/划分树
标签:hdu
原文地址:http://blog.csdn.net/neko01/article/details/41092333