QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#240694#7108. Couleurucup-team2307#WA 1ms3828kbC++144.7kb2023-11-05 17:39:012023-11-05 17:39:01

Judging History

你现在查看的是最新测评结果

  • [2023-11-05 17:39:01]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3828kb
  • [2023-11-05 17:39:01]
  • 提交

answer

#include<bits/stdc++.h>
using ll = long long;
#define all(x) begin(x), end(x)
using namespace std;

struct Node {
    Node *l = 0, *r = 0;
    int sum = 0;
};

deque<Node> buffer;
Node *copy(Node *v) {
    if(!v) {
        buffer.push_back(Node());
    } else {
        buffer.push_back(Node(*v));
    }
    return &buffer.back();
}

Node *update(Node *root, int l, int r, int p, int v) {
    if(p < l || r < p) return root;
    Node *t = copy(root);
    t->sum += v;
    if(l != r) {
        int m = (l + r) / 2;
        t->l = update(t->l, l, m, p, v);
        t->r = update(t->r, m + 1, r, p, v);
    }
    return t;
}

int query(Node *v, int l, int r, int ql, int qr) {
    if(qr < l || r < ql || !v) return 0;
    if(ql <= l && r <= qr) return v->sum;
    int m = (l + r) / 2;
    return query(v->l, l, m, ql, qr) + query(v->r, m + 1, r, ql, qr);
} 

struct DS {
    int n;
    vector<Node*> roots;
    DS(vector<int> a) : n(a.size()) {
        vector<int> pos(n);
        for(int i = 0; i < n; i++)
            pos[a[i]] = i;
        Node *cur_root = 0;
        for(int i = 0; i < n; i++) {
            roots.push_back(cur_root = update(cur_root, 0, n - 1, pos[i], 1));
        }
    }
    int query(int l, int r, int k) {
        if(k < 0) return 0;
        return ::query(roots[k], 0, n - 1, l, r);
    }
};

int main() {
    cin.tie(0)->sync_with_stdio(0);
    int T;
    cin >> T;
    while(T--) {
        int n;
        cin >> n;
        vector<int> a(n);
        for(auto &i : a) cin >> i;
        
        {
            vector<array<int, 2>> b;
            for(int i = 0; i < n; i++)
                b.push_back({a[i], i});
            sort(all(b));
            for(int i = 0; i < n; i++)
                a[i] = lower_bound(all(b), array<int, 2>{a[i], i}) - b.begin();
        }

        DS ds(a);
        auto inv = [&](int l, int r) {
            ll ans = 0;
            for(int i = l + 1; i < r; i++) {
                ans += ds.query(l, i - 1, a[i]);
                // cout << l << " " << i << " " << a[i] << " " << ds.query(l, i - 1, a[i]) << endl;
            }
            ans = (r - l) * 1ll * (r - l - 1) / 2 - ans;
            return ans;
        };
        auto cross_inv = [&](int l, int r, int tl, int tr) {
            ll ans = 0;
            for(int i = l; i < r; i++) {
                ans += ds.query(tl, tr - 1, a[i]);
            }
            if(r > tr) {
                ll len = r - l;
                ll tlen = tr - tl;
                ans = len * tlen - ans;
            }
            return ans;
        };

        vector<ll> inv_count(n + 1);
        inv_count[n] = inv(0, n);

        set<int> del;
        del.insert(-1);
        del.insert(n);
        multiset<ll> vals;
        ll z = inv_count[n];
        vals.insert(inv_count[n]);
        cout << inv_count[n] << " ";//cout << ":";cout.flush();
        for(ll t, ti = n; ti--;) {
            cin >> t;
            t ^= z;
            t--;

            auto it = del.insert(t).first;
            int L, R;
            L = *--it; it++;
            R = *++it; it--;

            ll total_inv = inv_count[R];
            vals.erase(vals.find(total_inv));

            ll mid_inv = 0;
            // cout << L + 1 << " " << t << endl;
            mid_inv += (t - L - 1) - ds.query(L + 1, t - 1, a[t]);
            mid_inv += ds.query(t + 1, R - 1, a[t]);
            ll cr_inv, left_inv, right_inv;
            if(R - t < t - L + 1) {
                cr_inv = cross_inv(t + 1, R, L + 1, t);
            } else {
                cr_inv = cross_inv(L + 1, t, t + 1, R);
            }
            total_inv -= mid_inv + cr_inv;
            if(R - t < t - L + 1) {
                right_inv = inv(t + 1, R);
                left_inv = total_inv - right_inv;
            } else {
                left_inv = inv(L + 1, t);
                right_inv = total_inv - left_inv;
            }
            // cout << "data : " << left_inv << " " << right_inv << " " << mid_inv << " " << cr_inv << endl;
            inv_count[R] = right_inv;
            inv_count[t] = left_inv;

            vals.insert(left_inv);
            vals.insert(right_inv);
            z = *vals.rbegin();
            cout << z << ' ';//cout << ":";cout.flush();
        }
        cout << '\n';

        // for(int k = 0; k < n; k++)
        // for(int i = 0; i < n; i++) {
        //     ll val = 0;
        //     for(int j = i; j < n; j++) {
        //         val += a[j] <= k;
        //         ll t = ds.query(i, j, k);
        //         if(val != t) {
        //             cout << k << " : " << i << " " << j << " " << val << " " << t << endl;
        //         }
        //     }
        // }
        buffer.clear();
    }
}

详细

Test #1:

score: 0
Wrong Answer
time: 1ms
memory: 3828kb

input:

3
5
4 3 1 1 1
5 4 5 3 1
10
9 7 1 4 7 8 5 7 4 8
21 8 15 5 9 2 4 5 10 6
15
4 8 8 1 12 1 10 14 7 14 2 9 13 10 3
37 19 23 15 7 2 10 15 2 13 4 5 8 7 10

output:

7 0 0 0 0 0 
20 11 7 2 0 0 0 0 0 0 0 
42 31 21 14 14 4 1 1 1 0 0 0 0 0 0 0 

result:

wrong answer 1st lines differ - expected: '7 0 0 0 0', found: '7 0 0 0 0 0 '