QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#791335 | #9306. Tree Tweaking | IllusionaryDominance# | WA | 16ms | 5844kb | C++20 | 2.5kb | 2024-11-28 18:08:23 | 2024-11-28 18:08:29 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector <int>;
const int MAX_N = 100000 + 5;
const int MAX_M = 200 + 5;
const ll INF64 = 1e18;
int N, a[MAX_N], rnk[MAX_N], ql, qr, c[MAX_N];
int ls[MAX_N], rs[MAX_N];
ll f[MAX_M][MAX_M], ans;
void modify(int x, int v) {
for (int i = x; i <= N; i += i & -i) c[i] += v;
}
int query(int l, int r) {
l --;
int res = 0;
while (l < r) {
res += c[r];
r -= r & -r;
}
while (r < l) {
res -= c[l];
l -= l & -l;
}
return res;
}
void dfs2(int u, vi &node) {
if (!u) return ;
node.push_back(rnk[u]);
dfs2(ls[u], node);
dfs2(rs[u], node);
}
void dp(const vi &node) {
vi b(0);
for (auto x : node) {
assert(x >= ql);
if (x <= qr) b.push_back(x);
modify(a[x], 1);
}
auto cmp_by_a = [&](int i, int j) -> bool {
return a[i] < a[j];
};
sort(b.begin(), b.end(), cmp_by_a);
int M = b.size();
static int lw[MAX_M], rw[MAX_M];
for (int i = 0; i < M; i ++) {
if (i > 0) lw[i] = a[b[i - 1]] + 1;
else lw[i] = 1;
if (i == M - 1) rw[i] = N;
else rw[i] = a[b[i + 1]] - 1;
for (int j = i; j < M; j ++) {
f[i][j] = INF64;
}
}
for (int i = 1; i <= M; i ++) {
for (int j = 0; j + i - 1 < M; j ++) {
int l = j, r = j + i - 1;
for (int k = l; k <= r; k ++) {
f[l][r] = min(f[l][r], f[l][k - 1] + f[k + 1][r] + query(lw[l], rw[r]) - 1);
}
}
}
ans += f[0][M - 1];
for (auto x : node) modify(a[x], -1);
}
void dfs(int u, int dep) {
if (ql <= rnk[u] && rnk[u] <= qr) {
vi node(0);
dfs2(u, node);
ans += 1ll * dep * node.size();
dp(node);
return ;
}
ans += dep;
if (ls[u]) dfs(ls[u], dep + 1);
if (rs[u]) dfs(rs[u], dep + 1);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> N;
for (int i = 1; i <= N; ++ i) cin >> a[i], rnk[a[i]] = i;
int top = 0; static int st[MAX_N], fa[MAX_N];
for (int i = 1; i <= N; i ++){
while(top && rnk[st[top]] > rnk[i]) -- top;
ls[i] = st[top + 1]; fa[st[top + 1]] = i;
rs[st[top]] = i; fa[i] = st[top];
st[++ top] = i;
}
cin >> ql >> qr;
dfs(st[1], 1);
cout << ans << endl;
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3808kb
input:
8 2 4 5 7 1 3 8 6 3 6
output:
24
result:
ok 1 number(s): "24"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3812kb
input:
5 5 1 2 3 4 3 5
output:
14
result:
ok 1 number(s): "14"
Test #3:
score: 0
Accepted
time: 1ms
memory: 5592kb
input:
7 3 2 4 6 7 5 1 1 7
output:
17
result:
ok 1 number(s): "17"
Test #4:
score: -100
Wrong Answer
time: 16ms
memory: 5844kb
input:
97865 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 10...
output:
4779891754
result:
wrong answer 1st numbers differ - expected: '4779092019', found: '4779891754'