题意:问区间内不超过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. Enterprise Architect

    Enterprise Architect 是一款计算机辅助软件工程,这款软件用于设计和构建软件系统.业务流程建模及更多通用的建模.EA不同于普通的UML画图工具(如VISIO),它将支撑系统开发的全过 ...

  2. Java代码实现封装多级树结构对象

    前言: 在开发中,我们经常见到,前端展示树状结构的,这时候就需要后端去封装一个多级树结构对象,前端根据这样结构的数据去渲染数据,这篇文章讲的是如何封装成多级树结构对象. 正文: 1.先封装个树结构的对 ...

  3. react创建项目很慢,最后提示fetch failed的解决方法

    $ cnpm install -g create-react-app //创建react全局变量 $ create-react-app my-app //创建一个react项目 国内使用 npm 速度 ...

  4. 测试12.2.0.1RAC PDB级别的Failover

    关键步骤:手工添加服务名A并启动(已验证默认的服务名测试验证无法实现Failover) [oracle@db90 ~]$ srvctl add service -db orcl -service A ...

  5. parcel 在js中导入 html 文件

    parcel不支持将html文件导入为字符串,如果您对parcel使用熟练,直接使用 parcel-plugin-phtml 插件即可,此插件使用 .phtml 后缀 为什么用parcel? 因为从我 ...

  6. 在嵌入式开发中应该这样理解嵌入式C编程

    一.新手常常问的一个问题:C语言和嵌入式C编程有什么区别?而嵌入式工程师一般都会告诉你,其区别在于嵌入式的C语言是跑在嵌入式的开发板上的,CPU和电脑不一样,所以编译器也是不一样的,生成的可执行程序也 ...

  7. object tracking 词汇积累

    1. off-the-shelf adj. 现成的:常备的:成品的 adv. 现成地:无需作重大修改地 commercial off-the-shelf商用现货商规成品商业货架产品供应 off-the ...

  8. 一个handle使用更新线程的实例

    handle更新线程实例 package com.example.administrator.handle; import android.app.Activity;import android.os ...

  9. centos7安装nginx1.10.1

    安装nginx. 1.首先在根目录下创建一个software文件夹用来存储下载的压缩包. 2.然后cd跳转的software文件夹下,进行压缩包的下载 wget -c https://nginx.or ...

  10. python & mysql 操作(compare_sum_fee)

    [1]源码 工作中涉及的python工具源码实现 费用比较工具源码: #!/usr/bin/python3 #coding = utf-8 import time, sqlite3, datetime ...