QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#791388#9306. Tree TweakingIllusionaryDominance#WA 6ms6000kbC++202.7kb2024-11-28 18:21:542024-11-28 18:21:55

Judging History

This is the latest submission verdict.

  • [2024-11-28 18:21:55]
  • Judged
  • Verdict: WA
  • Time: 6ms
  • Memory: 6000kb
  • [2024-11-28 18:21:54]
  • Submitted

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);
}

void dfs3(int u, int dep) {
    if (!u) return ;
    ans += dep;
    dfs3(ls[u], dep + 1);
    dfs3(rs[u], dep + 1);
}

void dfs4(int u) {
    if (rnk[u] > qr) {
        dfs3(u, 0);
        return ;
    }
    if (ls[u]) dfs4(ls[u]);
    if (rs[u]) dfs4(rs[u]);
}

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);
    dfs4(st[1]);
    cout << ans << endl;
    
    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3556kb

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: 3804kb

input:

5
5 1 2 3 4
3 5

output:

14

result:

ok 1 number(s): "14"

Test #3:

score: 0
Accepted
time: 0ms
memory: 3784kb

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: 6ms
memory: 6000kb

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:

9278728699

result:

wrong answer 1st numbers differ - expected: '4779092019', found: '9278728699'