当前位置:网站首页>【8.3】代码源 - 【喵 ~ 喵 ~ 喵~】【树】【与】
【8.3】代码源 - 【喵 ~ 喵 ~ 喵~】【树】【与】
2022-08-05 03:51:00 【ZhgDgE】
#865. 喵 ~ 喵 ~ 喵~
题意:有 m ( 1 ≤ m ≤ 1 0 5 ) m(1\leq m\leq 10^5) m(1≤m≤105) 次操作,每次操作:如果 o p = 1 op=1 op=1 ,则在可重集合中添加一个区间 [ l , r ] ( 1 ≤ l ≤ r ≤ 1 0 5 ) [l,r](1\leq l\leq r\leq 10^5) [l,r](1≤l≤r≤105) ;如果 o p = 2 op=2 op=2 ,则给定一个区间 [ l , r ] ( 1 ≤ l ≤ r ≤ 1 0 5 ) [l,r](1\leq l\leq r\leq 10^5) [l,r](1≤l≤r≤105) ,问集合中有多少个区间和给定区间相交。
思路:这种问题有一个比较经典的解决思路。假设当前有 m m m 个区间 ,给定区间 [ L , R ] [L,R] [L,R] ,则与给定区间相交的区间数量为 :左端点在 R R R 左边的区间数量 减去 右端点在 L L L 左边的区间数量,即 ∑ i = 1 m [ l i ≤ R ] − ∑ i = 1 m [ r i < L ] \sum_{i=1}^m[l_i\leq R]-\sum_{i=1}^m[r_i<L] ∑i=1m[li≤R]−∑i=1m[ri<L] 。因为左端点在 R R R 左边的区间,如果不与给定区间相交,则必然是右端点在 L L L 左边。
那么实现方式就是开两个树状数组,一个维护 { l } \{l\} { l} 一个维护 { r } \{r\} { r} ,就可以了。
AC代码:http://oj.daimayuan.top/submission/317153
#154. 树
题意:有一棵 n ( 1 ≤ n ≤ 3000 ) n(1\leq n\leq 3000) n(1≤n≤3000) 个节点的以 1 1 1 为根的有根树。现在可以对这棵树进行若干次操作,每一次操作可以选择树上的一个点然后删掉这个点和它的儿子之间的所有边。
现在想要知道对于每一个 k ∈ [ 1 , n ] k∈[1,n] k∈[1,n] ,最少需要多少次操作才能让图中恰好存在 k k k 个联通块。
思路:初始连通块数量为 1 1 1 ,定义 s i z ( i ) siz(i) siz(i) 表示点 i i i 有多少个儿子,如果删去 i i i 的子向边则连通块数量会增加 s i z ( i ) siz(i) siz(i) 。
那么问题就转化成了 01 背包问题:背包体积上限为 k ( k ∈ [ 1 , n ] ) k(k\in[1,n]) k(k∈[1,n]) ,每个物品只有一个,体积为 s i z ( i ) siz(i) siz(i) ,价值为 1 1 1 ,问恰好填满背包 k k k 个体积的最小价值。
AC代码:http://oj.daimayuan.top/submission/317187
#155. 与
题意:给定 n , k ( 1 ≤ n , k ≤ 1 0 4 ) n,k(1\leq n, k\leq 10^4) n,k(1≤n,k≤104) 。计算有多少长度为 k k k 的数组 a 1 , a 2 , ⋯ , a k a_1,a_2,\cdots,a_k a1,a2,⋯,ak ,满足:
- ∑ i = 1 k a i = n , a i ≥ 0 ∑_{i=1}^ka_i=n,a_i≥0 ∑i=1kai=n,ai≥0 。
- 对于任意的 i = 1 , … , k − 1 i=1,…,k−1 i=1,…,k−1 有 a i AND a i + 1 = a i + 1 a_i ~\text{AND} ~a_{i+1}=a_{i+1} ai AND ai+1=ai+1 。其中 AND \text{AND} AND 是与操作。
输出答案对 1 0 9 + 7 10^9+7 109+7 取模的结果。
思路:考虑二进制上的每一位,从 1 ∼ k 1\sim k 1∼k 必然是 1 , 1 , ⋯ , 1 , 0 , ⋯ , 0 , 0 1,1,\cdots,1,0,\cdots,0,0 1,1,⋯,1,0,⋯,0,0 ,那我们从二进制的角度定义,这样状态数量更少,转移起来更快。定义 d p ( b i t , j ) dp(bit,j) dp(bit,j) 表示前 0 ∼ b i t 0\sim bit 0∼bit 上这些位可以为 0 / 1 0/1 0/1 ,且 b i t ∼ inf bit\sim \inf bit∼inf 位上都为 0 0 0 ,且总和为 j j j 时的方案数 。那么我们枚举 b i t bit bit 位上有多少个 1 1 1 ,转移方程就是 d p ( b i t , j ) = ∑ j − k × 2 b i t ≥ 0 d p ( b i t − 1 , j − k × 2 b i t ) dp(bit,j)=\sum_{j-k\times 2^{bit} \geq 0}dp(bit-1,j - k\times 2^{bit}) dp(bit,j)=∑j−k×2bit≥0dp(bit−1,j−k×2bit)
边栏推荐
- Common open source databases under Linux, how many do you know?
- Defect detection (image processing part)
- 关于#SQL#的迭代、父子结构查询问题,如何解决?
- Redis key基本命令
- Index Mysql in order to optimize paper 02 】 【 10 kinds of circumstances and the principle of failure
- The sword refers to Offer--find the repeated numbers in the array (three solutions)
- 36-Jenkins-Job迁移
- Burp installation and proxy settings
- MRTK3开发Hololens应用-手势拖拽、旋转 、缩放物体实现
- Android interview question - how to write with his hands a non-blocking thread safe queue ConcurrentLinkedQueue?
猜你喜欢

