#include <bits/stdc++.h> using namespace std; #define rep(i,a,n) for (int i=a;i<n;i++) #define per(i,a,n) for (int i=n-1;i>=a;i--) #define pb push_back #define eb emplace_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second #define SZ(x) ((int)(x).size()) typedef vector<int> VI; typedef basic_string<int> BI; typedef long long ll; typedef pair<int,int> PII; typedef double db; mt19937 mrand(1); const ll mod=1000000007; int rnd(int x) { return mrand() % x;} ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} //ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;} // head const int N=101000; int n,k,m,ps[N],pre[N],nxt[N],lab[N],qs[N]; int bg[N],ed[N]; ll sig[N],sm[N],ls[N],rs[N]; char s[N]; const int LOGN=19; template<class T> struct STTable { int n; T f[LOGN+1][N]; void init(vector<T> a) { // 0-based n=SZ(a); assert(n < (1<<LOGN) && n < N); rep(i,0,n) f[0][i]=a[i]; rep(j,1,LOGN) for (int i=0;i+(1<<j)-1<n;i++) { f[j][i]=min(f[j-1][i],f[j-1][i+(1<<(j-1))]); } } T query(int l,int r) { assert(l<=r); int len=31-__builtin_clz(r-l+1); return min(f[len][l],f[len][r-(1<<len)+1]); } }; ll f[N]; int opt[N]; STTable<PII> f1,f2; int main() { scanf("%d%d",&n,&k); scanf("%s",s); for (int i=1;i<=n;i++) { if (s[i-1]=='(') ps[i]=ps[i-1]+1; else ps[i]=ps[i-1]-1; } map<int,int> pos; auto getsmseg=[&](int l,int r) { assert(lab[l]==lab[r]&&l<=r); return sm[r]-sm[l]+(ll)(qs[r]-qs[l])*(qs[r]-qs[l]+1)/2; }; for (int i=0;i<=n;i++) { if (!pos.count(ps[i])) pre[i]=-1; else { int l=pos[ps[i]]; if (!pos.count(ps[i]-1)||pos[ps[i]-1]<l) pre[i]=l; else pre[i]=-1; } //printf("%d %d\n",i,pre[i]); pos[ps[i]]=i; if (pre[i]==-1) { lab[i]=++m; qs[i]=0; sig[i]=0; bg[i]=i; } else { lab[i]=lab[pre[i]]; bg[i]=bg[pre[i]]; qs[i]=qs[pre[i]]+1; sig[i]=getsmseg(pre[i]+1,i-1); sm[i]=sm[pre[i]]+sig[i]; } } rep(i,0,n+1) nxt[i]=-1; rep(i,0,n+1) if (pre[i]!=-1) nxt[pre[i]]=i; per(i,0,n+1) { if (nxt[i]==-1) ed[i]=i; else ed[i]=ed[nxt[i]]; } per(i,0,n+1) rs[i]=rs[ed[i]+1]+getsmseg(i,ed[i]); rep(i,0,n+1) ls[i]=(bg[i]?ls[bg[i]-1]:0)+getsmseg(bg[i],i); vector<PII> mv(n+1); rep(i,0,n+1) mv[i]=mp(ps[i],i); f1.init(mv); rep(i,0,n+1) mv[i]=mp(ps[i],-i); f2.init(mv); auto weight=[&](int l,int r) { if (l>r) return 0ll; //assert(l<=r); PII minu=f1.query(l-1,r),minv=f2.query(l-1,r); int u=minu.se,v=-minv.se; return getsmseg(u,v)+rs[l-1]-rs[u]+ls[r]-ls[v]; }; auto solve=[&](ll c,int ty=0){ static int vo[N],po[N]; int p=1,q=0; f[0]=0; auto val=[&](int j,int i) { //let val(j,i) = f[j]+w(j,i) return f[j]+weight(j+1,i)+c; }; auto check=[&](int p,int q,int r) { return (ty==0)?val(p,r)<=val(q,r):val(p,r)<val(q,r); }; rep(i,0,n){ while(p<=q && check(i,vo[q],po[q]))q--;// if(p>q)po[++q]=i+1,vo[q]=i; else{ int l=po[q],r=n; while(l<=r){ int mid=(l+r)>>1; if(check(i,vo[q],mid))r=mid-1;// else l=mid+1; } if(l<=n) po[++q]=l,vo[q]=i; } while(p<q && i+1>=po[p+1])p++; f[i+1]=val(vo[p],i+1); opt[i+1]=vo[p]; } int x=n,cnt=0; while (x!=0) x=opt[x],cnt++; return cnt; }; ll pl=0,pr=(ll)n*n; while (pl+1<pr) { ll md=(pl+pr)>>1; int w=solve(md,0); if (w>=k) pl=md; else pr=md; } solve(pl,0); printf("%lld\n",f[n]-k*pl); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | #include <bits/stdc++.h> using namespace std; #define rep(i,a,n) for (int i=a;i<n;i++) #define per(i,a,n) for (int i=n-1;i>=a;i--) #define pb push_back #define eb emplace_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second #define SZ(x) ((int)(x).size()) typedef vector<int> VI; typedef basic_string<int> BI; typedef long long ll; typedef pair<int,int> PII; typedef double db; mt19937 mrand(1); const ll mod=1000000007; int rnd(int x) { return mrand() % x;} ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} //ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;} // head const int N=101000; int n,k,m,ps[N],pre[N],nxt[N],lab[N],qs[N]; int bg[N],ed[N]; ll sig[N],sm[N],ls[N],rs[N]; char s[N]; const int LOGN=19; template<class T> struct STTable { int n; T f[LOGN+1][N]; void init(vector<T> a) { // 0-based n=SZ(a); assert(n < (1<<LOGN) && n < N); rep(i,0,n) f[0][i]=a[i]; rep(j,1,LOGN) for (int i=0;i+(1<<j)-1<n;i++) { f[j][i]=min(f[j-1][i],f[j-1][i+(1<<(j-1))]); } } T query(int l,int r) { assert(l<=r); int len=31-__builtin_clz(r-l+1); return min(f[len][l],f[len][r-(1<<len)+1]); } }; ll f[N]; int opt[N]; STTable<PII> f1,f2; int main() { scanf("%d%d",&n,&k); scanf("%s",s); for (int i=1;i<=n;i++) { if (s[i-1]=='(') ps[i]=ps[i-1]+1; else ps[i]=ps[i-1]-1; } map<int,int> pos; auto getsmseg=[&](int l,int r) { assert(lab[l]==lab[r]&&l<=r); return sm[r]-sm[l]+(ll)(qs[r]-qs[l])*(qs[r]-qs[l]+1)/2; }; for (int i=0;i<=n;i++) { if (!pos.count(ps[i])) pre[i]=-1; else { int l=pos[ps[i]]; if (!pos.count(ps[i]-1)||pos[ps[i]-1]<l) pre[i]=l; else pre[i]=-1; } //printf("%d %d\n",i,pre[i]); pos[ps[i]]=i; if (pre[i]==-1) { lab[i]=++m; qs[i]=0; sig[i]=0; bg[i]=i; } else { lab[i]=lab[pre[i]]; bg[i]=bg[pre[i]]; qs[i]=qs[pre[i]]+1; sig[i]=getsmseg(pre[i]+1,i-1); sm[i]=sm[pre[i]]+sig[i]; } } rep(i,0,n+1) nxt[i]=-1; rep(i,0,n+1) if (pre[i]!=-1) nxt[pre[i]]=i; per(i,0,n+1) { if (nxt[i]==-1) ed[i]=i; else ed[i]=ed[nxt[i]]; } per(i,0,n+1) rs[i]=rs[ed[i]+1]+getsmseg(i,ed[i]); rep(i,0,n+1) ls[i]=(bg[i]?ls[bg[i]-1]:0)+getsmseg(bg[i],i); vector<PII> mv(n+1); rep(i,0,n+1) mv[i]=mp(ps[i],i); f1.init(mv); rep(i,0,n+1) mv[i]=mp(ps[i],-i); f2.init(mv); auto weight=[&](int l,int r) { if (l>r) return 0ll; //assert(l<=r); PII minu=f1.query(l-1,r),minv=f2.query(l-1,r); int u=minu.se,v=-minv.se; return getsmseg(u,v)+rs[l-1]-rs[u]+ls[r]-ls[v]; }; auto solve=[&](ll c,int ty=0){ static int vo[N],po[N]; int p=1,q=0; f[0]=0; auto val=[&](int j,int i) { //let val(j,i) = f[j]+w(j,i) return f[j]+weight(j+1,i)+c; }; auto check=[&](int p,int q,int r) { return (ty==0)?val(p,r)<=val(q,r):val(p,r)<val(q,r); }; rep(i,0,n){ while(p<=q && check(i,vo[q],po[q]))q--;// if(p>q)po[++q]=i+1,vo[q]=i; else{ int l=po[q],r=n; while(l<=r){ int mid=(l+r)>>1; if(check(i,vo[q],mid))r=mid-1;// else l=mid+1; } if(l<=n) po[++q]=l,vo[q]=i; } while(p<q && i+1>=po[p+1])p++; f[i+1]=val(vo[p],i+1); opt[i+1]=vo[p]; } int x=n,cnt=0; while (x!=0) x=opt[x],cnt++; return cnt; }; ll pl=0,pr=(ll)n*n; while (pl+1<pr) { ll md=(pl+pr)>>1; int w=solve(md,0); if (w>=k) pl=md; else pr=md; } solve(pl,0); printf("%lld\n",f[n]-k*pl); } |