\(\mathcal{Description}\)

  OurTeam & OurOJ.

  给定一棵 \(n\) 个顶点的树,每个顶点标有字符 ()。将从 \(u\) 到 \(v\) 的简单有向路径上的字符串成括号序列,记其正则匹配的子串个数为 \(\operatorname{ans}(u,v)\)。求:

\[\sum_{u=1}^n\sum_{v=1}^n\operatorname{ans}(u,v)\bmod998244353
\]

  \(n\le2\times10^5\)。

\(\mathcal{Solution}\)

  可以先回忆一下括号树嗷。

  来看看链怎么做 owo,现有结点按 \(1\sim n\) 从左到右编号,记 \(s(i,j)\) 表示从 \(i\) 到 \(j\) 串成的括号序列。令 \(\operatorname{match}(i)\) 为最大的 \(j<i\),满足 \(s(j,i)\) 正则匹配。定义状态 \(f(i)\) 表示 \(s(1,i)\) 中,以 \(i\) 结尾的正则子串贡献。那么:

\[f(i)=f(\operatorname{match}(i)-1)+\operatorname{match}(i)
\]

  即,先保证最短的以 \(i\) 结尾的正则,起点就可以在前面任选了。而事实上,终点也能任选,那么答案为:

\[\sum_{i=1}^nf(i)(n-i+1)
\]

  需要正反分别做一次嗷。


  那么,搬到树上,一个正则会贡献多少次呢?如图(混V的请告诉我背景是谁吖~):

  

  不难发现,\((u,v)\)(或 \((v,u)\))若正则匹配,则它对答案的贡献为 \(siz_u\times siz_v\)。

  好啦,开始 \(\text{DSU on Tree}\) 吧!

  注意到我们只关心一些子树大小的信息,所以这样设计状态:

  • \(f(u,i)\) 表示 \(u\) 子树内某一点 \(v\) 到 \(u\),构成的串有 \(i\) 个 ( 失配,且所有 ) 被匹配的 \(siz_v\) 之和。
  • \(g(u,i)\) 表示 \(u\) 到其子树内某一点 \(v\),构成的串有 \(i\) 个 ) 失配,且所有 ( 被匹配的 \(siz_v\) 之和。

  好奇怪的定义 qwq,该怎样理解呢?

  考虑一条 \(v-u-w\) 的有向树链,其中 \(u\) 是 \(v\) 与 \(w\) 的 \(\text{LCA}\)。若 \(v-u\) 长成 (...((...(,\(u-w\) 长成 ...)...)...)),其中 ... 是已匹配的括号。可见 \(v-u-w\) 是正则匹配的,而这正对应了我们的状态 \(f(u,4)\) 和 \(g(u,4)\)!

  接着考虑轻重儿子信息对答案的贡献,如图:

  \(\text{DFS}\) 轻儿子的时候,用线段树动态维护前缀的 ),后缀的 ( 是否出现失配的情况,若一个点加入后不存在失配,则用 DP 信息更新答案。合并信息时类似,但加入最后一个点 \(u\) 时:

  • \(s_u=\texttt{'('}\),\(f(u,i+1)=f(v,i)\),\(g(u,i-1)=f(v,i)\)。

  • \(s_u=\texttt{')'}\),\(f(u,i-1)=f(v,i)\),\(g(u,i+1)=f(v,i)\)。

  这……总不可能 \(\mathcal O(siz)\) 地遍历第二维吧 qwq。事实上,发现这只是一个单纯的数组位移,初始时开两倍数组,用一个指针指向数组实际的 \(0\) 号为即可 \(\mathcal O(1)\) 实现了。

  以上两幅配图来自 Lucky_Glass 的题解

\(\mathcal{Code}\)

#include <cstdio>

const int MAXN = 2e5, MOD = 998244353;
int n, ecnt, head[MAXN + 5], siz[MAXN + 5], son[MAXN + 5];
int ans, aryf[MAXN * 2 + 5], aryg[MAXN * 2 + 5], *f, *g;
char s[MAXN + 5]; inline int add ( int a, const int b ) { return ( a += b ) < MOD ? a : a - MOD; }
inline int sub ( int a, const int b ) { return ( a -= b ) < 0 ? a + MOD : a; }
inline int mul ( long long a, const int b ) { return ( a *= b ) < MOD ? a : a % MOD; } inline int rint () {
int x = 0; char s = getchar ();
for ( ; s < '0' || '9' < s; s = getchar () );
for ( ; '0' <= s && s <= '9'; s = getchar () ) x = x * 10 + ( s ^ '0' );
return x;
} struct Edge { int to, nxt; } graph[MAXN + 5]; inline void link ( const int s, const int t ) {
graph[++ ecnt] = { t, head[s] };
head[s] = ecnt;
} struct SegmentTree {
int mn[MAXN * 2 + 5], tag[MAXN * 2 + 5]; inline int id ( const int l, const int r ) { return ( l + r ) | ( l != r ); }
inline void pushad ( const int l, const int r, const int v ) {
int rt = id ( l, r );
mn[rt] += v, tag[rt] += v;
}
inline void pushdn ( const int l, const int r ) {
int rt = id ( l, r ), mid = l + r >> 1;
if ( ! tag[rt] ) return ;
pushad ( l, mid, tag[rt] ), pushad ( mid + 1, r, tag[rt] );
tag[rt] = 0;
}
inline void pushup ( const int l, const int r ) {
int rt = id ( l, r ), mid = l + r >> 1, lc = id ( l, mid ), rc = id ( mid + 1, r );
mn[rt] = mn[lc] < mn[rc] ? mn[lc] : mn[rc];
}
inline void update ( const int l, const int r, const int ul, const int ur, const int v ) {
if ( ul <= l && r <= ur ) return pushad ( l, r, v );
int mid = l + r >> 1; pushdn ( l, r );
if ( ul <= mid ) update ( l, mid, ul, ur, v );
if ( mid < ur ) update ( mid + 1, r, ul, ur, v );
pushup ( l, r );
}
inline bool check () { return mn[id ( 1, n )] >= 0; }
} preT, sufT; // ((... and ...)), preT->g, sufT->f. inline void init ( const int u ) {
siz[u] = 1;
for ( int i = head[u], v; i; i = graph[i].nxt ) {
init ( v = graph[i].to ), siz[u] += siz[v];
if ( siz[son[u]] < siz[v] ) son[u] = v;
}
} inline void update ( const int u, const int dep, const int k ) {
preT.update ( 1, n, 1, dep, s[u] == '(' ? k : -k );
sufT.update ( 1, n, 1, dep, s[u] == ')' ? k : -k );
} inline void calc ( const int u, int cnt, const int dep ) {
cnt += s[u] == ')' ? 1 : -1, update ( u, dep, 1 );
if ( sufT.check () ) ans = add ( ans, mul ( siz[u], f[cnt] ) );
if ( preT.check () ) ans = add ( ans, mul ( siz[u], g[-cnt] ) );
for ( int i = head[u]; i; i = graph[i].nxt ) calc ( graph[i].to, cnt, dep + 1 );
update ( u, dep, -1 );
} inline void coll ( const int u, int cnt, const int dep ) {
cnt += s[u] == '(' ? 1 : -1, update ( u, dep, -1 );
if ( sufT.check () ) f[cnt] = add ( f[cnt], siz[u] );
if ( preT.check () ) g[-cnt] = add ( g[-cnt], siz[u] );
for ( int i = head[u]; i; i = graph[i].nxt ) coll ( graph[i].to, cnt, dep + 1 );
update ( u, dep, 1 );
} inline void solve ( const int u, const bool keep ) {
for ( int i = head[u], v; i; i = graph[i].nxt ) {
if ( ( v = graph[i].to ) ^ son[u] ) {
solve ( v, false );
}
}
if ( son[u] ) solve ( son[u], true );
if ( s[u] == '(' ) ans = add ( ans, mul ( g[1], n - siz[son[u]] ) );
if ( s[u] == ')' ) ans = add ( ans, mul ( f[1], n - siz[son[u]] ) );
for ( int i = head[u], v; i; i = graph[i].nxt ) {
if ( ( v = graph[i].to ) ^ son[u] ) {
*f = add ( *f, n - siz[v] ), g[0] = add ( *g, n - siz[v] );
update ( u, 1, 1 );
calc ( v, s[u] == ')' ? 1 : -1, 2 );
*f = sub ( *f, n - siz[v] ), g[0] = sub ( *g, n - siz[v] );
update ( u, 1, -1 );
coll ( v, 0, 1 );
}
}
if ( s[u] == '(' ) *f = add ( *f, siz[u] ), -- f, *g ++ = 0;
if ( s[u] == ')' ) *g = add ( *g, siz[u] ), -- g, *f ++ = 0;
if ( ! keep ) {
for ( int i = 0; i <= siz[u]; ++ i ) f[i] = g[i] = 0;
f = aryf + n, g = aryg + n;
}
} int main () {
scanf ( "%d %s", &n, s + 1 );
for ( int i = 2; i <= n; ++ i ) link ( rint (), i );
init ( 1 );
f = aryf + n, g = aryg + n;
solve ( 1, true );
printf ( "%d\n", ans );
return 0;
}

\(\mathcal{Update}\)

  然后你就发现……只需要维护前缀、后缀最大值,与当前前缀、后缀和比较就砍掉 \(\log\) 了 owo!

#include <cstdio>

const int MAXN = 2e5, MOD = 998244353;
int n, ecnt, head[MAXN + 5], siz[MAXN + 5], son[MAXN + 5];
int ans, aryf[MAXN * 2 + 5], aryg[MAXN * 2 + 5], *f, *g;
char s[MAXN + 5]; inline int add ( int a, const int b ) { return ( a += b ) < MOD ? a : a - MOD; }
inline int sub ( int a, const int b ) { return ( a -= b ) < 0 ? a + MOD : a; }
inline int mul ( long long a, const int b ) { return ( a *= b ) < MOD ? a : a % MOD; }
inline void chkmax ( int& a, const int b ) { if ( a < b ) a = b; } inline int rint () {
int x = 0; char s = getchar ();
for ( ; s < '0' || '9' < s; s = getchar () );
for ( ; '0' <= s && s <= '9'; s = getchar () ) x = x * 10 + ( s ^ '0' );
return x;
} struct Edge { int to, nxt; } graph[MAXN + 5]; inline void link ( const int s, const int t ) {
graph[++ ecnt] = { t, head[s] };
head[s] = ecnt;
} inline void init ( const int u ) {
siz[u] = 1;
for ( int i = head[u], v; i; i = graph[i].nxt ) {
init ( v = graph[i].to ), siz[u] += siz[v];
if ( siz[son[u]] < siz[v] ) son[u] = v;
}
} inline void calc ( const int u, int cnt, int pre, int suf ) {
cnt += s[u] == ')' ? 1 : -1;
chkmax ( pre, -cnt ), chkmax ( suf, cnt );
if ( suf == cnt ) ans = add ( ans, mul ( siz[u], f[cnt] ) );
if ( pre == -cnt ) ans = add ( ans, mul ( siz[u], g[-cnt] ) );
for ( int i = head[u]; i; i = graph[i].nxt ) calc ( graph[i].to, cnt, pre, suf );
} inline void coll ( const int u, int cnt, int pre, int suf ) {
cnt += s[u] == '(' ? 1 : -1;
chkmax ( pre, -cnt ), chkmax ( suf, cnt );
if ( suf == cnt ) f[cnt] = add ( f[cnt], siz[u] );
if ( pre == -cnt ) g[-cnt] = add ( g[-cnt], siz[u] );
for ( int i = head[u]; i; i = graph[i].nxt ) coll ( graph[i].to, cnt, pre, suf );
} inline void solve ( const int u, const bool keep ) {
for ( int i = head[u], v; i; i = graph[i].nxt ) {
if ( ( v = graph[i].to ) ^ son[u] ) {
solve ( v, false );
}
}
if ( son[u] ) solve ( son[u], true );
if ( s[u] == '(' ) ans = add ( ans, mul ( g[1], n - siz[son[u]] ) );
if ( s[u] == ')' ) ans = add ( ans, mul ( f[1], n - siz[son[u]] ) );
int pre = s[u] == '(' ? 1 : -1, suf = s[u] == ')' ? 1 : -1;
for ( int i = head[u], v; i; i = graph[i].nxt ) {
if ( ( v = graph[i].to ) ^ son[u] ) {
*f = add ( *f, n - siz[v] ), g[0] = add ( *g, n - siz[v] );
calc ( v, s[u] == ')' ? 1 : -1, pre, suf );
*f = sub ( *f, n - siz[v] ), g[0] = sub ( *g, n - siz[v] );
coll ( v, 0, 0, 0 );
}
}
if ( s[u] == '(' ) *f = add ( *f, siz[u] ), -- f, *g ++ = 0;
if ( s[u] == ')' ) *g = add ( *g, siz[u] ), -- g, *f ++ = 0;
if ( ! keep ) {
for ( int i = 0; i <= siz[u]; ++ i ) f[i] = g[i] = 0;
f = aryf + n, g = aryg + n;
}
} int main () {
scanf ( "%d %s", &n, s + 1 );
for ( int i = 2; i <= n; ++ i ) link ( rint (), i );
init ( 1 );
f = aryf + n, g = aryg + n;
solve ( 1, true );
printf ( "%d\n", ans );
return 0;
}

