QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#87743 | #4808. Great Party | codicon | WA | 3ms | 3560kb | C++23 | 6.7kb | 2023-03-14 08:36:38 | 2023-03-14 08:36:39 |
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 {
if (l/SQRTN == other.l/SQRTN) {
return r < other.r;
}
else {
return l/SQRTN < other.l/SQRTN;
}
}
};
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] ^ 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';
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 3404kb
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: 3536kb
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: 2ms
memory: 3560kb
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: 3400kb
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: 2ms
memory: 3392kb
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: 3448kb
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: 3ms
memory: 3440kb
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: 2ms
memory: 3480kb
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: -100
Wrong Answer
time: 2ms
memory: 3428kb
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 536 20 1144 2068 395 65 2611 148 10 3 1956 10 146 2611 33 167 6 27 316 604 2977 369 4596 2830 132 1226 53 164 2974 131 3059 798 1496 90 1946 225 65 76 423 335 646 266 1088 907 963 2682 2205 452 149 224 5 3783 1323 102 1229 576 2201 291 677 362 962 1 226 149 723 117 103 3370 911 132 1 90 1143 200...
result:
wrong answer 2nd numbers differ - expected: '534', found: '536'