第十四周 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 ...
随机推荐
- 树莓派 - RPi.GPIO
RPi.GPIO是通过Python/C API实现的,C代码操作底层寄存器, python通过Python/C API调用这些C接口. 这是关于RPi.GPIO项目的介绍. 其中提到了有python ...
- Python之面向对象类和对象
Python之面向对象类和对象 定义一个类:class 定义类的语法: class Test(object): """ 类里定义一类事物共同的技能. 可以是变量,也可是函 ...
- [Android] java代码无错误,但跳转失败
今天在调代码的时候,出现了这样的问题,我晕了半天,才找到解决办法. 查看日志发现:Initialize Binary Program Cache: Load Failed 从来没见过这种问题,Java ...
- python书籍推荐:Python数据科学手册
所属网站分类: 资源下载 > python电子书 作者:today 链接:http://www.pythonheidong.com/blog/article/448/ 来源:python黑洞网 ...
- 技能CD 效果 shader
技能CD特效 这个效果主要是利用反正切函数完成.atan2(x,y)的返回值是[-PI,PI],这个支持4个象限的反正切函数.关于圆角计算,在上篇文章中有介绍. 现在,我们来看看反正切函数的效果: 在 ...
- keil mdk uvision使用技巧
语法检测&代码提示 中文友好: tab 可以选中一大块代码,一起缩进 快速注释 先选中你要注释的代码区,然后右键,选择Advanced,Comment Selection 就可以了 查找替换 ...
- Chrome & CORS & Fetch API & Chrome 多开,应用分身
Chrome & CORS & Fetch API Chrome 浏览器的跨域设置 https://www.cnblogs.com/cshi/p/5660039.html https: ...
- Android TextView内容过长加省略号
在Android TextView中有个内容过长加省略号的属性,即ellipsize,用法如下: 在xml中: android:ellipsize = "end" //省略号在结尾 ...
- CF676E:The Last Fight Between Human and AI
人类和电脑在一个多项式上进行博弈,多项式的最高次项已知,一开始系数都不确定.电脑先开始操作,每次操作可以确定某次项的系数,这个系数可以是任意实数.给出一个博弈中间状态,最后如果这个多项式被x-K整除就 ...
- [bzoj2091][Poi2010]The Minima Game_动态规划
The Minima Game bzoj-2091 Poi-2010 题目大意:给出N个正整数,AB两个人轮流取数,A先取.每次可以取任意多个数,直到N个数都被取走.每次获得的得分为取的数中的最小值, ...