HDU 4417 Super Mario(主席树 区间不超过k的个数)题解
题意:问区间内不超过k的个数
思路:显然主席树,把所有的值离散化一下,然后主席树求一下小于等于k有几个就行。注意,他给你的k不一定包含在数组里,所以问题中的询问一起离散化。
代码:
#include<cmath>
#include<set>
#include<map>
#include<queue>
#include<cstdio>
#include<vector>
#include<cstring>
#include <iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 1e5 + ;
const int M = maxn * ;
const ull seed = ;
const int INF = 0x3f3f3f3f;
const int MOD = ;
int a[maxn], root[maxn], tot;
int n, m;
vector<int> vv;
int getId(int x){
return lower_bound(vv.begin(), vv.end(),x) - vv.begin() + ;
}
struct node{
int lson, rson;
int sum;
}T[maxn * ];
void init(){
memset(T, , sizeof(T));
tot = ;
vv.clear();
}
int lowbit(int x){
return x&(-x);
}
void update(int l, int r, int &now, int pre, int v, int pos){
T[++tot] = T[pre], T[tot].sum += v, now = tot;
if(l == r) return;
int m = (l + r) >> ;
if(m >= pos)
update(l, m, T[now].lson, T[pre].lson, v, pos);
else
update(m + , r, T[now].rson, T[pre].rson, v, pos);
}
int query(int l, int r, int now, int pre, int v){
if(l == r){
if(v >= l)
return T[now].sum - T[pre].sum;
return ;
}
if(r <= v)
return T[now].sum - T[pre].sum;
int m = (l + r) >> ;
int sum = ;
if(v < m){
return query(l, m, T[now].lson, T[pre].lson, v);
}
else{
sum = query(m + , r, T[now].rson, T[pre].rson, v);
return T[T[now].lson].sum - T[T[pre].lson].sum + sum;
}
}
struct ask{
int l, r, k;
}q[maxn];
int main(){
int T, ca = ;
scanf("%d", &T);
while(T--){
init();
scanf("%d%d", &n, &m);
for(int i = ; i <= n; i++){
scanf("%d", &a[i]);
vv.push_back(a[i]);
}
for(int i = ; i <= m; i++){
scanf("%d%d%d", &q[i].l, &q[i].r, &q[i].k);
q[i].l++, q[i].r++;
vv.push_back(q[i].k);
}
sort(vv.begin(), vv.end());
vv.erase(unique(vv.begin(), vv.end()), vv.end());
for(int i = ; i <= n; i++){
update(, vv.size(), root[i], root[i - ], , getId(a[i]));
}
printf("Case %d:\n", ca++);
for(int i = ; i <= m; i++){
printf("%d\n", query(, vv.size(), root[q[i].r], root[q[i].l - ], getId(q[i].k)));
}
}
return ;
}
HDU 4417 Super Mario(主席树 区间不超过k的个数)题解的更多相关文章
- HDU 4417 Super Mario 主席树
分析:找一个区间里小于等于h的数量,然后这个题先离散化一下,很简单 然后我写这个题主要是熟悉一下主席树,其实这个题完全可以离线做,很简单 但是学了主席树以后,我发现,在线做,一样简单,而且不需要思考 ...
- HDU 4417 Super Mario 主席树查询区间小于某个值的个数
#include<iostream> #include<string.h> #include<algorithm> #include<stdio.h> ...
- HDU 4417 Super Mario(划分树)
Super Mario Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU 4417 Super Mario(划分树问题求不大于k的数有多少)
Super Mario Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU 4417 - Super Mario ( 划分树+二分 / 树状数组+离线处理+离散化)
题意:给一个数组,每次询问输出在区间[L,R]之间小于H的数字的个数. 此题可以使用划分树在线解决. 划分树可以快速查询区间第K小个数字.逆向思考,判断小于H的最大的一个数字是区间第几小数,即是答案. ...
- HDU 4417 Super Mario ( 离线树状数组 )
把数值和查询放在一起从小到大排序,纪录每个数值的位置,当遇到数值时就更新到树状数组中,遇到查询就直接查询该区间和. #include <cstdio> #include <cstri ...
- HDU 4417 Super Mario(划分树+二分)
题目链接 #include <cstdio> #include <cstring> #include <algorithm> using namespace std ...
- zoj2112 树状数组+主席树 区间动第k大
Dynamic Rankings Time Limit: 10000MS Memory Limit: 32768KB 64bit IO Format: %lld & %llu Subm ...
- HDU4417 - Super Mario(主席树)
题目大意 给定一个数列,每次要求你查询区间[L,R]内不超过K的数的数量 题解 和静态的区间第K大差不多,这题是<=K,先建立好n颗主席树,然后用第R颗主席树区间[1,K]内数的数量减去第L-1 ...
随机推荐
- windows环境在本地配nginx
本地搭建了前端项目,但奈何有时候需要https访问的需求,所以做了一个尝试在本地(windows环境)下配置nginx,最终的效果就是 搭建的时候,遇到两个问题: 第一个是如果要在本地搭建https, ...
- c++简单程序设计 实验一
实验内容: 2-28 实现一个简单的菜单程序,运行时显示“Menu:A(dd) D(elete) S(ort) Q(uit),Selete one:”提示用户输入.A表示增加,D表示删除, S表示排序 ...
- nuget包管理nuget服务器发布包时出现请求报错 406 (Not Acceptable)
在window服务器上部署nuget服务器时,发布包时出现请求报错 406 (Not Acceptable) 验证用户名.密码正确的情况下,还是出现上面错误.后面跟踪服务器日志,发现window\te ...
- Visual Studio 2019 16.0 正式版下载
Visual Studio 2019 16.0 最新版 现在流行 官方版直接下载 在线选择性安装https://www.visualstudio.com/zh-hans/downloads/ Vis ...
- Kali-Dos洪水攻击之Hping3
在计算机行业,拒绝服务(DoS)或分布式拒绝服务(DDoS)攻击是指不法分子企图让某机器或网络资源无法被预期的用户所使用.虽然执行DoS攻击的方式.动机和目标不一样,但通常包括设法临时性或无限期中断或 ...
- 聊聊call、apply、bind的故事
说到call.apply.bind,前端的胖友可是不陌生.以下就从几个方面分别聊聊它们. 是什么?(what?) 实际上它们真正的样子是这样的: Function.prototype.call(thi ...
- 线性二次型调节器LQR/LQC算法解析及求解器代码(matlab)
参考链接:http://120.52.51.14/stanford.edu/class/ee363/lectures/dlqr.pdf 本文参考讲义中的第20页PPT,根据Hamilton-Jacob ...
- intellij idea 官方帮助文档翻译(前言)
对于java程序员来说,intellj idea这个编辑器,应该很多人都听说过,但用的人似乎并不是很多. 这也没办法,毕竟现在网上的教程,培训班授课,大多还是以eclipse为主. eclipse固然 ...
- HTML学记笔记
<!DOCTYPE html><html lang="zh-CN"><head> <meta charset="UTF-8&qu ...
- 字段like多个条件(or关系)简写
字段 like ‘匹配串1’or 字段 like ‘匹配串2’or ... 有如下简写方式 oracle: select * from tablex where REGEXP_LIKE(字段名, ' ...