#include <algorithm> #include <vector> #include <queue> #include <cstdio> #include <cstring> #include "teatr.h" #include "message.h" #define ll long long #define PII pair<int,int> #define VP vector< PII > const int IS = sizeof(int); const int TH = 5000000; const int MIL = 1234567; using namespace std; ll merge(int* a, int from, int mid, int to, int* b) { ll res = 0; int i = from; int j = mid; for(int k=from;k<to;k++) { if(i < mid && (j >= to || a[i] <= a[j])) { b[k] = a[i]; i++; } else { res += mid-i; b[k] = a[j]; j++; } } return res; } ll solve(int* b, int from, int to, int* a) { if(to - from < 2) { return 0; } int mid = (from + to) / 2; ll res = 0; res += solve(a, from, mid, b); res += solve(a, mid, to, b); res += merge(b, from, mid, to, a); return res; } ll solve(int* a, int n) { int* b = new int[n]; memcpy(b,a,n*IS); return solve(a,0,n,b); } ll solveSmall(int from, int to) { int s = to - from; int* a = new int[s]; for(int i=0;i<s;i++) { a[i] = GetElement(from + i); } return solve(a, s); } ll solveBig(int from, int to) { int mid = (from + to) / 2; int* lo = new int[MIL]; int* hi = new int[MIL]; ll* slo = new ll[MIL]; memset(lo,0,MIL*IS); memset(hi,0,MIL*IS); memset(slo,0,MIL*sizeof(ll)); for(int i=from;i<mid;i++) { lo[GetElement(i)]++; } for(int i=MIL-2;i>=0;i--) { slo[i] = slo[i+1] + lo[i]; } for(int i=mid;i<to;i++) { hi[GetElement(i)]++; } int i = 1; int j = 1; ll res = 0; while(i < MIL && j < MIL) { while(i < MIL && lo[i] == 0) { i++; } while(j < MIL && hi[j] == 0) { j++; } if(i < MIL && j < MIL) { if(i <= j) { i++; } else { res += hi[j]*slo[i]; j++; } } } return res; } int isBig(PII& p) { return p.second - p.first > TH; } VP splitIntoNodes(int n) { VP res; queue<PII> q; q.push(make_pair(0,n)); while(!q.empty()) { PII p = q.front(); q.pop(); res.push_back(p); if(isBig(p)) { int mid = (p.first + p.second) / 2; q.push(make_pair(p.first,mid)); q.push(make_pair(mid,p.second)); } } reverse(res.begin(), res.end()); return res; } int main(int argc, char** argv) { int me = MyNodeId(); int nodes = NumberOfNodes(); int n = GetN(); vector< PII > ps = splitIntoNodes(n); ll res = 0; if(me < ps.size()) { PII myWork = ps[me]; if(isBig(myWork)) { res = solveBig(myWork.first, myWork.second); } else { res = solveSmall(myWork.first, myWork.second); } if(me > 0) { PutLL(0, res); Send(0); } } if(me == 0) { for(int i=1;i<ps.size();i++) { Receive(i); res += GetLL(i); } printf("%lld\n", res); } 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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | #include <algorithm> #include <vector> #include <queue> #include <cstdio> #include <cstring> #include "teatr.h" #include "message.h" #define ll long long #define PII pair<int,int> #define VP vector< PII > const int IS = sizeof(int); const int TH = 5000000; const int MIL = 1234567; using namespace std; ll merge(int* a, int from, int mid, int to, int* b) { ll res = 0; int i = from; int j = mid; for(int k=from;k<to;k++) { if(i < mid && (j >= to || a[i] <= a[j])) { b[k] = a[i]; i++; } else { res += mid-i; b[k] = a[j]; j++; } } return res; } ll solve(int* b, int from, int to, int* a) { if(to - from < 2) { return 0; } int mid = (from + to) / 2; ll res = 0; res += solve(a, from, mid, b); res += solve(a, mid, to, b); res += merge(b, from, mid, to, a); return res; } ll solve(int* a, int n) { int* b = new int[n]; memcpy(b,a,n*IS); return solve(a,0,n,b); } ll solveSmall(int from, int to) { int s = to - from; int* a = new int[s]; for(int i=0;i<s;i++) { a[i] = GetElement(from + i); } return solve(a, s); } ll solveBig(int from, int to) { int mid = (from + to) / 2; int* lo = new int[MIL]; int* hi = new int[MIL]; ll* slo = new ll[MIL]; memset(lo,0,MIL*IS); memset(hi,0,MIL*IS); memset(slo,0,MIL*sizeof(ll)); for(int i=from;i<mid;i++) { lo[GetElement(i)]++; } for(int i=MIL-2;i>=0;i--) { slo[i] = slo[i+1] + lo[i]; } for(int i=mid;i<to;i++) { hi[GetElement(i)]++; } int i = 1; int j = 1; ll res = 0; while(i < MIL && j < MIL) { while(i < MIL && lo[i] == 0) { i++; } while(j < MIL && hi[j] == 0) { j++; } if(i < MIL && j < MIL) { if(i <= j) { i++; } else { res += hi[j]*slo[i]; j++; } } } return res; } int isBig(PII& p) { return p.second - p.first > TH; } VP splitIntoNodes(int n) { VP res; queue<PII> q; q.push(make_pair(0,n)); while(!q.empty()) { PII p = q.front(); q.pop(); res.push_back(p); if(isBig(p)) { int mid = (p.first + p.second) / 2; q.push(make_pair(p.first,mid)); q.push(make_pair(mid,p.second)); } } reverse(res.begin(), res.end()); return res; } int main(int argc, char** argv) { int me = MyNodeId(); int nodes = NumberOfNodes(); int n = GetN(); vector< PII > ps = splitIntoNodes(n); ll res = 0; if(me < ps.size()) { PII myWork = ps[me]; if(isBig(myWork)) { res = solveBig(myWork.first, myWork.second); } else { res = solveSmall(myWork.first, myWork.second); } if(me > 0) { PutLL(0, res); Send(0); } } if(me == 0) { for(int i=1;i<ps.size();i++) { Receive(i); res += GetLL(i); } printf("%lld\n", res); } return 0; } |