QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#87766 | #4808. Great Party | codicon | RE | 3ms | 3808kb | C++23 | 6.6kb | 2023-03-14 09:32:48 | 2023-03-14 09:32:49 |
Judging History
answer
//
// Created by codicon on 3/13/2023 at 11:21 AM.
//
// O(N sqrt(Q))
/*
* First, for a given set of piles, S, who wins?
*
* Observation:
* 1 pile:
* First player wins
* 2 piles:
* Let's say that player A performs an operation and now there is only 1 pile left.
* Then, by our solution for 1 pile, player B wins.
*
* Denote the piles where we remove 1 stone from each pile as S'.
* Thus, the winner of the game is the winner of nim on S'.
*
* This is because either:
* The game of nim is played to completion (all piles have 1 stone now).
* Then, the loser removes the final stone from some pile. Now there is 1 pile left.
* or
* The loser completely removes a pile before the game of nim is completed.
* In this case, there is also 1 pile left.
* 3 piles:
* WLOG, say the piles have stones: a < b < c.
* Then, player 1 can simply remove stones from c until there are b-a left.
* Then, they merge those b-a stones into a. Now the piles are (b, b).
*
* Thus, player 1 wins since the game has reduced to the 2 pile case and
* player 1 wins nim on (b-1, b-1).
* 4 piles:
* Again, if player A removes a pile, the game reduces to the 3 pile case and
* the player A loses.
*
* Thus, winner of the game on S' is the winner.
*
* Proof of Full Strategy via Induction:
* It seems like player 1 wins when there are an odd number of piles and
* if there are an even number of piles, the winner is the winner of nim on S'.
*
* Base Case:
* For n = 1 the statement is true.
*
* Inductive Hypothesis:
* Let n >= 1 such that the statement is true for n.
*
* Inductive Step:
* Let's prove the statement for n+1
*
* If n is odd:
* No player wants to remove any pile and thus the winner is the winner of nim on S'.
*
* If n is even:
* Let's prove that it is always possible for the first player to
* remove stones from some pile and then merge the remaining stones into
* some other pile such that they would win nim on S'.
*
* For player 1 to win nim on S', the xor-sum after they move must be 0.
*
* Consider how the operations affect the pile sizes and thus xor-sum on S':
* We have to pick some pile X (has X+1 stones in S). Then, remove X and add
* up to X stones to some pile A (has A+1 stones in S). We need the xor-sum = 0.
* Thus, we must change A to A xor (S' xor X). Here S' xor X denotes xor-sum(S') xor X.
*
* This means we can do so iff A <= (A xor (S' xor X)) <= A + X.
*
* Realize that xoring A by (S' xor X) can increase A by at most (S' xor X).
* Accordingly, let's start by finding a pile X such that X >= S' xor X.
* Thus, we will at least be able to ensure:
* A xor (S' xor X) <= A + X.
*
* Let sum(i), be the sum of the ith bits of all piles in S'.
*
* Then, iterate i from the most significant bit to the least.
* If sum(i) is even, continue.
* Otherwise, sum(i) is odd, so find any pile X such that
* the ith bit is on.
*
* This means X >= S' xor X since they have the same bits in positions > i
* and X has the ith bit on whilst S' xor X does not.
*
* If we reach i = 1 and sum(i) is even, then that just means
* X = S' xor X for any X, so X >= S' xor X.
*
* Now, to satisfy the entire condition: A <= A xor (S' xor X) <= A + X,
* we need to satisfy A <= A xor (S' xor X).
* Now, the cool part is that since n is even, we can find a pile A
* that satisfies the condition for any X!
*
* Let i be the most significant on bit in (S' xor X)
* Since n is even, there must exist at least one pile A such that
* the ith bit is off.
* Thus, A < A xor (S' xor X), so A <= A xor (S' xor X).
*
* If there is no most significant on bit in (S' xor X), then (S' xor X) = 0.
* Thus, A xor (S' xor X) = A, so A <= A xor (S' xor X).
*
* Thus, we've proved that if n is even, we can find pile X and A in S' such that
* we can remove some stones from pile X and add the remaining to A such that xor-sum(S') = 0.
*
* Full Solution:
* To actually answer queries, we just need to use Mo's algorithm to maintain:
* 1. The number of odd length subarrays
* 2. The number of even length subarrays with xor-sum != 0.
*/
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5, MAXXOR = 1.1e6, SQRTN = 300;
int n, q;
int a[MAXN + 1];
long long ans[MAXN];
int pxor[MAXN + 1];
int pxor_cnt[2][MAXXOR + 1];
struct query {
int i, l, r;
bool operator < (const query other) const {
return make_pair(i/SQRTN, r) < make_pair(other.l/SQRTN, other.r);
}
};
vector<query> queries;
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> q;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 0; i < q; i++) {
int l, r; cin >> l >> r;
queries.push_back({i, l, r});
}
for (int i = 1; i <= n; i++) {
pxor[i] = (a[i]-1) ^ pxor[i-1];
}
// Mo's
sort(queries.begin(), queries.end());
int l = 1, r = 0;
pxor_cnt[0][0]++;
long long cans = 0;
auto add = [&](int idx, bool left) {
cans += (r-l+1)/2 - pxor_cnt[idx-left&1][pxor[idx-left]]++; // even length, diff xor
cans += (r-l+2)/2; // odd length
};
auto remove = [&](int idx, bool left) {
cans -= (r-l+1)/2 - --pxor_cnt[idx-left&1][pxor[idx-left]]; // even length, diff xor
cans -= (r-l+2)/2; // odd length
};
for (auto q: queries) {
while (q.l < l) {
l--;
add(l, true);
}
while (q.r > r) {
r++;
add(r, false);
}
while (q.l > l) {
remove(l, true);
l++;
}
while (q.r < r) {
remove(r, false);
r--;
}
ans[q.i] = cans;
}
for (int i = 0; i < q; i++) {
cout << ans[i] << '\n';
}
}
详细
Test #1:
score: 100
Accepted
time: 2ms
memory: 3492kb
input:
4 5 1 2 2 4 1 2 2 3 3 4 1 3 2 4
output:
3 2 3 5 5
result:
ok 5 number(s): "3 2 3 5 5"
Test #2:
score: 0
Accepted
time: 2ms
memory: 3380kb
input:
4 5 5 6 7 8 1 2 2 3 3 4 1 3 2 4
output:
3 3 3 6 6
result:
ok 5 number(s): "3 3 3 6 6"
Test #3:
score: 0
Accepted
time: 0ms
memory: 3500kb
input:
10 10 3 7 3 1 6 6 10 3 3 3 9 10 5 10 3 7 5 6 5 6 9 10 3 10 1 4 6 6 1 4
output:
2 18 14 2 2 2 33 10 1 10
result:
ok 10 numbers
Test #4:
score: 0
Accepted
time: 1ms
memory: 3404kb
input:
10 8 91 63 1 34 50 11 10 73 96 67 5 9 2 7 2 5 4 7 1 10 3 3 1 4 5 10
output:
15 21 10 10 55 1 10 21
result:
ok 8 numbers
Test #5:
score: 0
Accepted
time: 0ms
memory: 3420kb
input:
10 6 9 539 285 408 615 861 951 413 319 368 4 4 8 10 1 7 3 9 2 3 2 10
output:
1 6 28 28 3 45
result:
ok 6 numbers
Test #6:
score: 0
Accepted
time: 2ms
memory: 3396kb
input:
10 6 1348 7002 4687 6325 8253 5750 2464 5509 6543 8704 3 9 4 8 8 8 8 9 2 9 9 10
output:
28 15 1 3 36 3
result:
ok 6 numbers
Test #7:
score: 0
Accepted
time: 2ms
memory: 3444kb
input:
10 8 59041 28802 92255 14246 65768 79252 70656 81265 98363 85237 1 6 9 10 4 7 6 8 9 10 1 2 1 3 4 5
output:
21 3 10 6 3 3 6 3
result:
ok 8 numbers
Test #8:
score: 0
Accepted
time: 1ms
memory: 3444kb
input:
10 7 28607 249948 373828 584253 989446 308313 199311 253174 283937 133758 2 4 1 2 4 9 7 8 7 8 2 6 1 1
output:
6 3 21 3 3 15 1
result:
ok 7 numbers
Test #9:
score: 0
Accepted
time: 2ms
memory: 3416kb
input:
100 98 6 9 6 10 8 10 3 4 7 5 4 10 2 10 4 5 2 1 7 1 3 1 4 1 1 2 6 9 3 10 2 5 3 2 6 2 1 7 7 6 5 4 2 5 3 2 7 2 6 2 9 7 10 7 4 2 9 3 3 7 9 1 4 9 6 1 5 5 8 3 7 5 8 3 9 5 8 7 8 6 6 3 2 3 8 1 8 1 5 9 1 8 6 3 3 7 10 6 5 5 48 72 14 46 23 28 37 84 1 65 45 72 9 19 9 81 37 53 47 50 25 26 26 88 51 54 53 69 22 94...
output:
316 534 20 1144 2054 394 64 2596 149 10 3 1944 10 148 2598 35 169 6 28 318 605 2964 366 4571 2815 130 1220 52 160 2964 129 3040 793 1491 89 1932 227 64 77 420 332 646 267 1081 902 956 2668 2191 449 149 223 5 3763 1312 102 1221 578 2188 289 681 367 960 1 226 151 720 116 101 3348 905 132 1 88 1140 199...
result:
ok 98 numbers
Test #10:
score: 0
Accepted
time: 2ms
memory: 3412kb
input:
100 96 78 64 64 32 42 65 94 54 35 27 83 6 6 90 86 45 93 72 56 80 5 31 91 87 56 10 100 79 53 69 98 93 79 28 89 5 14 4 12 100 50 98 99 6 29 38 85 37 34 85 15 76 73 7 91 93 28 10 74 23 96 77 27 84 21 79 62 69 14 81 18 93 1 23 11 1 13 72 8 91 11 45 50 67 21 42 55 44 95 57 4 85 17 96 84 46 22 56 19 58 1 ...
output:
2761 1029 4167 3989 28 90 626 625 2614 494 942 626 253 2200 171 3389 250 4539 251 941 558 55 3308 251 1372 349 55 662 2688 737 378 66 699 136 2335 44 90 151 856 626 1270 45 1121 815 406 1477 77 228 525 1760 45 210 629 2070 66 1760 9 463 15 1220 557 591 190 699 463 28 816 1076 118 44 492 44 1942 2616...
result:
ok 96 numbers
Test #11:
score: 0
Accepted
time: 2ms
memory: 3376kb
input:
100 98 354 899 868 821 878 486 413 896 872 129 97 801 838 279 971 756 250 24 245 883 731 887 106 204 381 204 678 33 340 987 80 662 845 955 39 654 989 24 105 822 704 690 573 800 243 372 939 338 986 246 983 445 196 931 290 150 610 635 809 496 779 722 596 920 67 16 391 728 575 975 469 413 82 768 680 43...
output:
21 741 21 253 1275 325 465 703 3 276 105 495 465 3 136 1224 1652 1 66 780 253 1225 2484 55 1484 120 528 1175 1484 45 300 496 1378 3239 465 1 190 630 2850 1830 1952 325 3 1 36 1325 153 435 231 1225 406 6 153 465 28 105 351 666 1769 253 2211 1891 819 406 2345 496 1830 21 528 300 2628 10 2016 6 1710 10...
result:
ok 98 numbers
Test #12:
score: 0
Accepted
time: 2ms
memory: 3488kb
input:
100 98 4940 7560 6878 1066 6510 7648 7900 5423 1384 5142 6451 2335 6486 5411 5312 3059 1974 7844 4678 9963 926 8585 6379 1352 179 919 4870 3315 1907 8327 519 9396 1628 9268 7695 9932 9154 1056 2772 3916 4397 5712 5614 7771 2968 6540 6205 2692 6900 3920 6688 2278 7843 7293 8751 5538 2638 6424 5234 27...
output:
861 351 528 2415 21 1176 2278 78 3 45 465 903 55 1035 45 171 190 120 2145 105 3 465 28 703 3570 210 66 3081 861 561 15 1081 2145 3 3 91 378 210 15 630 1596 666 990 703 66 55 45 820 55 1431 465 4186 78 276 153 136 3 231 2556 861 666 1540 300 15 15 325 2016 1128 2415 105 2850 496 55 55 1225 1770 780 2...
result:
ok 98 numbers
Test #13:
score: 0
Accepted
time: 1ms
memory: 3724kb
input:
100 98 79873 84742 75872 96693 10191 985 31311 53705 98909 81177 17919 79656 53853 81691 50226 50507 73858 61819 29518 85453 34982 53653 93823 31917 3269 21003 95730 52976 65728 25570 21628 88764 20155 36740 90961 65470 511 72479 12397 89701 31897 5413 84398 51940 52729 43338 40696 80980 67508 12297...
output:
2850 276 210 91 1891 1081 3 2485 946 2850 10 136 465 28 136 36 3741 496 1176 55 1378 45 45 465 105 2485 3 741 465 1596 1225 136 1891 1225 1378 231 351 3081 136 861 946 2278 45 1953 171 406 6 21 703 105 3570 6 91 703 1891 4278 741 105 210 1225 990 435 3828 66 1540 2080 91 3160 66 946 351 55 28 1378 2...
result:
ok 98 numbers
Test #14:
score: 0
Accepted
time: 2ms
memory: 3808kb
input:
100 97 870479 594266 17979 445914 279977 701082 808468 978143 124772 925808 486496 725792 13151 914792 840639 180601 194842 81935 895414 306762 943164 688186 185644 68600 41614 718973 668908 952621 806142 891060 222118 647764 302429 649853 411693 527647 102818 427196 466555 290753 496356 16854 65968...
output:
171 2016 1081 6 3 465 2278 1 2775 3570 703 36 1378 595 15 15 36 2016 4095 45 36 1128 630 3403 406 741 36 1830 1770 21 1711 1081 153 2926 2701 780 136 595 91 595 2926 210 15 36 1485 171 325 903 171 528 351 2628 1711 300 435 2775 190 78 3 1540 6 10 903 10 820 15 2278 2926 91 2346 496 210 1035 3655 122...
result:
ok 97 numbers
Test #15:
score: 0
Accepted
time: 2ms
memory: 3376kb
input:
1000 998 7 7 4 6 4 2 1 1 4 6 7 8 8 6 2 7 8 8 5 4 9 1 1 9 6 6 9 5 3 3 10 3 9 6 9 10 2 8 8 2 7 8 3 7 6 4 7 5 9 3 7 1 4 8 3 9 1 1 3 6 10 5 7 3 5 3 9 5 10 9 3 2 9 10 9 7 4 7 5 6 6 6 4 5 2 10 8 2 9 8 7 10 5 2 4 9 7 4 6 9 9 7 3 1 5 3 6 2 6 1 2 6 4 6 8 7 10 2 1 3 2 10 8 5 2 8 2 9 8 3 1 1 4 3 1 2 9 8 2 2 2 ...
output:
52639 55406 72778 54893 31854 3069 4317 385800 65839 83181 191315 12145 14934 27816 39539 80779 151139 242323 57860 14575 225042 8888 9297 130307 106835 20703 71224 46746 4052 2153 101782 16162 2619 2957 31847 145806 174061 63334 173537 124849 6000 38902 14911 17210 104593 14245 96191 41493 47304 73...
result:
ok 998 numbers
Test #16:
score: 0
Accepted
time: 3ms
memory: 3372kb
input:
1000 996 51 45 23 20 19 16 22 67 57 30 61 32 80 46 38 76 62 6 22 74 7 25 20 16 70 78 100 47 39 39 17 63 46 2 11 28 75 51 58 47 12 32 66 62 7 8 57 59 100 29 32 81 11 90 26 16 88 57 39 52 10 6 94 5 93 70 67 26 75 37 6 98 51 88 11 29 97 28 36 41 63 62 66 82 8 32 52 17 95 46 14 2 60 51 15 54 20 80 63 78...
output:
2402 70241 57082 16389 460411 87228 24207 6081 29295 464 23109 1029 76305 816 21438 281265 5750 28316 24211 209 20626 70231 67599 16767 76337 75565 95332 69830 79912 23995 104667 156491 1704 8737 115967 173075 191139 26702 9830 363626 147079 54719 402035 75144 126292 1426 36965 114954 395816 23565 8...
result:
ok 996 numbers
Test #17:
score: 0
Accepted
time: 3ms
memory: 3620kb
input:
1000 1000 646 746 291 354 931 823 84 484 142 439 893 10 999 669 753 794 503 753 459 997 46 375 445 106 365 862 458 833 445 387 76 536 430 347 87 577 649 308 764 674 799 229 623 259 159 105 385 108 380 808 79 149 972 318 377 260 555 835 915 882 542 73 647 190 864 41 850 361 937 318 659 392 18 15 279 ...
output:
1430 147618 3569 106439 56590 12244 189327 6782 5250 7996 32359 122703 4464 84206 11320 210 265961 45 40450 33914 29383 19295 45 20296 171310 350 5050 5459 2413 2483 313084 55 6104 4003 3399 14019 28190 37926 230743 73497 199922 7136 28 60348 49120 3485 5993 18903 39035 78159 1225 2016 90473 3398 18...
result:
ok 1000 numbers
Test #18:
score: -100
Runtime Error
input:
1000 996 8525 156 5909 2878 6958 7610 1178 9088 2285 9626 2329 4812 7629 3037 137 9397 6502 8279 1731 3105 9715 1633 2117 3898 3588 7368 3409 8272 8877 7020 3515 6534 4005 4434 2286 1872 4184 3778 1591 3435 6662 5849 9512 2133 9650 6969 7399 2641 2915 8740 1741 7508 5142 725 2919 795 7227 951 5816 4...