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.)
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

将砖块高度val和玛丽能跳的高度H都存起来,更新p[k].val<=q[i].h的,区间求和的时候就能保证当前更新的都是小于玛丽能跳的的高度。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<limits.h>
typedef long long LL;
using namespace std;
const int maxn=1e5+10;
struct tree{int l,r;int cnt;
}t[maxn<<2];
struct node1{int val;int pos;bool operator<(const node1 l1)const{return val<l1.val;}
}p[maxn];//保存玛丽的原始位置和能跳的高度
struct node2{int l,r;int id,h;bool operator<(const node2 l2)const{return h<l2.h;}
}q[maxn];//保存m次询问
int ans[maxn];
void pushup(int rs)
{t[rs].cnt=t[rs<<1].cnt+t[rs<<1|1].cnt;
}
void build(int rs,int l,int r)
{t[rs].l=l;t[rs].r=r;t[rs].cnt=0;if(l==r)return ;int mid=(l+r)>>1;build(rs<<1,l,mid);build(rs<<1|1,mid+1,r);
}
void update(int rs,int pos)
{t[rs].cnt++;if(t[rs].l==t[rs].r)return ;int mid=(t[rs].l+t[rs].r)>>1;if(pos<=mid)  update(rs<<1,pos);else   update(rs<<1|1,pos);
}
int query(int l,int r,int rs)
{if(t[rs].l==l&&t[rs].r==r)return t[rs].cnt;int mid=(t[rs].l+t[rs].r)>>1;if(r<=mid)   return query(l,r,rs<<1);else if(l>mid)  return query(l,r,rs<<1|1);else  return query(l,mid,rs<<1)+query(mid+1,r,rs<<1|1);
}
int main()
{int t,n,m;int cas=1;scanf("%d",&t);while(t--){scanf("%d%d",&n,&m);build(1,1,n);for(int i=1;i<=n;i++){scanf("%d",&p[i].val);p[i].pos=i;}for(int i=1;i<=m;i++){scanf("%d%d%d",&q[i].l,&q[i].r,&q[i].h);q[i].id=i;}sort(p+1,p+1+n);sort(q+1,q+1+m);int k=1;for(int i=1;i<=m;i++){while(k<=n&&p[k].val<=q[i].h){update(1,p[k].pos);k++;}ans[q[i].id]=query(q[i].l+1,q[i].r+1,1);}printf("Case %d:\n",cas++);for(int i=1;i<=m;i++)printf("%d\n",ans[i]);}return 0;
}
另外树状数组也可以做。类似的单点更新和区间求和问题都可以用树状数组来写。
类似离线线段树的思想将节点和询问保存排序后依次更新,就能保证结果的正确性。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<limits.h>
typedef long long LL;
using namespace std;
const int maxn=1e5+10;
int cnt[maxn];
struct node1{int val;int pos;bool operator<(const node1 l1)const{return val<l1.val;}
}p[maxn];//保存玛丽的原始位置和能跳的高度
struct node2{int l,r;int id,h;bool operator<(const node2 l2)const{return h<l2.h;}
}q[maxn];//保存m次询问
int ans[maxn];
int lowbit(int x)
{return x&(-x);
}
void update(int x)
{while(x<maxn){cnt[x]++;x+=lowbit(x);}
}
int query(int x)
{int s=0;while(x>0){s+=cnt[x];x-=lowbit(x);}return s;
}
int main()
{int t,n,m;int cas=1;scanf("%d",&t);while(t--){memset(cnt,0,sizeof(cnt));scanf("%d%d",&n,&m);for(int i=1;i<=n;i++){scanf("%d",&p[i].val);p[i].pos=i;}for(int i=1;i<=m;i++){scanf("%d%d%d",&q[i].l,&q[i].r,&q[i].h);q[i].id=i;}sort(p+1,p+1+n);sort(q+1,q+1+m);int k=1;for(int i=1;i<=m;i++){while(k<=n&&p[k].val<=q[i].h){update(p[k].pos);k++;}ans[q[i].id]=query(q[i].r+1)-query(q[i].l);}printf("Case %d:\n",cas++);for(int i=1;i<=m;i++)printf("%d\n",ans[i]);}return 0;
}

