Submission #468404

# Submission time Handle Problem Language Result Execution time Memory
468404 2021-08-28T03:32:02 Z cpp219 XOR Sum (info1cup17_xorsum) C++14
100 / 100
1066 ms 49948 KB
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define fs first
#define sc second
#define debug(y) cout<<y,exit(0)
using namespace std;
typedef pair<ll,ll> LL;
const ll N = 1e6 + 9;
const ll Log2 = 23;
const ll inf = 1e9 + 7;

vector<ll> g[2];
ll n,a[N],pos[N],val[N],ans,b[N];

ll Getval(ll x,ll i){
    return (((1 << i + 1) - 1) & x);
}

bool chk(ll x,ll i){
    return (x >> i) & 1;
}

void updateArr(){
    for (ll i = 1;i <= n;i++) b[pos[i]] = a[i];
    for (ll i = 1;i <= n;i++) a[i] = b[i];
}

int main(){
    ios_base::sync_with_stdio(NULL); cin.tie(0); cout.tie(0);
    #define task "test"
    if (fopen(task".INP","r")){
        freopen(task".INP","r",stdin);
        freopen(task".out","w",stdout);
    }
    cin>>n;
    for (ll i = 1;i <= n;i++) cin>>a[i];
    for (ll bit = 0;bit < 30;bit++){
        g[0].clear(); g[1].clear();
        for (ll i = 1;i <= n;i++) g[chk(a[i],bit)].push_back(i);
        ll now = 1;
        for (auto i : g[0]) pos[i] = now++;
        for (auto i : g[1]) pos[i] = now++;
        updateArr();
        for (ll i = 1;i <= n;i++) val[i] = Getval(a[i],bit);
        ll tmp = 0;
        ll st1 = n,en1 = n,st2 = n,en2 = n;

        for (ll i = 1;i <= n;i++){
            st1 = min(en1,st1); st2 = min(en2,st2);
            while(st1 > 0 && val[st1] >= (1 << bit) - val[i]) st1--;
            while(en1 > 0 && val[en1] > (1 << bit + 1) - 1 - val[i]) en1--;
            tmp += max(0ll,en1 - max(st1,i - 1));
            //cout<<st1<<" "<<en1; return 0;
            //debug(tmp);
            while(st2 > 0 && val[st2] >= (1 << bit) + (1 << bit + 1) - val[i]) st2--;
            while(en2 > 0 && val[en2] > (1 << bit + 2) - 2 - val[i]) en2--;
            tmp += max(0ll,en2 - max(st2,i - 1));
            //cout<<tmp<<"\n";
        }
        tmp %= 2; //debug(tmp);
        ans += tmp*(1 << bit);
    }
    cout<<ans;
}

/* stuff you should look for
	* int overflow, array bounds
	* special cases (n=1?)
	* do smth instead of nothing and stay organized
	* WRITE STUFF DOWN
	* DON'T GET STUCK ON ONE APPROACH
*/

Compilation message

xorsum.cpp: In function 'long long int Getval(long long int, long long int)':
xorsum.cpp:17:22: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
   17 |     return (((1 << i + 1) - 1) & x);
      |                    ~~^~~
xorsum.cpp: In function 'int main()':
xorsum.cpp:52:51: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
   52 |             while(en1 > 0 && val[en1] > (1 << bit + 1) - 1 - val[i]) en1--;
      |                                               ~~~~^~~
xorsum.cpp:56:65: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
   56 |             while(st2 > 0 && val[st2] >= (1 << bit) + (1 << bit + 1) - val[i]) st2--;
      |                                                             ~~~~^~~
xorsum.cpp:57:51: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
   57 |             while(en2 > 0 && val[en2] > (1 << bit + 2) - 2 - val[i]) en2--;
      |                                               ~~~~^~~
xorsum.cpp:33:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   33 |         freopen(task".INP","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
xorsum.cpp:34:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   34 |         freopen(task".out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 460 KB Output is correct
2 Correct 5 ms 460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 760 ms 49948 KB Output is correct
2 Correct 708 ms 47264 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 760 ms 49948 KB Output is correct
2 Correct 708 ms 47264 KB Output is correct
3 Correct 851 ms 49788 KB Output is correct
4 Correct 827 ms 48416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 460 KB Output is correct
2 Correct 5 ms 460 KB Output is correct
3 Correct 103 ms 5524 KB Output is correct
4 Correct 112 ms 5572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 460 KB Output is correct
2 Correct 5 ms 460 KB Output is correct
3 Correct 760 ms 49948 KB Output is correct
4 Correct 708 ms 47264 KB Output is correct
5 Correct 851 ms 49788 KB Output is correct
6 Correct 827 ms 48416 KB Output is correct
7 Correct 103 ms 5524 KB Output is correct
8 Correct 112 ms 5572 KB Output is correct
9 Correct 1066 ms 49792 KB Output is correct
10 Correct 1032 ms 49804 KB Output is correct
11 Correct 1029 ms 49824 KB Output is correct