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.

题目链接

  • 题意是马里奥路经1到n,每一个整数点上都有一块砖,每一块砖都有自己的高度h[i],下面有q个询问:l,r,h,问,在[l,r],马里奥能跳h这么高,一共能顶到多少块砖。
  • 很容易看出来是主席树,用主席树查一下小于等于这个高度的砖块在这个区间有多少个就好了。主席树区间查询,要注意的就是别忘了初始化,最后查询的时候判断一下这个区间位置是不是0,如果是0直接就不用查了,要不然会TLE
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 1e5+5;
int cnt, n, m, x, y, k;
int root[maxn], a[maxn];
struct node
{int l, r, sum;  //sum is value for this segment
}T[maxn*20];
vector<int> v;inline int Get_Id(int x)
{return lower_bound(v.begin(), v.end(), x)-v.begin()+1;
}void update(int l, int r, int &x, int y, int pos)
{//x is current tree, and y is the previous tree. At the begining, the current one is initializated by the previous oneT[++cnt] = T[y];    T[cnt].sum++;   x = cnt;    //return x as the node's id of the root of current treeif(l == r)  return ;int mid = (l+r)>>1;if(mid >= pos)  update(l, mid, T[x].l, T[y].l, pos);else update(mid+1, r, T[x].r, T[y].r, pos);
}int query(int l, int r, int x, int y, int left, int right)
{//cout << "(left, right): (" << left << ", " << right << "), (l, r): (" << l << ", " << r << ")" << endl;if(left <= l && r <= right) return (T[y].sum-T[x].sum);int mid = (l+r)>>1;/*Amazing! You can't write your code like as follows:int result = 0;if(left <= mid) result += query(l, mid, T[x].l, T[y].l, left, right);if(right > mid) result += query(mid+1, r, T[x].r, T[y].r, left, right);, or your code will mle*/if(right <= mid) return query(l, mid, T[x].l, T[y].l, left, right);else if(left > mid) return query(mid+1, r, T[x].r, T[y].r, left, right);else return query(l, mid, T[x].l, T[y].l, left, right)+query(mid+1, r, T[x].r, T[y].r, left, right);
}int main()
{int T;cin >> T;for(int cas = 1; cas <= T; cas++){//initializationmemset(root, 0, sizeof(root));cnt = 0;v.clear();scanf("%d%d", &n, &m);for(int i = 1; i <= n; i++) scanf("%d", &a[i]), v.push_back(a[i]);  //array v is used for discretizationprintf("Case %d:\n", cas);//discretizatesort(v.begin(), v.end());v.erase(unique(v.begin(), v.end()), v.end());int Max_Size = v.size();//end of discretizatefor(int i = 1; i <= n; i++) update(1, Max_Size, root[i], root[i-1], Get_Id(a[i]));for(int i = 1; i <= m; i++){scanf("%d%d%d", &x, &y, &k);int _right = upper_bound(v.begin(), v.end(), k)-v.begin();//cout << "right: " << _right << endl;//note that: you should compare _right with 0, and if not, your code will tleprintf("%d\n", _right ? query(1, Max_Size, root[x], root[y+1], 1, _right) : 0);}}return 0;
}

Super Mario(主席树)相关推荐

  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(主席树/线段树+离线)

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

  4. HDU4417 Super Mario 主席树

    欢迎访问~原文出处--博客园-zhouzhendong 去博客园看该题解 题目传送门 - HDU4417 题意概括 给定一个长度为n的区间,同时给出m个询问,每次询问在区间[l,r]中有多少个数小于或 ...

  5. HDU4417 Super Mario(离线树状数组或者主席树+二分)

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

  6. HDU 4417 Super Mario(线段树离线处理/主席树)

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

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

  9. HDU 4417 Super Mario(划分树问题求不大于k的数有多少)

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

  10. hdu4417 Super Mario(树状数组+离线区间操作)

    题目链接 Problem Description Mario is world-famous plumber. His "burly" figure and amazing jum ...

最新文章

  1. Xamarin Essentials教程振动Vibration
  2. WPF中自定义窗体标题栏
  3. winform弹出唯一窗体的方法
  4. 设置tomcat自动启动的相关脚本
  5. 捡对象引流脚本 内容_对象和索引流
  6. 【JS 逆向百例】无限 debugger 绕过,某网站互动数据逆向
  7. 程序员如何通过 Git 来更好地提交消息?
  8. Turbo C 2.0安装及其使用
  9. 【电路基础】第1章-电路的基本规律(1)
  10. 华为服务器磁盘IO性能查看,磁盘io性能
  11. 主观能动性存在的一个理论根据
  12. 计算机nie,聂眉宁-西南石油大学 - 计算机科学学院
  13. image caption学习笔记
  14. Openbci升级版使用方法
  15. 《数据库原理与应用》学习笔记(一):概论
  16. FLV方式实现网页FFmpeg推流无插件播放
  17. 东子破解修改oracle10g的最大连接数
  18. apqp过程流程图及编写规则_APQP各阶段输入及输出流程图(含输出资料清单)
  19. 2500 tons quarry machine
  20. Web 开发项目的6个最佳Java框架

热门文章

  1. 金蝶中间件上传文件报错
  2. 论文笔记之---Speed and accuracy trade-offs for modern convolutional object detectors
  3. Java语言 实验报告(三)
  4. 启悦高速公路噪音测试软件,启悦基础隔音,新手第一次发贴,有不足之处请见谅[已完更]...
  5. 关于软件工程是不是教会不怎么会写程序的人开发软件的个人观点
  6. 我对360安全卫士的一些评价
  7. 【系统分析师之路】2012年系统分析师上午综合知识真题
  8. matlab科学计数法输入_matlab中科学计数法怎么表示
  9. 锤子位置服务器,我的世界1.7.10锤子科技神秘服务器
  10. STM32变频器技术方案,源码+图纸,供参考设计与学习