CF1178F2 Long Colorful Strip 题解 搜索

Long Colorful Strip

传送门

题面翻译

题目描述

这是 F 题的第二个子任务。F1 和 F2 的区别仅在对于 m m m 和时间的限制上

n + 1 n+1 n+1 种颜色标号从 0 0 0 n n n,我们有一条全部染成颜色 0 0 0 的长为 m m m 的纸带。

Alice 拿着刷子通过以下的过程来给纸带染色:

我们按照从 1 1 1 n n n 的顺序进行染色,进行每次染色时,我们选取一个区间 [ a i , b i ] [a_i,b_i] [ai,bi] 0 ≤ a i < b i ≤ m 0 \le a_i < b_i \le m 0ai<bim,并且这个区间内必定是单种颜色。

染色到最后,纸带上有各种颜色除了颜色 0 0 0。给出纸带最终的状态,问有多少种不同的染色方案能到达最终状态。输出时结果模 998244353 998244353 998244353

输入格式

第一行有两个整数 n n n m m m 1 ≤ n ≤ 500 1 \le n \le 500 1n500 n ≤ m ≤ 1 0 6 n \le m \le 10^6 nm106 n n n 代表除了颜色 0 0 0 有多少种颜色, m m m 代表纸带的长度。

第二行有 m m m 个用空格分隔的整数 c 1 , c 2 , … , c m c_1,c_2,\dots,c_m c1,c2,,cm 1 < = c i < = n 1<=c_i<=n 1<=ci<=n,代表完成染色后纸带的最终状态。保证最终状态中能在纸带上找到从 1 1 1 n n n 所有的颜色。

输出格式

输入一个单独的整数,即 Alice 可行的染色方案数模 998244353 998244353 998244353

题目描述

This is the second subtask of problem F. The only differences between this and the first subtask are the constraints on the value of m m m and the time limit. It is sufficient to solve this subtask in order to hack it, but you need to solve both subtasks in order to hack the first one.

There are n + 1 n+1 n+1 distinct colours in the universe, numbered 0 0 0 through n n n . There is a strip of paper m m m centimetres long initially painted with colour 0 0 0 .

Alice took a brush and painted the strip using the following process. For each i i i from 1 1 1 to n n n , in this order, she picks two integers 0 ≤ a i < b i ≤ m 0 \leq a_i < b_i \leq m 0ai<bim , such that the segment [ a i , b i ] [a_i, b_i] [ai,bi] is currently painted with a single colour, and repaints it with colour i i i .

Alice chose the segments in such a way that each centimetre is now painted in some colour other than 0 0 0 . Formally, the segment [ i − 1 , i ] [i-1, i] [i1,i] is painted with colour c i c_i ci ( c i ≠ 0 c_i \neq 0 ci=0 ). Every colour other than 0 0 0 is visible on the strip.

Count the number of different pairs of sequences { a i } i = 1 n \{a_i\}_{i=1}^n {ai}i=1n , { b i } i = 1 n \{b_i\}_{i=1}^n {bi}i=1n that result in this configuration.

Since this number may be large, output it modulo 998244353 998244353 998244353 .

输入格式

The first line contains a two integers n n n , m m m ( 1 ≤ n ≤ 500 1 \leq n \leq 500 1n500 , n ≤ m ≤ 1 0 6 n \leq m \leq 10^6 nm106 ) — the number of colours excluding the colour 0 0 0 and the length of the paper, respectively.

The second line contains m m m space separated integers c 1 , c 2 , … , c m c_1, c_2, \ldots, c_m c1,c2,,cm ( 1 ≤ c i ≤ n 1 \leq c_i \leq n 1cin ) — the colour visible on the segment [ i − 1 , i ] [i-1, i] [i1,i] after the process ends. It is guaranteed that for all j j j between 1 1 1 and n n n there is an index k k k such that c k = j c_k = j ck=j .

输出格式

Output a single integer — the number of ways Alice can perform the painting, modulo 998244353 998244353 998244353 .

样例 #1

样例输入 #1

3 3
1 2 3

样例输出 #1

5

样例 #2

样例输入 #2

2 3
1 2 1

样例输出 #2

1

样例 #3

样例输入 #3

2 3
2 1 2

样例输出 #3

0

样例 #4

样例输入 #4

7 7
4 5 1 6 2 3 7

样例输出 #4

165

样例 #5

样例输入 #5

8 17
1 3 2 2 7 8 2 5 5 4 4 4 1 1 6 1 1

样例输出 #5

20

提示

