QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#135807 | #5526. Jewel of Data Structure Problems | UNos_maricones# | WA | 17ms | 4348kb | C++20 | 2.0kb | 2023-08-06 02:47:33 | 2023-08-06 02:47:37 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std ;
#define ff first
#define ss second
#define pb push_back
typedef long long ll;
typedef pair<ll,ll> ii;
typedef long double lf;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll SQ = 320;
const ll N = 2e5+5;
const ll mod = 998244353;
const ll oo = 1e18;
int bit[N];
void update (int i, int x) {
for (++i; i < N; i += (i & (-i))) bit[i] += x;
}
int query (int i) {
int sum = 0;
for (++i; i; i -= (i & (-i))) sum += bit[i];
return sum;
}
int main(){
#ifdef LOCAL
freopen("input.txt","r",stdin);
#endif // LOCAL
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int n, q; cin >> n >> q;
vector <int> p(n);
for (auto &e : p) cin >> e;
int cnt = 0;
vector <int> parity(n);
vector <int> val(n);
for (int i = 0; i < n; ++i) cnt += (p[i] == i + 1);
int invs = 0;
for (int i = n - 1; i >= 0; --i) {
int x = query(p[i]);
val[i] += x;
invs += x;
update (p[i], 1);
}
for (int i = 0; i < N; ++i) bit[i] = 0;
int nodd = 0;
for (int i = 0; i < n; ++i) {
int x = query(n - p[i]);
val[i] += x;
parity[i] = val[i]%2;
nodd+=parity[i];
// cout << i << ' ' << val[i] << '\n';
update(n - p[i], 1);
}
// cout << invs << '\n';
for (int i = 0; i < q; ++i) {
int u, v; cin >> u >> v;
u--;v--;
cnt -= (p[u] == u + 1);
cnt -= (p[v] == v + 1);
nodd -= (parity[u] + parity[v]);
swap(p[u], p[v]);
cnt += (p[u] == u + 1);
cnt += (p[v] == v + 1);
if (abs(v - u) % 2) {
int sv = parity[v];
parity[v] = parity[u] ^ 1;
parity[u] = sv ^ 1;
nodd += (parity[u] + parity[v]);
}
if (cnt == n) cout << -1 << '\n';
else {
if (invs % 2 == i % 2) cout << n << '\n';
else {
if (nodd) 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: 4248kb
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: 17ms
memory: 4348kb
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: 14ms
memory: 4300kb
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 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