#include <iostream> #include <algorithm> #include "teatr.h" #include "message.h" using namespace std; const int S = 1 << 20; struct tree { int tab[2*S]; void insert( int p, int v ) { p += S; tab[p] += v; p /= 2; while( p != 0 ) { tab[p] = tab[2*p] + tab[2*p + 1]; p /= 2; } } int query( int a, int b ) { a += S - 1; b += S + 1; int r = 0; while( a + 1 < b ) { if( a%2 == 0 ) r = r + tab[a+1]; if( b%2 == 1 ) r = r + tab[b-1]; a /= 2; b /= 2; } return r; } }; tree panek; int main() { ios_base::sync_with_stdio(0); int N = GetN(); int id = MyNodeId(); if( id == 0 ) { unsigned long long int wynik = 0; for( int i = 0; i < N; i ++ ) { int a; a = GetElement(i); wynik += panek.query(a+1,1000000); panek.insert(a,1); } cout << wynik << "\n"; } return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | #include <iostream> #include <algorithm> #include "teatr.h" #include "message.h" using namespace std; const int S = 1 << 20; struct tree { int tab[2*S]; void insert( int p, int v ) { p += S; tab[p] += v; p /= 2; while( p != 0 ) { tab[p] = tab[2*p] + tab[2*p + 1]; p /= 2; } } int query( int a, int b ) { a += S - 1; b += S + 1; int r = 0; while( a + 1 < b ) { if( a%2 == 0 ) r = r + tab[a+1]; if( b%2 == 1 ) r = r + tab[b-1]; a /= 2; b /= 2; } return r; } }; tree panek; int main() { ios_base::sync_with_stdio(0); int N = GetN(); int id = MyNodeId(); if( id == 0 ) { unsigned long long int wynik = 0; for( int i = 0; i < N; i ++ ) { int a; a = GetElement(i); wynik += panek.query(a+1,1000000); panek.insert(a,1); } cout << wynik << "\n"; } return 0; } |