【可持久化线段树】【主席树】[HDU4417]Super Mario
来源:互联网 发布:网络运营商有哪些 编辑:程序博客网 时间:2024/05/01 23:32
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.
裸的模板题目,就是询问区间第K小这里注意一下数组是从0开始的就行了
#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>using namespace std;const int MAXN = 1e5+10;struct Tree{ int ch[2]; int sum;}pool[MAXN*40+10];int queries[MAXN+10][3], uniq[MAXN*2+10], val[MAXN+10], tot, n, m, Len, roots[MAXN+10];void Insert(int &u, int l, int r, int v){ ++tot; pool[tot] = pool[u]; u = tot; pool[u].sum ++; if(l == r) return ; int mid = (l + r) >> 1; if(v <= mid) Insert(pool[u].ch[0], l, mid, v); else Insert(pool[u].ch[1], mid+1, r, v);}int Query(int a, int b, int l, int r, int H){ if(r <= H) return pool[b].sum - pool[a].sum; if(l > H) return 0; int mid = (l + r) >> 1; return Query(pool[a].ch[0], pool[b].ch[0], l, mid, H) + Query(pool[a].ch[1], pool[b].ch[1], mid+1, r, H);}void Init(){ while(tot){ pool[tot].ch[0] = pool[tot].ch[1] = pool[tot].sum = 0; tot--; }}void Read(){ int tmp=0; scanf("%d%d", &n, &m); for(int i=1;i<=n;i++){ scanf("%d", &val[i]); uniq[++tmp] = val[i]; } for(int i=1;i<=m;i++){ scanf("%d%d%d", &queries[i][0], &queries[i][1], &queries[i][2]); uniq[++tmp] = queries[i][2]; } sort(uniq+1, uniq+1+tmp); Len = unique(uniq+1, uniq+1+tmp) - uniq - 1; for(int i=1;i<=n;i++) val[i] = lower_bound(uniq+1, uniq+1+Len, val[i]) - uniq; for(int i=1;i<=m;i++) queries[i][2] = lower_bound(uniq+1, uniq+1+Len, queries[i][2]) - uniq;}void Solve(){ for(int i=1;i<=n;i++){ roots[i] = roots[i-1]; Insert(roots[i], 1, Len, val[i]); } for(int i=1;i<=m;i++) printf("%d\n", Query(roots[queries[i][0]], roots[queries[i][1]+1], 1, Len, queries[i][2]));}int main(){ int T, t=0; scanf("%d", &T); while(t<T){ printf("Case %d:\n", ++t); Init(); Read(); Solve(); } return 0;}
0 0
- 【可持久化线段树】【主席树】[HDU4417]Super Mario
- HDU 4417 Super Mario [可持久化线段树 主席树]
- hdu4417 Super Mario (主席树+二分)
- hdu4417 Super Mario (主席树入门)
- 主席树总结-hdu4417 Super Mario
- HDU4417 Super Mario 主席树 / 树状数组
- Hdu4417 Super Mario(主席树+离散化)
- hdu4417 Super Mario 线段树端点更新
- HDU4417 Super Mario 线段树离线操作
- hdu4417- Super Mario(离线,线段树)
- HDU4417 Super Mario(划分树+二分)
- HDU 4417 Super Mario(线段树离线处理/主席树)
- 线段树 super mario
- Super Mario(线段树 + 离散化)
- spoj3267 D-query 主席树(可持久化线段树)
- 可持久化线段树(主席树)
- 主席树(可持久化线段树)入门专题
- 主席树(可持久化线段树)学习笔记
- java编译脚本的进阶之路
- uva11384 - Help is needed for Dexter (递归)
- fiddler是程序抓包必备软件之一,分享一个关于fiddler的技术博客
- Event-Driven Data Management for Microservices
- Hibernate高级技术
- 【可持久化线段树】【主席树】[HDU4417]Super Mario
- Codeforces Round #340 (Div. 2) A B C D
- angularjs 复制
- Linux 搭建LAMP 源码编译详细文档
- 空间滤波&频域滤波(2)
- 代码:用二分法求2x^3-4x^2+3x-6=0在-10~10之间的根(函数)
- 我是如何入门html和php的
- STM32 堆和栈的学习
- 读书笔记 精通CSS 高级Web标准解决方案(第2版)1~3章