QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#791819 | #9306. Tree Tweaking | IllusionaryDominance | WA | 9ms | 7816kb | C++20 | 3.8kb | 2024-11-28 21:11:06 | 2024-11-28 21:11:07 |
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);
}
ll calc(const vi &A) {
static int b[MAX_N];
int m = 0;
for (auto x : A) b[++ m] = x;
sort(b + 1, b + m + 1);
int n = A.size();
static int rnk[MAX_N], st[MAX_N], ch[MAX_N][2];
for (int i = 1; i <= n; i ++) {
rnk[lower_bound(b + 1, b + m + 1, A[i - 1]) - b] = i;
}
int top = 0;
for (int i = 1; i <= n; ++ i){
while(top && rnk[st[top]] > rnk[i]) ch[i][0] = st[top], -- top;
ch[st[top]][1] = i;
st[++ top] = i;
}
queue <pair <int, int>> q;
q.push(pair(st[1], 0));
ll res = 0;
while (!q.empty()) {
auto [u, d] = q.front();
q.pop();
res += d;
if (ch[u][0]) q.push(pair(ch[u][0], d + 1));
if (ch[u][1]) q.push(pair(ch[u][1], d + 1));
}
return res;
}
void dp(const vi &node, int L, int R) {
vi b(0), tmp(0);
for (auto x : node) {
assert(x >= ql);
if (x <= qr) b.push_back(x);
else tmp.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);
sort(tmp.begin(), tmp.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] = L;
if (i == M - 1) rw[i] = R;
else rw[i] = a[b[i + 1]] - 1;
for (int j = i; j < M; j ++) {
f[i][j] = INF64;
}
}
static ll cost[MAX_M];
vi name;
for (int i = 0, j = 0; i < M; i ++) {
name.clear();
for ( ; j < (int)tmp.size() && a[tmp[j]] < a[b[i]]; j ++) name.push_back(a[tmp[j]]);
cost[i] = calc(name);
}
name.clear();
for (auto x : tmp) {
if (a[x] > a[b.back()]) name.push_back(a[x]);
}
cost[M] = calc(name);
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 + cost[l] + cost[r + 1]);
}
}
}
ans += f[0][M - 1];
for (auto x : node) modify(a[x], -1);
}
void dfs(int u, int dep, int L = 1, int R = N) {
if (ql <= rnk[u] && rnk[u] <= qr) {
vi node(0);
dfs2(u, node);
ans += 1ll * dep * node.size();
dp(node, L, R);
return ;
}
ans += dep;
if (ls[u]) dfs(ls[u], dep + 1, L, u - 1);
if (rs[u]) dfs(rs[u], dep + 1, u + 1, R);
}
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]) ls[i] = st[top], -- top;
rs[st[top]] = i;
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: 1ms
memory: 5628kb
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: 1ms
memory: 7656kb
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: 5876kb
input:
7 3 2 4 6 7 5 1 1 7
output:
17
result:
ok 1 number(s): "17"
Test #4:
score: 0
Accepted
time: 6ms
memory: 6444kb
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:
4779092019
result:
ok 1 number(s): "4779092019"
Test #5:
score: 0
Accepted
time: 5ms
memory: 6428kb
input:
90053 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 176 177 178 179 180 181 182 183 184 185 186 187 ...
output:
4045831111
result:
ok 1 number(s): "4045831111"
Test #6:
score: 0
Accepted
time: 6ms
memory: 7816kb
input:
100000 1 2 3 4 5 6 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 1...
output:
4990056230
result:
ok 1 number(s): "4990056230"
Test #7:
score: 0
Accepted
time: 6ms
memory: 6856kb
input:
100000 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 1...
output:
4990482700
result:
ok 1 number(s): "4990482700"
Test #8:
score: 0
Accepted
time: 6ms
memory: 7752kb
input:
100000 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 1...
output:
4994277470
result:
ok 1 number(s): "4994277470"
Test #9:
score: 0
Accepted
time: 6ms
memory: 6228kb
input:
100000 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 1...
output:
4991797502
result:
ok 1 number(s): "4991797502"
Test #10:
score: 0
Accepted
time: 6ms
memory: 6192kb
input:
100000 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 1...
output:
4992096630
result:
ok 1 number(s): "4992096630"
Test #11:
score: 0
Accepted
time: 3ms
memory: 6332kb
input:
100000 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 1...
output:
4991378130
result:
ok 1 number(s): "4991378130"
Test #12:
score: 0
Accepted
time: 6ms
memory: 6800kb
input:
100000 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 197 198 199 20...
output:
4990065230
result:
ok 1 number(s): "4990065230"
Test #13:
score: 0
Accepted
time: 5ms
memory: 6848kb
input:
100000 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 1...
output:
4991613286
result:
ok 1 number(s): "4991613286"
Test #14:
score: 0
Accepted
time: 6ms
memory: 6128kb
input:
100000 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 1...
output:
4992365820
result:
ok 1 number(s): "4992365820"
Test #15:
score: 0
Accepted
time: 5ms
memory: 6664kb
input:
100000 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 1...
output:
4992762661
result:
ok 1 number(s): "4992762661"
Test #16:
score: 0
Accepted
time: 5ms
memory: 6076kb
input:
100000 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 1...
output:
4991912205
result:
ok 1 number(s): "4991912205"
Test #17:
score: -100
Wrong Answer
time: 9ms
memory: 7772kb
input:
100000 26024 93049 7783 16602 86687 42006 24967 72232 61703 87842 99276 55658 26859 5207 64343 56070 77523 56145 27208 12527 36727 50469 48268 77211 13316 85211 62658 86029 1232 54191 32511 41813 64157 23763 98521 43414 63843 30732 55482 73178 78314 59291 36957 70984 11955 57167 17167 92685 90920 85...
output:
14570584
result:
wrong answer 1st numbers differ - expected: '2157797', found: '14570584'