题目描述

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

  1. CF1264D2 Beautiful Bracket Sequence

    我们枚举每两个字符的空档,统计一个空档左边有 \(l\) 个左括号, 右边有 \(r\) 个右括号,左边有 \(u\) 个问号,右边有 \(v\) 个问号. 则对于 \(p\) 的答案 \(ans_p ...

  2. CF1264D2 Beautiful Bracket Sequence (hard version)

    考虑\(D1\)的\(O(n^2)\),我们直接进行组合处理. 考虑在\(p\)这个位置,左边有\(l\)个(,右边有\(r\)个),左边有\(l\)个问号,右边有\(r\)个问号. 这个位置的贡献为 ...

  3. CF1264D1 Beautiful Bracket Sequence (easy version)

    考虑在一个确定的括号序列中,我们可以枚举中间位置,按左右最长延伸出去的答案计算. 我们很自然的思考,我们直接维护左右两边,在删除一些字符后能够延伸的最长长度. 我们设\(f_{i,j}\)为\(i\) ...

  4. Codeforces 1264D - Beautiful Bracket Sequence(组合数学)

    Codeforces 题面传送门 & 洛谷题面传送门 首先对于这样的题目,我们应先考虑如何计算一个括号序列 \(s\) 的权值.一件非常显然的事情是,在深度最深的.是原括号序列的子序列的括号序 ...

  5. UESTC 1546 Bracket Sequence

                                        Bracket Sequence Time Limit: 3000MS   Memory Limit: 65536KB   64 ...

  6. CF#138 div 1 A. Bracket Sequence

    [#138 div 1 A. Bracket Sequence] [原题] A. Bracket Sequence time limit per test 2 seconds memory limit ...

  7. CodeForces 670E Correct Bracket Sequence Editor(list和迭代器函数模拟)

    E. Correct Bracket Sequence Editor time limit per test 2 seconds memory limit per test 256 megabytes ...

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

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

  10. (中等) UESTC 94 Bracket Sequence,线段树+括号。

    There is a sequence of brackets, which supports two kinds of operations. we can choose a interval [l ...

随机推荐

  1. Docker 安装Redis 无法使用配置文件设置密码问题

    背景 最近开发需要使用各种组件,如果都到开发机上安装,会占用电脑资源较多.所以使用docker容器来安装这些组件.例如 redis .mongodb.mysql.rabitmq.elasticsear ...

  2. QA|20211013|SecureCRT:如图,有很多^,中文显示有问题,乱码,如何解决

    Q1:如图,有很多^,中文显示有问题,乱码,如何解决 Q2:securecrt的vi展示有问题:少很多字.有很多^M和^,光标无法移动到最右侧 A: 首先检查当前编码格式: 1 echo $LANG ...

  3. Android 调试桥 (adb) 使用教程/示例

    sidebar: auto Android 调试桥 (adb) Android 调试桥 (adb) 是一种功能多样的命令行工具,可让您与设备进行通信.adb 命令可用于执行各种设备操作,例如安装和调试 ...

  4. KRPano插件一键解密大师 支持最新版KRPano XML/JS解密 ,支持分析下载静态/动态网站资源

    KRPano插件一键解密大师,可以一键解密KRPano的XML/JS插件,并可以分析下载静态和动态网站的所有资源.软件下载安装即可使用,解密仅需鼠标一键点击即可,无需配置任何开发环境,方便全景开发人员 ...

  5. 【SpringBoot实战】开发入门--快速创建springboot程序

    前言 本片博客记录快速创建springboot工程的使用spring initializr创建.开发环境JDK1.8.IDEA.maven. SpringBoot 优点 可快速构建spring应用 直 ...

  6. 2018-D

    2018-D 新建数据库 test0317,目录为考试目录,并在完成建表后备份 1.建表: use [test0317]; create table [STD_INFO]( [std_id] int ...

  7. 「hackerrank - 101hack43」K-Inversion Permutations

    link. 原问题即:请你给出不同的序列 \(\{a_n\}\) 的数量,满足 \(0\leqslant a_i<i\),且 \(\sum a_i=k\). 那么写出 \({a_n}\) 的 o ...

  8. MySQL系列之优化——1.优化哲学、2. 优化工具的使用、3. 优化思路分解、4. MySQL参数优化测试、5.1 参数优化、6. 参数优化结果、7. 锁的监控及处理、8. 主从优化

    文章目录 1.优化哲学 1.1 为什么优化? 1.2 优化风险 1.3 谁参与优化 1.4 优化方向 1.5 优化的范围及思路 优化效果和成本的评估: 2. 优化工具的使用 2.1 系统层面的 2.1 ...

  9. GPT-4 API waitlist

    Skip to main content Site Navigation GPT-4 API waitlist We're making GPT-4 available as an API for d ...

  10. Tarjan强连通分量详解

    1.简介: 在阅读下列内容之前,请务必了解 图论相关概念 中的基础部分. 强连通的定义是:有向图 G 强连通是指,G 中任意两个结点连通. 强连通分量(Strongly Connected Compo ...