CF1327F AND Segments
Description
要求构造满足下列条件的长度为 \(n\) 的序列 \(a\) 的个数:
- 每个数值域在 \([0, 2 ^ k)\)
- \(m\) 个限制条件 \(l, r, x\),需要满足 \(a_l\ \text{and}\ a_{l+1} \text{and}\ ... \text{and}\ a_{r} = x\)。 (二进制按位与)
\(n, m \le 5 · 10^5\)
Solution
二进制表示,每个数都是一个 \(k\) 位的 \(01\) 串。按位与限制,不同位互不影响,考虑分别做再乘法原理乘起来。
现在问题转化为了,构造长度为 \(n\) 的 \(01\) 串,有 \(m\) 个限制。
- 要么为一段区间都是 \(1\)。
- 或是一段区间必须有一个 \(0\)。
不妨枚举 \(0\) 的出现位置。
状态设计
\(f_i\) 表示前 \(i\) 个位置,第 \(i\) 个位置为 \(0\)。\([1, i]\) 区间内包含的所有区间条件已经满足的方案数。
状态转移
- 如果第 \(i\) 位必须填 \(1\)(这个用差分 \(O(n)\) 预处理),那么 \(f_i = 0\)。做完这步,第一个限制肯定满足,因为填 \(0\) 的机会全部被否认掉了。
- 否则,枚举上一个 \(0\) 的位置 \(0 \le j < i\),即 \((j, i)\) 区间全部填 \(1\),\(f_i = \sum f_j\)。为了满足第二个条件,那么 \((j, i)\) 区间必然不能完全包含第二个限制的任何一个区间,即 \(j\) 必须 \(\ge \max(l)\),其中 \(r \le i - 1\) 的。
优化
对于 \(f_i = \sum f_j\)。发现 \(j\) 所取的是一个滑动窗口 \([\max(l), i - 1]\)。即左右端点都是不降的,这样搞一个变量动态维护这个和即可。
初始状态:\(f_0 = 1\)
答案:\(\sum f_{j}^{n}\)(最后一波连续的 \(1\) 里也不能包含第二个限制中的任意区间)
时间复杂度
\(O(kn)\)
Code
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;
const int N = 500005, P = 998244353;
int n, K, m, ans = 1;
int cnt[N], pre[N], f[N];
int L[N], R[N], X[N];
int inline solve(int w) {
memset(f, 0, sizeof f);
memset(cnt, 0, sizeof cnt);
memset(pre, 0, sizeof pre);
for (int i = 1; i <= m; i++) {
if (X[i] >> w & 1) cnt[L[i]]++, cnt[R[i] + 1]--;
else pre[R[i]] = max(pre[R[i]], L[i]);
}
for (int i = 1; i <= n; i++) cnt[i] += cnt[i - 1];
f[0] = 1;
int s = 1, j = 0;
for (int i = 1; i <= n; i++) {
if (!cnt[i]) f[i] = s;
(s += f[i]) %= P;
while (j < pre[i]) s = ((LL)s - f[j++] + P) % P;
}
int res = 0;
for (int i = j; i <= n; i++) (res += f[i]) %= P;
return res;
}
int main() {
scanf("%d%d%d", &n, &K, &m);
for (int i = 1; i <= m; i++) scanf("%d%d%d", L + i, R + i, X + i);
for (int k = 0; k < K; k++) ans = (LL)ans * solve(k) % P;
printf("%d\n", ans);
}
CF1327F AND Segments的更多相关文章
- [LeetCode] Number of Segments in a String 字符串中的分段数量
Count the number of segments in a string, where a segment is defined to be a contiguous sequence of ...
- Greenplum记录(一):主体结构、master、segments节点、interconnect、performance monitor
结构:Client--master host--interconnect--segment host 每个节点都是单独的PG数据库,要获得最佳的性能需要对每个节点进行独立优化. master上不包含任 ...
- Application package 'AndroidManifest.xml' must have a minimum of 2 segments.
看了源码就是packagename里面必须包含一个. 源码在: ./sdk/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/id ...
- segments&cache
Segments 执行效果 命令 在 sense 里边执行 GET /abcd/_segments 前边的是索引名称,后边是请求 段信息 说明 索引是面向分片的,是由于索引是由一个或多个分片( ...
- [UCSD白板题] Points and Segments
Problem Introduction The goal in this problem is given a set of segments on a line and a set of poin ...
- [UCSD白板题] Covering Segments by Points
Problem Introduction You are given a set of segments on a line and your goal is to mark as few point ...
- MAPPING SEGMENTS TO PAGES
The segmentation and paging mechanisms provide in the support a wide variety of approaches to memory ...
- Codeforces Round #337 (Div. 2) D. Vika and Segments 线段树 矩阵面积并
D. Vika and Segments Vika has an infinite sheet of squared paper. Initially all squares are whit ...
- Leetcode: Number of Segments in a String
Count the number of segments in a string, where a segment is defined to be a contiguous sequence of ...
随机推荐
- kafka常见面试题
1.Kafka 中的 ISR(InSyncRepli).OSR(OutSyncRepli).AR(AllRepli)代表什么? 1.AR = ISR+OSR ISR: kafka 使用多副本来保证消息 ...
- Innodb自增主键与sql_mode
1.自增主键 1.设置自增主键 建表设置自增主键,设置自增主键需要唯一约束,否则会报错.(即指定列数据唯一) mysql> create table test_zz(id int auto_in ...
- 磁盘构造/msdos分区(fdisk)格式化(mkfs)和挂载
分区不是必要的,分区是与系统盘分开,防止数据丢失. 磁盘使用流程:查看磁盘(fdisk -l)---分区---格式化(创建文件系统)----挂载(自动挂载) 分区表类型:msdos(一般是系统分区) ...
- 配置内网访问的TV
前言 通过内网模式访问tv远程机器 方法 云主机配置 一台云主机,云主机申请两个公网IP 云主机启动两个frps进程绑定到两个内网的ip 客户端配置 远程一台linux跳板机运行frpc,启动两个进程 ...
- java.lang.IllegalStateException: Duplicate key 20
这个我在公司遇到的一个问题.原因:使用Map<String, String> RelationMap = relation.stream().collect(Collectors.toMa ...
- 基于tcp的socket通信
# socket # socekt是应用层与TCP/IP协议族通信的中间软件抽象层,它是一组接口.在设计模式中,socket其实就是一个门面模式,它 # 把复杂的tcp/ip协议族隐藏在socket接 ...
- Spark SQL | 目前Spark社区最活跃的组件之一
Spark SQL是一个用来处理结构化数据的Spark组件,前身是shark,但是shark过多的依赖于hive如采用hive的语法解析器.查询优化器等,制约了Spark各个组件之间的相互集成,因此S ...
- 【Azure Redis 缓存 Azure Cache For Redis】使用Redis自带redis-benchmark.exe命令测试Azure Redis的性能
问题描述 关于Azure Redis的性能问题,在官方文档中,可以查看到不同层级Redis的最大连接数,每秒处理请求的性能. 基本缓存和标准缓存 C0 (250 MB) 缓存 - 最多支持 256 个 ...
- Android系统添加key和keypad
平台:MTK 一.添加一个按键 1.在DCT tool keypad list 文件增加新按键的选项alps\mediatek\source\dct\Keypad_YuSu.cmp中添加新键,如SMS ...
- Wasp XT合成器功能介绍
本章节将采用图文结合的方式给大家讲解电音编曲软件FL Studio中的Wasp XT合成器的相关功能,感兴趣的朋友可以一起来交流哦. 下面我们一起来看看吧 Wasp XT是一个3振荡器合成器,它包含一 ...