QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#276641#5526. Jewel of Data Structure Problemsucup-team859WA 21ms3556kbC++172.3kb2023-12-06 02:00:102023-12-06 02:00:11

Judging History

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

  • [2023-12-06 02:00:11]
  • 评测
  • 测评结果:WA
  • 用时:21ms
  • 内存:3556kb
  • [2023-12-06 02:00:10]
  • 提交

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];
        inv[i] %= 2;
    }

    answer /= 2;
    answer %= 2;

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

    for (int tt = 1; tt <= q; tt++) {
        int l, r;
        cin >> 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);

        if (placed == n) {
            cout << -1 << "\n";
        } else {
            odd -= (inv[l] % 2);
            odd -= (inv[r] % 2);

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

            odd += (inv[l] % 2);
            odd += (inv[r] % 2);

            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: 100
Accepted
time: 1ms
memory: 3428kb

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
3
5

result:

ok 6 numbers

Test #2:

score: 0
Accepted
time: 20ms
memory: 3440kb

input:

2 200000
1 2
2 1
2 1
2 1
1 2
1 2
2 1
1 2
2 1
2 1
1 2
2 1
2 1
2 1
2 1
2 1
2 1
1 2
2 1
2 1
1 2
1 2
2 1
1 2
1 2
1 2
1 2
2 1
1 2
1 2
1 2
2 1
2 1
1 2
2 1
2 1
2 1
2 1
1 2
1 2
2 1
2 1
1 2
2 1
1 2
1 2
2 1
1 2
1 2
2 1
2 1
1 2
1 2
2 1
2 1
2 1
1 2
1 2
1 2
1 2
2 1
1 2
2 1
1 2
1 2
2 1
1 2
2 1
1 2
2 1
2 1
2 1
2 1...

output:

2
-1
2
-1
2
-1
2
-1
2
-1
2
-1
2
-1
2
-1
2
-1
2
-1
2
-1
2
-1
2
-1
2
-1
2
-1
2
-1
2
-1
2
-1
2
-1
2
-1
2
-1
2
-1
2
-1
2
-1
2
-1
2
-1
2
-1
2
-1
2
-1
2
-1
2
-1
2
-1
2
-1
2
-1
2
-1
2
-1
2
-1
2
-1
2
-1
2
-1
2
-1
2
-1
2
-1
2
-1
2
-1
2
-1
2
-1
2
-1
2
-1
2
-1
2
-1
2
-1
2
-1
2
-1
2
-1
2
-1
2
-1
2
-1
2
-1
2
-1
...

result:

ok 200000 numbers

Test #3:

score: -100
Wrong Answer
time: 21ms
memory: 3556kb

input:

3 200000
2 1 3
2 1
1 3
2 3
2 3
1 3
2 1
2 1
1 3
1 2
3 1
3 1
2 1
1 2
2 1
2 3
2 1
1 3
1 2
1 2
2 3
1 2
2 1
3 2
3 2
1 3
3 2
1 3
2 1
2 1
3 2
2 1
1 3
1 2
1 2
3 1
2 3
2 1
3 2
3 1
1 2
1 2
2 3
1 2
1 2
3 2
3 1
1 2
3 1
1 2
1 3
1 2
2 3
2 3
3 2
2 1
1 3
2 1
3 1
2 1
3 1
3 1
2 3
1 3
2 1
3 2
2 1
3 1
2 3
3 1
2 3
1 3
1...

output:

-1
3
1
3
-1
3
-1
3
1
3
1
3
1
3
1
3
1
3
1
3
-1
3
1
3
1
3
-1
3
-1
3
1
3
-1
3
1
3
1
3
1
3
1
3
1
3
1
3
-1
3
1
3
1
3
1
3
1
3
1
3
-1
3
-1
3
1
3
1
3
1
3
1
3
-1
3
1
3
-1
3
-1
3
-1
3
1
3
-1
3
-1
3
1
3
1
3
1
3
1
3
-1
3
1
3
1
3
1
3
-1
3
-1
3
-1
3
-1
3
1
3
1
3
1
3
1
3
-1
3
-1
3
1
3
-1
3
1
3
1
3
1
3
-1
3
-1
3
1
...

result:

wrong answer 3rd numbers differ - expected: '2', found: '1'