Submission #389411

# Submission time Handle Problem Language Result Execution time Memory
389411 2021-04-14T04:02:06 Z cheissmart XOR Sum (info1cup17_xorsum) C++14
45 / 100
1600 ms 39500 KB
#include <bits/stdc++.h>
#define F first
#define S second
#define V vector
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define ALL(v) (v).begin(), (v).end()
#define debug(x) cerr << "LINE(" << __LINE__ << "): " << #x << " is " << x << endl

using namespace std;

typedef long long ll;
typedef pair<int, int> pi;
typedef V<int> vi;

const int INF = 1e9 + 7, N = 1e5 + 1;

struct trie {
    int t[N * 30][2], sz[N * 30];
    int hb, cnt;
    trie(int _hb) {
        hb = _hb;
        memset(t, 0, sizeof t);
        memset(sz, 0, sizeof sz);
        cnt = 0;
    }
    void add(int x) {
        int u = 0;
        for(int i = hb; i >= 0; i--) {
            int c = x >> i & 1;
            if(t[u][c] == 0) t[u][c] = ++cnt;
            u = t[u][c];
            sz[u]++;
        }
    }
    int carry(int u, int x, int bit) {
        if(u == 0 || bit < 0) return 0;
        int ans = 0;
        if(x >> bit & 1) {
            ans += sz[t[u][1]];
            ans += carry(t[u][0], x, bit - 1);
        } else {
            ans += carry(t[u][1], x, bit - 1);
        }
        return ans;
    }
    int qry(int x) {
        int c = x >> hb & 1;
        int ans = 0;
        for(int i = 0; i < 2; i++) {
            if(t[0][i] == 0) continue;
            int res = c ^ i;
            if(res == 0) ans += carry(t[0][i], x, hb - 1);
            else ans += sz[t[0][i]] - carry(t[0][i], x, hb - 1);
        }
        return ans;
    }
};

signed main()
{
    ios::sync_with_stdio(0), cin.tie(0);

    mt19937 rng(time(0));
    int n;
    cin >> n;
    //n = 10;
    vi a(n);
    for(int i = 0; i < n; i++) {
        cin >> a[i];
        //a[i] = rng() % 100;
    }

    /*int myans = 0;
    for(int i = 0; i < n; i++)
        for(int j = i; j < n; j++)
            myans ^= a[i] + a[j];
    debug(myans);
    */
    int ans = 0;
    for(int bit = 0; bit < 30; bit++) {
        ll odd_cnt = 0;
        trie t(bit);
        for(int i = 0; i < n; i++) {
            t.add(a[i]);
            odd_cnt += t.qry(a[i]);
        }
        //debug(odd_cnt);
        if(odd_cnt & 1) ans |= 1 << bit;
    }
    cout << ans << '\n';

}
# Verdict Execution time Memory Grader output
1 Correct 161 ms 35532 KB Output is correct
2 Correct 162 ms 35528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 1658 ms 39500 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1658 ms 39500 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 161 ms 35532 KB Output is correct
2 Correct 162 ms 35528 KB Output is correct
3 Correct 1068 ms 35836 KB Output is correct
4 Correct 1058 ms 35916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 161 ms 35532 KB Output is correct
2 Correct 162 ms 35528 KB Output is correct
3 Execution timed out 1658 ms 39500 KB Time limit exceeded
4 Halted 0 ms 0 KB -