Super Mario
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4090 Accepted Submission(s): 1883
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
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
4
0
0
3
1
2
0
1
5
1
题意:对于一个序列,查询每个h在区间中的大小排名,用主席树就可以了。
然而这一题内存限制32Mb,经过不断地优化,过的时候还占用32711Kb,唉,别人没用结构体的,内存占用只有3000Kb。
还有一个遗憾,这里的Solve函数其实可以用一个lower_bound()函数替代。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct Node{
int rs,ls,sum;
}tr[];
int A[],B[];
int rt[],pos,cnt;
void Build(int &node,int a,int b)
{
node=++cnt;
if(a==b)return;
int mid=(a+b)>>;
Build(tr[node].ls,a,mid);
Build(tr[node].rs,mid+,b);
} void Insert(int pre,int &node,int a,int b)
{
node=++cnt;
tr[node].ls=tr[pre].ls;
tr[node].rs=tr[pre].rs;
tr[node].sum=tr[pre].sum+;
if(a==b)return;
int mid=(a+b)>>;
if(mid>=pos)Insert(tr[pre].ls,tr[node].ls,a,mid);
else Insert(tr[pre].rs,tr[node].rs,mid+,b);
}
int Query(int pre,int node,int p,int a,int b)
{
if(b<=p)return tr[node].sum-tr[pre].sum;
int ret=;
ret=Query(tr[pre].ls,tr[node].ls,p,a,(a+b)>>);
if(p>(a+b)>>)
ret+=Query(tr[pre].rs,tr[node].rs,p,((a+b)>>)+,b);
return ret;
}
void Solve(int pre,int node,int h,int a,int b)
{
if(a==b){
pos=a;
return;
}
if(B[((a+b)>>)+]<=h)Solve(tr[pre].rs,tr[node].rs,h,((a+b)>>)+,b);
if(pos==-&&B[a]<=h)Solve(tr[pre].ls,tr[node].ls,h,a,(a+b)>>);
}
void Init()
{
memset(tr,,sizeof(tr));
cnt=;
}
int main()
{
int Q,n,q,kase=;
scanf("%d",&Q);
while(Q--)
{
Init();
printf("Case %d:\n",++kase);
scanf("%d%d",&n,&q);
for(int i=;i<=n;B[i]=A[i],i++)
scanf("%d",&A[i]);
sort(B+,B+n+);
Build(rt[],,n);
for(int i=;i<=n;i++)
{
pos=lower_bound(B+,B+n+,A[i])-B;
Insert(rt[i-],rt[i],,n);
}
int l,r,h;
for(int i=;i<=q;i++)
{
scanf("%d%d%d",&l,&r,&h);l++;r++;
pos=-;Solve(rt[l-],rt[r],h,,n);
if(pos==-){
printf("0\n");
continue;
}
printf("%d\n",Query(rt[l-],rt[r],pos,,n));
}
}
return ;
}