QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#276648#5526. Jewel of Data Structure Problemsucup-team859WA 0ms3596kbC++172.4kb2023-12-06 02:16:452023-12-06 02:16:46

Judging History

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

  • [2023-12-06 02:16:46]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3596kb
  • [2023-12-06 02:16:45]
  • 提交

answer

#include <bits/stdc++.h>

#define lsb(x) (x & (-x))

using ull = unsigned long long;
using ll = long long;

using namespace std;

struct Fenwick {
    Fenwick(int n) {
        this->n = n;
        data.resize(n + 1);
    }

    void add(int pos, int val) {
        for (int i = pos; i <= n; i += lsb(i)) {
            data[i] += val;
        }
    }

    int get(int pos) {
        int answer = 0;
        for (int i = pos; i > 0; i -= lsb(i)) {
            answer += data[i];
        }
        return answer;
    }

    vector<int> data;
    int n;
};

int main() {
#ifdef HOME
    ifstream cin("input.in");
    ofstream cout("output.out");
#endif
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    int n, q;
    cin >> n >> q;

    vector<int> arr(n + 1);
    int placed = 0;
    for (int i = 1; i <= n; i++) {
        cin >> arr[i];
        placed += (arr[i] == i);
    }

    Fenwick pref(n + 1), suff(n + 1);
    vector<int> inv(n + 1);
    for (int i = 1; i <= n; i++) {
        pref.add(arr[i], 1);
        inv[i] = i - pref.get(arr[i]);
    }

    ll answer = 0;
    for (int i = n; i >= 1; i--) {
        inv[i] += suff.get(arr[i]);
        suff.add(arr[i], 1);
        answer += inv[i];
    }

    answer /= 2;
    answer %= 2;

    int odd = 0;
    for (int i = 1; i <= n; i++) {
        inv[i] %= 2;
        odd += inv[i];
    }

    for (int tt = 1; tt <= q; tt++) {
        int l, r;
        cin >> l >> r;
        if (l > r) swap(l, r);

        answer ^= 1;
        placed -= (arr[l] == l);
        placed -= (arr[r] == r);
        swap(arr[l], arr[r]);
        placed += (arr[l] == l);
        placed += (arr[r] == r);
        
        odd -= inv[l];
        odd -= inv[r];

        inv[l] += r - l;
        inv[r] += r - l;
        inv[l] %= 2;
        inv[r] %= 2;

        odd += inv[l];
        odd += inv[r];

        // cerr << tt << " " << answer << " " << placed << " " << odd << "\n";
        // for (int i = 1; i <= n; i++) {
        //     cerr << inv[i] << " ";
        // }
        // cerr << "\n\n";

        if (placed == n) {
            cout << -1 << "\n";
        } else if (answer == 1) {
            cout << n << "\n";
        } else if (odd > 0) {
            cout << n - 1 << "\n";
        } else {
            cout << n - 2 << "\n";
        }
    }

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 3596kb

input:

5 6
2 1 3 4 5
1 2
1 2
1 4
2 1
3 5
1 3

output:

-1
5
4
5
4
5

result:

wrong answer 5th numbers differ - expected: '3', found: '4'