HDU
来源:互联网 发布:数据库删除语句delete 编辑:程序博客网 时间:2024/05/14 08:53
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.
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.)
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
Case 1:4003120151
解题思路:可持久化线段树模板题。与区间第k大一样道理!
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<string>#include<vector>#include <ctime>#include<queue>#include<set>#include<map>#include<list>#include<stack>#include<iomanip>#include<cmath>#include<bitset>#define INF 0x3f3f3f3fconst int Max=30*100000;typedef long long int ll;using namespace std;int ls[Max];int rs[Max];int sum[Max];int tot=0;int root[Max];ll a[100100],b[100100];int n,m;void build(int &root,int l,int r){ root=++tot; if(l==r) { sum[root]=0; ls[root]=root; rs[root]=root; return ; } int mid=(l+r)/2; build(ls[root],l,mid); build(rs[root],mid+1,r); sum[root]=sum[ls[root]]+sum[rs[root]];}void update(int last,int &root,int l,int r,int x,int v){ root=++tot; ls[root]=ls[last]; rs[root]=rs[last]; sum[root]=sum[last]; if(l==r) { sum[root]+=v; ls[root]=root; rs[root]=root; return ; } int mid=(l+r)/2; if(x<=mid) update(ls[last],ls[root],l,mid,x,v); else update(rs[last],rs[root],mid+1,r,x,v); sum[root]=sum[ls[root]]+sum[rs[root]];}int query(int root,int l,int r,int x){ if(l==r){ return sum[root]; } int mid=(l+r)/2; if(x>mid) return sum[ls[root]]+query(rs[root],mid+1,r,x); else return query(ls[root],l,mid,x);}int main(){ int T; scanf("%d",&T); for(int cas=1;cas<=T;cas++){ tot=0; memset(root,0,sizeof(root)); scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) { scanf("%I64d",&a[i]); b[i]=a[i]; } sort(b+1,b+n+1); int cnt=unique(b+1,b+n+1)-(b+1); b[++cnt]=INF; build(root[0],1,cnt); for(int i=1;i<=n;i++){ int x=lower_bound(b+1,b+cnt+1,a[i])-b; update(root[i-1],root[i],1,cnt,x,1); } printf("Case %d:\n",cas); for(int i=1;i<=m;i++){ int l,r; ll v; scanf("%d%d%I64d",&l,&r,&v);l++;r++; int x=upper_bound(b+1,b+cnt+1,v)-b-1; int R=0,L=0; if(x>0){ L=query(root[l-1],1,cnt,x); R=query(root[r],1,cnt,x); } printf("%d\n",R-L); } } return 0;}
阅读全文
0 0
- hdu
- hdu
- HDU
- hdu ()
- hdu
- hdu
- HDU
- HDU
- hdu
- hdu
- HDU
- Hdu
- hdu
- hdu-
- hdu
- hdu
- hdu
- HDU
- 如何搭配不同样式的图标
- 编写高性能js注意点
- opencv学习——Otsu's二值化
- tensorflow之tensorboard
- PAT刷题:1025. PAT Ranking (25)
- HDU
- Win7中的Cmd命令行的使用技巧
- 贴一篇 写好的上下移动排序代码
- Miller_Rabin素数测试[Fermat小定理][二次探测定理][同余式][Wilson定理]
- Dubbo框架
- C++primer函数部分课后题
- git打两个版本的增量包
- Android-内存泄漏-ValueAnimator
- python内置函数