Educational Codeforces Round 137 (Rated for Div. 2) - F. Intersection and Union
(线段树 + 思维)or 动态dp
[Problem - F - Codeforces](https://codeforces.com/contest/1743/problem/E)
题意
数轴上有 \(n\) 个线段 \([l,r]\;(0<=l<=r<=3*10^5)\) ,表示有一个集合 \(s_i\) 为 \([l,r]\)
有三种集合运算,交、并、异或(两个集合异或 = 并 - 交)
\(S=s_i\;op\;s_2\;op\;...\;op\;s_n\) ,对于 \(n-1\) 个 op 的选择,有 \(3^{n-1}\) 种,求这 \(3^{n-1}\) 个 S 的大小之和
思路1
单独考虑一个元素 \(x\), 设 \(a\) 为包含 x 的一个集合,\(b\) 为不包含 \(x\) 的一个集合
关键是发现如下性质:
- 两个 b 搞不出来 x
- a 交,并 a 可以保留 x,有 2 种
- a 并,异或 b 可以保留 x,有 2 种
因此若最终想保留 x,则需要找到最后一个包含 x 的集合的下标 last (用线段树来维护单点最大值和区间赋值)
前 last - 2 个操作无所谓;后面的 n - last + 1 个操作只有 2 种选择才能保留 x
因此保留 x 的方案数为 \(3^{last-2}*2^{n-last+1}\) 个
思路2
同样单独考虑一个元素 x
设 \(f[i][0/1]\) 表示前 i 个集合操作完后,x 是否在当前集合中的方案数
当 x 在 \(s_i\) 中时
- \(f[i][0]=f[i-1][0]+f[i-1][1]\)
- \(f[i][1]=2*f[i-1][0]+2*f[i-1][1]\)
当 x 不在 \(s_i\) 中时
- \(f[i][0]=3*f[i-1][0]+f[i-1][1]\)
- \(f[i][1]=2*f[i-1][1]\)
但这样转移的复杂度为 \(O(n*3e5)\)
由于每个集合都是一个区间,可以用线段树来优化,可以对每个区间中所有数整体来转移
上述转移可写成矩阵的形式:
- x 在 \(s_i\) 中时
\[\begin{bmatrix}
f[i][0]\\
f[i][1]
\end{bmatrix}=
\begin{bmatrix}
1&1\\
2&2
\end{bmatrix}*
\begin{bmatrix}
f[i-1][0]\\
f[i-1][1]
\end{bmatrix}
\]- x 不在 \(s_i\) 中时
\[\begin{bmatrix}
f[i][0]\\
f[i][1]
\end{bmatrix}=
\begin{bmatrix}
3&1\\
2&0
\end{bmatrix}*
\begin{bmatrix}
f[i-1][0]\\
f[i-1][1]
\end{bmatrix}
\]
可用线段树对一个区间整体乘上一个矩阵
代码1
#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
typedef long long ll;
typedef pair<int, int> PII;
const int N = 3e5 + 10;
const int mod = 998244353;
int n;
struct SegmentTree {
#define ls u << 1
#define rs u << 1 | 1
struct T {
int l, r;
ll v;
ll add;
}tr[N << 2];
void pushup(int u) {
tr[u].v = max(tr[ls].v, tr[rs].v);
}
void update(T& rt, int add) {
rt.add = add, rt.v = add;
}
void pushdown(int u) {
if (tr[u].add) {
update(tr[ls], tr[u].add);
update(tr[rs], tr[u].add);
tr[u].add = 0;
}
}
void build(int u, int l, int r) {
tr[u].l = l, tr[u].r = r, tr[u].v = tr[u].add = 0;
if (tr[u].l == tr[u].r) {
return ;
}
int mid = (tr[u].l + tr[u].r) >> 1;
build(ls, l, mid), build(rs, mid + 1, r);
pushup(u);
return ;
}
void modify(int u, int l, int r, int v) {
if (tr[u].l >= l && tr[u].r <= r) {
update(tr[u], v);
return ;
}
pushdown(u);
int mid = (tr[u].l + tr[u].r) >> 1;
if (l <= mid) modify(ls, l, r, v);
if (r > mid) modify(rs, l, r, v);
pushup(u);
return ;
}
int query(int u, int l, int r) {
if (tr[u].l >= l && tr[u].r <= r) {
return tr[u].v;
}
pushdown(u);
int mid = (tr[u].l + tr[u].r) >> 1;
int res = 0;
if (l <= mid) res = query(ls, l, r);
if (r > mid) res = max(res, query(rs, l, r));
return res;
}
}tr;
ll qmi(ll a, ll b)
{
ll ans = 1;
while(b)
{
if (b & 1)
ans = ans * a % mod;
b >>= 1;
a = a * a % mod;
}
return ans;
}
int main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n;
tr.build(1, 0, N - 10);
for (int i = 1; i <= n; i++)
{
int l, r;
cin >> l >> r;
tr.modify(1, l, r, i);
}
ll ans = 0;
for (int i = 0; i <= N - 10; i++)
{
int last = tr.query(1, i, i);
if (last == 0)
continue;
int a = max(0, last - 2);
int b = n - 1 - a;
ans += qmi(3, a) * qmi(2, b) % mod;
ans %= mod;
}
cout << ans << endl;
return 0;
}
Educational Codeforces Round 137 (Rated for Div. 2) - F. Intersection and Union的更多相关文章
- Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings
Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings 题目连接: http://cod ...
- Educational Codeforces Round 71 (Rated for Div. 2)-F. Remainder Problem-技巧分块
Educational Codeforces Round 71 (Rated for Div. 2)-F. Remainder Problem-技巧分块 [Problem Description] ...
- Educational Codeforces Round 137 (Rated for Div. 2) A-F
比赛链接 A 题解 知识点:数学. \(4\) 位密码,由两个不同的数码组成,一共有 \(C_4^2\) 种方案.从 \(10-n\) 个数字选两个,有 \(C_{10-n}^2\) 种方案.结果为 ...
- Educational Codeforces Round 50 (Rated for Div. 2) F - Relatively Prime Powers(数学+容斥)
题目链接:http://codeforces.com/contest/1036/problem/F 题意: 题解:求在[2,n]中,x != a ^ b(b >= 2 即为gcd)的个数,那么实 ...
- Educational Codeforces Round 58 (Rated for Div. 2) F dp + 优化(新坑) + 离线处理
https://codeforces.com/contest/1101/problem/F 题意 有n个城市,m辆卡车,每辆卡车有起点\(s_i\),终点\(f_i\),每公里油耗\(c_i\),可加 ...
- Educational Codeforces Round 42 (Rated for Div. 2)F - Simple Cycles Edges
http://codeforces.com/contest/962/problem/F 求没有被两个及以上的简单环包含的边 解法:双联通求割顶,在bcc中看这是不是一个简单环,是的话把整个bcc的环加 ...
- Educational Codeforces Round 41 (Rated for Div. 2)F. k-substrings
题意比较麻烦略 题解:枚举前缀的中点,二分最远能扩展的地方,lcp来check,然后线段树维护每个点最远被覆盖的地方,然后查询线段树即可 //#pragma GCC optimize(2) //#pr ...
- Educational Codeforces Round 33 (Rated for Div. 2) F. Subtree Minimum Query(主席树合并)
题意 给定一棵 \(n\) 个点的带点权树,以 \(1\) 为根, \(m\) 次询问,每次询问给出两个值 \(p, k\) ,求以下值: \(p\) 的子树中距离 \(p \le k\) 的所有点权 ...
- Educational Codeforces Round 47 (Rated for Div. 2)F. Dominant Indices 线段树合并
题意:有一棵树,对于每个点求子树中离他深度最多的深度是多少, 题解:线段树合并快如闪电,每个节点开一个权值线段树,递归时合并即可,然后维护区间最多的是哪个权值,到x的深度就是到根的深度减去x到根的深度 ...
- Educational Codeforces Round 56 (Rated for Div. 2) F - Vasya and Array dp好题
F - Vasya and Array dp[ i ][ j ] 表示用了前 i 个数字并且最后一个数字是 j 的方案数. dp[ i ][ j ] = sumdp [i - 1 ][ j ], 这样 ...
随机推荐
- jmeter json提取器提取某个属性的所有值
json 提取器各字段说明: Variable names:保存的变量名,后面使用${Variable names}引用 JSON Path expressions:调试通过的json path表达 ...
- 英华学堂网课助手Linux版本
首先我们下去GitHub把文件下载下来记得 脚本地址: https://github.com/aoaostar/mooc/releases/latest 这几个版本随便下哪个都可以,下载完之后我们通过 ...
- CompletableFuture 使用总结
转载请注明出处: 1.Future使用对比 Future表示一个异步计算的结果.它提供了isDone()来检测计算是否已经完成,并且在计算结束后,可以通过get()方法来获取计算结果.在异步计算中,F ...
- 第六节 FAF与GP不工作保护区的绘制
飞行程序设计软件实践 前一篇文章中,通过风标设计2023插件,我们在CAD中绘制了FAP方式下的精密进近保护区. 接着这个话题我们继续来看一下FAF方式下的保护区应该怎样绘制,以及OAS参数的其它用法 ...
- Android录屏实现
使用方案: mediacodec + mediaprojection + mediamuxer MediaProjectionManager主要作用是获得录屏权限 startActivityForRe ...
- C++Day09 深拷贝、写时复制(cow)、短字符串优化
一.std::string 的底层实现 1.深拷贝 1 class String{ 2 public: 3 String(const String &rhs):m_pstr(new char[ ...
- ant design pro 配置路由 显示页面步骤详解
第一步 在 src/views 下新建页面的vue文件,如果相关页面有多个,可以新建一个文件夹来放置相关文件. 给页面里写几个字,等会可以看到哦~~ 第二步 将文件加入菜单和路由 进入这个文件 ...
- win32com操作word API精讲 第十集 Paragraphs & Paragraph接口 (一)
本课程<win32com操作word API精讲&项目实战>以视频为主,文字为辅,公众号ID:一灯编程 在word编程中,Range和Paragraph(s)接口无愧于劳模接口的称 ...
- 在线程里使用线程外的变量为什么一定要是final类型
public class CyclicBarrierDemo { public static void main(String[] args) { /* * 七龙珠 * */ CyclicBarrie ...
- 系列化和反序列化的概述-对象的序列化_Object Output Stream类
系列化和反序列化的概述 Java提供了一种对象序列化的机制.用一个字节序列可以表示一个对象,该字节序列包含该对象的数据对象的类型和对象中存储的属性等信息.字节序列写出到文件之后,相当于文件中持久保存了 ...