CF1264D2.Beautiful Bracket Sequence (hard version)

省选/NOI-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

This is the hard version of this problem. The only difference is the limit of nn - the length of the input string. In this version, 1≤n≤1061 \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 00.
  • If "s" is a correct bracket sequence with depth dd then "(s)" is a correct bracket sequence with depth d+1d + 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 ss and tt.

For a (not necessarily correct) bracket sequence ss, we define its depth as the maximum depth of any correct bracket sequence induced by removing some characters from ss (possibly zero). For example: the bracket sequence $s = $"())(())" has depth 22, because by removing the third character we obtain a correct bracket sequence "()(())" with depth 22.

Given a string aa consists of only characters '(', ')' and '?'. Consider all (not necessarily correct) bracket sequences obtained by replacing all characters '?' in aa by either '(' or ')'. Calculate the sum of all the depths of all these bracket sequences. As this number can be large, find it modulo 998244353998244353.

Hacks in this problem can be done only if easy and hard versions of this problem was solved.

这是本题的困难版本。唯一的区别在于 nn(输入字符串的长度)的限制。在本版本中,1≤n≤1061 \leq n \leq 10^6。

我们定义合法括号序列及其深度如下:

  • 空字符串是一个深度为 00 的合法括号序列;
  • 若字符串 “s” 是一个深度为 dd 的合法括号序列,则 “(s)” 是一个深度为 d+1d + 1 的合法括号序列;
  • 若字符串 “s” 和 “t” 均为合法括号序列,则它们的连接 “st” 也是一个合法括号序列,其深度等于 ss 与 tt 深度的最大值。

对于一个(未必合法的)括号序列 ss,我们定义其深度为:通过从 ss 中删除若干字符(可以为零个)所能得到的所有合法括号序列中,最大的深度。例如:括号序列 $s = $"())(())" 的深度为 22,因为删去第三个字符后可得到合法括号序列 "()(())",其深度为 22。

给定一个仅由字符 '(', ')' 和 '?' 组成的字符串 aa。考虑所有将 aa 中每个 '?' 替换为 '(' 或 ')' 后得到的(未必合法的)括号序列。计算所有这些括号序列的深度之和。由于该数可能很大,请对 998244353998244353 取模。

本题的 hack 操作仅在本题的简单版本与困难版本均已被解决时才允许进行。

输入格式

The only line contains a non-empty string consist of only '(', ')' and '?'. The length of the string is at most 10610^6.

唯一一行包含一个非空字符串,该字符串仅由 '('、')' 和 '?' 组成。字符串长度至多为 10610^6。

输出格式

Print the answer modulo 998244353998244353 in a single line.

在一行中输出答案对 998244353998244353 取模的结果。

输入输出样例

  • 输入#1

    ??

    输出#1

    1
  • 输入#2

    (?(?))

    输出#2

    9

说明/提示

In the first test case, we can obtain 44 bracket sequences by replacing all characters '?' with either '(' or ')':

  • "((". Its depth is 00;
  • "))". Its depth is 00;
  • ")(". Its depth is 00;
  • "()". Its depth is 11.

So, the answer is 1=0+0+0+11 = 0 + 0 + 0 + 1.

In the second test case, we can obtain 44 bracket sequences by replacing all characters '?' with either '(' or ')':

  • "(((())". Its depth is 22;
  • "()()))". Its depth is 22;
  • "((()))". Its depth is 33;
  • "()(())". Its depth is 22.

So, the answer is 9=2+2+3+29 = 2 + 2 + 3 + 2.

在第一个测试用例中,我们可以通过将所有字符 '?' 替换为 '(' 或 ')',得到 44 个括号序列:

  • "((". 其深度为 00;
  • "))". 其深度为 00;
  • ")(". 其深度为 00;
  • "()". 其深度为 11。

因此,答案为 1=0+0+0+11 = 0 + 0 + 0 + 1。

在第二个测试用例中,我们可以通过将所有字符 '?' 替换为 '(' 或 ')',得到 44 个括号序列:

  • "(((())". 其深度为 22;
  • "()()))". 其深度为 22;
  • "((()))". 其深度为 33;
  • "()(())". 其深度为 22。

因此,答案为 9=2+2+3+29 = 2 + 2 + 3 + 2。

输入解题思路,AI测评打分。不知道怎么写?

首页