QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#276645 | #5526. Jewel of Data Structure Problems | ucup-team859 | WA | 23ms | 3548kb | C++17 | 2.4kb | 2023-12-06 02:07:40 | 2023-12-06 02:07:41 |
Judging History
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);
odd -= (inv[l] % 2);
odd -= (inv[r] % 2);
inv[l] += r - l;
inv[r] += r - l;
inv[l] %= 2;
inv[r] %= 2;
odd += (inv[l] % 2);
odd += (inv[r] % 2);
// 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;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3432kb
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: 23ms
memory: 3548kb
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: 3548kb
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 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