답안 #551148

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
551148 2022-04-19T23:06:59 Z Olympia XOR Sum (info1cup17_xorsum) C++17
100 / 100
963 ms 12412 KB
#include <iostream>
#include <vector>
#include <iomanip>
#include <algorithm>
#include <cassert>
#include <map>
#include <complex>
#include <cmath>
#include <set>
#include <queue>
using namespace std;

#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
int64_t res (vector<int>& a, int x) {
    int64_t r = a.size() - 1;
    int64_t ans = 0;
    for (int i = 0; i < a.size(); i++) {
        if (a[i] + a[0] > x) {
            break;
        }
        while (a[r] + a[i] > x) {
            r--;
        }
        ans += r + 1 - (r >= i);
    }
    ans /= 2;
    for (int i = 0; i < a.size(); i++) {
        ans += (2 * a[i] <= x);
    }
    return ans;
}
int64_t res (vector<int>&a, int l, int r) {
    return res(a, r) - res(a, l - 1);
}
int main () {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n; cin >> n;
    vector<int> v(n);
    for (int i = 0; i < n; i++) {
        cin >> v[i];
    }
    sort(v.begin(), v.end());
    int64_t ans = 0;
    for (int b = 30; b >= 0; b--) {
        vector<int> v2;
        vector<int> v1;
        for (int i = 0; i < v.size(); i++) {
            if (v[i] & (1 << (b + 1))) {
                v[i] ^= (1 << (b + 1));
                v2.push_back(v[i]);
            } else {
                v1.push_back(v[i]);
            }
        }
        int l = 0, r = 0;
        v.clear();
        while (true) {
            if (l == (int)v1.size() && r == (int)v2.size()) break;
            else if (l == (int)v1.size()) v.push_back(v2[r++]);
            else if (r == (int)v2.size()) v.push_back(v1[l++]);
            else if (v1[l] <= v2[r]) v.push_back(v1[l++]);
            else if (v1[l] > v2[r]) v.push_back(v2[r++]);
        }
        int64_t a = 0;
        a += res(v, (1 << b), (1 << (b + 1)) - 1);
        a += res(v, (1 << b) + (1 << (b + 1)), (1 << (b + 2)) - 1);
        ans += ((int64_t)1 << b) * (a % 2);
    }
    cout << ans;
}

Compilation message

xorsum.cpp:14: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
   14 | #pragma GCC optimization ("O3")
      | 
xorsum.cpp:15: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
   15 | #pragma GCC optimization ("unroll-loops")
      | 
xorsum.cpp: In function 'int64_t res(std::vector<int>&, int)':
xorsum.cpp:19:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   19 |     for (int i = 0; i < a.size(); i++) {
      |                     ~~^~~~~~~~~~
xorsum.cpp:29:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   29 |     for (int i = 0; i < a.size(); i++) {
      |                     ~~^~~~~~~~~~
xorsum.cpp: In function 'int main()':
xorsum.cpp:50:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   50 |         for (int i = 0; i < v.size(); i++) {
      |                         ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 340 KB Output is correct
2 Correct 5 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 680 ms 12412 KB Output is correct
2 Correct 622 ms 11748 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 680 ms 12412 KB Output is correct
2 Correct 622 ms 11748 KB Output is correct
3 Correct 753 ms 12348 KB Output is correct
4 Correct 721 ms 11988 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 340 KB Output is correct
2 Correct 5 ms 340 KB Output is correct
3 Correct 96 ms 1604 KB Output is correct
4 Correct 100 ms 1612 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 340 KB Output is correct
2 Correct 5 ms 340 KB Output is correct
3 Correct 680 ms 12412 KB Output is correct
4 Correct 622 ms 11748 KB Output is correct
5 Correct 753 ms 12348 KB Output is correct
6 Correct 721 ms 11988 KB Output is correct
7 Correct 96 ms 1604 KB Output is correct
8 Correct 100 ms 1612 KB Output is correct
9 Correct 963 ms 12252 KB Output is correct
10 Correct 932 ms 12260 KB Output is correct
11 Correct 926 ms 12220 KB Output is correct