主席树:HDU 4417 Super Mario
2016-02-19 21:46
357 查看
Super Mario
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4090 Accepted Submission(s): 1883
[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
题意:对于一个序列,查询每个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[2500010]; int A[100010],B[100010]; int rt[100010],pos,cnt; void Build(int &node,int a,int b) { node=++cnt; if(a==b)return; int mid=(a+b)>>1; Build(tr[node].ls,a,mid); Build(tr[node].rs,mid+1,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+1; if(a==b)return; int mid=(a+b)>>1; if(mid>=pos)Insert(tr[pre].ls,tr[node].ls,a,mid); else Insert(tr[pre].rs,tr[node].rs,mid+1,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=0; ret=Query(tr[pre].ls,tr[node].ls,p,a,(a+b)>>1); if(p>(a+b)>>1) ret+=Query(tr[pre].rs,tr[node].rs,p,((a+b)>>1)+1,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)>>1)+1]<=h)Solve(tr[pre].rs,tr[node].rs,h,((a+b)>>1)+1,b); if(pos==-1&&B[a]<=h)Solve(tr[pre].ls,tr[node].ls,h,a,(a+b)>>1); } void Init() { memset(tr,0,sizeof(tr)); cnt=0; } int main() { int Q,n,q,kase=0; scanf("%d",&Q); while(Q--) { Init(); printf("Case %d:\n",++kase); scanf("%d%d",&n,&q); for(int i=1;i<=n;B[i]=A[i],i++) scanf("%d",&A[i]); sort(B+1,B+n+1); Build(rt[0],1,n); for(int i=1;i<=n;i++) { pos=lower_bound(B+1,B+n+1,A[i])-B; Insert(rt[i-1],rt[i],1,n); } int l,r,h; for(int i=1;i<=q;i++) { scanf("%d%d%d",&l,&r,&h);l++;r++; pos=-1;Solve(rt[l-1],rt[r],h,1,n); if(pos==-1){ printf("0\n"); continue; } printf("%d\n",Query(rt[l-1],rt[r],pos,1,n)); } } return 0; }
相关文章推荐
- pixhawk自学笔记之从串口获取光流数据
- [Android Pro] CountDownTimer倒计时
- 《你一定爱读的极简欧洲史》:从六个维度高度概括欧洲历史,五星推荐
- PHP之创建对象的基本形式
- 【hdu 2896】病毒侵袭 题解&代码(C++)
- 【css】table
- HDU2504:又见GCD
- OC运算
- Linux应用程序地址布局
- 《大话设计模式》读书笔记2 策略模式
- POJ3278==抓住那头牛
- Android学习一
- JavaScript之BOM
- Music List
- gitk安装(linux,window)
- Lua Challenge -- From Python Challenge
- Spark MLlib 1.6 -- 分类和回归篇
- 如何彻底卸载MySQL(转载)
- ASP.NET取得客户端IP地址及计算机名
- 性能优化--JS、CSS压缩合并