HDU 4417 Super Mario(主席树)
来源:互联网 发布:牧原股份 知乎 编辑:程序博客网 时间:2024/05/14 17:02
Super Mario
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6955 Accepted Submission(s): 2997
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
110 100 5 2 7 5 4 3 8 7 7 2 8 63 5 01 3 11 9 40 1 03 5 55 5 14 6 31 5 75 7 3
Sample Output
Case 1:4003120151
Source
2012 ACM/ICPC Asia Regional Hangzhou Online
Recommend
liuyiding
题意: 给出n个数m个问题让求出区间[l,r]内比 k小的有多少个。
分析:直接上主席树,查询的时候类似于线段树区间和。
AC代码:
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;#define MAX 100010int a[MAX],sorta[MAX];int root[MAX<<5];int cnt;struct Tree{int sum,lson,rson;}tree[MAX<<5];void bulid(int &v,int l,int r){v=++cnt;tree[v].sum=0;if(l==r)return;int mid=(l+r)>>1;bulid(tree[v].lson,l,mid);bulid(tree[v].rson,mid+1,r);}int Binary_search(int *p,int l,int r,int aim){while(l<=r){int mid=(l+r)>>1;if(p[mid]<=aim)l=mid+1;elser=mid-1;}return r;}void update(int p,int &v,int l,int r,int s){v=++cnt;tree[v]=tree[p];tree[v].sum++;if(l==r)return;int mid=(l+r)>>1;if(mid<s)update(tree[p].rson,tree[v].rson,mid+1,r,s);elseupdate(tree[p].lson,tree[v].lson,l,mid,s);}int query(int p,int v,int l,int r,int k){if(k<0)return 0;if(k>=r)return tree[v].sum-tree[p].sum;int mid=(l+r)>>1,ret=0;if(k>=l)ret+=query(tree[p].lson,tree[v].lson,l,mid,k);if(k>mid)ret+=query(tree[p].rson,tree[v].rson,mid+1,r,k);return ret;}int main(){int T,cas=0;scanf("%d",&T);while(T--){printf("Case %d:\n",++cas);cnt=0;int n,m;scanf("%d%d",&n,&m);for(int i=1;i<=n;i++){scanf("%d",&a[i]);sorta[i]=a[i];}sort(sorta+1,sorta+n+1);int t=1;for(int i=2;i<=n;i++)if(sorta[i]!=sorta[i-1])sorta[++t]=sorta[i];bulid(root[0],1,t);for(int i=1;i<=n;i++){int s=Binary_search(sorta,1,t,a[i]);update(root[i-1],root[i],1,t,s);}while(m--){int l,r,k;scanf("%d%d%d",&l,&r,&k);l++,r++;int y=Binary_search(sorta,1,t,k);printf("%d\n",query(root[l-1],root[r],1,t,y));}}}
阅读全文
0 0
- HDU 4417 Super Mario(主席树)
- HDU 4417-Super Mario(主席树)
- hdu 4417 Super Mario(主席树)
- hdu 4417 Super Mario (主席树+二分)
- 【HDU 4417】Super Mario 【主席树】
- HDU-4417 Super Mario (主席树)
- Hdu 4417 Super Mario(主席树+二分)
- HDU 4417 Super Mario(线段树离线处理/主席树)
- hdu-4417-Super Mario-只查询的主席树
- [hdu 4417 Super Mario] 主席树+离散化 OR 分块
- hdu 4417 Super Mario(主席树||cdq分治+树状数组)
- [Hdu] 4417 Super Mario (主席树模板题)
- hdu-4417-Super Mario(无修改的主席树-求小于等于k的个数)
- HDU 4417 Super Mario(树状数组离线处理 or 主席树)
- HDU 4417 Super Mario (主席树,区间小于k的个数)
- hdu4417 Super Mario (主席树入门)
- hdu 4417 Super Mario (划分树)
- hdu 4417 Super Mario(划分树)
- JAVA中循环删除list中元素的方法总结
- Sorting It All Out (拓扑排序)
- [NOIP2009] 靶形数独 骚气的大爆搜
- Java面试02|Java集合
- multiple definition
- HDU 4417 Super Mario(主席树)
- 数据结构实验之图论七:驴友计划
- python3.5 + pyqt5 + pyclarm (windows)
- HDU 3709 Balanced Number (数位DP)
- rsync 命令总结
- unix环境高级编程一书中部分错误处理函数
- Perl split字符串分割函数用法指南
- gitlab clone是报内存溢出
- JAVA多态和动态绑定详解