Leetcode315

题意很简单,给定一个序列,求每一个数的右边有多少小于它的数。 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) 主席树的更多相关文章

  1. [LeetCode] 315. Count of Smaller Numbers After Self (Hard)

    315. Count of Smaller Numbers After Self class Solution { public: vector<int> countSmaller(vec ...

  2. leetcode 315. Count of Smaller Numbers After Self 两种思路(欢迎探讨更优解法)

    说来惭愧,已经四个月没有切 leetcode 上的题目了. 虽然工作中很少(几乎)没有用到什么高级算法,数据结构,但是我一直坚信 "任何语言都会过时,只有数据结构和算法才能永恒". ...

  3. leetcode 315. Count of Smaller Numbers After Self 两种思路

    说来惭愧,已经四个月没有切 leetcode 上的题目了. 虽然工作中很少(几乎)没有用到什么高级算法,数据结构,但是我一直坚信 "任何语言都会过时,只有数据结构和算法才能永恒". ...

  4. [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 ...

  5. LeetCode 315. Count of Smaller Numbers After Self

    原题链接在这里:https://leetcode.com/problems/count-of-smaller-numbers-after-self/ 题目: You are given an inte ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 315 Count of Smaller Numbers After Self 计算右侧小于当前元素的个数

    给定一个整型数组 nums,按要求返回一个新的 counts 数组.数组 counts 有该性质: counts[i] 的值是  nums[i] 右侧小于nums[i] 的元素的数量.例子:给定 nu ...

随机推荐

  1. laravel框架应用和composer扩展包开发

    laravel5.5+ laravel官方地址 laravel是目前最流行的php框架,发展势头迅猛,应用非常广泛,有丰富的扩展包可以应付你能想到的各种应用场景,laravel框架思想前卫,跟随时代潮 ...

  2. 在rubymine中集成heroku插件

    先安装heroku,参见http://www.cnblogs.com/jecyhw/p/4906990.html Heroku安装之后,就自动安装上git,目录为C:\Program Files (x ...

  3. form 表单onclick事件 禁止表单form提交

    最近遇到一次处理form数据的过滤,采用了button的onclick事件来检查,发现return false后表单仍然提交了. 于是仔细研究了下onclick.onsubmit.submit集合函数 ...

  4. LeetCode(47)Permutations II

    题目 Given a collection of numbers that might contain duplicates, return all possible unique permutati ...

  5. javamail实现注册激活邮件

    http://www.jb51.net/article/111926.htm https://www.cnblogs.com/ganchuanpu/archive/2016/11/29/6115691 ...

  6. idea 修改Git密码和账号方法

    IDEA修改git账号及密码的方法: 1.file->settings->passwords 2.重启IDEA 3.执行一次提交或更新 当执行提交或更新之后,idea会自动提示输入账号.密 ...

  7. 获取某一个<tr>中<td>的值

    $("#trId").children("td").eq(0).text(};    //当前行的第一个<td>的值    <td>下标 ...

  8. mysql proxy讀寫分流(二)-加入RW splitting

    上一篇中提到 安裝LUA及MySQL Proxy後,接下來就是RW splitting(讀寫分流)的部份了 整體的概念圖跟上一篇MySQL Proxy安裝方式相同,丫忠再補上一個對應port的圖表: ...

  9. linux & command line & console & logs

    linux & command line & console & logs how to get the logs form linux command console htt ...

  10. Django开发:(3.2)ORM:多表操作

    表关系总结: 一对多:在多的表中建立关联字段 多对多:创建第三张表(关联表):id 和 两个关联字段 一对一:在两张表中的任意一张表中建立关联字段(关联字段一定要加 unique 约束) 子查询:一次 ...