【題目描述】
Give you an integer array (index from 0 to n-1, where n is the size of this array, data value from 0 to 10000) . For each element Ai in the array, count the number of element before this element Ai is smaller than it and return count number array.
Notice:We suggest you finish problem?Segment Tree Build,Segment Tree Query II?and?Count of Smaller Number?first.
給定一個(gè)整數(shù)數(shù)組(下標(biāo)由 0 到 n-1, n 表示數(shù)組的規(guī)模,取值范圍由 0 到10000)。對(duì)于數(shù)組中的每個(gè)ai元素,請(qǐng)計(jì)算ai前的數(shù)中比它小的元素的數(shù)量。
【注】:做此題前最好先完成?Segment Tree Build,Segment Tree Query II和Count of Smaller Number
【題目鏈接】
www.lintcode.com/en/problem/count-of-smaller-number-before-itself/
【題目解析】
此題可用線段樹(shù)來(lái)做。
首先以0-10000為區(qū)間建樹(shù),并將所有區(qū)間count設(shè)為0。每一個(gè)最小區(qū)間(即葉節(jié)點(diǎn))的count代表到目前為止遇到的該數(shù)的數(shù)量。
然后開(kāi)始遍歷數(shù)組,遇到A[i]時(shí),去查0-A[i]-1區(qū)間的count,即為比A[i]小的數(shù)的數(shù)量
查完后將A[i]區(qū)間的count加1即可
【參考答案】
www.jiuzhang.com/solutions/count-of-smaller-number-before-itself/