第一部分---线段树:https://leetcode.com/tag/segment-tree/  

【218】The Skyline Problem 

【307】Range Sum Query - Mutable 

【308】Range Sum Query 2D - Mutable 

【315】Count of Smaller Numbers After Self 

【493】Reverse Pairs 

【699】Falling Squares (我的线段树第一题,2019年1月24日)

在 X 轴上落方块,问最后整个区间内的最高的高度是多少。 

Input: [[1, 2], [2, 3], [6, 1]]
Output: [2, 5, 5]
Explanation:

After the first drop of positions[0] = [1, 2]:
_aa
_aa
-------
The maximum height of any square is 2.


After the second drop of positions[1] = [2, 3]:
__aaa
__aaa
__aaa
_aa__
_aa__
--------------
The maximum height of any square is 5.  
The larger square stays on top of the smaller square despite where its center
of gravity is, because squares are infinitely sticky on their bottom edge.


After the third drop of positions[1] = [6, 1]:
__aaa
__aaa
__aaa
_aa
_aa___a
--------------
The maximum height of any square is still 5.

Thus, we return an answer of [2, 5, 5].

题解:用了线段树的单点更新,只能beats 1.9% == 如果用区间更新的话, 应该快很多。但是这是第一题线段树,纪念一下。(我的线段树写的都是 base 0)

 1 class Solution {
 2 public:
 3     const static int MAX_SIZE = 1 << 15;
 4     struct SegmentTree {
 5         void init(int _n) {
 6             n = 1;
 7             while (n < _n) {
 8                 n *= 2;
 9             }
10             for (int i = 0; i < n * 2 -1; ++i) { dat[i] = 0; }
11         }  
12         #define lson(k) k*2+1
13         #define rson(k) k*2+2
14         #define father(k) (k-1)/2
15         inline void pushup(int k) { dat[k] = max(dat[lson(k)], dat[rson(k)]); }
16         void update(int k, int value) {
17             k += n - 1;
18             dat[k] = value;
19             while (k > 0) {
20                 k = (k-1)/2;
21                 pushup(k);
22             }
23         }
24         int query(int a, int b, int k, int l, int r) {
25             if (r <= a || b <= l) { return 0; }
26             if (a <= l && r <= b) {
27                 return dat[k];
28             } else {
29                 int vl = query(a, b, lson(k), l, (l+r)/2);
30                 int vr = query(a, b, rson(k), (l+r)/2, r);
31                 return max(vl, vr);
32             }
33         }
34         void print() {
35             for (int i = 0; i < 2 * n - 1; ++i) {
36                 printf("%d ", dat[i]);
37             }
38             printf("\n");
39         }
40         int n, dat[MAX_SIZE];
41     };
42     vector<int> fallingSquares(vector<pair<int, int>>& positions) {
43         int size = positions.size();
44         set<int> st;
45         for (auto pos : positions) {
46             st.insert(pos.first), 
47             st.insert(pos.first + pos.second - 1);
48         }
49         vector<int> nums(st.begin(), st.end());
50         SegmentTree seg;
51         seg.init((int)st.size());
52         vector<int> ans;
53         for (auto pos : positions) {
54             int l = pos.first, r = pos.first + pos.second - 1, h = pos.second;
55             int idxL = distance(st.begin(), st.find(l)), idxR = distance(st.begin(), st.find(r));
56             int base = seg.query(idxL, idxR+1, 0, 0, seg.n);
57             for (int i = idxL; i <= idxR; ++i) {
58                 seg.update(i, base + h);
59             }
60             int maxx = seg.query(0, (int)st.size(), 0, 0, seg.n);
61             ans.push_back(maxx);
62         }
63         return ans;
64     }
65 };
View Code

相关文章:

  • 2021-10-07
  • 2021-11-11
  • 2022-02-01
  • 2022-12-23
  • 2022-12-23
  • 2021-11-23
  • 2021-10-05
猜你喜欢
  • 2022-12-23
  • 2021-09-15
  • 2021-10-14
  • 2021-04-04
  • 2021-09-04
  • 2021-10-30
相关资源
相似解决方案