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
Sample Output
Case 1:
4
0
0
3
1
2
0
1
5
1
题意:对于一个序列,查询每个h在区间中的大小排名,用主席树就可以了。
然而这一题内存限制32Mb,经过不断地优化,过的时候还占用32711Kb,唉,别人没用结构体的,内存占用只有3000Kb。
还有一个遗憾,这里的Solve函数其实可以用一个lower_bound()函数替代。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 using namespace std; 6 struct Node{ 7 int rs,ls,sum; 8 }tr[2500010]; 9 int A[100010],B[100010]; 10 int rt[100010],pos,cnt; 11 void Build(int &node,int a,int b) 12 { 13 node=++cnt; 14 if(a==b)return; 15 int mid=(a+b)>>1; 16 Build(tr[node].ls,a,mid); 17 Build(tr[node].rs,mid+1,b); 18 } 19 20 void Insert(int pre,int &node,int a,int b) 21 { 22 node=++cnt; 23 tr[node].ls=tr[pre].ls; 24 tr[node].rs=tr[pre].rs; 25 tr[node].sum=tr[pre].sum+1; 26 if(a==b)return; 27 int mid=(a+b)>>1; 28 if(mid>=pos)Insert(tr[pre].ls,tr[node].ls,a,mid); 29 else Insert(tr[pre].rs,tr[node].rs,mid+1,b); 30 } 31 int Query(int pre,int node,int p,int a,int b) 32 { 33 if(b<=p)return tr[node].sum-tr[pre].sum; 34 int ret=0; 35 ret=Query(tr[pre].ls,tr[node].ls,p,a,(a+b)>>1); 36 if(p>(a+b)>>1) 37 ret+=Query(tr[pre].rs,tr[node].rs,p,((a+b)>>1)+1,b); 38 return ret; 39 } 40 void Solve(int pre,int node,int h,int a,int b) 41 { 42 if(a==b){ 43 pos=a; 44 return; 45 } 46 if(B[((a+b)>>1)+1]<=h)Solve(tr[pre].rs,tr[node].rs,h,((a+b)>>1)+1,b); 47 if(pos==-1&&B[a]<=h)Solve(tr[pre].ls,tr[node].ls,h,a,(a+b)>>1); 48 } 49 void Init() 50 { 51 memset(tr,0,sizeof(tr)); 52 cnt=0; 53 } 54 int main() 55 { 56 int Q,n,q,kase=0; 57 scanf("%d",&Q); 58 while(Q--) 59 { 60 Init(); 61 printf("Case %d: ",++kase); 62 scanf("%d%d",&n,&q); 63 for(int i=1;i<=n;B[i]=A[i],i++) 64 scanf("%d",&A[i]); 65 sort(B+1,B+n+1); 66 Build(rt[0],1,n); 67 for(int i=1;i<=n;i++) 68 { 69 pos=lower_bound(B+1,B+n+1,A[i])-B; 70 Insert(rt[i-1],rt[i],1,n); 71 } 72 int l,r,h; 73 for(int i=1;i<=q;i++) 74 { 75 scanf("%d%d%d",&l,&r,&h);l++;r++; 76 pos=-1;Solve(rt[l-1],rt[r],h,1,n); 77 if(pos==-1){ 78 printf("0 "); 79 continue; 80 } 81 printf("%d ",Query(rt[l-1],rt[r],pos,1,n)); 82 } 83 } 84 return 0; 85 }