(线段树 + 思维)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

  1. 单独考虑一个元素 \(x\), 设 \(a\) 为包含 x 的一个集合,\(b\) 为不包含 \(x\) 的一个集合

  2. 关键是发现如下性质:

    1. 两个 b 搞不出来 x
    2. a 交,并 a 可以保留 x,有 2 种
    3. a 并,异或 b 可以保留 x,有 2 种
  3. 因此若最终想保留 x,则需要找到最后一个包含 x 的集合的下标 last (用线段树来维护单点最大值和区间赋值)

    前 last - 2 个操作无所谓;后面的 n - last + 1 个操作只有 2 种选择才能保留 x

    因此保留 x 的方案数为 \(3^{last-2}*2^{n-last+1}\) 个

思路2

  1. 同样单独考虑一个元素 x

  2. 设 \(f[i][0/1]\) 表示前 i 个集合操作完后,x 是否在当前集合中的方案数

  3. 当 x 在 \(s_i\) 中时

    1. \(f[i][0]=f[i-1][0]+f[i-1][1]\)
    2. \(f[i][1]=2*f[i-1][0]+2*f[i-1][1]\)

    当 x 不在 \(s_i\) 中时

    1. \(f[i][0]=3*f[i-1][0]+f[i-1][1]\)
    2. \(f[i][1]=2*f[i-1][1]\)
  4. 但这样转移的复杂度为 \(O(n*3e5)\)

  5. 由于每个集合都是一个区间,可以用线段树来优化,可以对每个区间中所有数整体来转移

  6. 上述转移可写成矩阵的形式:

    1. 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}
    \]
    1. 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}
      \]
  7. 可用线段树对一个区间整体乘上一个矩阵

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

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

  2. Educational Codeforces Round 71 (Rated for Div. 2)-F. Remainder Problem-技巧分块

    Educational Codeforces Round 71 (Rated for Div. 2)-F. Remainder Problem-技巧分块 [Problem Description] ​ ...

  3. Educational Codeforces Round 137 (Rated for Div. 2) A-F

    比赛链接 A 题解 知识点:数学. \(4\) 位密码,由两个不同的数码组成,一共有 \(C_4^2\) 种方案.从 \(10-n\) 个数字选两个,有 \(C_{10-n}^2\) 种方案.结果为 ...

  4. 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)的个数,那么实 ...

  5. 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\),可加 ...

  6. Educational Codeforces Round 42 (Rated for Div. 2)F - Simple Cycles Edges

    http://codeforces.com/contest/962/problem/F 求没有被两个及以上的简单环包含的边 解法:双联通求割顶,在bcc中看这是不是一个简单环,是的话把整个bcc的环加 ...

  7. Educational Codeforces Round 41 (Rated for Div. 2)F. k-substrings

    题意比较麻烦略 题解:枚举前缀的中点,二分最远能扩展的地方,lcp来check,然后线段树维护每个点最远被覆盖的地方,然后查询线段树即可 //#pragma GCC optimize(2) //#pr ...

  8. Educational Codeforces Round 33 (Rated for Div. 2) F. Subtree Minimum Query(主席树合并)

    题意 给定一棵 \(n\) 个点的带点权树,以 \(1\) 为根, \(m\) 次询问,每次询问给出两个值 \(p, k\) ,求以下值: \(p\) 的子树中距离 \(p \le k\) 的所有点权 ...

  9. Educational Codeforces Round 47 (Rated for Div. 2)F. Dominant Indices 线段树合并

    题意:有一棵树,对于每个点求子树中离他深度最多的深度是多少, 题解:线段树合并快如闪电,每个节点开一个权值线段树,递归时合并即可,然后维护区间最多的是哪个权值,到x的深度就是到根的深度减去x到根的深度 ...

  10. 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 ], 这样 ...

随机推荐

  1. UVA439 Knight Moves

    原题Vjudge 题目大意 有一个骑士,他可以骑马日字型跳跃,问他从A点到B点最少要几步 解题思路 这题就是一个特别裸的广搜板子 它的主要问题在于输入输出 输入的数据我们可以用\(pair\)读入,第 ...

  2. 解决MySQL Connector/ODBC驱动无法安装Error1918

    1.问题描述 我在一台windows服务器上安装好mysql之后,再安装mysql的ODBC连接驱动时,报错如下: 2.解决方法 之所以出现安装失败是由于缺少Miscrosoft Visual C++ ...

  3. C语言写的 史上最公平的投票系统

    #include<stdio.h> #include<string.h> #define MMM 4 struct student { char name[10]; int c ...

  4. 刷题笔记——3002.买图书 & 2763.计算(a+b)/c的值

    题目1 3002.买图书 代码 while True: try: n,m=map(float,input().strip().split()) if(n==10 and m==1): print('{ ...

  5. pymysql在读取bit类型时显示x00的解决办法

    #用于转换bit在pymysql中显示为x00的问题from pymysql import convertersconverions = converters.conversionsconverion ...

  6. three.js一步一步来--如何画出一个转动的正方体

    基础知识--正方体代码如下 <template> <div style="width:1000px; height:800px"> <h1>正方 ...

  7. WPF中下拉框即可以选择项也可以作为只读文本框使用

    1.需求 当前在开发的系统需要一个这样的控件. (1)可以选择已有的选择项,类似于ComboBox选择: (2)可以通过其他按钮点击,选择一个文件,选择后,把文件路径显示到控件上,并且处于只读状态,行 ...

  8. rust 模块、路径、项目类型等相关内容

    rust 模块路径 转载 https://blog.csdn.net/wowotuo/article/details/107591501 rust 项目编译类型 转载 https://blog.51c ...

  9. spring in action day-06 JMS -ActiveMQ Artemi

    JMS -ActiveMQ Artemi JMS:它是一个规范,类似于jdbctemplate Spring提供了jmstemplate来发送和接收消息. 搭建JMS环境 1.引入依赖 我们要使用的消 ...

  10. rt-thread模糊到清晰系列: timer.c

    #include <rtthread.h> #include <rthw.h> /* hard timer list */ static rt_list_t rt_timer_ ...