Warning: input could be > 10000...

Solution by segment tree:

struct Node
{
Node(int s, int e) : start(s), end(e), cnt(), left(nullptr), right(nullptr)
{};
int start;
int end;
int cnt;
//
Node *left;
Node *right;
};
class Solution {
Node *pRoot; void update(int v)
{
Node *p = pRoot; while(p)
{
p->cnt ++;
if(p->start == p->end) break;
int mid = (p->start + p->end) / ;
if(v <= mid)
{
if(!p->left)
{
p->left = new Node(p->start, mid);
}
p = p->left;
}
else
{
if(!p->right)
{
p->right = new Node(mid + , p->end);
}
p = p->right;
}
}
}
int query(Node *p, int v)
{
if(!p) return ; int mid = (p->start + p->end) / ;
if(v < mid)
{
return query(p->left, v);
}
else if(v == mid)
{
return p->left ? p->left->cnt : ;
}
// v > mid
return (p->left ? p->left->cnt : ) + query(p->right, v);
}
public:
/**
* @param A: An integer array
* @return: Count the number of element before this element 'ai' is
* smaller than it and return count number array
*/
vector<int> countOfSmallerNumberII(vector<int> &A) { pRoot = new Node(, ); vector<int> ret;
for(auto v : A)
{
ret.push_back(query(pRoot, v - ));
update(v);
}
return ret;
}
};

LintCode "Count of Smaller Number before itself"的更多相关文章

  1. Lintcode: Count of Smaller Number

    Give you an integer array (index from 0 to n-1, where n is the size of this array, value from 0 to 1 ...

  2. LeetCode "Count of Smaller Number After Self"

    Almost identical to LintCode "Count of Smaller Number before Self". Corner case needs to b ...

  3. Lintcode249 Count of Smaller Number before itself solution 题解

    [题目描述] Give you an integer array (index from 0 to n-1, where n is the size of this array, data value ...

  4. Lintcode248 Count of Smaller Number solution 题解

    [题目描述] Give you an integer array (index from 0 to n-1, where n is the size of this array, value from ...

  5. Count of Smaller Number before itself

    Give you an integer array (index from 0 to n-1, where n is the size of this array, value from 0 to 1 ...

  6. [Swift]LeetCode315. 计算右侧小于当前元素的个数 | Count of Smaller Numbers After Self

    You are given an integer array nums and you have to return a new countsarray. The counts array has t ...

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

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

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

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

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

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

随机推荐

  1. 使用jQuery设置disabled属性与移除disabled属性

    Readonly只针对input和textarea有效,而disabled对于所有的表单元素都有效,下面为大家介绍下使用jQuery设置disabled属性   表单中readOnly和disable ...

  2. windows服务与桌面交互

    最近做服务与桌面交互的尝试,结果发现windows service 无法和桌面程序进行交互,后来在网上查资料,发现了下面的连接 http://www.cnblogs.com/gnielee/archi ...

  3. Java-->多线程复制(文件指针)

    --> 这里用到两种方法...其实也不算两种,就一点点不一样而已... ---> Test 测试类 package com.dragon.java.multithreadcopy; imp ...

  4. Java--继承和super关键字

    一.Java中方法的参数传递(重点) Java中参数传递都是值传递 Java中的值分两种: 1.如果传递的参数是基本数据类型: 传递的值就是基本数据类型的值. 传递的时候,其实是把基本数据类型的值,复 ...

  5. 关于 Ajax 提交参数格式,及返回类型json

    function Login() {                   $.ajax({                           //提交方式               type:&q ...

  6. TextView中的图文混排

    ImageSpan imageSpanMenu1 = new ImageSpan(activity,menuResId1); SpannableString contentMenu1 = new Sp ...

  7. Codeforces Round #190 (Div. 2) B. Ciel and Flowers

    链接:http://codeforces.com/contest/322/problem/B 这题做错了.没考虑周全. #include <cstdio> #include <cst ...

  8. iOS应用日志:开始编写日志组件与异常日志

    应用日志(一):开始编写日志组件 对于那些做后端开发的工程师来说,看 LOG解Bug应该是理所当然的事,但我接触到的移动应用开发的工程师里面,很多人并没有这个意识,查Bug时总是一遍一遍的试图重现,试 ...

  9. Lua快速入门

    -- 两个横线开始单行的注释 --[[ 加上两个[和]表示 多行的注释. --]] ---------------------------------------------------- -- 1. ...

  10. bootstrap 固定底部导航自适应

    在使用bootstrap 底部导航的时候遇到了一个问题 -- 当我的内容超过一屏的时候,底部的部分内容会被固定的导航内容遮盖 自己写了一个JS脚本,解决自适应的问题 <nav class=&qu ...