题意:问区间内不超过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的个数)题解的更多相关文章

  1. HDU 4417 Super Mario 主席树

    分析:找一个区间里小于等于h的数量,然后这个题先离散化一下,很简单 然后我写这个题主要是熟悉一下主席树,其实这个题完全可以离线做,很简单 但是学了主席树以后,我发现,在线做,一样简单,而且不需要思考 ...

  2. HDU 4417 Super Mario 主席树查询区间小于某个值的个数

    #include<iostream> #include<string.h> #include<algorithm> #include<stdio.h> ...

  3. HDU 4417 Super Mario(划分树)

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

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

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

  5. HDU 4417 - Super Mario ( 划分树+二分 / 树状数组+离线处理+离散化)

    题意:给一个数组,每次询问输出在区间[L,R]之间小于H的数字的个数. 此题可以使用划分树在线解决. 划分树可以快速查询区间第K小个数字.逆向思考,判断小于H的最大的一个数字是区间第几小数,即是答案. ...

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

    把数值和查询放在一起从小到大排序,纪录每个数值的位置,当遇到数值时就更新到树状数组中,遇到查询就直接查询该区间和. #include <cstdio> #include <cstri ...

  7. HDU 4417 Super Mario(划分树+二分)

    题目链接 #include <cstdio> #include <cstring> #include <algorithm> using namespace std ...

  8. zoj2112 树状数组+主席树 区间动第k大

    Dynamic Rankings Time Limit: 10000MS   Memory Limit: 32768KB   64bit IO Format: %lld & %llu Subm ...

  9. HDU4417 - Super Mario(主席树)

    题目大意 给定一个数列,每次要求你查询区间[L,R]内不超过K的数的数量 题解 和静态的区间第K大差不多,这题是<=K,先建立好n颗主席树,然后用第R颗主席树区间[1,K]内数的数量减去第L-1 ...

随机推荐

  1. KXO151 Programming & Problem Solving

    Page 1 of 9KXO151 Programming & Problem SolvingAIEN-SOU - 2019Assignment 2Deadline for Submissio ...

  2. find 详解

    find 详解 原文请访问http://itlab.idcquan.com/linux/SHELL/949102.html 当最初登录到系统中时, u m a s k命令确定了你创建文件的缺省模式.这 ...

  3. 发现了一个比较有意思的url参数

    今天登录阿里云发现需要二次验证了,手机号不是我的很麻烦,然后就看到有个手机app快捷登录的方式,点进去一看,链接地址是这样的http://qd.alibaba.com/onekey.htm?spm=0 ...

  4. 单机单网卡最大 tcp 长连接数是 65535 吗

    在Linux里,如果是作为客户端或者负载均衡器的节点连接多个服务器,在connect()服务器之前, 调用bind()先绑定IP地址(通常是在多网卡的场景),即使使用bind(IP, port=0), ...

  5. 创建docker公共仓库

    1.注册公共仓库的账号 https://hub.docker.com 账号:user-a 密码:pwd 2.Linux中登录docker公共仓库 docker login 3.上传一个docker镜像 ...

  6. 2019年春季学期第四周作业Compile Summarize

    这个作业属于哪个课程 C语言程序设计一 这个作业要求在哪里 2019春季学期第四周作业 我的课程目标 重新学习有关数组的问题 这个作业在哪个具体方面帮助我实现目标 对于置换有了新的见解 参考文献 中国 ...

  7. vi光标移动

    1.上下左右移动 k  :上移一行 j  :下移一行 h :左移一行 l :右移一行 2.移到当前屏幕的首.中.尾部 H :移到当前屏幕的首部 M    :移到当前屏幕的中部 L :移到当前屏幕的尾部 ...

  8. Schlumberger Petrel 2016.3 地震解释 油藏模拟

    Schlumberger Petrel 2016.3 地震解释 油藏模拟世界上顶尖的三维地质建模软件,软件为用户提供的工具可以用于地震解释.地质建模.油藏数 值模拟等方面的使用,清晰的地质模型可以描述 ...

  9. 训练集(train set),验证集(validation set)和测试集(test set)

    把数据集分为三部分,分别为:训练集(train set),验证集(validation set)和测试集(test set). 具体比例有各种说法.待补充 测试集是为了测模型泛化能力,不能在训练的时候 ...

  10. 关于Linux与Windows的在服务器的一些区别

    我们平时说学习运维要依托于Linux系统,因为在服务器领域Linux基本取得了市场,那么Linux在服务器领域与Windows相比有哪些优势呢?我们来看下:我们选择服务器主要是成本,安全稳定,这两大方 ...