http://acm.hdu.edu.cn/showproblem.php?pid=4417
Super Mario
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2720 Accepted Submission(s): 1322
Problem Description
Mario is world-famous plumber. His “burly” figure and amazing jumping ability reminded in our memory. Now the poor princess is in trouble again and Mario needs to save his lover. We regard the road to the boss’s castle as a line (the length is n), on every integer point i there is a brick on height hi. Now the question is how many bricks in [L, R] Mario can hit if the maximal height he can jump is H.
Input
The first line follows an integer T, the number of test data.
For each test data:
The first line contains two integers n, m (1 <= n <=10^5, 1 <= m <= 10^5), n is the length of the road, m is the number of queries.
Next line contains n integers, the height of each brick, the range is [0, 1000000000].
Next m lines, each line contains three integers L, R,H.( 0 <= L <= R < n 0 <= H <= 1000000000.)
For each test data:
The first line contains two integers n, m (1 <= n <=10^5, 1 <= m <= 10^5), n is the length of the road, m is the number of queries.
Next line contains n integers, the height of each brick, the range is [0, 1000000000].
Next m lines, each line contains three integers L, R,H.( 0 <= L <= R < n 0 <= H <= 1000000000.)
Output
For each case, output "Case X: " (X is the case number starting from 1) followed by m lines, each line contains an integer. The ith integer is the number of bricks Mario can hit for the ith query.
Sample Input
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
Sample Output
Case 1: 4 0 0 3 1 2 0 1 5 1
Source
题意: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;
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。