题库chevron_right分类chevron_right线段树
segment

线段树

57 道题目
简单: 1中等: 11困难: 45

线段树 是技术面试里最常出现的能力维度之一。建议先掌握基础题型的边界处理,再逐步过渡到模式识别和复杂度 trade-off。

面试场景

高频考察问题建模、边界条件与口头表达的清晰度。

常见误区

只背模板不解释为什么,容易在追问里失分。

练习策略

每轮练 3-5 题,固定复盘复杂度和可替代解法。

推荐练习顺序

#题目难度
218

天际线问题

The Skyline Problem requires calculating a city's silhouette using array manipulation and divide-and-conquer techniques …

困难
307

区域和检索 - 数组可修改

Implement a mutable range sum query using efficient design patterns to handle multiple updates and range sum queries.

中等
315

计算右侧小于当前元素的个数

Solve the Count of Smaller Numbers After Self problem using binary search and optimized algorithms.

困难
327

区间和的个数

Count the number of subarray sums within a given inclusive range using optimized divide-and-conquer techniques efficient…

困难
406

根据身高重建队列

Reconstruct a queue based on the given heights and the number of taller people in front, using sorting and binary indexe…

中等
493

翻转对

Count the number of reverse pairs in a given integer array using efficient algorithms like binary search and merge sort.

困难
673

最长递增子序列的个数

This problem challenges you to find the number of longest increasing subsequences in a given array of integers.

中等
699

掉落的方块

Solve Falling Squares by efficiently computing maximum stack heights using arrays with segment tree optimization techniq…

困难
715

Range 模块

Design a RangeModule to track and query half-open intervals using segment trees or ordered sets.

困难
729

我的日程安排表 I

Implement a calendar supporting non-overlapping event bookings using binary search for efficient insertion and conflict …

中等
731

我的日程安排表 II

Implement a calendar that allows double bookings but prevents triple bookings, managing overlapping intervals efficientl…

中等
732

我的日程安排表 III

Implement My Calendar III to track maximum overlapping events efficiently using binary search and segment tree technique…

困难
850

矩形面积 II

The problem involves calculating the total area covered by multiple rectangles, ensuring overlap is counted only once.

困难
1157

子数组中占绝大多数的元素

Efficiently find the majority element in any subarray using a data structure optimized for multiple range queries.

困难
1395

统计作战单位数

Count the number of valid three-soldier teams using ratings with a state transition dynamic programming approach efficie…

中等
1505

最多 K 次交换相邻数位后得到的最小整数

Reorder digits using at most k adjacent swaps to produce the smallest possible integer, leveraging greedy selection effi…

困难
1521

找到最接近目标值的函数值

In this problem, you'll use binary search to find the closest value of a mysterious function to a given target.

困难
1622

奇妙序列

Implement a Fancy sequence supporting append, addAll, and multAll operations efficiently using cumulative math design te…

困难
1649

通过指令创建有序数组

The problem asks to compute the cost of inserting elements into a sorted array using a series of instructions.

困难
1687

从仓库到码头运输箱子

Optimize the minimum number of trips to deliver boxes to ports under strict ship constraints using dynamic programming t…

困难
2080

区间内查询数字的频率

Design a data structure to handle efficient frequency queries for subarrays, focusing on hash-based lookups and efficien…

中等
2179

统计数组中好三元组数目

Count Good Triplets in an Array requires tracking index orders across two permutations efficiently using binary search.

困难
2213

由单个字符重复的最长子字符串

Solve Longest Substring of One Repeating Character by maintaining mergeable run information under character updates afte…

困难
2276

统计区间中的整数数目

Design and implement a data structure to efficiently add intervals and count the total number of integers covered by the…

困难
2286

以组为单位订音乐会的门票

Design a ticketing system to allocate concert seats in specific groupings while efficiently handling seat reservations.

困难
2407

最长递增子序列 II

Determine the longest increasing subsequence in an array where consecutive elements differ by at most k using dynamic pr…

困难
2424

最长上传前缀

Calculate the longest continuous uploaded prefix in a video stream efficiently using a mix of binary search and data str…

中等
2426

满足不等式的数对数目

Count pairs in two arrays satisfying a given inequality condition using binary search over the valid answer space.