In the first example, there are 5 5 5 ways, all depicted in the figure below. Here, 0 0 0 is white, 1 1 1 is red, 2 2 2 is green and 3 3 3 is blue.

Below is an example of a painting process that is not valid, as in the second step the segment 1 3 is not single colour, and thus may not be repainted with colour 2 2 2 .

In the second example, Alice must first paint segment 0 3 with colour 1 1 1 and then segment 1 2 with colour 2 2 2 .

解题思路

前言

Luogu居然没有搜索题解,这对萌新十分不友好,所以就由本人来贡献一篇搜索题解吧。

作者建议

该题为这题的进阶版,建议先完成这题再完成本题。

正文

本题有 n < m n<m n<m 的情况,所以与 Short 版解法略有不同。( Short 版做法戳这里)

先将 c c c 数组去重,将连续的一段相同颜色并做一格,便于处理。相关代码如下:

for (int i = 1; i <= m; i++) {x = rd();if (x != a[len]) {a[++len] = x;}
}
对于 n = m n=m n=m 的情况:

与 Short 版处理方法相同。

对于 n < m n<m n<m 的情况:
  • 记录下进过前文所描述的去重操作后数组长度,若大于 2 × n 2 \times n 2×n 则一定无解,输出 0 ,并结束程序(否则会 RE ,本人亲测有效)。
  • 在 dfs 函数中,要进行一些改动。
    • 首先,要记录该区间编号最小的颜色在整个数组中第一次与最后一次出现的位置。若其中一个不在该区间内,则该区间无符合条件的涂色方法。

    • 对于符合条件的区间,进行如下操作(为了便于讲解,先放张图):
      图1

      • 对于该区间编号最小颜色第一次出现位置前于最后一次出现后的部分(红色部分),仍然照 Short 版操作:
      for (int i = l; i <= minid; i++) {tmp1 = (tmp1 + dfs(l, i - 1) * dfs(i, minidl - 1) % Mod) % Mod;
      }
      for (int i = minid; i <= r; i++) {tmp2 = (tmp2 + dfs(minidr + 1, i) * dfs(i + 1, r) % Mod) % Mod;
      }
      tong[l][r] = tmp1 * tmp2 % Mod;
      
      • 对于编号最小颜色出现位置的相隔部分(蓝色部分) ,继续放入 dfs 函数中处理,根据乘法原理,将它与 t o n g l , r tong_{l,r} tongl,r 相乘,就是该区间的方案数了。相关代码片段:
        for (int i = l; i <= r; i++) {if (a[i] == a[minid]) {if (top) {tong[l][r] = (tong[l][r] * (dfs(top + 1, i - 1) % Mod)) % Mod;}top = i;}}
      
  • 最后, t o n g 1 , l e n tong_{1,len} tong1,len 就是最终总方案数了。

AC Code

#include <bits/stdc++.h>
using namespace std;
#define int long long
inline int rd() {int x = 0, f = 1;char ch = getchar();while (ch < '0' || ch > '9') {if (ch == '-')f = -1;ch = getchar();}while (ch >= '0' && ch <= '9') {x = (x << 1) + (x << 3) + (ch ^ 48);ch = getchar();}return x * f;
}
inline void write(int x) {if (x < 0)x = ~x + 1, putchar('-');if (x > 9) write(x / 10);putchar(x % 10 + '0');
}
const int Mod = 998244353;
const int Maxn = 2200 + 5, Maxm = 2e6 + 5;
int n, m, a[Maxm];
bool vis[Maxm];
int tong[Maxn][Maxn]/*区块l~r的方案数*/, minid, L[Maxm], R[Maxm];
inline int dfs(int l, int r) {if (tong[l][r] != -1) {return tong[l][r];} else if (l >= r) {return tong[l][r] = 1;} else {int minid = l, tmp1 = 0, tmp2 = 0, top = 0, minidl, minidr;for (int i = l; i <= r; i++) {//求当前区块编号最小颜色if (a[i] < a[minid]) {minid = i;}}minidl = L[a[minid]];minidr = R[a[minid]];if (!(l <= minidl && r >= minidr) && !(r <  minidl && l > minidr)) {return tong[l][r] = 0;}for (int i = l; i <= minid; i++) {tmp1 = (tmp1 + dfs(l, i - 1) * dfs(i, minidl - 1) % Mod) % Mod;}for (int i = minid; i <= r; i++) {tmp2 = (tmp2 + dfs(minidr + 1, i) * dfs(i + 1, r) % Mod) % Mod;}tong[l][r] = tmp1 * tmp2 % Mod;for (int i = l; i <= r; i++) {if (a[i] == a[minid]) {if (top) {tong[l][r] = (tong[l][r] * (dfs(top + 1, i - 1) % Mod)) % Mod;}top = i;}}return tong[l][r];}
}
inline void work() {memset(tong, -1, sizeof(tong));int len = 0, tmp, x;n = rd();m = rd();for (int i = 1; i <= m; i++) {x = rd();if (x != a[len]) {a[++len] = x;}}if (len > 2 * n) {cout << 0 << endl;return;}for (int i = 1; i <= len; i++) {R[a[i]] = i;}for (int i = len; i >= 1; i--) {L[a[i]] = i;}for (int i = 1; i <= len; i++) {tong[i][i] = L[a[i]] == i && R[a[i]] == i;}dfs(1, len);write(tong[1][len] % Mod);
}
signed main() {ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);work();return 0;
}

不会就问问它(曾经的特邀嘉宾)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/629023.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Java设计模式之原型模式详解

Java设计模式之原型模式详解 大家好&#xff0c;我是免费搭建查券返利机器人赚佣金就用微赚淘客系统3.0的小编&#xff0c;也是冬天不穿秋裤&#xff0c;天冷也要风度的程序猿&#xff01;在今天的篇章中&#xff0c;我们将揭开Java设计模式中的另一面纱——原型模式。这个模式…

一、ArcGIS Pro SDK for Microsoft .NET 开发环境配置

ArcGIS Pro二次开发需要的工具&#xff1a; 1.Visual Studio 2.ArcGIS Pro SDK 一、Visual Studio安装 经过查阅资料&#xff0c;ArcGIS Pro3.0版本需要安装Visual Studio2022版&#xff0c;因为只有22版的才会有有ArcGIS Pro3.0以上版对应ArcGIS Pro SDK&#xff0c;因此&…

sqlserver->clickhouse迁移数据

网上看了很多cdc的方案&#xff0c;说实在的&#xff0c;flink也不好弄&#xff0c;主要是驱动。 flink sql中的方案&#xff0c;好不容易弄到了所有的驱动&#xff0c;结果&#xff0c;字段非常难搞&#xff0c;例如uuid对应flink的String&#xff0c;结果执行flink sql的时候…

vue的sync语法糖的使用

Vue的.sync语法糖是一个用于双向数据绑定的指令&#xff0c;可以在子组件中用来监听父组件传递下来的props的变化&#xff0c;并在需要的时候发出一个自定义事件来通知父组件进行数据更新。 在Vue中&#xff0c;.sync语法糖的使用方法如下&#xff1a; 在父组件中&#xff0c…

第十一部分 隐含规则 (一)

目录 一、使用隐含规则 二、隐含规则一览 1、编译 C 程序的隐含规则 2、编译 C程序的隐含规则 3、编译 Pascal 程序的隐含规则 4、编译 Fortran/Ratfor 程序的隐含规则 5、预处理 Fortran/Ratfor 程序的隐含规则 6、编译 Modula-2 程序的隐含规则 7、汇编和汇编预处理…

如何编译openssl的早期版本的共享库,如openssl 1.0

背景介绍 最近在为客户排查问题的时候&#xff0c;发现客户提供的日志是加密的&#xff0c;解密工具依赖到了openssl 1.0的共享库。可是手头没有这么老版本的openssl共享库。因此只好手动编译一个出来。 编译步骤 因为openssl 1.0是比较老的版本&#xff0c;很多系统上的库已…

常用Java代码-Java中的注解处理器(Annotation Processing)

注解处理器&#xff08;Annotation Processing&#xff09;是Java编译时的一种机制&#xff0c;它允许开发者在源代码上添加注解&#xff0c;并通过注解处理器生成源代码、报告编译器错误或警告、或者进行其他编译时操作。 在Java中&#xff0c;注解处理器通常使用Java的反射和…

新能源汽车智慧充电桩解决方案:智慧化综合管理与数字化高效运营

一、方案概述 TSINGSEE青犀&触角云新能源汽车智慧充电桩解决方案基于管理运营平台&#xff0c;覆盖业务与应用、数据传输与梳理、多端开发、搭建等模块&#xff0c;融合AI、5G、Wi-Fi 、移动支付等技术&#xff0c;实现充电基础设施由数字化向智能化演进&#xff0c;通过构…

【算法题】57. 插入区间

题目 给你一个 无重叠的 &#xff0c;按照区间起始端点排序的区间列表。 在列表中插入一个新的区间&#xff0c;你需要确保列表中的区间仍然有序且不重叠&#xff08;如果有必要的话&#xff0c;可以合并区间&#xff09;。 示例 1&#xff1a; 输入&#xff1a;intervals …

翻译: Pyenv管理Python版本从入门到精通一

你是否经常在管理系统上多个Python版本时遇到困难&#xff1f;这可能是一个艰巨的任务&#xff0c;尤其是在处理需要不同Python版本的不同项目时。 但别担心&#xff0c;有一个解决方案&#xff1a;pyenv。就像一个熟练的杂技演员&#xff0c;pyenv可以轻松处理多个Python版本…

Vue插槽(slot)的使用

Vue插槽(slot)是一种强大的功能&#xff0c;它允许您在组件模板中插入自定义内容。插槽可以让你更灵活地构建组件&#xff0c;以满足不同的使用场景。 使用说明&#xff1a; 定义插槽&#xff1a;在组件模板中&#xff0c;使用<slot>标签来定义插槽。使用插槽&#xff…

连接超时的问题

连接超时的问题 通用第三方工具连接超时 connect timeout 方案一&#xff1a; /etc/ssh/sshd_config node1上操作&#xff0c;图是错的 方案二&#xff1a; windows上Hosts文件域名解析有问题 比如&#xff1a; 192.168.xx.100 node1 192.168.xx.161 node1 两个都解析成node…

绝地求生:【PC】未授权程序使用行为的相关公告

各位玩家大家好&#xff0c; 最近闲游盒通过PUBG玩家社区收到了关于未授权程序的举报&#xff0c;举报称有人在游戏内使用了能测量玩家间的距离并辅助迫击炮射击的未授权辅助程序。为此&#xff0c;我们想就该事项向大家进行如下公告&#xff1a; 使用此类未授权程序的行为违反…

23/76-LeNet

LeNet 早期成功的神经网络。 先使用卷积层来学习图片空间信息。 然后使用全连接层转换到类别空间。 #In[]LeNet,上世纪80年代的产物,最初为了手写识别设计from d2l import torch as d2l import torch from torch import nn from torch.nn.modules.loss import CrossEntropyLos…

无法加载操作系统,原因是关键系统驱动程序丢失或包含错误

bcdboot c:\windows /l zh-cn 用这个命令解决了,没有进入时候蓝屏了,不知道为什么 问题 无法加载操作系统,原因是关键系统驱动程序丢失或包含错误上午因为有点事就没有像往常一样打开电脑,下午回到家休息了一会本来准备打开电脑开始我愉快地下午生活,没想到一个自动恢复给…

工业平板定制方案_基于联发科、紫光展锐平台的工业平板电脑方案

工业平板主板采用联发科MT6762平台方案&#xff0c;搭载Android 11.0操作系统&#xff0c; 主频最高2.0GHz&#xff0c;效能有大幅提升;采用12nm先进工艺&#xff0c;具有低功耗高性能的特点。 该工业平板主板搭载了IMG GE8320图形处理器&#xff0c;最高主频为680MHz, 支持108…

Java设计模式之访问者模式详解

Java设计模式之访问者模式详解 大家好&#xff0c;我是免费搭建查券返利机器人赚佣金就用微赚淘客系统3.0的小编&#xff0c;也是冬天不穿秋裤&#xff0c;天冷也要风度的程序猿&#xff01;今天&#xff0c;让我们一同踏上Java设计模式之旅&#xff0c;探索访问者模式&#x…

Flume 之自定义Sink

1、简介 前文我们介绍了 Flume 如何自定义 Source&#xff0c; 并进行案例演示&#xff0c;本文将接着前文&#xff0c;自定义Sink&#xff0c;在这篇文章中&#xff0c;将使用自定义 Source 和 自定义的 Sink 实现数据传输&#xff0c;让大家快速掌握Flume这门技术。 2、自定…

JVM与HotSpot

JVM和HotSpot 1、概念 JVM是虚拟机的规范&#xff0c;HotSpot是jvm的具体实现 HotSpot包括一个解释器和两个编译器&#xff08;client 和 server&#xff0c;二选一的&#xff09;&#xff0c;解释与编译混合执行模式&#xff0c;默认启动解释执行。 编译器&#xff1a;java源…

121_买卖股票的最佳时机

描述 给定一个数组 prices &#xff0c;它的第 i 个元素 prices[i] 表示一支给定股票第 i 天的价格。 你只能选择某一天买入这只股票&#xff0c;并选择在未来的某一个不同的日子卖出该股票。设计一个算法来计算你所能获取的最大利润。 返回你可以从这笔交易中获取的最大利润。…