Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
287f550
Update 210.Course-Schedule-II.cpp
Suri-Feng Jun 6, 2022
c3f929c
Merge pull request #41 from Suri-Feng/patch-1
wisdompeak Jun 6, 2022
3de10b8
Create range_max.cpp
wisdompeak Jun 6, 2022
c922f7b
Update range_max.cpp
wisdompeak Jun 6, 2022
0c2f74d
Update 699.Falling-Squares_SegmentTree_LazyTag.cpp
wisdompeak Jun 6, 2022
a57433c
Rename 699.Falling-Squares_SegmentTree_LazyTag.cpp to 699.Falling-Squ…
wisdompeak Jun 6, 2022
91fd197
Update and rename 699.Falling-Squares_segTree.cpp to 699.Falling-Squa…
wisdompeak Jun 6, 2022
3cbb303
Update 699.Falling-Squares_SegTree_v1.cpp
wisdompeak Jun 6, 2022
f25b0b1
Update 699.Falling-Squares_SegTree_v1.cpp
wisdompeak Jun 6, 2022
e277cf1
Update 699.Falling-Squares_SegTree_v2.cpp
wisdompeak Jun 6, 2022
c5eade9
Rename 699.Falling-Squares.cpp to 699.Falling-Squares_Heap_v1.cpp
wisdompeak Jun 6, 2022
b14ca1a
Rename 699.Falling-Squares-v2.cpp to 699.Falling-Squares_Heap_v2.cpp
wisdompeak Jun 6, 2022
384f39f
Update and rename 218.The-Skyline-Problem_segTree.cpp to 218.The-Skyl…
wisdompeak Jun 6, 2022
2d2b215
Update and rename 218.The-Skyline-Problem_SegmentTree_lazyTag.cpp to …
wisdompeak Jun 6, 2022
7b78f70
Update range_max.cpp
wisdompeak Jun 6, 2022
e349c5e
Update 699.Falling-Squares_SegTree_v2.cpp
wisdompeak Jun 6, 2022
d8bf11b
Create range_sum.cpp
wisdompeak Jun 6, 2022
ec66da3
Delete SegmentTree_Basic.cpp
wisdompeak Jun 6, 2022
e806c19
Delete SegmentTree_LazyTag.cpp
wisdompeak Jun 6, 2022
f0c7a41
Update 370.Range-Addition_SegmentTree_lazyTag.cpp
wisdompeak Jun 6, 2022
4a3ea52
Rename 370.Range-Addition_SegmentTree_lazyTag.cpp to 370.Range-Additi…
wisdompeak Jun 6, 2022
45a4754
Update Readme.md
wisdompeak Jun 6, 2022
aa41220
Rename 370.Range-Addition.cpp to 370.Range-Addition_DiffArray.cpp
wisdompeak Jun 6, 2022
9967544
Delete 370.Range-Addition_segTree.cpp
wisdompeak Jun 6, 2022
3fb99e6
Rename 370.Range-Addition_SegTree_v2.cpp to 370.Range-Addition_SegTre…
wisdompeak Jun 6, 2022
c2fb413
Update 370.Range-Addition_SegTree.cpp
wisdompeak Jun 6, 2022
3caca8a
Create range_sum_increase_by.cpp
wisdompeak Jun 6, 2022
0a04cc0
Update Readme.md
wisdompeak Jun 6, 2022
3fe5b54
Create range_module.cpp
wisdompeak Jun 6, 2022
225b845
Delete SegmentTree_LazyTag_Discretization.cpp
wisdompeak Jun 6, 2022
67d0c05
Create 2286.Booking-Concert-Tickets-in-Groups.cpp
wisdompeak Jun 6, 2022
c0c253e
Rename Segment_Tree/2286.Booking-Concert-Tickets-in-Groups.cpp to Seg…
wisdompeak Jun 6, 2022
8a3543b
Update Readme.md
wisdompeak Jun 6, 2022
2bd117e
Update 2286.Booking-Concert-Tickets-in-Groups.cpp
wisdompeak Jun 6, 2022
d931aab
Create Readme.md
wisdompeak Jun 6, 2022
4ca4143
Update range_sum.cpp
wisdompeak Jun 6, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions BFS/210.Course-Schedule-II/210.Course-Schedule-II.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ class Solution {
vector<int> findOrder(int numCourses, vector<vector<int>>& prerequisites)
{
int n = numCourses;
unordered_map<int,vector<int>>nextCourses(n);
unordered_map<int,int>degree(n);
vector<vector<int>>nextCourses(n);
vector<int>degree(n, 0);

for (auto edge: prerequisites)
{
Expand Down
5 changes: 2 additions & 3 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,17 +271,15 @@
[1902](https://github.com/wisdompeak/LeetCode/tree/master/Tree/1902.Depth-of-BST-Given-Insertion-Order),

#### [Segment Tree](https://github.com/wisdompeak/LeetCode/blob/master/Segment_Tree/)
* ``Basics``
[307.Range-Sum-Query-Mutable](https://github.com/wisdompeak/LeetCode/blob/master/Segment_Tree/307.Range-Sum-Query-Mutable/) (H-)
[1526.Minimum-Number-of-Increments-on-Subarrays-to-Form-a-Target-Array](https://github.com/wisdompeak/LeetCode/tree/master/Greedy/1526.Minimum-Number-of-Increments-on-Subarrays-to-Form-a-Target-Array) (H-)
[1649.Create-Sorted-Array-through-Instructions](https://github.com/wisdompeak/LeetCode/tree/master/Divide_Conquer/1649.Create-Sorted-Array-through-Instructions) (H-)
[1157.Online-Majority-Element-In-Subarray](https://github.com/wisdompeak/LeetCode/tree/master/Binary_Search/1157.Online-Majority-Element-In-Subarray) (H)
* ``Lazy Tag``
[370.Range-Addition](https://github.com/wisdompeak/LeetCode/tree/master/Segment_Tree/370.Range-Addition) (H)
[218.The-Skyline-Problem](https://github.com/wisdompeak/LeetCode/blob/master/Segment_Tree/218.The-Skyline-Problem) (H+)
[699.Falling-Squares](https://github.com/wisdompeak/LeetCode/tree/master/Segment_Tree/699.Falling-Squares) (H)
* ``Others``
[715.Range-Module](https://github.com/wisdompeak/LeetCode/tree/master/Segment_Tree/715.Range-Module) (H)
[2286.Booking-Concert-Tickets-in-Groups](https://github.com/wisdompeak/LeetCode/tree/master/Segment_Tree/2286.Booking-Concert-Tickets-in-Groups) (H-)

#### [Binary Index Tree]
[307.Range-Sum-Query-Mutable](https://github.com/wisdompeak/LeetCode/blob/master/Segment_Tree/307.Range-Sum-Query-Mutable/) (M)
Expand Down Expand Up @@ -1229,6 +1227,7 @@
* ``扫描线 / 差分数组``
[252.Meeting-Rooms](https://github.com/wisdompeak/LeetCode/tree/master/Others/252.Meeting-Rooms) (M)
[253.Meeting-Rooms-II](https://github.com/wisdompeak/LeetCode/tree/master/Others/253.Meeting-Rooms-II) (M+)
[370.Range-Addition](https://github.com/wisdompeak/LeetCode/tree/master/Segment_Tree/370.Range-Addition) (H-)
[056.Merge-Intervals](https://github.com/wisdompeak/LeetCode/tree/master/Others/056.Merge-Intervals) (M)
[057.Insert-Intervals](https://github.com/wisdompeak/LeetCode/tree/master/Others/057.Insert-Interval) (M)
[732.My-Calendar-III](https://github.com/wisdompeak/LeetCode/tree/master/Others/732.My-Calendar-III) (M)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// 支持动态开点

class SegTree
{
public:
int start,end,status;
SegTree* left;
SegTree* right;
SegTree(int a, int b, int s):start(a),end(b),status(s),left(NULL),right(NULL){}

void remove(SegTree* &node)
{
if (node==NULL) return;
remove(node->left);
remove(node->right);
delete node;
node = NULL;
return;
}

void setStatus(int a, int b, int s)
{
if (a>=end || b<=start)
return;
if (a<=start && b>=end && s>=status)
{
remove(left);
remove(right);
status = s;
return;
}
if (a<=start && b>=end && s<status && left==NULL)
return;
if (left==NULL)
{
int mid = start+(end-start)/2;
left = new SegTree(start,mid,status);
right = new SegTree(mid,end,status);
}
left->setStatus(a,b,s);
right->setStatus(a,b,s);
status = max(left->status,right->status);
}
};

class Solution {
public:
vector<vector<int>>results;
vector<vector<int>> getSkyline(vector<vector<int>>& buildings)
{
if (buildings.size()==0) return {};

SegTree* root = new SegTree(0,INT_MAX,0);
for (auto q:buildings)
root->setStatus(q[0],q[1],q[2]);

DFS(root);
if (results.back()[1]!=0) results.push_back({INT_MAX,0});

vector<vector<int>>filteredResults;
for (auto p: results)
{
if (filteredResults.size()!=0 && p[1]==filteredResults.back()[1])
continue;
filteredResults.push_back({p[0],p[1]});
}
if (filteredResults.size()!=0 && filteredResults[0][1]==0) filteredResults.erase(filteredResults.begin());
return filteredResults;
}

void DFS(SegTree* node)
{
if (node->left==NULL)
results.push_back({node->start,node->status});
else
{
DFS(node->left);
DFS(node->right);
}
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
// 线段树大小在初始化时固定。支持Lazy Tag(延迟标记)

class SegTreeNode
{
public:
SegTreeNode* left = NULL;
SegTreeNode* right = NULL;
int start, end;
int info; // the maximum value of the range
bool tag;

SegTreeNode(int a, int b, int val) // init for range [a,b] with val
{
tag = 0;
start = a, end = b;
if (a==b)
{
info = val;
return;
}
int mid = (a+b)/2;
if (left==NULL)
{
left = new SegTreeNode(a, mid, val);
right = new SegTreeNode(mid+1, b, val);
info = max(left->info, right->info); // check with your own logic
}
}

void pushDown()
{
if (tag==1 && left)
{
left->info = info;
right->info = info;
left->tag = 1;
right->tag = 1;
tag = 0;
}
}

void updateRange(int a, int b, int val) // set range [a,b] with val
{
if (b < start || a > end ) // not covered by [a,b] at all
return;
if (a <= start && end <=b) // completely covered within [a,b]
{
info = val;
tag = 1;
return;
}

if (left)
{
pushDown();
left->updateRange(a, b, val);
right->updateRange(a, b, val);
info = max(left->info, right->info); // write your own logic
}
}

int queryRange(int a, int b) // query the maximum value within range [a,b]
{
if (b < start || a > end )
{
return INT_MIN; // check with your own logic
}
if (a <= start && end <=b)
{
return info; // check with your own logic
}

if (left)
{
pushDown();
int ret = max(left->queryRange(a, b), right->queryRange(a, b));
info = max(left->info, right->info); // check with your own logic
return ret;
}

return info; // should not reach here
}

};

class Solution {
vector<pair<int,int>>height; // {idx, h}
public:
vector<vector<int>> getSkyline(vector<vector<int>>& buildings)
{
set<int>Set;
for (auto & building: buildings)
{
Set.insert(building[0]);
Set.insert(building[1]);
}
int id = 0;
unordered_map<int,int>pos2idx;
unordered_map<int,int>idx2pos;
for (auto x:Set)
{
pos2idx[x] = id;
idx2pos[id] = x;
id++;
}

int n = pos2idx.size();
SegTreeNode* root = new SegTreeNode(0, n-1, 0);

sort(buildings.begin(), buildings.end(), [](vector<int>&a, vector<int>&b){return a[2]<b[2];});

for (auto & building: buildings)
{
root->updateRange(pos2idx[building[0]], pos2idx[building[1]]-1, building[2]);
}

DFS(root);

vector<vector<int>>rets;
for (int i=0; i<height.size(); i++)
{
auto [idx, h] = height[i];
rets.push_back({idx2pos[idx], h});
while (i+1 < height.size() && height[i+1].second == height[i].second)
i++;
}
return rets;
}

void DFS(SegTreeNode* node)
{
if (node->start==node->end || node->tag==1)
{
height.push_back({node->start, node->info});
return;
}
DFS(node->left);
DFS(node->right);
}

};

This file was deleted.

Loading