困难
2569

更新数组后处理求和查询

Solve the Handling Sum Queries After Update problem using arrays and segment trees with lazy propagation for efficiency.

困难
2659

将数组清空

Solve the "Make Array Empty" problem using binary search to determine the minimum number of operations required.

困难
2736

最大和查询

Find the maximum sum of paired elements from two arrays under query constraints using efficient binary search techniques…

困难
2916

子数组不同元素数目的平方和 II

Compute the sum of squares of distinct elements in all subarrays using state transition dynamic programming efficiently.

困难
2926

平衡子序列的最大和

Learn to find the maximum sum of a balanced subsequence using dynamic programming and careful state transitions efficien…

困难
2940

找到 Alice 和 Bob 可以相遇的建筑

Determine the leftmost building where Alice and Bob can meet using a binary search over valid move sequences.

困难
3072

将元素分配到两个数组中 II

Distribute elements into two arrays based on conditions, utilizing a Binary Indexed Tree for efficient counting and simu…

困难
3117

划分数组得到最小的值之和

Solve the problem of dividing an array into subarrays to match specified bitwise AND values using dynamic programming.

困难
3161

物块放置查询

Determine if blocks can be placed on an infinite number line using queries, leveraging binary search over the valid answ…

困难
3165

不包含相邻元素的子序列的最大和

Compute the maximum sum of a subsequence where no two adjacent elements are selected after each array update efficiently…

困难
3171

找到按位或最接近 K 的子数组

Find a subarray with bitwise OR closest to a given value k with minimal absolute difference.

困难
3187

数组中的峰值

Determine peaks in a dynamic integer array using efficient Binary Indexed Tree updates and range queries for fast result…

困难
3209

子数组按位与值为 K 的数目

The problem asks to find the number of subarrays with a given AND value in an array, utilizing binary search for optimiz…

困难
3291

形成目标字符串需要的最少字符串数 I

Use dynamic programming to split target into the fewest prefixes that match any word prefix, while ruling out dead posit…

中等
3292

形成目标字符串需要的最少字符串数 II

Compute the minimum number of valid strings from an array needed to construct a given target string efficiently using dy…

困难
3380

用点构造面积最大的矩形 I

Find the maximum area of a rectangle formed by given points on a plane with unique coordinates.

中等
3382

用点构造面积最大的矩形 II

Find the largest rectangle on a plane using given points while avoiding any interior points and optimizing with math and…

困难
3410

删除所有值为某个元素后的最大子数组和

Maximize Subarray Sum After Removing All Occurrences of One Element involves finding the optimal subarray sum with one a…

困难
3420

统计 K 次操作以内得到非递减子数组的数目

This problem asks you to count non-decreasing subarrays in a given array after applying at most k operations.

困难
3454

分割正方形 II

Separate Squares II requires finding the minimum y-coordinate such that squares' areas are split evenly above and below …

困难
3477

水果成篮 II

Determine the number of fruit types that remain unplaced after all allocations in the "Fruits Into Baskets II" problem.

简单
3479

水果成篮 III

Fruits Into Baskets III requires placing fruits into baskets efficiently using binary search over the answer space for c…

中等
3480

删除一个冲突对后最大子数组数目

Maximize the count of subarrays after removing one conflicting pair using array traversal and segment tree logic efficie…

困难
3501

操作后最大活跃区段数 II

Maximize the number of active sections in a binary string with at most one trade.

困难
3515

带权树中的最短路径

Solve the Shortest Path in a Weighted Tree using binary-tree traversal and efficient state tracking for queries.

困难
3525

求出数组的 X 值 II

The "Find X Value of Array II" problem requires calculating the number of ways to remove a suffix from an array such tha…

困难
3569

分割数组后不同质数的最大数目

Compute the maximum number of distinct prime numbers after sequentially updating array elements with efficient preproces…

困难
3605

数组的最小稳定性因子

The problem requires finding the minimum stability factor of an array by utilizing binary search and math-based optimiza…

困难
3624

位计数深度为 K 的整数数目 II

This problem challenges you to efficiently calculate the number of integers with popcount-depth equal to K using array a…

困难

关联高频模式

LeetCode 线段树题型题解:57题训练路线