5555
5656<!-- 这里可写通用的实现逻辑 -->
5757
58- 题目数据规模不大,直接模拟即可。
58+ ** 方法一:模拟**
59+
60+ 题目数据规模不大,可以直接模拟。
61+
62+ ** 方法一:树状数组**
63+
64+ 树状数组,也称作“二叉索引树”(Binary Indexed Tree)或 Fenwick 树。 它可以高效地实现如下两个操作:
65+
66+ 1 . ** 单点更新** ` update(x, delta) ` : 把序列 x 位置的数加上一个值 delta;
67+ 1 . ** 前缀和查询** ` query(x) ` :查询序列 ` [1,...x] ` 区间的区间和,即位置 x 的前缀和。
68+
69+ 这两个操作的时间复杂度均为 ` O(log n) ` 。
70+
71+ 树状数组最基本的功能就是求比某点 x 小的点的个数(这里的比较是抽象的概念,可以是数的大小、坐标的大小、质量的大小等等)。
72+
73+ 比如给定数组 ` a[5] = {2, 5, 3, 4, 1} ` ,求 ` b[i] = 位置 i 左边小于等于 a[i] 的数的个数 ` 。对于此例,` b[5] = {0, 1, 1, 2, 0} ` 。
74+
75+ 解决方案是直接遍历数组,每个位置先求出 ` query(a[i]) ` ,然后再修改树状数组 ` update(a[i], 1) ` 即可。当数的范围比较大时,需要进行离散化,即先进行去重并排序,然后对每个数字进行编号。
5976
6077<!-- tabs:start -->
6178
6683``` python
6784class Solution :
6885 def processQueries (self , queries : List[int ], m : int ) -> List[int ]:
69- nums = list (range (1 , m + 1 ))
70- res = []
71- for num in queries:
72- res.append(nums.index(num))
73- nums.remove(num)
74- nums.insert(0 , num)
75- return res
86+ p = list (range (1 , m + 1 ))
87+ ans = []
88+ for v in queries:
89+ j = p.index(v)
90+ ans.append(j)
91+ p.pop(j)
92+ p.insert(0 , v)
93+ return ans
94+ ```
95+
96+ ``` python
97+ class BinaryIndexedTree :
98+ def __init__ (self , n ):
99+ self .n = n
100+ self .c = [0 ] * (n + 1 )
101+
102+ @ staticmethod
103+ def lowbit (x ):
104+ return x & - x
105+
106+ def update (self , x , delta ):
107+ while x <= self .n:
108+ self .c[x] += delta
109+ x += BinaryIndexedTree.lowbit(x)
110+
111+ def query (self , x ):
112+ s = 0
113+ while x > 0 :
114+ s += self .c[x]
115+ x -= BinaryIndexedTree.lowbit(x)
116+ return s
117+
118+ class Solution :
119+ def processQueries (self , queries : List[int ], m : int ) -> List[int ]:
120+ n = len (queries)
121+ pos = [0 ] * (m + 1 )
122+ tree = BinaryIndexedTree(m + n)
123+ for i in range (1 , m + 1 ):
124+ pos[i] = n + i
125+ tree.update(n + i, 1 )
126+
127+ ans = []
128+ for i, v in enumerate (queries):
129+ j = pos[v]
130+ tree.update(j, - 1 )
131+ ans.append(tree.query(j))
132+ pos[v] = n - i
133+ tree.update(n - i, 1 )
134+ return ans
76135```
77136
78137### ** Java**
@@ -82,18 +141,74 @@ class Solution:
82141``` java
83142class Solution {
84143 public int [] processQueries (int [] queries , int m ) {
85- List<Integer > nums = new LinkedList<> ();
86- for (int i = 0 ; i < m; ++ i) {
87- nums . add(i + 1 );
144+ List<Integer > p = new LinkedList<> ();
145+ for (int i = 1 ; i <= m; ++ i) {
146+ p . add(i);
88147 }
89- int [] res = new int [queries. length];
148+ int [] ans = new int [queries. length];
90149 int i = 0 ;
91- for (int num : queries) {
92- res[i++ ] = nums. indexOf(num);
93- nums. remove(Integer . valueOf(num));
94- nums. add(0 , num);
150+ for (int v : queries) {
151+ int j = p. indexOf(v);
152+ ans[i++ ] = j;
153+ p. remove(j);
154+ p. add(0 , v);
155+ }
156+ return ans;
157+ }
158+ }
159+ ```
160+
161+ ``` java
162+ class BinaryIndexedTree {
163+ private int n;
164+ private int [] c;
165+
166+ public BinaryIndexedTree (int n ) {
167+ this . n = n;
168+ c = new int [n + 1 ];
169+ }
170+
171+ public void update (int x , int delta ) {
172+ while (x <= n) {
173+ c[x] += delta;
174+ x += lowbit(x);
175+ }
176+ }
177+
178+ public int query (int x ) {
179+ int s = 0 ;
180+ while (x > 0 ) {
181+ s += c[x];
182+ x -= lowbit(x);
183+ }
184+ return s;
185+ }
186+
187+ public static int lowbit (int x ) {
188+ return x & - x;
189+ }
190+ }
191+
192+ class Solution {
193+ public int [] processQueries (int [] queries , int m ) {
194+ int n = queries. length;
195+ BinaryIndexedTree tree = new BinaryIndexedTree (m + n);
196+ int [] pos = new int [m + 1 ];
197+ for (int i = 1 ; i <= m; ++ i) {
198+ pos[i] = n + i;
199+ tree. update(n + i, 1 );
200+ }
201+ int [] ans = new int [n];
202+ int k = 0 ;
203+ for (int i = 0 ; i < n; ++ i) {
204+ int v = queries[i];
205+ int j = pos[v];
206+ tree. update(j, - 1 );
207+ ans[k++ ] = tree. query(j);
208+ pos[v] = n - i;
209+ tree. update(n - i, 1 );
95210 }
96- return res ;
211+ return ans ;
97212 }
98213}
99214```
@@ -104,24 +219,82 @@ class Solution {
104219class Solution {
105220public:
106221 vector<int > processQueries(vector<int >& queries, int m) {
107- vector<int > nums (m);
108- iota(nums .begin(), nums .end(), 1);
109- vector<int > res ;
110- for (int num : queries)
222+ vector<int > p (m);
223+ iota(p .begin(), p .end(), 1);
224+ vector<int > ans ;
225+ for (int v : queries)
111226 {
112- int idx = -1 ;
227+ int j = 0 ;
113228 for (int i = 0; i < m; ++i)
114229 {
115- if (nums[ i] == num) {
116- idx = i;
230+ if (p[ i] == v)
231+ {
232+ j = i;
117233 break;
118234 }
119235 }
120- res.push_back(idx);
121- nums.erase(nums.begin() + idx);
122- nums.insert(nums.begin(), num);
236+ ans.push_back(j);
237+ p.erase(p.begin() + j);
238+ p.insert(p.begin(), v);
239+ }
240+ return ans;
241+ }
242+ };
243+ ```
244+
245+ ```cpp
246+ class BinaryIndexedTree {
247+ public:
248+ int n;
249+ vector<int> c;
250+
251+ BinaryIndexedTree(int _n): n(_n), c(_n + 1){}
252+
253+ void update(int x, int delta) {
254+ while (x <= n)
255+ {
256+ c[x] += delta;
257+ x += lowbit(x);
258+ }
259+ }
260+
261+ int query(int x) {
262+ int s = 0;
263+ while (x > 0)
264+ {
265+ s += c[x];
266+ x -= lowbit(x);
267+ }
268+ return s;
269+ }
270+
271+ int lowbit(int x) {
272+ return x & -x;
273+ }
274+ };
275+
276+ class Solution {
277+ public:
278+ vector<int> processQueries(vector<int>& queries, int m) {
279+ int n = queries.size();
280+ vector<int> pos(m + 1);
281+ BinaryIndexedTree* tree = new BinaryIndexedTree(m + n);
282+ for (int i = 1; i <= m; ++i)
283+ {
284+ pos[i] = n + i;
285+ tree->update(n + i, 1);
286+ }
287+ vector<int> ans;
288+ for (int i = 0; i < n; ++i)
289+ {
290+ int v = queries[i];
291+ int j = pos[v];
292+ tree->update(j, -1);
293+ ans.push_back(tree->query(j));
294+ pos[v] = n - i;
295+ tree->update(n - i, 1);
123296 }
124- return res ;
297+ return ans ;
125298 }
126299};
127300```
@@ -130,24 +303,75 @@ public:
130303
131304``` go
132305func processQueries (queries []int , m int ) []int {
133- nums := make([]int, m)
134- for i := 0; i < m; i++ {
135- nums [i] = i + 1
306+ p := make ([]int , m)
307+ for i := range p {
308+ p [i] = i + 1
136309 }
137- var res []int
138- for _, num := range queries {
139- idx := -1
140- for i := 0; i < m; i++ {
141- if nums [i] == num {
142- idx = i
310+ ans := []int {}
311+ for _ , v := range queries {
312+ j := 0
313+ for i := range p {
314+ if p [i] == v {
315+ j = i
143316 break
144317 }
145318 }
146- res = append(res, idx)
147- nums = append(nums[:idx], nums[idx+1:]...)
148- nums = append([]int{num}, nums...)
319+ ans = append (ans, j)
320+ p = append (p[:j], p[j+1 :]...)
321+ p = append ([]int {v}, p...)
322+ }
323+ return ans
324+ }
325+ ```
326+
327+ ``` go
328+ type BinaryIndexedTree struct {
329+ n int
330+ c []int
331+ }
332+
333+ func newBinaryIndexedTree (n int ) *BinaryIndexedTree {
334+ c := make ([]int , n+1 )
335+ return &BinaryIndexedTree{n, c}
336+ }
337+
338+ func (this *BinaryIndexedTree ) lowbit (x int ) int {
339+ return x & -x
340+ }
341+
342+ func (this *BinaryIndexedTree ) update (x , delta int ) {
343+ for x <= this.n {
344+ this.c [x] += delta
345+ x += this.lowbit (x)
346+ }
347+ }
348+
349+ func (this *BinaryIndexedTree ) query (x int ) int {
350+ s := 0
351+ for x > 0 {
352+ s += this.c [x]
353+ x -= this.lowbit (x)
354+ }
355+ return s
356+ }
357+
358+ func processQueries (queries []int , m int ) []int {
359+ n := len (queries)
360+ pos := make ([]int , m+1 )
361+ tree := newBinaryIndexedTree (m + n)
362+ for i := 1 ; i <= m; i++ {
363+ pos[i] = n + i
364+ tree.update (n+i, 1 )
365+ }
366+ ans := []int {}
367+ for i , v := range queries {
368+ j := pos[v]
369+ tree.update (j, -1 )
370+ ans = append (ans, tree.query (j))
371+ pos[v] = n - i
372+ tree.update (n-i, 1 )
149373 }
150- return res
374+ return ans
151375}
152376```
153377
0 commit comments