#include <iostream> #include "message.h" typedef long long int llint; #define MAX_N (100*1000*1000) #define MAX_ELEM (1<<20) #if 1 #include "teatr.h" #else int GetN() { return MAX_N; } int GetElement(int k) { return (k%5)+1; } #endif #if defined( MY_MESSAGE ) #define tls thread_local #else #define tls #endif tls int cnt[MAX_ELEM*2]={0}; int num_larger(int elem) { int res=0; int index=MAX_ELEM+elem; while(index) { if(!(index&1)) res+=cnt[index+1]; index/=2; } return res; } void insert(int elem) { int index=MAX_ELEM+elem; while(index) { ++cnt[index]; index/=2; } } void insert_n(int elem,int n) { int index=MAX_ELEM+elem; while(index) { cnt[index]+=n; index/=2; } } llint solve_range(int start,int len) { llint res=0; for(int i=0;i<len;++i) { int elem=GetElement(start+i); res+=num_larger(elem); insert(elem); } return res; } llint solve_single_cpu() { llint res=solve_range(0,GetN()); return res; } llint solve_distributed() { int node_id=MyNodeId(); int num_nodes=NumberOfNodes(); int start=(GetN()+num_nodes-1)/num_nodes*node_id; int end=(GetN()+num_nodes-1)/num_nodes*(node_id+1); if(end>GetN())end=GetN(); int len=end-start; for(int i=0;i<len;++i) insert(GetElement(start+i)); for(int i=0;i<MAX_ELEM;++i) if(cnt[MAX_ELEM+i]) for(int target_id=node_id+1;target_id<num_nodes;++target_id) { PutInt(target_id,i); PutInt(target_id,cnt[MAX_ELEM+i]); } for(int target_id=node_id+1;target_id<num_nodes;++target_id) { PutInt(target_id,0); Send(target_id); } for (int i=0;i<MAX_ELEM*2;++i) cnt[i]=0; for (int source_id=0;source_id<node_id;++source_id) { Receive(source_id); while(1) { int value=GetInt(source_id); if(!value)break; int value_cnt=GetInt(source_id); insert_n(value,value_cnt); } } llint local_res=solve_range(start,len); PutLL(0,local_res); Send(0); llint res=0; if(!node_id) for(int i=0;i<num_nodes;++i) { Receive(i); llint instance_res=GetLL(i); res+=instance_res; } return res; } #ifdef MY_MESSAGE int my_main( int _id ) #else int main() #endif { #ifdef MY_MESSAGE { extern thread_local int id; id = _id; } #endif int n=GetN(); llint res= (n<MAX_N/50)?solve_single_cpu(): solve_distributed(); if(!MyNodeId()) std::cout<<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 154 155 156 157 158 159 160 | #include <iostream> #include "message.h" typedef long long int llint; #define MAX_N (100*1000*1000) #define MAX_ELEM (1<<20) #if 1 #include "teatr.h" #else int GetN() { return MAX_N; } int GetElement(int k) { return (k%5)+1; } #endif #if defined( MY_MESSAGE ) #define tls thread_local #else #define tls #endif tls int cnt[MAX_ELEM*2]={0}; int num_larger(int elem) { int res=0; int index=MAX_ELEM+elem; while(index) { if(!(index&1)) res+=cnt[index+1]; index/=2; } return res; } void insert(int elem) { int index=MAX_ELEM+elem; while(index) { ++cnt[index]; index/=2; } } void insert_n(int elem,int n) { int index=MAX_ELEM+elem; while(index) { cnt[index]+=n; index/=2; } } llint solve_range(int start,int len) { llint res=0; for(int i=0;i<len;++i) { int elem=GetElement(start+i); res+=num_larger(elem); insert(elem); } return res; } llint solve_single_cpu() { llint res=solve_range(0,GetN()); return res; } llint solve_distributed() { int node_id=MyNodeId(); int num_nodes=NumberOfNodes(); int start=(GetN()+num_nodes-1)/num_nodes*node_id; int end=(GetN()+num_nodes-1)/num_nodes*(node_id+1); if(end>GetN())end=GetN(); int len=end-start; for(int i=0;i<len;++i) insert(GetElement(start+i)); for(int i=0;i<MAX_ELEM;++i) if(cnt[MAX_ELEM+i]) for(int target_id=node_id+1;target_id<num_nodes;++target_id) { PutInt(target_id,i); PutInt(target_id,cnt[MAX_ELEM+i]); } for(int target_id=node_id+1;target_id<num_nodes;++target_id) { PutInt(target_id,0); Send(target_id); } for (int i=0;i<MAX_ELEM*2;++i) cnt[i]=0; for (int source_id=0;source_id<node_id;++source_id) { Receive(source_id); while(1) { int value=GetInt(source_id); if(!value)break; int value_cnt=GetInt(source_id); insert_n(value,value_cnt); } } llint local_res=solve_range(start,len); PutLL(0,local_res); Send(0); llint res=0; if(!node_id) for(int i=0;i<num_nodes;++i) { Receive(i); llint instance_res=GetLL(i); res+=instance_res; } return res; } #ifdef MY_MESSAGE int my_main( int _id ) #else int main() #endif { #ifdef MY_MESSAGE { extern thread_local int id; id = _id; } #endif int n=GetN(); llint res= (n<MAX_N/50)?solve_single_cpu(): solve_distributed(); if(!MyNodeId()) std::cout<<res; return 0; } |