Solution -「LOCAL」大括号树的更多相关文章

  1. Solution -「LOCAL」Drainage System

    \(\mathcal{Description}\)   合并果子,初始果子的权值在 \(1\sim n\) 之间,权值为 \(i\) 的有 \(a_i\) 个.每次可以挑 \(x\in[L,R]\) ...

  2. Solution -「LOCAL」Burning Flowers

      灼之花好评,条条生日快乐(假装现在 8.15)! \(\mathcal{Description}\)   给定一棵以 \(1\) 为根的树,第 \(i\) 个结点有颜色 \(c_i\) 和光亮值 ...

  3. Solution -「LOCAL」画画图

    \(\mathcal{Description}\)   OurTeam.   给定一棵 \(n\) 个点的树形随机的带边权树,求所有含奇数条边的路径中位数之和.树形生成方式为随机取不连通两点连边直到全 ...

  4. Solution -「LOCAL」ZB 平衡树

    \(\mathcal{Description}\)   OurOJ.   维护一列二元组 \((a,b)\),给定初始 \(n\) 个元素,接下来 \(m\) 次操作: 在某个位置插入一个二元组: 翻 ...

  5. Solution -「LOCAL」人口迁徙

    \(\mathcal{Description}\)   \(n\) 个点,第 \(i\) 个点能走向第 \(d_i\) 个点,但从一个点出发至多走 \(k\) 步.对于每个点,求有多少点能够走到它. ...

  6. Solution -「LOCAL」「cov. HDU 6864」找朋友

    \(\mathcal{Description}\)   Link.(几乎一致)   给定 \(n\) 个点 \(m\) 条边的仙人掌和起点 \(s\),边长度均为 \(1\).令 \(d(u)\) 表 ...

  7. Solution -「LOCAL」模板

    \(\mathcal{Description}\)   OurOJ.   给定一棵 \(n\) 个结点树,\(1\) 为根,每个 \(u\) 结点有容量 \(k_u\).\(m\) 次操作,每次操作 ...

  8. Solution -「LOCAL」割海成路之日

    \(\mathcal{Description}\)   OurOJ.   给定 \(n\) 个点的一棵树,有 \(1,2,3\) 三种边权.一条简单有向路径 \((s,t)\) 合法,当且仅当走过一条 ...

  9. Solution -「LOCAL」二进制的世界

    \(\mathcal{Description}\)   OurOJ.   给定序列 \(\{a_n\}\) 和一个二元运算 \(\operatorname{op}\in\{\operatorname{ ...

随机推荐

  1. sqlserver - 判断字段是否是纯数字

    PATINDEX('%[^0-9|.|-|+]%',w.waterMeterNo)=0 如 SELECT w.* FROM [dbo].[waterMeterInfo] w where isnull( ...

  2. win 10 遇到某文件一直在占用导致无法关闭,或者去任务管理器找不到服务怎么办?具体解决

    1. 打开 cmd 指令框 ,输入 perfmon 回车 就会出来这个 点击  打开资源监视器, 在句柄搜索框搜索 那个占用资源的文件或软件关键词 ,如下 搜索酷狗 将有关的选项,右键选中后 打开菜单 ...

  3. FileReader()读取文件、图片上传预览

    前言 FileReader 对象允许Web应用程序异步读取存储在用户计算机上的文件(或原始数据缓冲区)的内容,使用 File 或 Blob 对象指定要读取的文件或数据. 其中File对象可以是来自用户 ...

  4. vmware快速扩容虚拟磁盘

    在使用vmware进行虚拟化的时候,会遇到虚拟磁盘不够用的情况,以前的办法都是使用lvm进行管理扩容,目前在linux上可以实现快速扩容了,具体方法如下: 该方法参考阿里云在线扩容文档:文档地址 其中 ...

  5. Java传递变量和对象的区别

    传递对象 public class Demo03 { //引用传递:(实际上还是值传递)对于引用数据类型来说,传递的则是地址的副本(对象的地址).但由于地址副本和原来的类似,因此传递过去后形参也只想同 ...

  6. 【刷题-LeetCode】150 Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...

  7. golang中结构体和结构体指针的内存管理

    p1是结构体,p2是结构体指针. 2. 声明并赋值结构体和结构体指针 package main import "fmt" type Person struct { name str ...

  8. IoC容器-Bean管理XML方式(引入外部属性文件)

    IoC操作Bean管理(引入外部属性文件) 1,直接配置数据库信息 (1)配置德鲁伊连接池 (2)引入德鲁伊连接池依赖jar包 2,通过引入外部属性文件配置数据库连接池 (1)创建外部属性文件,pro ...

  9. centos7语言更改

    vim /etc/locale.conf 把 LANG="en_US.UTF-8" 改为 LANG="zh_CN.UTF-8"

  10. Ubuntu SVN 搭建

    SVN是Subversion的简称,是一个开放源代码的版本控制系统,相较于RCS.CVS,它采用了分支管理系统,它的设计目标就是取代CVS.互联网上很多版本控制服务已从CVS迁移到Subversion ...