#include "message.h" #include "teatr.h" #include "bits/stdc++.h" using namespace std; #define MP make_pair #define PB push_back #define ST first #define ND second #define ALL(x) (x).begin(),(x).end() #define SZ(x) ((int)(x).size()) //#define int LLI #define FOR(i,a,b) for(int i = (a); i <= (b); ++i) #define REP(i,n) FOR(i,0,(int)(n)-1) #define F1(i,n) FOR(i,1,n) typedef long long LLI; typedef unsigned long long LLU; typedef long double LD; typedef pair<int, int> pii; typedef pair<LLI, LLI> pll; typedef vector<int> vi; template<class TH> void _dbg(const char *sdbg, TH h){cerr<<sdbg<<"="<<h<<"\n";} template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) { while(*sdbg!=',') cerr<<*sdbg++; cerr<<"="<<h<<","; _dbg(sdbg+1, a...); } #define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #define dbgV(R) {cerr << #R << "=["; for(int e : R) cerr << e << ", "; cerr << "E]\n";} #define dbgA(R, n) {cerr << #R << "=["; for(int i = 0; i <= n; ++i) cerr << R[i] << ", "; cerr << "E]\n";} #define boost ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) const int MX = 1e6 + 7; int fenwick[MX]; int cnt[MX], suf[MX]; inline void insert(int x){ for(int i = x; i < MX; i += (i & (-i))) ++fenwick[i]; } inline int query(int x){ int wyn = 0; for(int i = x; i > 0; i -= (i & (-i))) wyn += fenwick[i]; return wyn; } int32_t main(){ const int n = GetN(); const int ID = MyNodeId(), ile = NumberOfNodes(); const int bs = (n + ile - 1) / ile; const int l = ID * bs, r = min(n - 1, (ID + 1) * bs - 1); if(l >= n) return 0; LLI wyn = 0; for(int i = r; i >= l; --i){ int a = GetElement(i); wyn += query(a - 1); if(i > l) insert(a); } if(ID != 0){ PutLL(0, wyn); Send(0); } else{ int K = 0; REP(nr, ile){ for(int i = K; i > 0; --i) suf[i] = suf[i + 1] + cnt[i]; int l1 = (nr * bs), r1 = min(n - 1, (nr + 1) * bs - 1); if(l1 < n && l1 <= r1){ FOR(i, l1, r1){ int a = GetElement(i); K = max(K, a); ++cnt[GetElement(i)]; wyn += suf[a + 1]; } } else break; } for(int i = 1; i * bs < n; ++i){ 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 | #include "message.h" #include "teatr.h" #include "bits/stdc++.h" using namespace std; #define MP make_pair #define PB push_back #define ST first #define ND second #define ALL(x) (x).begin(),(x).end() #define SZ(x) ((int)(x).size()) //#define int LLI #define FOR(i,a,b) for(int i = (a); i <= (b); ++i) #define REP(i,n) FOR(i,0,(int)(n)-1) #define F1(i,n) FOR(i,1,n) typedef long long LLI; typedef unsigned long long LLU; typedef long double LD; typedef pair<int, int> pii; typedef pair<LLI, LLI> pll; typedef vector<int> vi; template<class TH> void _dbg(const char *sdbg, TH h){cerr<<sdbg<<"="<<h<<"\n";} template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) { while(*sdbg!=',') cerr<<*sdbg++; cerr<<"="<<h<<","; _dbg(sdbg+1, a...); } #define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #define dbgV(R) {cerr << #R << "=["; for(int e : R) cerr << e << ", "; cerr << "E]\n";} #define dbgA(R, n) {cerr << #R << "=["; for(int i = 0; i <= n; ++i) cerr << R[i] << ", "; cerr << "E]\n";} #define boost ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) const int MX = 1e6 + 7; int fenwick[MX]; int cnt[MX], suf[MX]; inline void insert(int x){ for(int i = x; i < MX; i += (i & (-i))) ++fenwick[i]; } inline int query(int x){ int wyn = 0; for(int i = x; i > 0; i -= (i & (-i))) wyn += fenwick[i]; return wyn; } int32_t main(){ const int n = GetN(); const int ID = MyNodeId(), ile = NumberOfNodes(); const int bs = (n + ile - 1) / ile; const int l = ID * bs, r = min(n - 1, (ID + 1) * bs - 1); if(l >= n) return 0; LLI wyn = 0; for(int i = r; i >= l; --i){ int a = GetElement(i); wyn += query(a - 1); if(i > l) insert(a); } if(ID != 0){ PutLL(0, wyn); Send(0); } else{ int K = 0; REP(nr, ile){ for(int i = K; i > 0; --i) suf[i] = suf[i + 1] + cnt[i]; int l1 = (nr * bs), r1 = min(n - 1, (nr + 1) * bs - 1); if(l1 < n && l1 <= r1){ FOR(i, l1, r1){ int a = GetElement(i); K = max(K, a); ++cnt[GetElement(i)]; wyn += suf[a + 1]; } } else break; } for(int i = 1; i * bs < n; ++i){ Receive(i); wyn += GetLL(i); } cout << wyn; } } |