QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#297546 | #5246. Nawiasowe podziały [B] | zhaohaikun | 0 | 126ms | 210852kb | C++14 | 4.2kb | 2024-01-04 18:27:58 | 2024-01-04 18:28:00 |
Judging History
answer
// MagicDark
#include <bits/stdc++.h>
#define debug cerr << "[" << __LINE__ << "] "
#define SZ(x) (int) x.size() - 1
#define all(x) x.begin(), x.end()
#define ms(x, y) memset(x, y, sizeof x)
#define F(i, x, y) for (int i = (x); i <= (y); i++)
#define DF(i, x, y) for (int i = (x); i >= (y); i--)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
template <typename T> inline void chkmax(T& x, T y) {x = max(x, y);}
template <typename T> inline void chkmin(T& x, T y) {x = min(x, y);}
template <typename T> inline void read(T &x) {
x = 0; int f = 1; char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
x *= f;
}
const int N = 2e6 + 10;
int n, k, a[N], q[N];
string st;
vector <int> ls[N], rs[N];
ll val;
int fa[N], tot, id[N];
vector <int> vv[N], v[N];
int find(int x) {return x == fa[x] ? x : fa[x] = find(fa[x]);}
pair <ll, int> f[N], dp[N];
int pre[N];
ll sum[N], h[N], w[N];
pair <ll, int> operator + (pair <ll, int> x, pair <ll, int> y) {
return {x.first + y.first, x.second + y.second};
}
void DP(int x) {
if (x <= n) return dp[x] = make_pair(val * 2, 1), void();
for (int i: v[x]) DP(i);
// F(i, 1, SZ(v[x])) dfs(v[x][i]);
int l = 1, r = 0;
q[++r] = 0;
F(i, 1, v[x].size()) {
pre[i] = pre[i - 1] + (v[x][i - 1] <= n);
sum[i] = sum[i - 1] + h[v[x][i - 1]];
while (l < r && make_pair(w[q[l + 1]] - w[q[l]], f[q[l]].second) < make_pair(2ll * (pre[q[l + 1]] - pre[q[l]]) * pre[i], f[q[l + 1]].second)) l++;
f[i] = dp[v[x][i - 1]] + make_pair(w[q[l]] + 2 * sum[i - 1] + (ll) pre[i] * pre[i] - pre[i] - 2 * pre[i] * pre[q[l]], f[q[l]].second);
w[i] = f[i].first - 2 * sum[i] + (ll) pre[i] * pre[i] + pre[i];
// debug << i << " " << q[l] << " " << f[i].first << " " << f[i].second << " " << w[q[l]] + 2 * sum[i - 1] + (ll) pre[i] * pre[i] - pre[i] - 2 * pre[i] * pre[q[l]] << endl;
while (l <= r && pre[q[r]] == pre[i] && make_pair(w[i], f[i].second) < make_pair(w[q[r]], f[q[r]].second)) r--;
if (l <= r && pre[q[r]] == pre[i]) continue;
while (l < r && (__int128) (w[i] - w[q[r]]) * (pre[q[r]] - pre[q[r - 1]]) > (__int128) (w[q[r]] - w[q[r - 1]]) * (pre[i] - pre[q[r]])) r--;
q[++r] = i;
// debug << v[x][i - 1] << " " << q[l] << " " << w[q[l]] << " " << 2 * sum[i - 1] + (ll) pre[i] * pre[i] - pre[i] - 2 * pre[i] * pre[q[l]] << " " << f[i].first << " " << v[x][i - 1] << " " << dp[v[x][i - 1]].first << endl;
}
dp[x].first = 1e18;
F(i, 1, v[x].size()) chkmin(dp[x], f[i] + make_pair(2 * sum[v[x].size()] - 2 * sum[i] + (ll) (pre[v[x].size()] - pre[i]) * (pre[v[x].size()] - pre[i] - 1), 0));//, debug << f[i].first << endl;
h[x] = sum[v[x].size()] + (ll) pre[v[x].size()] * (pre[v[x].size()] - 1) / 2;
// debug << x << " " << h[x] << " " << dp[x].first << " " << dp[x].second << endl;
}
signed main() {
cin.tie(0) -> sync_with_stdio(0); // don't use puts
cin >> n >> k >> st;
k--;
st = ' ' + st;
n++;
F(i, 1, n) vv[(a[i] = i == 1 ? 0 : a[i - 1] + (st[i - 1] == '(' ? 1 : -1)) + n].push_back(i);
tot = n;
DF(i, 2 * n, 0) {
for (int j: vv[i]) {
// debug << j << endl;
fa[j] = j;
int t = find(j - 1);
if (t && a[t] == a[j]) id[j] = id[t], fa[t] = j;
else id[j] = ++tot;
if (t && a[t] != a[j]) v[id[j]].push_back(id[t]);
v[id[j]].push_back(j);
t = find(j + 1);
if (t) v[id[j]].push_back(id[t]), fa[t] = j;
}
}
// F(i, 1, tot) {
// for (int j: v[i]) debug << i << " " << j << endl;
// }
// F(i, 1, tot) v[i].insert(v[i].begin(), 0);
// F(i, 1, n) {
// while (q[0] && a[q[q[0]]] >= a[i]) q[0]--;
// if (rs[q[q[0]]].size()) {
// if (a[rs[q[q[0]]].front()] != a[i]) swap(rs[q[q[0]]], ls[i]);
// }
// rs[q[q[0]]].push_back(i);
// }
// val = 2;
// DP(tot);
// debug << dp[tot].first << " " << dp[tot].second << endl;
// return 0;
ll l = -1, r = 1e10, ans = -1;
while (l + 1 < r) {
ll mid = (l + r) >> 1;
val = mid;
DP(tot);
if (!k) {
ans = h[tot];
break;
}
if (dp[tot].second < k) r = mid;//, debug << val << " " << dp[tot].first / 2 << " " << val * k << endl;
else l = mid, ans = dp[tot].first / 2 - val * k;
}
cout << ans;
return 0;
}
/* why?
*/
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 1
Accepted
time: 15ms
memory: 202204kb
input:
1 1 (
output:
0
result:
ok single line: '0'
Test #2:
score: 0
Accepted
time: 27ms
memory: 202140kb
input:
1 1 )
output:
0
result:
ok single line: '0'
Test #3:
score: 0
Accepted
time: 31ms
memory: 202264kb
input:
2 1 ()
output:
1
result:
ok single line: '1'
Test #4:
score: 0
Accepted
time: 26ms
memory: 200224kb
input:
2 1 )(
output:
0
result:
ok single line: '0'
Test #5:
score: 0
Accepted
time: 16ms
memory: 200156kb
input:
2 2 ()
output:
0
result:
ok single line: '0'
Test #6:
score: 0
Accepted
time: 19ms
memory: 202172kb
input:
2 2 )(
output:
0
result:
ok single line: '0'
Test #7:
score: 0
Accepted
time: 21ms
memory: 200156kb
input:
10 4 ()))(()(()
output:
0
result:
ok single line: '0'
Test #8:
score: -1
Wrong Answer
time: 19ms
memory: 202256kb
input:
15 4 ())))()(()()(((
output:
0
result:
wrong answer 1st lines differ - expected: '1', found: '0'
Subtask #2:
score: 0
Wrong Answer
Test #16:
score: 0
Wrong Answer
time: 27ms
memory: 202260kb
input:
300 25 ((()()()(()())(((())())()))()((()(())))((()()))((()()))()()(((())())))((((())(()()()())(((()))(()())()(())())()()()()())(()((()()()))))(())()((()))((()))(()(()())))((()(())(((()))))((()()()()()())())(((()))()()()(())())(())()(((()))()((()()())))((())())(((()())())((()))(()()()()(())(()())((()...
output:
94
result:
wrong answer 1st lines differ - expected: '90', found: '94'
Subtask #3:
score: 0
Wrong Answer
Test #33:
score: 1
Accepted
time: 20ms
memory: 200248kb
input:
4000 1 (((((())())()((())(())(()()())()())(()(())(()))()(()))(()((())()()()()(()())(()()()))()(())()()(()(()()())))(()))(()(((()))()())((())(()()(()()))))((()((()())())((())())((()()))(())(())(()(()()))(((())())())((())()()(()))(()()()(()(())(()))()())((()))((())()(())()())))(((()(()()()()())(())())...
output:
5599
result:
ok single line: '5599'
Test #34:
score: -1
Wrong Answer
time: 31ms
memory: 200524kb
input:
4000 3 (((((((())))()()((((())(())()())()())(()(()())())))(()((())()((()()()((())()()))()))(())(()(()))()(()()()()((()(()))())()((()()()))(())(()()((())())))))(()(((((())))())((()()(((()()))())())())(())(()())(((()))())(()((()()()))(((()))())())())((())(())((())(())()(())()(((()))((())))))(((((())))...
output:
4628
result:
wrong answer 1st lines differ - expected: '4568', found: '4628'
Subtask #4:
score: 0
Wrong Answer
Test #54:
score: 1
Accepted
time: 32ms
memory: 202312kb
input:
4000 2 ((())(()(())))()((())()()()(())()()(())()((()))()()((()()))((())())((())())(()())()(())(()(()))((()())())(()())((()()))(()()())((()))()())(()(()))((()()())(())()((())()))(())((())((())))(((()(())))()(())())(()(())(())())((())(())())((()(()))()()()()(()())())(((()))(())())((()())())((())()((()...
output:
18432
result:
ok single line: '18432'
Test #55:
score: 0
Accepted
time: 23ms
memory: 202308kb
input:
4000 5 (((((((()))(())(()(())()()(())(()))()()(((()))())()(()))(((()(()()))()())()()(()(())()(())))((()()()())(((())))((()())()(()()())()()()())()()(()(()))))())((()()())((((((()))()())(()())())())()((()()()(()()((()))()()())(())()())()()()))(())())()(((((()()(()(())()()))))((()()((())())((()())))((...
output:
4336
result:
ok single line: '4336'
Test #56:
score: -1
Wrong Answer
time: 35ms
memory: 200344kb
input:
4000 13 (((((()()))(()()(()((())(()()()))(()))(((()()(()()))()())((())()())()())((()()(())())((()()()()))(())()(())())()((()(())())()()()()())((()(()()()(()())))(()()))(((())(()()))()(())))))()((())((((()()())(()()))(())((()((())))())()(((())()()(()()())(()))()(((()))(((())()()(())))))(((()())))(()(...
output:
4198
result:
wrong answer 1st lines differ - expected: '3928', found: '4198'
Subtask #5:
score: 0
Wrong Answer
Test #75:
score: 0
Wrong Answer
time: 107ms
memory: 205448kb
input:
100000 3 (()((())(()))(())((()())))(((()()))(()))(()()()((())))()()((())(())((())(()(()))())((())))()()((())(()()))(((())))((())()(()())(()())()(()(()()())))(()(()()))()(()((())()()()()())((()))(())(())())(((()))()(()()())()(()()))((()()))(((()()()()))(()))(((())))(((()))(()()())(()(()()))(())(()())...
output:
5680755
result:
wrong answer 1st lines differ - expected: '3832013', found: '5680755'
Subtask #6:
score: 0
Wrong Answer
Test #89:
score: 0
Wrong Answer
time: 106ms
memory: 208436kb
input:
100000 6 ((((()()((())))()((()())()())((())()(((()()))())((())((()()()))()()()(())))(((()(())(()()))(())())(())))(((()))((((())))()))(()((())(())(())((()())))(()(()(()))(())(()()()(())())(()(())(()(()))(()())()((()))()()))(()()(())))((()())()((())((())()()(((()))(())))((())())()((()())(()))))()(((()...
output:
126165
result:
wrong answer 1st lines differ - expected: '126039', found: '126165'
Subtask #7:
score: 0
Wrong Answer
Test #103:
score: 0
Wrong Answer
time: 93ms
memory: 208764kb
input:
80000 81 ((((((()())((()((()))))(()())()(()(((()(()())(()())())(())))()(())(()()))()))(((()(()))((()))(())((((())))(())()((()))())((()(()())))(())(()()))(((()()))(()((())))())()(((()()))(()())()()(((()((()))(())))((())(()((())))())(()())))()()()((()()(()()(())()()((()))(())()))((()(())()()))(()(()()...
output:
93291
result:
wrong answer 1st lines differ - expected: '87799', found: '93291'
Subtask #8:
score: 0
Wrong Answer
Test #129:
score: 0
Wrong Answer
time: 86ms
memory: 209748kb
input:
79856 243 (())()(())(())(()()())()(())()(()())()(())()(())()((()))(())(()(())())()(()())()()(())(())((()()))(())(()()())(()())((()())()())(())()()(())()(())((()()))()()(()()())(())()()(()())(())(())((()))(())(())()()()()()(()(()))(()(())()(()))()()()()()()()()((()()))()()()()()(())()(())(()())()(()(...
output:
954630
result:
wrong answer 1st lines differ - expected: '794572', found: '954630'
Subtask #9:
score: 0
Wrong Answer
Test #155:
score: 0
Wrong Answer
time: 110ms
memory: 210852kb
input:
99599 64 ((((((()(((())()))))((((((()))()())())())(()(()(()())))(()(()()(())())((()(()()))((()))()(())()())()(()(())()((()()())()())()(((())()))(()))((())(()(())()))(()))())()(((())(((())()((())))())(()()(()))(()())()((())((()())()(())())(())()(())((())(()()(()))((()))(()()())))()(((()))(())))(())((...
output:
112855
result:
wrong answer 1st lines differ - expected: '110447', found: '112855'
Subtask #10:
score: 0
Wrong Answer
Test #181:
score: 0
Wrong Answer
time: 126ms
memory: 208504kb
input:
100000 256 ((((())()(())())(((()))()((())()(()(())))))(((()(()))(((()))()()()()))()((((()))(())((()))))((()))(()((()()()()())()())(())(())()()(()()))(((()()))))())((((()))()((())()(()(())((()))))()((()()()()((()))))()(()(()()(()(())(()))))(())(((())))((()()((()))()())()())()((()())))()(((((()()))))(...
output:
117655
result:
wrong answer 1st lines differ - expected: '107402', found: '117655'