您的位置:首页 > 其它

hdu-4417 Super Mario(树状数组 + 划分树)

2016-04-05 23:48 495 查看
题目链接:

Super Mario

Time Limit: 2000/1000 MS (Java/Others)

Memory Limit: 32768/32768 K (Java/Others)

[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
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.)

[align=left]Output[/align]
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.

[align=left]Sample Input[/align]

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

[align=left]Sample Output[/align]

Case 1:
4
0
0
3
1
2
0
1
5
1

题意:

给定一个数列.给区间[l,r]问此区间内<=k的数目是多少;

思路:

可以用树状数组离线处理,把原序列按从小到大排序,询问也按k的大小排序,然后一边询问一边update;最近学了划分树,也可以做这题,看明天有时间想一想怎么做吧;

AC代码:

/*
树状数组的代码:
4417    202MS    3744K    1873 B    G++    2014300227

*/

#include <bits/stdc++.h>
using namespace std;
const int N=1e5+4;
int a
,n,m,sum
,ans
;
struct node
{
friend bool operator< (node x,node y)
{
if(x.num == y.num)return x.pos < y.pos;
return x.num < y.num;
}
int l,r,pos,num;
};
node po
;
struct nod
{
friend bool operator< (nod x,nod y)
{
if(x.a==y.a)return x.pos<y.pos;
return x.a<y.a;
}
int a,pos;
};
nod p
;
int lowbit(int x)
{
return x&(-x);
}
void update(int x)
{
while(x<=n)
{
sum[x]++;
x+=lowbit(x);
}
}
int query(int x)
{
int s=0;
while(x>0)
{
s+=sum[x];
x-=lowbit(x);
}
return s;
}
int main()
{
int t;
scanf("%d",&t);
int cnt=1;
while(t--)
{
memset(sum,0,sizeof(sum));
printf("Case %d:\n",cnt++);
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%d",&p[i].a);
p[i].pos=i;
}
sort(p+1,p+n+1);
for(int i=0;i<m;i++)
{
scanf("%d%d%d",&po[i].l,&po[i].r,&po[i].num);
po[i].l++;
po[i].r++;
po[i].pos=i;
}
sort(po,po+m);
int fp=1;
for(int i=0;i<m;i++)
{
if(p[fp].a<=po[i].num)
{
while(p[fp].a<=po[i].num&&fp<=n)
{
update(p[fp].pos);
fp++;
}
}
if(po[i].l==1)ans[po[i].pos]=query(po[i].r);
else
{
ans[po[i].pos]=query(po[i].r)-query(po[i].l-1);
}
}
for(int i=0;i<m;i++)
{
printf("%d\n",ans[i]);
}

}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: