[CF1264D]Beautiful Bracket Sequence
题目描述
This is the hard version of this problem. The only difference is the limit of $ n $ - the length of the input string. In this version, $ 1 \leq n \leq 10^6 $ .
Let's define a correct bracket sequence and its depth as follow:
- An empty string is a correct bracket sequence with depth $ 0 $ .
- If "s" is a correct bracket sequence with depth $ d $ then "(s)" is a correct bracket sequence with depth $ d + 1 $ .
- If "s" and "t" are both correct bracket sequences then their concatenation "st" is a correct bracket sequence with depth equal to the maximum depth of $ s $ and $ t $ .
For a (not necessarily correct) bracket sequence $ s $ , we define its depth as the maximum depth of any correct bracket sequence induced by removing some characters from $ s $ (possibly zero). For example: the bracket sequence $ s = $ "())(())" has depth $ 2 $ , because by removing the third character we obtain a correct bracket sequence "()(())" with depth $ 2 $ .
Given a string $ a $ consists of only characters '(', ')' and '?'. Consider all (not necessarily correct) bracket sequences obtained by replacing all characters '?' in $ a $ by either '(' or ')'. Calculate the sum of all the depths of all these bracket sequences. As this number can be large, find it modulo $ 998244353 $ .
Hacks in this problem can be done only if easy and hard versions of this problem was solved.
输入格式
The only line contains a non-empty string consist of only '(', ')' and '?'. The length of the string is at most $ 10^6 $ .
考虑 \(O(n^2)\)
先尝试求出深度。一个括号序列我们最后一定可以把他删成 ((((....)))) 的形式,也就是在括号序列中找到一个位置 \(i\) , \(s_i=\)'(' 且 \(l\) 左边的左括号数量等于其右边的右括号数量。枚举这个 \(i\) 在哪。设 \(i\) 前面有 \(l_i\) 个左括号, \(p_i\) 个问号,后面有 \(r_i\) 个右括号,\(q_i\) 个问号。
\(\begin{aligned}
&\sum\limits_{i=1}^n\sum\limits_{j=0}^{p_i}(l_i+j)\binom{p_i}{j}\binom{q_i}{j+l_i-r_i}\\&=\sum\limits_{i=1}^nl_i\sum\limits_{j=0}^{p_i}\binom{p_i}{j}\binom{q_i}{q_i-j-l_i+r_i}+\sum\limits_{i=1}^n\sum\limits_{j=0}^{p_i}j\binom{p_i}{j}\binom{q_i}{q_i-j-l_i+r_i}\\&=l_i\binom{p_i+q_i}{q_i-l_i+r_i}+\sum\limits_{i=0}^{p_i}p_i\binom{p_i-1}{j-1}\binom{q_i}{q_i-j-l_i+r_i}
\\&=l_i\binom{p_i+q_i}{q_i-l_i+r_i}+p_i\binom{p_i+q_i-1}{q_i-l_i+r_i-1}
\end{aligned}\)
#include<bits/stdc++.h>
using namespace std;
const int N=2e6+5,P=998244353;
char s[N];
int l[N],p[N],r[N],q[N],jc[N],iv[N],inv[N],n,ans;
int C(int n,int m)
{
if(n<m||m<0)
return 0;
return jc[n]*1LL*iv[m]%P*iv[n-m]%P;
}
int main()
{
scanf("%s",s+1),n=strlen(s+1);
for(int i=1;s[i];i++)
{
l[i]=l[i-1]+(s[i]=='(');
p[i]=p[i-1]+(s[i]=='?');
}
for(int i=n;i;i--)
{
r[i]=r[i+1]+(s[i]==')');
q[i]=q[i+1]+(s[i]=='?');
}
jc[0]=jc[1]=iv[0]=iv[1]=inv[1]=1;
for(int i=2;i<N;i++)
{
jc[i]=1LL*jc[i-1]*i%P;
inv[i]=1LL*(P-P/i)*inv[P%i]%P;
iv[i]=1LL*iv[i-1]*inv[i]%P;
}
for(int i=1;i<=n;i++)
{
(ans+=(1LL*l[i]*C(p[n],q[i+1]-l[i]+r[i+1])+1LL*p[i]*C(p[n]-1,q[i+1]-l[i]+r[i+1]-1))%P)%=P;
}
printf("%d",ans);
}
[CF1264D]Beautiful Bracket Sequence的更多相关文章
- CF1264D2 Beautiful Bracket Sequence
我们枚举每两个字符的空档,统计一个空档左边有 \(l\) 个左括号, 右边有 \(r\) 个右括号,左边有 \(u\) 个问号,右边有 \(v\) 个问号. 则对于 \(p\) 的答案 \(ans_p ...
- CF1264D2 Beautiful Bracket Sequence (hard version)
考虑\(D1\)的\(O(n^2)\),我们直接进行组合处理. 考虑在\(p\)这个位置,左边有\(l\)个(,右边有\(r\)个),左边有\(l\)个问号,右边有\(r\)个问号. 这个位置的贡献为 ...
- CF1264D1 Beautiful Bracket Sequence (easy version)
考虑在一个确定的括号序列中,我们可以枚举中间位置,按左右最长延伸出去的答案计算. 我们很自然的思考,我们直接维护左右两边,在删除一些字符后能够延伸的最长长度. 我们设\(f_{i,j}\)为\(i\) ...
- Codeforces 1264D - Beautiful Bracket Sequence(组合数学)
Codeforces 题面传送门 & 洛谷题面传送门 首先对于这样的题目,我们应先考虑如何计算一个括号序列 \(s\) 的权值.一件非常显然的事情是,在深度最深的.是原括号序列的子序列的括号序 ...
- UESTC 1546 Bracket Sequence
Bracket Sequence Time Limit: 3000MS Memory Limit: 65536KB 64 ...
- CF#138 div 1 A. Bracket Sequence
[#138 div 1 A. Bracket Sequence] [原题] A. Bracket Sequence time limit per test 2 seconds memory limit ...
- CodeForces 670E Correct Bracket Sequence Editor(list和迭代器函数模拟)
E. Correct Bracket Sequence Editor time limit per test 2 seconds memory limit per test 256 megabytes ...
- Educational Codeforces Round 4 C. Replace To Make Regular Bracket Sequence 栈
C. Replace To Make Regular Bracket Sequence 题目连接: http://www.codeforces.com/contest/612/problem/C De ...
- Codeforces Beta Round #5 C. Longest Regular Bracket Sequence 栈/dp
C. Longest Regular Bracket Sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.c ...
- (中等) UESTC 94 Bracket Sequence,线段树+括号。
There is a sequence of brackets, which supports two kinds of operations. we can choose a interval [l ...
随机推荐
- 结果过滤器—MVC项目中结果过滤器(Result Filter)使用
一.什么是结果过滤器? 结果过滤器(ResultFilter),是对执行的Action结果进行处理的一种AOP思想,适用于任何需要直接环绕 View 或格式化处理的逻辑.结果过滤器可以替换或更改 Ac ...
- GitHub Deskhub使用
(适合已经知道git是啥但是还不太熟到同学看-) GitHub deskhub就是一个图形化的github管理工具啦,比起来命令行使用舒服100倍哈哈哈- 链接:https://desktop.git ...
- [ABC128E] Roadwork
2023-01-14 题目 题目传送门 翻译 翻译 难度&重要性(1~10):4 题目来源 AtCoder 题目算法 区间覆盖,线段树,双堆 解题思路 可以将问题转化为区间覆盖问题和单点查询问 ...
- from my mac
hello
- 5.go语言函数提纲
1 本篇前瞻 前端时间的繁忙,未曾更新go语言系列.由于函数非常重要,为此将本篇往前提一提,另外补充一些有关go新版本前面遗漏的部分. 需要恭喜你的事情是本篇学完,go语言中基础部分已经学完一半,这意 ...
- 【matplotlib 实战】--直方图
直方图,又称质量分布图,用于表示数据的分布情况,是一种常见的统计图表. 一般用横轴表示数据区间,纵轴表示分布情况,柱子越高,则落在该区间的数量越大.构建直方图时,首先首先就是对数据划分区间,通俗的说即 ...
- C#学习笔记---异常捕获和变量
异常捕获 使用异常捕获可以捕获出现异常的代码块,防止因为异常抛出造成的程序卡死的情况发生. try{}catch{}finally{}结构 //异常捕获 try { string str=Consol ...
- PowerShell 多平台一键生成 Blu-ray Live 分轨
前言 本人 n 年前的需求,需要自动化的将 Blu-ray Live 转换成 FLAC 格式的文件(自听&发种). ️ 注意:本脚本仅支持输出 flac ! 前提 计算机安装有 PowerSh ...
- WPF性能优化:Freezable 对象
Freezable是WPF中一个特殊的基类,用于创建可以冻结(Freeze)的可变对象.冻结一个对象意味着将其状态设置为只读,从而提高性能并允许在多线程环境中共享对象. Freezable的应用 我们 ...
- 计算机的数值转化与网络的IP地址分类与地址划分
数值转换 数字系统由来 远古时代是没有数字系统非位置化数字系统: 罗马数字 (I-1.II-2.III-3.IV-4.V-5.VI-6.VII-7.VIII-8.IX-9.X-10) 位置话数字化系统 ...