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 ...
随机推荐
- tcp/udp注意事项
- 查询Ceph的OSD占用内存
前言 之前写过一篇关于查询OSD的运行的CPU的情况的分享,本篇是讲的获取内存占用的,代码包括两种输出,一种是直接的表格,一种是可以方便解析的json 代码 直接上代码,python才用不久,所以可能 ...
- Go 语言设计哲学之四:项目布局-你如何设计项目结构
在多年的 Go 语言实践积累后逐渐形成了一种典型项目结构,如下图所示: 上面就是一个支持构建二进制可执行文件(在 src 下)的典型 Go 项目的结构. 1 src 目录: 存放项目要编译构建的可执行 ...
- MySQL索引背后的数据结构及原理
摘要 本文以MySQL数据库为研究对象,讨论与数据库索引相关的一些话题.特别需要说明的是,MySQL支持诸多存储引擎,而各种存储引擎对索引的支持也各不相同,因此MySQL数据库支持多种索引类型,如BT ...
- HDU100题简要题解(2030~2039)
HDU2030 汉字统计 题目链接 Problem Description 统计给定文本文件中汉字的个数. Input 输入文件首先包含一个整数n,表示测试实例的个数,然后是n段文本. Output ...
- 了解 MySQL的数据行、行溢出机制吗?
目录 一.行 有哪些格式? 二.紧凑的行格式长啥样? 三.MySQL单行能存多大体量的数据? 四.Compact格式是如何做到紧凑的? 五.什么是行溢出? 六.行 如何溢出? 七.思考一个问题 关注送 ...
- CorelDRAW快速制作闪耀钻石项链效果
今天小编为大家分享使用CorelDRAW快速制作闪耀钻石项链效果,过程并不是很复杂,主要用到刻刀工具.智能填充和渐变色的应用,待到一个角完成之后会走一点点捷径,利用旋转复制的方法做出完整的钻石效果,最 ...
- 【PUPPETEER】初探之原生frame切换(四)
一.知识点 page.frames() 使用frame.url() 获取framed的url x.getAttribute('x') 获取元素内值 二.实例 问:什么是iframe? 答:iframe ...
- JPA使用之@Query的常用写法
准备 实体 @Data @Table(name = "task_apply") @Entity public class TaskApply { @Id @GeneratedVal ...
- windows安装redis扩展
Thread Safety enabled 打开phpinfo() 看php版本是ts还是nts, 如上是ts版本的,所以需要安装redis的ts版本, redis的扩展下载地址 https://p ...