题解 CF1149C【Tree Generator™】

· · 题解

前言:写一发题解纪念这道让我想了两天的题目。

正题

首先我们把原串中的左括号看作 1,右括号看作 -1,然后对于这个序列做前缀和,记作 h。画出 h_i 关于 i 的图像,我们可以发现,对于下图(其中红色的线代表 h 的图像),所有黄色的线以上的每一座“山峰”就代表了一棵子树。

那么紧接着我们考虑,答案应该如何计算。首先我们考虑树上的的两点距离公式:dis_{x,y}=dep_x+dep_y-2\times dep_{lca}。我们把这个公式转化到 h 的图像上,可以发现 \forall x\le y,有:

dis_{x,y}=h_x+h_y-2\times\min_{i=x}^y h_i

那么答案即为 \max_{x\le y}dis_{x,y}。

我们发现这个东西不是很好维护,但是我们考虑到一个性质,那就是如果在 [x,y] 中有一个整数 k,且 h_k 不等于 \min_{i=x}^y h_i,那么答案只会更劣。因此我们就可以把这层 \min 的限制去掉,接下来问题就变简单了。

那么答案就可以转化为:

\max_{x\le y\le z}h_x+h_z-2\times h_y

看到这个东西,我们考虑用线段树维护。具体地,分别维护区间最大值(记作 mx),区间最小值(记作 mn),区间 \max_{l\le x\le y\le r}h_x-2\times h_y(记作 pl),区间 \max_{l\le x\le y\le r}h_y-2\times h_x(记作 pr),以及区间 \max_{l\le x\le y\le z\le r}h_x+h_z-2\times h_y(记作 ans)。

记 o 为线段树的当前节点,lc,rc 为 o 的左右儿子,具体转移如下:

mx_o=\max{(mx_{lc},mx_{rc})} mn_o=\min{(mn_{lc},mn_{rc})} pl_o=\max{(pl_{lc},pl_{rc},mx_{lc}-2\times mn_{rc})} pr_o=\max{(pr_{lc},pr_{rc},mx_{rc}-2\times mn_{lc})} ans_o=\max{(ans_{lc},ans_{rc},mx_{lc}+pr_{rc},pl_{lc}+mx_{rc})}

紧接着我们考虑交换操作,对于每一对交换的 (x,y) 不妨令 x\le y,如果 x>y 则交换。再来看上面的图:

我们发现如果 x 位置是左括号而 y 位置是右括号,那么就相当于把 [x,y-1] 中的所有 h_i 减 2(见绿色部分);如果 x 位置是右括号而 y 位置是左括号,那么就相当于把 [x,y-1] 中的所有 h_i 加 2(见蓝色部分)。于是我们只需要多一个区间加操作即可。

时间复杂度:\Theta(n\log n)。

Code:

#include <cstdio>
#include <algorithm>
using namespace std ;
const int MAXN = 2e5 + 10 ;
int n , q , a[MAXN] , lz[MAXN << 2] ;
char ch[MAXN] ;
#define lc (o << 1)
#define rc (o << 1 | 1)
#define mid ((l + r) >> 1)
struct sgt {
    int mx , mn , pl , pr , ans ;
    sgt (int mx = 0 , int mn = 0 , int pl = 0 , int pr = 0 , int ans = 0) : mx(mx) , mn(mn) , pl(pl) , pr(pr) , ans(ans) {}
    sgt operator + (sgt x) const {
        sgt ret ;
        ret.mx = max (mx , x.mx) , ret.mn = min (mn , x.mn) ;
        ret.pl = max (max (pl , x.pl) , mx - 2 * x.mn) ;
        ret.pr = max (max (pr , x.pr) , x.mx - 2 * mn) ;
        ret.ans = max (max (ans , x.ans) , max (pl + x.mx , mx + x.pr)) ;
        return ret ;
    }
} t[MAXN << 2] ;
void build (int o , int l , int r) {
    if (l == r) {t[o] = sgt (a[l] , a[l] , -a[l] , -a[l] , 0) ; return ;}
    build (lc , l , mid) , build (rc , mid + 1 , r) , t[o] = t[lc] + t[rc] ;
}
void pushdown (int o) {
    if (!o || !lz[o]) return ;
    t[lc].mx += lz[o] , t[lc].mn += lz[o] , t[lc].pl -= lz[o] , t[lc].pr -= lz[o] , lz[lc] += lz[o] ;
    t[rc].mx += lz[o] , t[rc].mn += lz[o] , t[rc].pl -= lz[o] , t[rc].pr -= lz[o] , lz[rc] += lz[o] ;
    lz[o] = 0 ;
}
void upd (int o , int l , int r , int x , int y , int k) {
    if (x <= l && r <= y) {
        t[o].mx += k , t[o].mn += k , t[o].pl -= k , t[o].pr -= k , lz[o] += k ;
        return ;
    }
    pushdown (o) ;
    if (x <= mid) upd (lc , l , mid , x , y , k) ;
    if (mid < y) upd (rc , mid + 1 , r , x , y , k) ;
    t[o] = t[lc] + t[rc] ;
//  printf ("%d:[%d %d]\n" , o , l , r) ;
//  printf ("mx-%d,mn-%d,pl-%d,pr-%d,ans-%d\n" , t[o].mx , t[o].mn , t[o].pl , t[o].pr , t[o].ans) ;
}
int main () {
    scanf ("%d%d" , &n , &q) , scanf ("%s" , ch + 1) ; n = 2 * n - 2 ;
    for (int i = 1 ; i <= n ; i++) a[i] = (a[i - 1] + (ch[i] == '(' ? 1 : -1)) ;
    build (1 , 1 , n) ;
    printf ("%d\n" , max (t[1].ans , t[1].mx)) ;
    while (q--) {
        int x , y ; scanf ("%d%d" , &x , &y) ;
        if (x > y) swap (x , y) ;
        if (ch[x] == '(' && ch[y] == ')') upd (1 , 1 , n , x , y - 1 , -2) ;
        else if (ch[x] == ')' && ch[y] == '(') upd (1 , 1 , n , x , y - 1 , 2) ;
        swap (ch[x] , ch[y]) , printf ("%d\n" , max (t[1].ans , t[1].mx)) ;
        //puts (ch + 1) ;
    }
    return 0 ;
}