#include <bits/stdc++.h> #include "message.h" #include "teatr.h" using namespace std; typedef long long LL; typedef long double LD; typedef unsigned long long ULL; typedef vector<int> VI; typedef set<int> SI; typedef multiset<int> MI; typedef pair<int, int> PII; typedef vector<pair<int, int> > VPII; const int INF = 1000000001; const LD EPS = 1e-9; const int MOD = 1000000007; const LL LLINF = 1000000000000000001; //813437586 #define FOR(i, b, e) for(int i = b; i <= e; i++) #define FORD(i, b, e) for(int i = b; i >= e; i--) #define ALL(c) (c).begin(), (c).end() #define SIZE(x) ((int)(x).size()) #define VAR(v, n) auto v = (n) #define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define MP make_pair #define PB push_back #define ST first #define ND second #define GGL(x) "Case #" << x << ": " /*************************** END OF TEMPLATE ***************************/ const int MAXN = 100000000; const int MAXP = 2000000; const int MAXW = 1000000; int n; int Inst; int Number; LL Wyn; LL Operacje[100]; int Tab[MAXP+5]; int Temp[MAXP+5]; LL Pref[MAXW]; void Merge(int Pocz, int Mid, int Kon) { FOR (i, Pocz, Kon) Temp[i] = Tab[i]; int a = Pocz, b = Mid+1, i = Pocz; while (a <= Mid && b <= Kon) { if (Temp[a] <= Temp[b]) Tab[i] = Temp[a], ++a; else Tab[i] = Temp[b], ++b, Wyn += Mid - a + 1; } while (a <= Mid) Tab[i] = Temp[a], ++a, ++i; while (b <= Kon) Tab[i] = Temp[b], ++b, ++i; } void MergeSort(int Pocz, int Kon) { if (Pocz >= Kon) return; int Mid = (Pocz+Kon) / 2; MergeSort(Pocz, Mid); MergeSort(Mid+1, Kon); Merge(Pocz, Mid, Kon); } void Zeruj() { FOR (i, 1, MAXW) Pref[i] = 0; } void Add(int Frag1, int Frag2) { int Pocz1 = (Frag1-1) * MAXP; int Kon1 = Pocz1 + MAXP - 1; int Pocz2 = min((Frag2-1) * MAXP, n-1); int Kon2 = min(Pocz2 + MAXP - 1, n-1); Zeruj(); FOR (i, Pocz2, Kon2) Pref[GetElement(i)]++; FOR (i, 2, MAXW) Pref[i] += Pref[i-1]; FOR (i, Pocz1, Kon1) Wyn += Pref[GetElement(i)-1]; } int main() { n = GetN(); Inst = MyNodeId(); Number = NumberOfNodes(); /* int x = 2; for (int i = 1; x <= MAXP; i++) { Operacje[i] = n/x; if (n%x != 0) Operacje[i]++; x *= 2; Operacje[i+1] = LLINF; } int Oper = Inst; x = 1; */ int Pocz = Inst * MAXP; int Kon = min(n, Pocz+MAXP-1); int x = 1; FOR (i, Pocz, Kon) Tab[x] = GetElement(i), ++x; MergeSort(1, Kon-Pocz+1); int IleCzesci = n / MAXP; if (n%MAXP) ++IleCzesci; int j = 1; while (IleCzesci > 0) Operacje[j] = --IleCzesci; x = Inst; int Oper = 1; while (Operacje[Oper] != 0) { while (x >= Operacje[Oper] && Operacje[Oper] != 0) x -= Operacje[Oper], ++Oper; if (Operacje[Oper] == 0) break; int Frag1 = Oper; int Frag2 = Oper + x + 1; Add(Frag1, Frag2); x += Number; } if (Inst != 0) PutLL(0, Wyn), Send(0); else { FOR (i, 1, Number-1) Receive(i), Wyn += GetLL(i); cout << Wyn; } }
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 | #include <bits/stdc++.h> #include "message.h" #include "teatr.h" using namespace std; typedef long long LL; typedef long double LD; typedef unsigned long long ULL; typedef vector<int> VI; typedef set<int> SI; typedef multiset<int> MI; typedef pair<int, int> PII; typedef vector<pair<int, int> > VPII; const int INF = 1000000001; const LD EPS = 1e-9; const int MOD = 1000000007; const LL LLINF = 1000000000000000001; //813437586 #define FOR(i, b, e) for(int i = b; i <= e; i++) #define FORD(i, b, e) for(int i = b; i >= e; i--) #define ALL(c) (c).begin(), (c).end() #define SIZE(x) ((int)(x).size()) #define VAR(v, n) auto v = (n) #define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define MP make_pair #define PB push_back #define ST first #define ND second #define GGL(x) "Case #" << x << ": " /*************************** END OF TEMPLATE ***************************/ const int MAXN = 100000000; const int MAXP = 2000000; const int MAXW = 1000000; int n; int Inst; int Number; LL Wyn; LL Operacje[100]; int Tab[MAXP+5]; int Temp[MAXP+5]; LL Pref[MAXW]; void Merge(int Pocz, int Mid, int Kon) { FOR (i, Pocz, Kon) Temp[i] = Tab[i]; int a = Pocz, b = Mid+1, i = Pocz; while (a <= Mid && b <= Kon) { if (Temp[a] <= Temp[b]) Tab[i] = Temp[a], ++a; else Tab[i] = Temp[b], ++b, Wyn += Mid - a + 1; } while (a <= Mid) Tab[i] = Temp[a], ++a, ++i; while (b <= Kon) Tab[i] = Temp[b], ++b, ++i; } void MergeSort(int Pocz, int Kon) { if (Pocz >= Kon) return; int Mid = (Pocz+Kon) / 2; MergeSort(Pocz, Mid); MergeSort(Mid+1, Kon); Merge(Pocz, Mid, Kon); } void Zeruj() { FOR (i, 1, MAXW) Pref[i] = 0; } void Add(int Frag1, int Frag2) { int Pocz1 = (Frag1-1) * MAXP; int Kon1 = Pocz1 + MAXP - 1; int Pocz2 = min((Frag2-1) * MAXP, n-1); int Kon2 = min(Pocz2 + MAXP - 1, n-1); Zeruj(); FOR (i, Pocz2, Kon2) Pref[GetElement(i)]++; FOR (i, 2, MAXW) Pref[i] += Pref[i-1]; FOR (i, Pocz1, Kon1) Wyn += Pref[GetElement(i)-1]; } int main() { n = GetN(); Inst = MyNodeId(); Number = NumberOfNodes(); /* int x = 2; for (int i = 1; x <= MAXP; i++) { Operacje[i] = n/x; if (n%x != 0) Operacje[i]++; x *= 2; Operacje[i+1] = LLINF; } int Oper = Inst; x = 1; */ int Pocz = Inst * MAXP; int Kon = min(n, Pocz+MAXP-1); int x = 1; FOR (i, Pocz, Kon) Tab[x] = GetElement(i), ++x; MergeSort(1, Kon-Pocz+1); int IleCzesci = n / MAXP; if (n%MAXP) ++IleCzesci; int j = 1; while (IleCzesci > 0) Operacje[j] = --IleCzesci; x = Inst; int Oper = 1; while (Operacje[Oper] != 0) { while (x >= Operacje[Oper] && Operacje[Oper] != 0) x -= Operacje[Oper], ++Oper; if (Operacje[Oper] == 0) break; int Frag1 = Oper; int Frag2 = Oper + x + 1; Add(Frag1, Frag2); x += Number; } if (Inst != 0) PutLL(0, Wyn), Send(0); else { FOR (i, 1, Number-1) Receive(i), Wyn += GetLL(i); cout << Wyn; } } |