QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#99444#5526. Jewel of Data Structure Problemswillow#TL 926ms24596kbC++142.3kb2023-04-22 15:50:162023-04-22 15:50:18

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-04-22 15:50:18]
  • 评测
  • 测评结果:TL
  • 用时:926ms
  • 内存:24596kb
  • [2023-04-22 15:50:16]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
int n, q, a[maxn], ans[maxn], val[maxn];
void Add(int x) {
	for(; x <= n; x += x & -x)
		++ val[x];
}
int Ask(int x) {
	int res = 0;
	for(; x; x -= x & -x)
		res += val[x];
	return res;
}
int cntodd, cnti;
void Mod(int x, int d) {
	if(ans[a[x]] & 1)
		cntodd += d;
	if(x == a[x])
		cnti += d;
}
int sum[maxn << 6], lc[maxn << 6], rc[maxn << 6], rt[maxn], tot;
void Upd(int &o, int l, int r, int p, int d) {
	if(!o)
		o = ++ tot;
	sum[o] += d;
	if(l == r)
		return;
	int mid = (l + r) >> 1;
	if(p <= mid)
		Upd(lc[o], l, mid, p, d);
	else
		Upd(rc[o], mid + 1, r, p, d);
}
int Que(int o, int l, int r, int ql, int qr) {
	if(!o)
		return 0;
	if(ql <= l && r <= qr)
		return sum[o];
	int mid = (l + r) >> 1, ans = 0;
	if(ql <= mid)
		ans += Que(lc[o], l, mid, ql, qr);
	if(qr > mid)
		ans += Que(rc[o], mid + 1, r, ql, qr);
	return ans;
}
void Update(int x, int v, int d) {
	for(; x <= n; x += x & -x)
		Upd(rt[x], 1, n, v, d);
}
int Query(int x, int l, int r) {
	int ans = 0;
	for(; x; x -= x & -x)
		ans += Que(rt[x], 1, n, l, r);
	return ans;
}
int main() {
	scanf("%d%d", &n, &q);
	long long all = 0;
	for(int i = 1; i <= n; ++ i) {
		scanf("%d", &a[i]);
		int w = Ask(n) - Ask(a[i]);
		all += w;
		ans[a[i]] += w;
		Add(a[i]);
	}
	memset(val, 0, sizeof val);
	for(int i = n; i; -- i) {
		ans[a[i]] += Ask(a[i]);
		Add(a[i]);
	}
	for(int i = 1; i <= n; ++ i)
		Mod(i, 1), Update(i, a[i], 1);
	for(int u, v; q --; ) {
		scanf("%d%d", &u, &v);
		Mod(u, -1), Mod(v, -1);
		Update(u, a[u], -1), Update(v, a[v], -1);
		swap(a[u], a[v]);
		Update(u, a[u], 1), Update(v, a[v], 1);
		// ans[a[u]] ^= 1, ans[a[v]] ^= 1;
		ans[a[u]] = a[u] - 1 - Query(u, 1, a[u] - 1) + Query(u, a[u] + 1, n), ans[a[v]] = a[v] - 1 - Query(v, 1, a[v] - 1) + Query(v, a[v] + 1, n);
// cerr << ans[a[u]] << " " << ans[a[v]] << endl;
		Mod(u, 1), Mod(v, 1);
		all ^= 1;
// cerr << all << " " << cnti << " " << cntodd << endl;
// for(int i = 1; i <= n; ++ i)
// 	cerr << a[i] << " ";
// cerr << endl;
// for(int i = 1; i <= n; ++ i)
// 	cerr << ans[i] << " ";
// cerr << endl;
		if(cnti == n) {
			puts("-1");
		}
		else if(all & 1) {
			printf("%d\n", n);
		}
		else if(cntodd) {
			printf("%d\n", n - 1);
		}
		else {
			printf("%d\n", n - 2);
		}
	}
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 8068kb

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: 38ms
memory: 6588kb

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
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
-1
3
2
3
-1
3
2
3
2
3
2
3
-1
3
-1
3
2
...

result:

ok 200000 numbers

Test #4:

score: 0
Accepted
time: 58ms
memory: 7672kb

input:

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

output:

4
-1
4
3
4
3
4
3
4
-1
4
3
4
3
4
2
4
3
4
3
4
3
4
3
4
3
4
3
4
3
4
-1
4
-1
4
2
4
3
4
3
4
2
4
2
4
3
4
3
4
-1
4
-1
4
3
4
-1
4
3
4
3
4
-1
4
-1
4
3
4
3
4
3
4
2
4
2
4
3
4
3
4
-1
4
3
4
3
4
3
4
3
4
3
4
3
4
3
4
3
4
2
4
3
4
3
4
3
4
3
4
3
4
3
4
3
4
3
4
3
4
3
4
3
4
3
4
3
4
3
4
3
4
3
4
3
4
3
4
3
4
3
4
3
4
3
4
3
4
...

result:

ok 200000 numbers

Test #5:

score: 0
Accepted
time: 71ms
memory: 6356kb

input:

5 200000
5 2 4 3 1
3 2
2 5
5 3
4 3
5 4
2 1
4 1
2 4
4 5
2 4
5 1
2 3
1 3
3 4
1 4
2 5
5 4
4 1
3 1
2 3
5 2
1 4
3 4
5 2
4 2
2 3
5 4
1 2
2 4
2 5
4 5
1 2
3 4
1 2
2 1
3 2
3 4
5 2
1 3
4 1
3 1
4 1
5 3
3 5
1 5
1 3
3 4
3 1
2 4
2 4
3 2
3 2
5 2
4 1
4 5
5 1
5 4
1 5
4 5
3 2
3 5
4 1
3 2
3 2
4 3
3 4
2 5
5 1
1 3
4 3
4...

output:

5
4
5
4
5
4
5
4
5
4
5
4
5
4
5
3
5
4
5
4
5
4
5
4
5
4
5
4
5
4
5
4
5
4
5
3
5
4
5
4
5
4
5
4
5
4
5
4
5
4
5
4
5
4
5
4
5
3
5
4
5
4
5
4
5
4
5
4
5
4
5
4
5
4
5
4
5
4
5
4
5
4
5
4
5
4
5
4
5
4
5
4
5
4
5
3
5
4
5
4
5
4
5
-1
5
4
5
4
5
-1
5
4
5
4
5
4
5
4
5
4
5
4
5
4
5
4
5
4
5
4
5
4
5
4
5
4
5
4
5
4
5
4
5
4
5
3
5
4
5
...

result:

ok 200000 numbers

Test #6:

score: 0
Accepted
time: 67ms
memory: 7656kb

input:

6 200000
4 2 5 3 6 1
1 2
4 6
5 4
1 6
6 5
4 2
5 3
6 2
6 5
1 4
6 3
6 5
2 3
4 5
4 1
3 6
5 6
2 4
3 2
2 3
6 1
1 3
1 3
3 6
1 6
2 5
3 4
1 4
4 1
4 6
3 5
6 2
6 5
4 1
5 6
5 4
1 6
2 4
6 3
1 3
5 2
1 6
1 3
1 3
3 6
6 5
3 2
6 4
6 4
3 2
3 1
5 3
6 3
6 5
3 5
2 5
4 2
1 5
1 2
3 4
3 2
4 6
3 5
2 1
5 4
1 4
5 3
1 5
5 4
3 1...

output:

6
5
6
4
6
5
6
5
6
5
6
5
6
5
6
5
6
5
6
5
6
5
6
5
6
5
6
5
6
5
6
5
6
5
6
5
6
5
6
5
6
5
6
5
6
5
6
5
6
5
6
5
6
4
6
5
6
5
6
5
6
5
6
5
6
5
6
5
6
5
6
5
6
5
6
5
6
5
6
5
6
5
6
5
6
5
6
5
6
5
6
5
6
5
6
5
6
5
6
5
6
5
6
5
6
5
6
5
6
5
6
5
6
5
6
5
6
5
6
5
6
5
6
4
6
5
6
5
6
4
6
4
6
5
6
5
6
5
6
5
6
5
6
4
6
5
6
5
6
5
...

result:

ok 200000 numbers

Test #7:

score: 0
Accepted
time: 76ms
memory: 6396kb

input:

7 200000
6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7
8
7
8
7
8
7
8
7
8
7
8
7
8
7
8
7
8
7
8
7
8
7
8
7
8
7
8
7
8
7
8
7
8
7
8
7
8
7
8
7
8
7
8
7
8
7
8
7
8
7
8
7
8
7
8
7
8
7
8
7
8
7
8
7
8
7
8
7
8
7
8
7
8
7
8
7
8
7
8
7
8
7
8
7
8
7
8
7
8
7
8
7
8
7
8
7
8
7
8
7
8
7
8
7
8
7
8
7
8
7
...

result:

ok 200000 numbers

Test #9:

score: 0
Accepted
time: 84ms
memory: 6448kb

input:

9 200000
4 3 8 9 2 1 7 5 6
9 6
1 6
4 1
7 3
7 9
5 8
6 2
1 4
2 3
3 8
5 8
7 4
6 4
3 7
9 8
3 8
9 5
9 3
6 3
7 8
1 6
1 9
2 3
7 6
9 1
9 5
1 6
9 7
6 7
3 5
6 7
5 7
7 6
2 6
2 6
8 4
2 8
3 9
5 8
3 9
6 2
6 9
8 5
2 5
6 5
8 5
6 2
1 5
7 6
6 9
2 8
6 9
2 5
8 9
6 8
2 5
1 7
3 2
9 1
6 7
8 9
3 7
5 3
4 7
7 2
8 2
2 3
8 2
6...

output:

9
8
9
8
9
8
9
8
9
8
9
8
9
8
9
8
9
8
9
8
9
8
9
8
9
8
9
8
9
8
9
8
9
8
9
8
9
8
9
8
9
8
9
8
9
8
9
8
9
8
9
8
9
8
9
8
9
8
9
8
9
8
9
8
9
8
9
8
9
8
9
8
9
8
9
8
9
8
9
8
9
8
9
8
9
8
9
8
9
8
9
8
9
8
9
8
9
8
9
8
9
8
9
8
9
8
9
8
9
8
9
8
9
8
9
8
9
8
9
8
9
8
9
8
9
8
9
8
9
8
9
8
9
8
9
8
9
8
9
8
9
8
9
8
9
8
9
8
9
8
...

result:

ok 200000 numbers

Test #10:

score: 0
Accepted
time: 91ms
memory: 6440kb

input:

10 200000
7 10 4 9 1 6 2 3 5 8
4 9
8 7
2 3
6 10
4 5
7 6
5 6
2 6
10 1
7 5
9 10
8 9
6 9
8 5
2 3
5 1
5 7
5 4
1 9
7 4
2 7
8 6
3 10
1 2
4 1
1 5
5 8
5 7
10 3
2 7
1 5
8 10
10 6
8 10
10 4
1 10
5 4
5 10
2 10
6 5
8 6
8 1
8 9
2 4
4 2
10 9
9 8
2 8
4 1
7 10
9 7
10 9
7 3
2 6
4 8
5 9
6 9
1 2
1 5
10 6
1 2
5 10
10 7...

output:

10
9
10
9
10
9
10
9
10
9
10
9
10
9
10
9
10
9
10
9
10
9
10
9
10
9
10
9
10
9
10
9
10
9
10
9
10
9
10
9
10
9
10
9
10
9
10
9
10
9
10
9
10
9
10
9
10
9
10
9
10
9
10
9
10
9
10
9
10
9
10
9
10
9
10
9
10
9
10
9
10
9
10
9
10
9
10
9
10
9
10
9
10
9
10
9
10
9
10
9
10
9
10
9
10
9
10
9
10
9
10
9
10
9
10
9
10
9
10
9
...

result:

ok 200000 numbers

Test #11:

score: 0
Accepted
time: 912ms
memory: 24596kb

input:

1000 200000
82 684 685 362 991 147 175 795 885 927 938 576 958 210 494 72 823 989 662 585 461 853 955 282 310 348 861 735 249 988 994 923 513 153 496 598 776 273 965 587 833 157 244 722 30 102 935 571 432 488 211 624 121 302 867 57 588 106 901 393 394 626 363 70 887 331 870 83 708 891 46 275 193 702...

output:

1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
100...

result:

ok 200000 numbers

Test #12:

score: 0
Accepted
time: 890ms
memory: 23148kb

input:

1000 200000
828 824 777 731 21 589 502 672 335 762 265 349 612 24 17 713 753 324 751 69 827 579 505 469 495 846 245 382 439 415 741 169 30 347 199 730 422 742 810 97 645 253 1 372 926 865 852 629 215 2 187 340 791 362 554 802 452 685 866 370 584 592 19 100 522 350 847 702 473 33 927 102 430 296 755 ...

output:

1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
100...

result:

ok 200000 numbers

Test #13:

score: 0
Accepted
time: 869ms
memory: 24412kb

input:

1000 200000
246 350 704 1 73 614 451 260 652 511 954 926 565 673 697 366 736 457 802 548 106 122 488 588 31 628 700 444 118 127 196 844 346 38 512 455 193 781 237 923 572 792 74 796 828 345 57 927 754 941 456 869 544 286 272 764 689 631 416 292 702 774 807 962 192 321 806 984 490 951 330 812 406 49 ...

output:

999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999...

result:

ok 200000 numbers

Test #14:

score: 0
Accepted
time: 926ms
memory: 21084kb

input:

1000 200000
858 424 321 709 582 405 422 646 542 18 17 972 681 365 13 376 735 236 848 522 404 569 540 66 245 228 73 721 765 918 108 977 814 258 436 817 216 359 929 67 179 387 901 949 344 793 902 733 923 68 44 518 626 76 830 99 890 759 107 669 498 134 914 366 724 620 935 202 83 226 196 909 195 570 296...

output:

1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
100...

result:

ok 200000 numbers

Test #15:

score: -100
Time Limit Exceeded

input:

1000 200000
195 622 863 16 431 576 39 212 950 47 185 490 855 277 272 433 671 854 793 636 226 705 547 773 351 420 681 778 954 15 980 971 270 996 489 795 509 590 789 167 545 199 343 13 700 285 561 400 599 626 784 600 846 313 326 382 514 556 359 213 273 215 562 619 573 191 744 815 148 464 575 959 817 9...

output:

1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
1000
999
100...

result: