#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn=1e6+5;
int sum[maxn*4],n;
int ans[maxn];
struct Node1
{
int h;
int num;
bool operator < (const Node1 temp) const
{
return h<temp.h;
}
} a[maxn];
struct Node2
{
int l,r,h;
int num;
bool operator < (const Node2 temp) const
{
return h<temp.h;
}
} b[maxn];
void Build(int l,int r,int rt)
{
if(l==r)
{
sum[rt]=0;
return ;
}
int mid=(l+r)/2;
Build(l,mid,2*rt);
Build(mid+1,r,2*rt+1);
}
void Update(int p,int add,int l,int r,int rt)
{
if(l==r)
{
sum[rt]=sum[rt]+add;
return ;
}
int mid=(l+r)/2;
if(p<=mid) Update(p,add,l,mid,2*rt);
else Update(p,add,mid+1,r,2*rt+1);
sum[rt]=sum[rt*2]+sum[rt*2+1];
}
int Query(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
return sum[rt];
}
int mid=(l+r)/2;
int ans1=0;
if(L<=mid) ans1=ans1+Query(L,R,l,mid,2*rt);
if(R>mid) ans1=ans1+Query(L,R,mid+1,r,2*rt+1);
return ans1;
}
int main()
{
/*Build(1,4,1);
Update(1,1,1,4,1);
Update(2,2,1,4,1);
Update(3,1,1,4,1);
Update(4,1,1,4,1);
int ans2=Query(1,3,1,4,1);
printf("%d\n",ans2);*/
int t,m,ui=1;
scanf("%d",&t);
while(t--)
{
memset(sum,0,sizeof(sum));
printf("Case %d:\n",ui++);
scanf("%d%d",&n,&m);
for(int i=1; i<=n; i++)
{
scanf("%d",&a[i].h);
a[i].num=i;
}
for(int i=1; i<=m; i++)
{
scanf("%d%d%d",&b[i].l,&b[i].r,&b[i].h);
b[i].num=i;
}
sort(a+1,a+1+n);
sort(b+1,b+1+m);
for(int i=1,j=1; i<=m; i++)
{
while(b[i].h>=a[j].h&&j<=n)
{
Update(a[j++].num,1,1,n,1);
}
ans[b[i].num]=Query(b[i].l+1,b[i].r+1,1,n,1);
}
for(int i=1; i<=m; i++)
{
printf("%d\n",ans[i]);
}
}
}
Super Mario
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4946 Accepted Submission(s): 2263
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
Recommend
根本没有理解线段树的实现。GG.