#include <bits/stdc++.h> #include <random> #define ll long long int #define pb push_back #define st first #define nd second #define pii pair<int,int> #define mp make_pair #define pll pair<long long,long long> #define ld long double #define ull unsigned long long #define mt make_tuple using namespace std; int n; string s; const int nax = 1e5 + 5; const int offset = nax; // wzo ll odp_wzo[nax]; ll koszt; int first_good[nax]; int which_instance[nax * 2]; int met_values[nax * 2]; int last_pos[nax * 2]; int prv_same[nax]; int nxt_same[nax]; int pref[nax]; const int BLOCK = 180; int a[nax]; int bylo[nax * 2]; void prepare(){ for(int i=0;i<nax*2;i++) met_values[i] = 0; fill(bylo, bylo + nax * 2, -1); int cumulative = 0; for(int i=1;i<=n;i++){ first_good[i] = 1; if(s[i] == '(') a[i] = 1; else a[i] = -1; cumulative += a[i]; if(bylo[offset + cumulative - 1] != -1){ first_good[i] = bylo[offset + cumulative - 1] + 2; } bylo[offset + cumulative] = i; } for(int i=0;i<nax*2;i++) last_pos[i] = -1; which_instance[0] = 1; met_values[offset] = 1; last_pos[offset] = 0; prv_same[0] = -1; int bal = 0; for(int i=1;i<=n;i++){ if(s[i] == '(') bal++; else bal--; int cur = ++met_values[offset + bal]; which_instance[i] = cur; int where_was = last_pos[offset + bal]; prv_same[i] = where_was; last_pos[offset + bal] = i; pref[i] = bal; } for(int i=0;i<nax*2;i++) last_pos[i] = n + 1; bal = 0; for(int i=n;i>=0;i--){ int where_was = last_pos[offset + bal]; nxt_same[i] = where_was; last_pos[offset + bal] = i; if(s[i] == '(') bal++; else bal--; } } ll dp[nax]; ll dp_after_SQRT_update[nax]; ll prefsum[nax]; const ll MNO = 1e6; void perform_SQRT_backpropagation(int from, int to){ // O(N) ? fill(prefsum, prefsum + n + 1, 0); for(int i=from;i<=to;i++){ int downto = first_good[i]; if(downto == i + 1) continue; if(first_good[i] == 1){ if(prv_same[i] == -1) continue; int dokad = prv_same[i]; prefsum[dokad] += MNO; } else{ int skad = downto - 1; int dokad = prv_same[i]; prefsum[dokad] += MNO; if(prv_same[skad] != -1){ prefsum[prv_same[skad]] -= MNO; } } } for(int i=to;i>=0;i--){ prefsum[i] += prefsum[nxt_same[i]]; } ll incr = 0; for(int i=to;i>=0;i--){ incr += prefsum[i]; dp_after_SQRT_update[i] = dp[i] + incr; } } bool alive[nax]; int nxtInstanceOfIthSQRT[nax * 2][BLOCK]; int last[nax * 2]; bool important[nax]; int rl_pref[nax]; ll calc2(){ const ll inf = 2e18; for(int i=1;i<=n;i++){ dp[i] = inf; rl_pref[i] = rl_pref[i - 1]; if(s[i] == '(') rl_pref[i]++; else rl_pref[i]--; } for(int i=0;i<=n;i++) alive[i] = true; dp[0] = 0LL; vector<int> candidates; vector<int> ubij; for(int START=1;START<=n;START+=BLOCK){ int KONIEC = min(n, START + BLOCK - 1); perform_SQRT_backpropagation(START, KONIEC); candidates.clear(); ubij.clear(); const int SZ = (KONIEC - START + 1); // liczymy jak beda wygladaly stare dpki za BLOCK operacji i wyznaczamy kandydatow int last_met = 0; candidates.pb(last_met); for(int j=1;j<START;j++){ if(alive[j]){ if(dp_after_SQRT_update[last_met] >= dp_after_SQRT_update[j]){ candidates.pb(j); important[j] = true; ubij.pb(last_met); } last_met = j; } } fill(last, last + nax * 2, n + 1); for(int j=KONIEC;j>=0;j--){ last[offset + rl_pref[j]] = which_instance[j]; if(important[j]){ for(int c=0;c<SZ;c++){ nxtInstanceOfIthSQRT[j][c] = last[offset + rl_pref[START + c]]; } } } // na pale wszystkie pary int c = -1; for(int i=START;i<=KONIEC;i++){ c++; int downto = first_good[i]; int bal = 0; ll incr = 0; int blockstart = START; for(int j=i;j>=blockstart+1;j--){ bal += a[j]; if(bal == 0 && j >= downto) incr += MNO; dp[j - 1] += incr; } // 20 % for(int x : candidates){ if(first_good[i] > x){ int last_ins = which_instance[i] - 1; int fi_ins = which_instance[downto - 1]; dp[x] += MNO * (last_ins - fi_ins + 1); } else{ int fi_ins = nxtInstanceOfIthSQRT[x][c]; int last_ins = which_instance[i] - 1; dp[x] += MNO * (last_ins - fi_ins + 1); } } ll sv = MNO * koszt + 1; for(int j=blockstart;j<i;j++){ dp[i] = min(dp[i], dp[j] + sv); } for(int j : candidates){ dp[i] = min(dp[i], dp[j] + sv); } } // czyscimy co trzeba int match_up_to = START - 1; for(int x : ubij) alive[x] = false; for(int x : candidates) important[x] = false; for(int j=0;j<=match_up_to;j++) dp[j] = dp_after_SQRT_update[j]; } return dp[n]; } ll calc(){ auto res = calc2(); return res % MNO; } void wzo(int k){ // ZMIEN GRANICE MOZE (?) prepare(); ll low = 0; ll high = 1e10 + 1e9; while(low < high){ ll mid = (low + high) / 2; koszt = mid; int tak = calc(); if(tak < k){ high = mid; } else{ low = mid + 1; } //cout << "TF" << mid << endl; } koszt = low; calc(); ll ansik = dp[n] / MNO - koszt * k; odp_wzo[k] = ansik; } void main_solve(){ int nn, kk; cin >> nn >> kk; string x = ""; cin >> x; n = nn; s = "$" + x; wzo(kk); cout << odp_wzo[kk] << "\n"; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); main_solve(); return 0; // ZMIEN BOUNDY NA BINSERCZU // !!! return 0; }
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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 | #include <bits/stdc++.h> #include <random> #define ll long long int #define pb push_back #define st first #define nd second #define pii pair<int,int> #define mp make_pair #define pll pair<long long,long long> #define ld long double #define ull unsigned long long #define mt make_tuple using namespace std; int n; string s; const int nax = 1e5 + 5; const int offset = nax; // wzo ll odp_wzo[nax]; ll koszt; int first_good[nax]; int which_instance[nax * 2]; int met_values[nax * 2]; int last_pos[nax * 2]; int prv_same[nax]; int nxt_same[nax]; int pref[nax]; const int BLOCK = 180; int a[nax]; int bylo[nax * 2]; void prepare(){ for(int i=0;i<nax*2;i++) met_values[i] = 0; fill(bylo, bylo + nax * 2, -1); int cumulative = 0; for(int i=1;i<=n;i++){ first_good[i] = 1; if(s[i] == '(') a[i] = 1; else a[i] = -1; cumulative += a[i]; if(bylo[offset + cumulative - 1] != -1){ first_good[i] = bylo[offset + cumulative - 1] + 2; } bylo[offset + cumulative] = i; } for(int i=0;i<nax*2;i++) last_pos[i] = -1; which_instance[0] = 1; met_values[offset] = 1; last_pos[offset] = 0; prv_same[0] = -1; int bal = 0; for(int i=1;i<=n;i++){ if(s[i] == '(') bal++; else bal--; int cur = ++met_values[offset + bal]; which_instance[i] = cur; int where_was = last_pos[offset + bal]; prv_same[i] = where_was; last_pos[offset + bal] = i; pref[i] = bal; } for(int i=0;i<nax*2;i++) last_pos[i] = n + 1; bal = 0; for(int i=n;i>=0;i--){ int where_was = last_pos[offset + bal]; nxt_same[i] = where_was; last_pos[offset + bal] = i; if(s[i] == '(') bal++; else bal--; } } ll dp[nax]; ll dp_after_SQRT_update[nax]; ll prefsum[nax]; const ll MNO = 1e6; void perform_SQRT_backpropagation(int from, int to){ // O(N) ? fill(prefsum, prefsum + n + 1, 0); for(int i=from;i<=to;i++){ int downto = first_good[i]; if(downto == i + 1) continue; if(first_good[i] == 1){ if(prv_same[i] == -1) continue; int dokad = prv_same[i]; prefsum[dokad] += MNO; } else{ int skad = downto - 1; int dokad = prv_same[i]; prefsum[dokad] += MNO; if(prv_same[skad] != -1){ prefsum[prv_same[skad]] -= MNO; } } } for(int i=to;i>=0;i--){ prefsum[i] += prefsum[nxt_same[i]]; } ll incr = 0; for(int i=to;i>=0;i--){ incr += prefsum[i]; dp_after_SQRT_update[i] = dp[i] + incr; } } bool alive[nax]; int nxtInstanceOfIthSQRT[nax * 2][BLOCK]; int last[nax * 2]; bool important[nax]; int rl_pref[nax]; ll calc2(){ const ll inf = 2e18; for(int i=1;i<=n;i++){ dp[i] = inf; rl_pref[i] = rl_pref[i - 1]; if(s[i] == '(') rl_pref[i]++; else rl_pref[i]--; } for(int i=0;i<=n;i++) alive[i] = true; dp[0] = 0LL; vector<int> candidates; vector<int> ubij; for(int START=1;START<=n;START+=BLOCK){ int KONIEC = min(n, START + BLOCK - 1); perform_SQRT_backpropagation(START, KONIEC); candidates.clear(); ubij.clear(); const int SZ = (KONIEC - START + 1); // liczymy jak beda wygladaly stare dpki za BLOCK operacji i wyznaczamy kandydatow int last_met = 0; candidates.pb(last_met); for(int j=1;j<START;j++){ if(alive[j]){ if(dp_after_SQRT_update[last_met] >= dp_after_SQRT_update[j]){ candidates.pb(j); important[j] = true; ubij.pb(last_met); } last_met = j; } } fill(last, last + nax * 2, n + 1); for(int j=KONIEC;j>=0;j--){ last[offset + rl_pref[j]] = which_instance[j]; if(important[j]){ for(int c=0;c<SZ;c++){ nxtInstanceOfIthSQRT[j][c] = last[offset + rl_pref[START + c]]; } } } // na pale wszystkie pary int c = -1; for(int i=START;i<=KONIEC;i++){ c++; int downto = first_good[i]; int bal = 0; ll incr = 0; int blockstart = START; for(int j=i;j>=blockstart+1;j--){ bal += a[j]; if(bal == 0 && j >= downto) incr += MNO; dp[j - 1] += incr; } // 20 % for(int x : candidates){ if(first_good[i] > x){ int last_ins = which_instance[i] - 1; int fi_ins = which_instance[downto - 1]; dp[x] += MNO * (last_ins - fi_ins + 1); } else{ int fi_ins = nxtInstanceOfIthSQRT[x][c]; int last_ins = which_instance[i] - 1; dp[x] += MNO * (last_ins - fi_ins + 1); } } ll sv = MNO * koszt + 1; for(int j=blockstart;j<i;j++){ dp[i] = min(dp[i], dp[j] + sv); } for(int j : candidates){ dp[i] = min(dp[i], dp[j] + sv); } } // czyscimy co trzeba int match_up_to = START - 1; for(int x : ubij) alive[x] = false; for(int x : candidates) important[x] = false; for(int j=0;j<=match_up_to;j++) dp[j] = dp_after_SQRT_update[j]; } return dp[n]; } ll calc(){ auto res = calc2(); return res % MNO; } void wzo(int k){ // ZMIEN GRANICE MOZE (?) prepare(); ll low = 0; ll high = 1e10 + 1e9; while(low < high){ ll mid = (low + high) / 2; koszt = mid; int tak = calc(); if(tak < k){ high = mid; } else{ low = mid + 1; } //cout << "TF" << mid << endl; } koszt = low; calc(); ll ansik = dp[n] / MNO - koszt * k; odp_wzo[k] = ansik; } void main_solve(){ int nn, kk; cin >> nn >> kk; string x = ""; cin >> x; n = nn; s = "$" + x; wzo(kk); cout << odp_wzo[kk] << "\n"; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); main_solve(); return 0; // ZMIEN BOUNDY NA BINSERCZU // !!! return 0; } |