Industry Status?Why do Internet companies prefer to spend 20k to recruit people rather than raise their salary to retain old employees~

Walter talked little knowledge | "remote passthrough" that something

Never put off till tomorrow what you can put - house lease management system based on the SSM

21 Days Learning Challenge (2) Use of Graphical Device Trees

Leading the highland of digital medicine, Zhongshan Hospital explores to create a "new paradigm" for future hospitals

MySql index learning and use; (I think it is detailed enough)

UE4 opens door via interaction (keyboard key)
![[Software testing] unittest framework for automated testing](/img/80/caedd5cf6dd61c9d75475866613cac.png)
[Software testing] unittest framework for automated testing

Detailed and comprehensive postman interface testing practical tutorial

36-Jenkins-Job迁移
随机推荐
UE4 opens doors with overlapping events
AI + Small Nucleic Acid Drugs | Eleven Completes $22 Million Seed Round Financing
The second council meeting of the Dragon Lizard Community was successfully held!Director general election, 4 special consultants joined
2022 High-level installation, maintenance, and removal of exam questions mock exam question bank and online mock exam
Kubernetes 网络入门
2022 Hangzhou Electric Multi-School 1st Game
商业智能BI业务分析思维:现金流量风控分析(一)营运资金风险
DEJA_VU3D - Cesium功能集 之 058-高德地图纠偏
Growth-based checkerboard corner detection method
国学*周易*梅花易数 代码实现效果展示 - 梅花心易
mutillidae下载及安装
Acid (ACID) Base (BASE) Principles for Database Design
[Filter tracking] based on matlab unscented Kalman filter inertial navigation + DVL combined navigation [including Matlab source code 2019]
.NET Application -- Helloworld (C#)
How to wrap markdown - md file
Common open source databases under Linux, how many do you know?
Android interview question - how to write with his hands a non-blocking thread safe queue ConcurrentLinkedQueue?
开发Hololens遇到The type or namespace name ‘HandMeshVertex‘ could not be found..
cross domain solution
36-Jenkins-Job Migration