Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
See all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
yinghaoyu
LeetCodeInHardWay
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
LeetCodeInHardWay
/
src
/
Problem_2276_CountIntervals.cc
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
75 lines (65 loc) · 1.92 KB
master
Breadcrumbs
LeetCodeInHardWay
/
src
/
Problem_2276_CountIntervals.cc
Copy path
Top
File metadata and controls
Code
Blame
75 lines (65 loc) · 1.92 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <vector>
using namespace std;
// 开点线段树的实现
class CountIntervals
{
private:
// 支持的最大范围
static const int n = 1000000000;
// 空间大小定成这个值是实验的结果
static const int LIMIT = 700001;
vector<int> left = vector<int>(LIMIT);
vector<int> right = vector<int>(LIMIT);
vector<int> sum = vector<int>(LIMIT);
int cnt = 1;
public:
CountIntervals() {}
void up(int h, int l, int r) { sum[h] = sum[l] + sum[r]; }
// 这个题的特殊性在于,只有改1的操作,没有改0的操作
// 理解这个就可以分析出不需要懒更新机制,原因有两个
// 1) 查询操作永远查的是整个范围1的数量,不会有小范围的查询,每次都返回sum[1]
// 这意味着只要能把sum[1]更新正确即可,up函数可以保证这一点
// 2) 一个范围已经全是1,那以后都会是1,没有必要把全是1的懒更新信息向下传递
// 这个函数的功能比线段树能做到的范围修改功能简单很多
// 功能有阉割就意味着存在优化的点
void setOne(int jobl, int jobr, int l, int r, int i)
{
if (sum[i] == r - l + 1)
{
return;
}
if (jobl <= l && r <= jobr)
{
sum[i] = r - l + 1;
}
else
{
int mid = (l + r) >> 1;
if (jobl <= mid)
{
if (left[i] == 0)
{
left[i] = ++cnt;
}
setOne(jobl, jobr, l, mid, left[i]);
}
if (jobr > mid)
{
if (right[i] == 0)
{
right[i] = ++cnt;
}
setOne(jobl, jobr, mid + 1, r, right[i]);
}
up(i, left[i], right[i]);
}
}
void add(int left, int right) { setOne(left, right, 1, n, 1); }
int count() { return sum[1]; }
};
/**
* Your CountIntervals object will be instantiated and called as such:
* CountIntervals* obj = new CountIntervals();
* obj->add(left,right);
* int param_2 = obj->count();
*/
You can’t perform that action at this time.