第十四周 Leetcode 315. Count of Smaller Numbers After Self(HARD) 主席树
题意很简单,给定一个序列,求每一个数的右边有多少小于它的数。 O(n^2)的算法是显而易见的。
用普通的线段树可以优化到O(nlogn)
我们可以直接套用主席树的模板。
主席树的功能是什么呢? 其实就是一句话。
原序列a的子序列a[l,r]在a排序后的序列b的子序列[L,R]中的个数。
显然本题只用到了主席树的一小部分功能。
const int N = 100000 + 5;
int a[N], b[N], rt[N * 20], ls[N * 20], rs[N * 20], sum[N * 20];
class Solution {
public: int n, k, tot, sz, ql, qr, x, q, T;
vector<int>out;
void Build(int& o, int l, int r){
if(l>r)return ;
o = ++ tot;
sum[o] = 0;
if(l == r) return;
int m = (l + r) >> 1;
Build(ls[o], l, m);
Build(rs[o], m + 1, r);
} int update(int& o, int l, int r, int last, int p){
o = ++ tot;
ls[o] = ls[last];
rs[o] = rs[last];
sum[o] = sum[last] + 1;
if(l == r) return l;
int m = (l + r) >> 1;
if(p <= b[m]) return update(ls[o], l, m, ls[last], p);
else return update(rs[o], m + 1, r, rs[last], p);
} int query(int ss, int tt, int l, int r, int k){
if(l == r) return l;
int m = (l + r) >> 1;
int cnt = sum[ls[tt]] - sum[ls[ss]];
if(k <= cnt) return query(ls[ss], ls[tt], l, m, k);
else return query(rs[ss], rs[tt], m + 1, r, k - cnt);
} int sumq(int cur,int l1,int r1,int l,int r)
{
if(l1<0||r1<0)return 0;
if(l1<=l&&r1>=r)return sum[cur];
int mid=(l+r)/2;
int ans=0;
if(l1<=mid)ans+= sumq(ls[cur],l1,r1,l,mid);
if(r1>mid)ans+= sumq(rs[cur],l1,r1,mid+1,r);
return ans;
} vector<int> countSmaller(vector<int>& nums){//freopen("t.txt","r",stdin);
T=1;
while(T--){
for(int i = 0; i < nums.size(); i ++)b[i] = nums[i];
sort(b , b + (int)nums.size());
out.resize((int)nums.size());
sz = unique(b , b +(int)nums.size()) - (b );
sz--;
tot=0;
Build(rt[nums.size()],0, sz); for(int i = nums.size()-1; i >= 0; i --)
{
int loc=update(rt[i], 0, sz, rt[i + 1], nums[i]);
out[i]=sumq(rt[i],0,loc-1,0,sz);
} }
return out;
}
};
如果对主席树有兴趣 可以看看POJ2104
第十四周 Leetcode 315. Count of Smaller Numbers After Self(HARD) 主席树的更多相关文章
- [LeetCode] 315. Count of Smaller Numbers After Self (Hard)
315. Count of Smaller Numbers After Self class Solution { public: vector<int> countSmaller(vec ...
- leetcode 315. Count of Smaller Numbers After Self 两种思路(欢迎探讨更优解法)
说来惭愧,已经四个月没有切 leetcode 上的题目了. 虽然工作中很少(几乎)没有用到什么高级算法,数据结构,但是我一直坚信 "任何语言都会过时,只有数据结构和算法才能永恒". ...
- leetcode 315. Count of Smaller Numbers After Self 两种思路
说来惭愧,已经四个月没有切 leetcode 上的题目了. 虽然工作中很少(几乎)没有用到什么高级算法,数据结构,但是我一直坚信 "任何语言都会过时,只有数据结构和算法才能永恒". ...
- [LeetCode] 315. Count of Smaller Numbers After Self 计算后面较小数字的个数
You are given an integer array nums and you have to return a new counts array. The countsarray has t ...
- LeetCode 315. Count of Smaller Numbers After Self
原题链接在这里:https://leetcode.com/problems/count-of-smaller-numbers-after-self/ 题目: You are given an inte ...
- 315.Count of Smaller Numbers After Self My Submissions Question
You are given an integer array nums and you have to return a new counts array. Thecounts array has t ...
- 315. Count of Smaller Numbers After Self
You are given an integer array nums and you have to return a new counts array. The counts array has ...
- 315. Count of Smaller Numbers After Self(Fenwick Tree)
You are given an integer array nums and you have to return a new counts array. The counts array has ...
- 315 Count of Smaller Numbers After Self 计算右侧小于当前元素的个数
给定一个整型数组 nums,按要求返回一个新的 counts 数组.数组 counts 有该性质: counts[i] 的值是 nums[i] 右侧小于nums[i] 的元素的数量.例子:给定 nu ...
随机推荐
- *****Python之进程线程*****
Python之进程线程 Python的threading模块 并发编程: 操作系统:位于底层硬件与应用软件之间的一层. 工作方式:向下管理硬件,向上提供接口. 进程:资源管理单位(容器) 线程:最 ...
- 三菱PLC FB库函数调用方法 (Gx Work2版本)
本文以 GxWorks2 软件为例 1.新建使用标签项目的工程文件 2.从其它库所在工程项目中导入库 3.选择库文件及FB功能块 4.插入FB功能块调用
- Shader Wave
Shader Wave 一.原理 1. 采用 UV 坐标为原始数据,生成每一条波浪线. 2. 使用 Unity 的 Time.y 作为时间增量,动态变换波形. 二.操作步骤 1. 首先使用纹理坐标生成 ...
- 经典算法入门 列表C/C++
排序:插入排序.选择排序.冒泡排序.归并排序.快速排序.基数排序.计数排序.桶排序 查找:二分查找 树:先根.中根.后跟遍历 图:深度优先.广度优先.最小生成树.单元最短路径.全成对最短路径 动态规划 ...
- BNUOJ 5235 Starship Troopers
Starship Troopers Time Limit: 5000ms Memory Limit: 32768KB This problem will be judged on HDU. Origi ...
- spark streaming 踩过的那些坑
系统背景 spark streaming + Kafka高级API receiver 目前资源分配(现在系统比较稳定的资源分配),独立集群 --driver-memory 50G --exec ...
- css3 & background & background-image
css3 & background & background-image https://developer.mozilla.org/en-US/docs/Web/CSS/backgr ...
- noip模拟赛 区间
分析:要遍历所有的区间,肯定是枚举左端点,然后再枚举右端点.关键是怎么高效地求区间&,|,一般而言是用前缀和的,但是&,|不满足区间可减性,所以可以考虑线段树?这道题不带修改操作,用线 ...
- [NOIP2004] 提高组 洛谷P1090 合并果子
题目描述 在一个果园里,多多已经将所有的果子打了下来,而且按果子的不同种类分成了不同的堆.多多决定把所有的果子合成一堆. 每一次合并,多多可以把两堆果子合并到一起,消耗的体力等于两堆果子的重量之和.可 ...
- Ubuntu 16.04安装SwitchHosts
下载: https://github.com/oldj/SwitchHosts/releases 解压: unzip SwitchHosts-linux-x64_v3.3.6.5287.zip 移动: ...