题解:P9356 「SiR-1」Bracket

· · 题解

首先是一个括号串在把相邻的 () 对不断消去之后,剩下的字符串必然形如 )...)(...( 的形式,所以只要进行至多一次变换操作就可以得到形如 (...()...)(... 或 ...)(...()...) 的形式。

此时插入操作部分的答案相当于将左右括号分别看作 1 和 -1,将序列求和之后和的绝对值,而变换操作会发生,当且仅当得到的串同时存在未匹配的右括号和左括号。

故考虑分开求两个部分的贡献。

插入部分是简单的,先把前缀和放入桶里,维护当前前缀和两边的和和数量,把绝对值拆开,对于每个前缀计算,由于每次前缀和只会变化 1,所以暴力加入和删除贡献即可。

考虑变换部分,对于一个需要变换的串,把其映射为“最右侧的未匹配左括号位置”和“最左侧的未匹配右括号位置”组成的对,则一个括号对 (a,b) 代表所有消去前位置形如 S_{a-i\dots b+j} 的串,其中 S_{a-i\dots a-1} 和 S_{b+1\dots b + j} 为合法的括号串,假设 cnt_i 表示一个左括号右侧最大的 k,满足存在一种划分方案使得其右侧存在紧邻的 k 个合法串。例如 ((())()(( 中第一个左括号的 cnt 值为 2,对于右括号的定义相反,那么一对括号对的贡献就是 (cnt_i + 1) \times (cnt_j + 1),即可以自由选择原来哪些串刚好被消去。

```cpp # include <bits/extc++.h> # include <bits/stdc++.h> # define rep(i, a, b) for(int i = (a);i <= (b);i ++) # define lop(i, a, b) for(int i = (a);i < (b);i ++) # define dwn(i, a, b) for(int i = (a);i >= (b);i --) # define int64 long long # define ___main signed main() # define f64 double # define f32 float # define f128 long double # define int128 __int128 # define i128 ((__int128)1) # define int32 signed # define un unsigned # define int16 short # define int8 char # define ret return # define inl inline # define reg register # define fn void # define elif else if # define pb push_back # define pp pop_back # define CASES() int T;cin >> T;while(T--) # define FaILurEg(s) freopen(s".in","r",stdin);freopen(s".out","w",stdout) #if defined(_MSC_VER) # define FORCE_INLINE __forceinline #else # define FORCE_INLINE __attribute__((always_inline)) inline #endif namespace packin { using std::cout; using std::cin; template<typename T>struct _Arr {T *arr;int size; FORCE_INLINE T& operator [](int i) {ret arr[i];}}; template<typename T>FORCE_INLINE fn read(T &x) {cin >> x;} template<typename T>FORCE_INLINE fn read(_Arr<T>a) {rep(i, 1, a.size) cin >> a.arr[i];} template<typename T>FORCE_INLINE fn read(T *a, int siz) {rep(i, 1, siz) cin >> a[i];} template<typename T, typename... Args>FORCE_INLINE void read(T &x, Args&... args) {read(x);read(args...);} inline void print_impl(const char* s) {while (*s) {cout << *s++;}} template<typename T, typename... Args>inline void print_impl(const char* s, const T& x, const Args&... args) {while (*s) {if (s[0] == '{' && s[1] == '}') {cout << x;print_impl(s + 2, args...);return;}cout << *s++;}} template<typename... Args>inline void print(const char* s, const Args&... args) {print_impl(s, args...);} inline void error_impl(const char*s){while(*s)std::cerr<<*s++;}template<class T,class...A>inline void error_impl(const char*s,const T&x,const A&...a){while(*s){if(s[0]=='{'&&s[1]=='}'){std::cerr<<x;error_impl(s+2,a...);return;}std::cerr<<*s++;}}template<class...A>inline void error(const char*s,const A&...a){error_impl(s,a...);} void IOS() {std::ios::sync_with_stdio(0);std::cin.tie(0);}} template<typename T>FORCE_INLINE T min(T a, T b) {ret (a < b)?a:b;} template<typename T>FORCE_INLINE T max(T a, T b) {ret (a > b)?a:b;} using packin::read; using packin::print; using packin::error; using std::pair; using std::vector;using packin::IOS; #define DEFp(a, A, b, B) typedef pair<a, b> p##A##B; #define DEFM(a, A, b, B) typedef std::map<a, b> $m##A##B; #define DEFMu(a, A, b, B) typedef std::unordered_map<a, b> $um##A##B; #define FOR_EACH_LEFT(m) \ m(int, i) \ m(long long, l) \ m(double, d) \ m(long double, ld) #define FOR_EACH_RIGHT(m, T1, S1) \ m(T1, S1, int, i) \ m(T1, S1, long long, l) \ m(T1, S1, double, d) \ m(T1, S1, long double, ld) // 笛卡尔积展开 #define EXPAND_LEFT(T1, S1) FOR_EACH_RIGHT(DEFp, T1, S1) // 一句触发 FOR_EACH_LEFT(EXPAND_LEFT) #undef EXPAND_LEFT #define EXPAND_LEFT(T1, S1) FOR_EACH_RIGHT(DEFM, T1, S1) FOR_EACH_LEFT(EXPAND_LEFT) #undef EXPAND_LEFT #define EXPAND_LEFT(T1, S1) FOR_EACH_RIGHT(DEFMu, T1, S1) FOR_EACH_LEFT(EXPAND_LEFT) #undef EXPAND_LEFT #define DEFX(TYPE, TY, X, x) typedef TYPE<X> TY##x; #define MAKELIST(m, TYPE, TY) m(TYPE, TY, int, i)m(TYPE, TY, long long, l)m(TYPE, TY, double, d)m(TYPE, TY, long double, ld) MAKELIST(DEFX, vector, $v);MAKELIST(DEFX, std::basic_string, $bs);MAKELIST(DEFX, std::set, $S); //w: word d Dword q qword typedef short $w;typedef int $d;typedef int64 $q;typedef int128 $o; typedef un short u$w;typedef un int u$d;typedef un int64 u$q;typedef un int128 u$o; typedef float $fd;typedef double $fq;typedef long double $fqu;/*Quint* */typedef __float128 $fo; #if __cplusplus>=202300 typedef _Float16 $fw;typedef __bf16 $fbw; #endif typedef std::string _Str; #define $v std::vector $d Cnt[4000005];u$q c2[2000005], c3[2000005];// n ^ 2 constexpr $d BASE = 2000000; ___main { IOS(); $d T; read(T); while(T --) { $d n;_Str S; read(n, S); // < S >= S $d s = 0;u$q sneg = 0,cneg = 0, spos = 0, cpos = 0, Sum = 0; $d pos = 2000000; Cnt[pos] ++; cpos = 1; pos --;// point to S - 1; rep(i, 0, n - 1) { if(S[i] == '(') { //pos + 1. pos ++;s ++; cneg += Cnt[pos]; sneg += Cnt[pos] * 1ll * (pos - BASE); cpos -= Cnt[pos]; spos -= Cnt[pos] * 1ll * (pos - BASE); Sum += cneg * s - sneg + spos - cpos * s; } else { cneg -= Cnt[pos]; sneg -= Cnt[pos] * (pos - BASE); cpos += Cnt[pos]; spos += Cnt[pos] * (pos - BASE); pos --;s --; Sum += cneg * s - sneg + spos - cpos * s; } //print("{} {} {} {}\n", sneg, cneg, spos, cpos); Cnt[s + BASE] ++; cpos ++;spos += s; } // part II rep(i, 0, n - 1) c2[i] = c3[i] = 0; $vi L, R; rep(i, 0, n - 1) if(S[i] == ')') { if(!L.empty()) { L.pp(); if(!L.empty()) c2[L.back()] ++; } } else L.pb(i); dwn(i, n - 1, 0) if(S[i] == '(') { if(!R.empty()) { R.pp(); if(!R.empty()) c3[R.back()] ++; } } else R.pb(i); c3[0] += (S[0] == ')'); rep(i, 1, n) c3[i] += c3[i - 1] + (S[i] == ')');//, print("{} {}\n"); // print("{}\n", Sum); rep(i, 0, n - 1) if(S[i] == '(') Sum += (1 + c2[i]) * c3[i]; print("{}\n", Sum); rep(i, BASE - n, BASE + n) Cnt[i] = 0; } } */ ```