Super Mario

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 12323    Accepted Submission(s): 5109

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

题目大意 : 输入N个数, M次询问, 每次询问 L 到 R 之间 ≤ K 的数有多少个

思路 : 主席树是求某个区间第k小的数是哪个, 这道题反过来了, 告诉你数让你求第几小, 所以可以二分答案, 注意二分答案的时候如果一直等于的话也要一直更新, 过程中存一下该数离散化之前是哪个数

Accepted code

#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;#define sc scanf
#define ls rt << 1
#define rs ls | 1
#define Min(x, y) x = min(x, y)
#define Max(x, y) x = max(x, y)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define MEM(x, b) memset(x, b, sizeof(x))
#define lowbit(x) ((x) & (-x))
#define P2(x) ((x) * (x))typedef long long ll;
const int MOD = 1e9 + 7;
const int MAXN = 1e5 + 100;
const int INF = 0x3f3f3f3f;
inline ll fpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t) % MOD; b >>= 1; t = (t*t) % MOD; }return r; }struct node
{int l, r, ans;
}t[MAXN * 20];
int p[MAXN], v[MAXN], n, m;
int root[MAXN], cnt, T, X;
vector <int> e;
void init() {MEM(t, 0); MEM(root, 0); cnt = 0;e.clear();
}
void Update(int l, int r, int &x, int y, int pos) {t[++cnt] = t[y]; t[cnt].ans++; x = cnt;if (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 pos) {if (l == r) return l;int mid = (l + r) >> 1;int sum = t[t[y].l].ans - t[t[x].l].ans;if (sum >= pos) return Query(l, mid, t[x].l, t[y].l, pos);else return Query(mid + 1, r, t[x].r, t[y].r, pos - sum);
}
int Fun(int L, int R, int K) {int len = R - L + 1;int l = 1, r = len, scnt = 0;int max_ = Query(1, n, root[L - 1], root[R], len);  // 区间最大值int min_ = Query(1, n, root[L - 1], root[R], 1);  // 区间最小值if (v[max_] <= K) return len;   // 大于最大直接退出else if (v[min_] > K) return 0;  // 小于最小直接退出while (l <= r) {   // 二分答案int mid = (l + r) >> 1;int stot = Query(1, n, root[L - 1], root[R], mid); if (v[stot] <= K) scnt = mid, l = mid + 1;  // 一直等于一直更新else r = mid - 1; }return scnt;
}int main()
{cin >> T;while (T--) {sc("%d %d", &n, &m); init();for (int i = 1; i <= n; i++) {sc("%d", &p[i]);e.push_back(p[i]);}sort(ALL(e));e.erase(unique(ALL(e)), e.end());for (int i = 1; i <= n; i++) {int tot = p[i];p[i] = lower_bound(ALL(e), p[i]) - e.begin() + 1;Update(1, n, root[i], root[i - 1], p[i]);v[p[i]] = tot;  // 离散化之前的数}printf("Case %d:\n", ++X);for (int i = 0; i < m; i++) {int L, R, K;sc("%d %d %d", &L, &R, &K);printf("%d\n", Fun(L + 1, R + 1, K));}}return 0;
}

#HDU 4417 Super Mario (主席树 + 二分)相关推荐

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

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

  2. hdu 4417 Super Mario 划分树+二分

    http://acm.hdu.edu.cn/showproblem.php?pid=4417 题意: 给定一个长度为n的序列,求区间[L,R]中小于h的个数: 思路: 分三种情况: 1:如果该区间最小 ...

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

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

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

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

  5. HDU 4417 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 ...

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

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

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

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

  9. HDU 4417 Super Mario(线段树)

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

最新文章

  1. 转载非原创:修改BB 的内容,回车后修改CC 的值
  2. Cassandra与RDBMS的设计差别
  3. iphone内存检测
  4. docker d盘_windows修改docker的默认存放位置
  5. Composer The openssl extension is required for SSL/TLS protection
  6. 打印的图片不清晰_如何调节图片kb,但又不改变图片的清晰度?
  7. 在新项目中使用 Vue3 使用总结
  8. TPS、QPS、系统吞吐量、并发用户数区别及性能术语的理解
  9. C#的多线程机制探索7
  10. python自动数据结构_Python 数据结构
  11. 解决vue的所有相关问题集合
  12. 数据分析最具价值的49个案例(建议收藏)
  13. 数学也荒唐:20个脑洞大开的数学趣题
  14. openpyxl 取消合并单元格且填充
  15. 外卖红包返利小程序源码系统
  16. 储存卡数据丢失恢复方法
  17. vue手写签名,canvas手写签名,canvas签名图片旋转
  18. mysql中ddl是什么_mysql ddl什么意思
  19. React 源码中的 Object.seal
  20. 游戏建模师具体干什么!30岁后进入行业算不算晚?

热门文章

  1. 搭建项目-快速搭建电商平台后台管理系统及逆向生成微服务基本功能
  2. 将无向图的邻接矩阵转为对应邻接表
  3. Java市场开始回暖!
  4. 一个班,排,连,营,有多少人
  5. 书单推荐|世界建筑日,感受几何与设计之美
  6. QQ 邮箱漂流瓶正式关闭;PostgreSQL 为 6 版本发布安全更新
  7. 支持券商的量化接口怎么使用python来执行交易过程?
  8. matlab可以实现从阻抗圆到导纳圆,阻抗与导纳圆图.ppt
  9. 前端牛不牛?只用27天全员就业,最高薪资19000元!
  10. 湖南省2015c语言真题及答案,湖南省2015年普通高等学校招生考试英语真题及答案...