KS1 (AUG 19 Division B)

#include "bits/stdc++.h"
using namespace std;

typedef unsigned long long ull;
typedef long long ll;

#define mod 1e9+7
#define eps 1e-9 

#define mp make_pair
#define pb push_back 

int main()
{
ios_base :: sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);

#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
freopen("error.txt","w",stderr);
#endif 

ll t,n;
cin >> t;

while(t--)
{
    vector<ll> varr[1000001];
    cin >> n; vector<ll> num(n);
    for(int i=0 ; i<n ; ++i) cin >> num[i];
    
    ll xr=0,ans=0; varr[0].pb(-1);

    for(int i=0 ; i<n ; ++i) { xr = xr^num[i]; varr[xr].pb(i); }
    
    for(int i=0 ; i<1000001 ; ++i) 
    {
        //if(varr[i].size()) cerr << i << " " << varr[i].size() << endl;
        for(int j=0 ; j<varr[i].size() ; ++j)
        {
            ans += j*varr[i][j] - (varr[i].size()-1-j)*varr[i][j];  
        }
        ans -= varr[i].size()*(varr[i].size()-1)/2;
    }
    cout << ans << endl; 

}

return 0;
} 

This is giving WA on some subtasks. I read the editorial and tried to implement it on my own. (using 0-based indexing) . Any hints where I am lacking would be helpful?
Off - Topic :
Also for some reason, this programs fails to read/write from txt files (had to use online IDEs) while using on Sublime Text 3 while others work fine. Any help regarding this would also be appreciated.
Thanks :slight_smile:

Please either format your code or (better!) link to your submission - the forum software has mangled it and it won’t compile! :slight_smile:

Edit:

vector<ll> varr[1000001];

This is likely to cause problems - why are you allocating 1000001 vectors on the stack?

2 Likes

I formatted the code. Also how does declaring it globally helps? (I did declare it globally, and now it works on sublime. Still WA in some subtasks.)

It seems that the static area of memory in Codechef’s Online Judge is larger than the stack.

Link to this solution?

2 Likes

It got accepted. Thanks. Just forgot to reset the vector array for each test case after declaring it globally. Thanks again.

Link to the AC

1 Like