链接

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的更多相关文章

  1. [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 ...

  2. Greenplum记录(一):主体结构、master、segments节点、interconnect、performance monitor

    结构:Client--master host--interconnect--segment host 每个节点都是单独的PG数据库,要获得最佳的性能需要对每个节点进行独立优化. master上不包含任 ...

  3. Application package 'AndroidManifest.xml' must have a minimum of 2 segments.

    看了源码就是packagename里面必须包含一个. 源码在: ./sdk/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/id ...

  4. segments&cache

    Segments 执行效果 命令  在 sense 里边执行  GET /abcd/_segments  前边的是索引名称,后边是请求 段信息 说明  索引是面向分片的,是由于索引是由一个或多个分片( ...

  5. [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 ...

  6. [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 ...

  7. MAPPING SEGMENTS TO PAGES

    The segmentation and paging mechanisms provide in the support a wide variety of approaches to memory ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. 接口自动化测试:apiAutoTest使用re 处理数据依赖

    目录 废话 2020/11/19 参数依赖 更新后的效果 新版依赖数据如何使用 源码地址 道谢 废话 目前在工作中写脚本的时候发现了一些之前开源的apiAutoTest的可优化项,后面应该也是会慢慢的 ...

  2. bluestore对象挂载到系统进行提取

    前言 之前在filestore里面,pg是直接暴露到文件系统的,也就是可以直接进去查看或者拷贝,在极端情况下,多个osd无法启动,pg无法导出的时候,那么对pg内部对象的操作处理,是可以作为最后恢复数 ...

  3. 记最近一次ceph故障修复

    前言 所谓吃一堑长一智,每次面对问题才是最好的学习机会,在面对问题的时候,尽量是能够自己去解决,或者去尝试能够最接近答案,确实无法解决再去寻求他人帮助,这样成长的会更快一些,在学校读书做题的时候,老师 ...

  4. Function(函数分享)第二节

    一.类型注解 1.1 类型注解 函数的类型注解分为两个部分:参数类型注解和返回值类型注解.其中返回值类型注解有时候我们可以直接省略,因为Typescript可以根据返回的语句来自动判断出返回值的类型. ...

  5. php插入一百万测试数据(实例)

    <?phpset_time_limit(0);function a(){ header("Content-Type:text/html;charset=utf-8"); $s ...

  6. linux qt 5.12.6 编译mysql驱动

    环境:ubuntu 18.4 x64.qt 5.12.6 问题:安装后是没有mysql的驱动的 解决过程: 各种搜索,先后安装了mysql mysql-client,mysql-server,和各种l ...

  7. Guitar Pro教程之虚拟吉他功能讲解

    上一章节我们讲述了Guitar Pro的组织小节的相关功能,那么本章节我们还是采用图文结合的方式为大家讲解关于{cms_selflink page='index' text='Guitar Pro'} ...

  8. 如何用Vegas完成视频编辑中的自动跟踪换图

    Vegas作为一款专业的视频剪辑软件,剪辑速度快捷,拥有各种实用工具和特效,同样也可以为用户实现视频换图的需求.今天小编就为大家讲解,如何利用Vegas自动跟踪进行换图,让视频能够更加便捷的呈现. 本 ...

  9. 自己动手实现java数据结构(九) 跳表

    1. 跳表介绍 在之前关于数据结构的博客中已经介绍过两种最基础的数据结构:基于连续内存空间的向量(线性表)和基于链式节点结构的链表. 有序的向量可以通过二分查找以logn对数复杂度完成随机查找,但由于 ...

  10. Java集合【8】-- ArrayList源码分析

    目录 1. ArrayList 1.1 ArrayList特点介绍 1.2 实现的接口和继承的类 2. 成员变量 3. 构造方法 4. 常用增删改查方法 添加元素 查询元素 更新元素 删除元素 5.自 ...