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 ...
随机推荐
- Mac OSX bash function 备份
# mount the android file image function mountAndroid { hdiutil attach ~/android.dmg.sparsefile.spars ...
- Mybatis+Mysql逆向工程
目录结构: pom文件: <?xml version="1.0" encoding="UTF-8"?> <project xmlns=&quo ...
- CentOS下软件安装与卸载常用命令总结
最近在折腾CentOS 7操作系统,主要是下载安装文件以及解决各项依赖问题,现对此过程中用到的有效的CentOS命令进行汇总总结. 1. 安装与卸载软件:yum.rpm.wget命令 首先,在Cent ...
- JAVA反射机制及理解
JAVA反射 往往当我们面对一项新的知识时,我们往往需要知道三个方面,它是什么,它能做什么,它比原有知识强在哪里,我们该怎么使用它.当你能够解决这些问题时,便意味着你已经对这项知识入门了. 首先: 反 ...
- keycode简记表
keycode值 实际含义 48到57 0到9 65到90 a到z(A到Z) 112到135 F1到F24 8 BackSpace(退格) 9 Tab 13 Enter(回车) 20 Caps_Loc ...
- linux awk 常见字符串处理
awk指定输出列: awk '{print $0} file' #打印所有列awk '{print $1}' file #打印第一列 awk '{print $1, $3}' file #打印第一和第 ...
- git merge后如何撤销
merge后发现冲突太多,或者合并的分支代码并不是最新,那就直接撤销再合并好了. git reset --hard HEAD 用来撤销还没commit 的merge,其实原理就是放弃index和工作区 ...
- git提交到一半关闭时
一:出现问题 最近写东西,在提交代码时,突然出现一大推文件... 忘记加.gitignore文件了,导致所有的安装依赖也都上传了.所以,点击了关闭按钮,当下一次提交时,出现了错误. Another g ...
- U面经Prepare: Print Binary Tree With No Two Nodes Share The Same Column
Give a binary tree, elegantly print it so that no two tree nodes share the same column. Requirement: ...
- Angular5入门与搭建项目
步骤 1. 设置开发环境 在开始工作之前,你必须设置好开发环境. 如果你的电脑里没有 Node.js®和 npm,请安装它们. 请先在终端/控制台窗口中运行命令 node -v 和 npm -v, 来 ...