QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#108658#5526. Jewel of Data Structure ProblemsLG_MonkeyTL 2ms3312kbC++141.1kb2023-05-25 22:16:362023-05-25 22:16:39

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-05-25 22:16:39]
  • 评测
  • 测评结果:TL
  • 用时:2ms
  • 内存:3312kb
  • [2023-05-25 22:16:36]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define pii pair<int, int>
#define mp make_pair
#define fi first
#define se second 
#define deb(var) cerr << #var << '=' << (var) << "; "
#define int long long
int n, q, a[200010];
int cnt[2];
int w[200010];
void add(int p, int x) {
	for (int i = p; i <= n; i += i & (-i)) w[i] += x;
}
int qry(int p) {
	int res = 0;
	for (int i = p; i; i -= i & (-i)) res += w[i];
	return res;
}
int rev = 0, flg = 0;
void rem(int p) {
	flg -= a[p] == p;
	cnt[(a[p] + 10000000 - p) & 1]--; 
}
void upd(int p) {
	flg += a[p] == p;
	cnt[(a[p] + 10000000 - p) & 1]++; 
}
signed main() {
	cin >> n >> q;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
		flg += a[i] == i;
		cnt[(a[i] + 10000000 - i) & 1]++;
		rev += qry(n) - qry(a[i]); add(a[i], 1);
	}
	rev &= 1;
	while (q--) {
		int u, v;
		cin >> u >> v;
		rem(u); rem(v);
		swap(a[u], a[v]);
		upd(u); upd(v);
		rev ^= 1;
		if (rev) {
			cout << n << "\n";
		} else {
			if (cnt[1]) {
				cout << n - 1 << "\n";
			} else {
				if (flg == n) cout << "-1\n";
				else {
					cout << n - 2 << "\n";
				}
			}
		}
	}
}

详细

Test #1:

score: 100
Accepted
time: 2ms
memory: 3312kb

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: -100
Time Limit Exceeded

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: