Submission #215975

# Submission time Handle Problem Language Result Execution time Memory
215975 2020-03-26T13:44:20 Z wendy_virgo XOR Sum (info1cup17_xorsum) C++14
100 / 100
1000 ms 25412 KB
#include <bits/stdc++.h>

using namespace std;

template<typename TH>
void _dbg(const char* sdbg, TH h)
{
	cerr << sdbg << " = " << h << "\n";
}

template<typename TH, typename... TA>
void _dbg(const char* sdbg, TH h, TA... t)
{
	while (*sdbg != ',') cerr << *sdbg++;
	cerr << " = " << h << ",";
	_dbg(sdbg + 1, t...);
}

#define db(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define chkpt cerr << "--- Checkpoint here ---\n";

const int N=1e6+6;

#define int64_t int

int n;
int64_t a[N],b[N],tmp[N],tmpB[N];

void Sub1(){
    int ans=0;
    for(int i=1;i<=n;++i){
        for(int j=i;j<=n;++j){
            ans^=(a[i]+a[j]);
        }
    }
    cout<<ans<<'\n';
}

void Sort(int pos){
    vector<int> freq(2);
    for(int i=1;i<=n;++i){
        freq[(a[i]>>pos)&(int64_t)1]++;
    }
    freq[1]+=freq[0];
    for(int i=n;i>=1;--i){
        int id=(a[i]>>pos)&(int64_t)1;
        b[freq[id]]=a[i];
        tmpB[freq[id]]=tmp[i];
        freq[id]--;
    }
    for(int i=1;i<=n;++i){
        a[i]=b[i];
        tmp[i]=tmpB[i];
    }
}

int Check(int pos){
    int res=0;
    vector<vector<int64_t>> vec(2);
    for(int i=1;i<=n;++i){
        vec[(a[i]>>pos)&1].push_back(tmp[i]);
    }
    int ptr=(int)vec[0].size()-1;
    int ptr1=(int)vec[1].size()-1;
    int ptr2=(int)vec[1].size()-1;
    for(int i=0;i<max(vec[0].size(),vec[1].size());++i){
        if(i<vec[0].size()){
            int64_t val=((int64_t)1<<pos)-vec[0][i];
            while(ptr>=0&&vec[0][ptr]>=val){
                ptr--;
            }
            if(ptr+1<=i){
                res^=(i-(ptr+1)+1)%2;
            }
            int64_t val1=((int64_t)1<<pos)-vec[0][i];
            while(ptr1>=0&&vec[1][ptr1]>=val1){
                ptr1--;
            }
            res^=(ptr1+1)%2;
        }
        if(i<vec[1].size()){
            int64_t val2=((int64_t)1<<pos)-vec[1][i];
            while(ptr2>=0&&vec[1][ptr2]>=val2){
                ptr2--;
            }
            if(ptr2+1<=i){
                res^=(i-(ptr2+1)+1)%2;
            }
        }
    }
    Sort(pos);
    for(int i=1;i<=n;++i){
        if((a[i]>>pos)&1){
            tmp[i]|=1<<pos;
        }
    }
    return res;
}

void Sub2(){
    int64_t ans=0;
    for(int i=0;i<=30;++i){
        if(Check(i)){
            ans+=(int64_t)1<<i;
        }
    }
    cout<<ans;
}

void Gen(){
    srand(time(0));
    freopen("info1cup17_xorsum.inp","w",stdout);
    int n=rand()%5+1;
    cout<<n<<'\n';
    for(int i=1;i<=n;++i){
        cout<<rand()%10+1<<' ';
    }
    exit(0);
}

inline int ReadInt()
{
    char c;
    int sign = 1;
    for (c = getchar(); c < '0' || c > '9'; c = getchar())
        if (c == '-') sign = -sign;
    int res = c - '0';
    for (c = getchar(); c >= '0' && c <= '9'; c = getchar())
        res = res * 10 + c - '0';
    return res * sign;
}

int32_t main()
{
//    Gen();
//    freopen("info1cup17_xorsum.inp","r",stdin);
//    freopen("info1cup17_xorsum.out","w",stdout);
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	n=ReadInt();
	for(int i=1;i<=n;++i){
        a[i]=ReadInt();
	}
//	Sub1();
	Sub2();
    return 0;
}

Compilation message

xorsum.cpp: In function 'int Check(int)':
xorsum.cpp:66:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0;i<max(vec[0].size(),vec[1].size());++i){
                 ~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
xorsum.cpp:67:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         if(i<vec[0].size()){
            ~^~~~~~~~~~~~~~
xorsum.cpp:81:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         if(i<vec[1].size()){
            ~^~~~~~~~~~~~~~
xorsum.cpp: In function 'void Gen()':
xorsum.cpp:112:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
     freopen("info1cup17_xorsum.inp","w",stdout);
     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 9 ms 512 KB Output is correct
2 Correct 9 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 799 ms 25412 KB Output is correct
2 Correct 749 ms 22864 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 799 ms 25412 KB Output is correct
2 Correct 749 ms 22864 KB Output is correct
3 Correct 867 ms 24772 KB Output is correct
4 Correct 823 ms 24476 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 512 KB Output is correct
2 Correct 9 ms 512 KB Output is correct
3 Correct 104 ms 3148 KB Output is correct
4 Correct 104 ms 3156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 512 KB Output is correct
2 Correct 9 ms 512 KB Output is correct
3 Correct 799 ms 25412 KB Output is correct
4 Correct 749 ms 22864 KB Output is correct
5 Correct 867 ms 24772 KB Output is correct
6 Correct 823 ms 24476 KB Output is correct
7 Correct 104 ms 3148 KB Output is correct
8 Correct 104 ms 3156 KB Output is correct
9 Correct 986 ms 25056 KB Output is correct
10 Correct 1000 ms 25156 KB Output is correct
11 Correct 995 ms 25068 KB Output is correct