当前位置:网站首页>【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)
边栏推荐
- iMedicalLIS监听程序(2)
- The most comprehensive exam questions for software testing engineers in 2022
- token、jwt、oauth2、session解析
- AI + Small Nucleic Acid Drugs | Eleven Completes $22 Million Seed Round Financing
- Swing有几种常用的事件处理方式?如何监听事件?
- Bosses, I noticed that a mysql CDC connector parameters scan. The incremental. Sna
- UE4 通过与其它Actor互动开门
- Based on holding YOLOv5 custom implementation of FacePose YOLO structure interpretation, YOLO data format conversion, YOLO process modification"
- Dive into how it works together by simulating Vite
- The second council meeting of the Dragon Lizard Community was successfully held!Director general election, 4 special consultants joined
猜你喜欢
![[GYCTF2020]EasyThinking](/img/40/973411c69d1e4766d22f6a4a7c7c01.png)
[GYCTF2020]EasyThinking

IJCAI2022 | DictBert: Pre-trained Language Models with Contrastive Learning for Dictionary Description Knowledge Augmentation

iMedicalLIS监听程序(2)

数据库设计的酸(ACID)碱(BASE)原则

Detailed and comprehensive postman interface testing practical tutorial

Index Mysql in order to optimize paper 02 】 【 10 kinds of circumstances and the principle of failure

Android interview question - how to write with his hands a non-blocking thread safe queue ConcurrentLinkedQueue?

Open-Falcon of operation and maintenance monitoring system

UE4 后期处理体积 (角色受到伤害场景颜色变淡案例)
![[Qixi Festival] Romantic Tanabata, code teaser.Turn love into a gorgeous three-dimensional scene and surprise her (him)!(send code)](/img/10/dafea90158adf9d43c4f025414fef7.png)
[Qixi Festival] Romantic Tanabata, code teaser.Turn love into a gorgeous three-dimensional scene and surprise her (him)!(send code)
随机推荐
[TA-Frost Wolf_may-"Hundred Talents Project"] Graphics 4.3 Real-time Shadow Introduction
Getting Started with Kubernetes Networking
public static <T> List<T> asList(T... a) 原型是怎么回事?
UE4 opens door via interaction (keyboard key)
Static method to get configuration file data
ASP.NET application--Hello World
cross domain solution
UE4 第一人称角色模板 添加生命值和调试伤害
How to find all fields with empty data in sql
UE4 在游戏运行时更改变量 (通过鼠标滑轮来更改第一人称角色的最大行走速度)
多御安全浏览器 V10.8.3.1 版正式发布,优化多项内容
presto启动成功后出现2022-08-04T17:50:58.296+0800 ERROR Announcer-3 io.airlift.discovery.client.Announcer
36-Jenkins-Job迁移
[Qixi Festival] Romantic Tanabata, code teaser.Turn love into a gorgeous three-dimensional scene and surprise her (him)!(send code)
21 Days Learning Challenge (2) Use of Graphical Device Trees
队列题目:最近的请求次数
调用阿里云oss和sms服务
MRTK3开发Hololens应用-手势拖拽、旋转 、缩放物体实现
UE4 通过重叠事件开启门
After the large pixel panorama is completed, what are the promotion methods?