HDU 4417 Super Mario(离线线段树or树状数组)相关推荐

  1. HDU 4417 Super Mario(线段树)

    Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  2. HDU 4417 Super Mario(线段树||树状数组+离线操作 之线段树篇)

    Mario is world-famous plumber. His "burly" figure and amazing jumping ability reminded in ...

  3. HDU 4417 Super Mario(离线 + 树状数组)

    题目: Mario is world-famous plumber. His "burly" figure and amazing jumping ability reminded ...

  4. HDU - 4417 Super Mario(主席树/线段树+离线)

    题目链接:点击查看 题目大意:给出由 n 个数的数列,再给出 m 次查询,每次查询需要输出 [ l , r ] 内小于等于 h 的数有多少个 题目分析:大晚上睡不着觉随便做做题,发现这个题目原来可以用 ...

  5. hdu 4417 Super Mario(可持久化线段树)

    题意:给你一些数,有多次询问,问你在l,r区间内小于k的数有多少个 思路:主席树大发好,虽然树状数组和线段树离线也可以做 代码: #include <set> #include <m ...

  6. HDU 4417 Super Mario(莫队 + 树状数组 + 离散化)

    Super Mario 思路 区间查找问题,容易想到离线莫队,确实这题就是莫队,接下来我们考虑如何维护区间高度值问题. 既然是离线嘛,我们容易想到离散化和他挂钩,想想这题是否需要离散化,高度的最大值是 ...

  7. HDU 4417 Super Mario(划分树)

    Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  8. hdu 4417 Super Mario 树状数组||主席树

    Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Prob ...

  9. #HDU 4417 Super Mario (主席树 + 二分)

    Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

最新文章

  1. 李开复从不缺席的AI夏令营开营:今年周志华俞勇张潼授课,1万人报名仅600入选...
  2. 安装后改中文界面_非常详尽图文KVM安装CentOS
  3. lnmp 一键安装包部署ThinkPHP5
  4. OpenCL memory object 之选择传输path
  5. python开发基本流程_第一阶段:Python开发基础 day06  Python基础语法入门--流程控制(二)...
  6. CentOS 系统盘迁移
  7. 域domain user用户对本地NTFS磁盘的写入权限问题
  8. 《长安十二时辰》火了!程序员版本过于真实!
  9. oracle删除数据库中的所有数据的拼接语句
  10. 【ML小结14】条件随机场CRF
  11. Windows XP Embedded SP2 + 简体中文语言包
  12. 【LeetCode】3月18日打卡-Day3
  13. 富士通Fujitsu DPK1786T 打印机驱动
  14. 使用百度API获取位置信息
  15. Intel开发手册下载地址
  16. python点击按钮打开游戏_Python如何入门?直接按这个方式玩炸弹超人小游戏,就能掌握编程...
  17. MM们必败潮物。。。。大眼睛的小秘密哦```````
  18. HMGK-being_hacked
  19. java代码实现短信接受验证码
  20. 2017南开秋奥鹏作业计算机,南开17秋学期《DirectX程序设计》在线作业(资料)...

热门文章

  1. python小技巧:一步步教你用Python实现
  2. 基于stm32f407的无线视屏传输项目
  3. C语言零基础学习日记
  4. Activity启动过程详解(Android P)
  5. python爬取王者荣耀皮肤高清图
  6. Calibre 3.0 正式版发布,开源电子书管理软件
  7. 腾达tenda U9无线网卡
  8. 用SQL语句向数据库添加date类型字段
  9. yum -- Failed connect to mirrors.aliyuncs.com:80; No route to host
  10. ZeroC Ice 暂记