Last active
July 2, 2018 21:24
-
-
Save sreeprasad/862a18e0d3794cef7f67aa2f7166d485 to your computer and use it in GitHub Desktop.
Counting Inversion from problem https://codeforces.com/contest/911/problem/D
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# include <iostream> | |
# include <algorithm> | |
# include <string> | |
using namespace std; | |
bool isOdd(int number){ | |
return number&1; | |
} | |
int changePairity(int number){ | |
return number^1; | |
} | |
int* inputNumbers(int N){ | |
int* array = new int[N]; | |
for(int i=0;i<N;i++){ | |
cin>>array[i]; | |
} | |
return array; | |
} | |
int findPairityOfInversion(int* array, int N){ | |
int pairityOfInversion; | |
for(int i=0;i<N;i++){ | |
for(int j=i+1;j<N;j++){ | |
pairityOfInversion ^= (array[i]>array[j]); | |
} | |
} | |
return pairityOfInversion; | |
} | |
int main() { | |
int N; | |
cin>>N; | |
int* array = inputNumbers(N); | |
int pairityOfInversion = findPairityOfInversion(array, N); | |
int queries; | |
cin>>queries; | |
string result[queries]; | |
for(int i=0;i<queries;i++){ | |
int L,R; | |
cin>>L>>R; | |
int numberOfSwaps = ((R-L+1) * (R-L))>>1; // equivalent to ((R-L+1) * (R-L))/2 | |
if(isOdd(numberOfSwaps)) { | |
pairityOfInversion = changePairity(pairityOfInversion); | |
} | |
if(isOdd(pairityOfInversion)){ | |
result[i] = "odd"; | |
} else { | |
result[i] = "even"; | |
} | |
} | |
for(int i=0;i<queries;i++){ | |
cout<<result[i]<<endl; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment