Skip to content

Instantly share code, notes, and snippets.

@MiSawa
Created June 14, 2015 17:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save MiSawa/581d08c040c4b60b83c8 to your computer and use it in GitHub Desktop.
Save MiSawa/581d08c040c4b60b83c8 to your computer and use it in GitHub Desktop.
template for dcj
#include <bits/stdc++.h>
#define all(x) begin(x),end(x)
#define rall(x) (x).rbegin(),(x).rend()
#define REP(i,b,n) for(int i=(int)(b);i<(int)(n);++i)
#define rep(i,n) REP(i,0,n)
#define repsz(i,v) rep(i,(v).size())
#define aur auto&
#define bit(n) (1LL<<(n))
#define eb emplace_back
#define mt make_tuple
#define fst first
#define snd second
using namespace std;
typedef long long ll;
//#define int long long
template<class C>int size(const C &c){ return c.size(); }
template<class T>bool chmin(T&a,const T&b){if(a<=b)return false;a=b;return true;}
template<class T>bool chmax(T&a,const T&b){if(a>=b)return false;a=b;return true;}
#include "problem_name.h"
#include "message.h"
pair<long long, long long> GetJob(const long long &n, int i = -1){//{{{
if(i == -1) i = MyNodeId();
const long long w = 1 + n / NumberOfNodes();;
const long long l = w * i;
const long long r = min<long long>(n, l + w);
return make_pair(l, r);
}//}}}
struct IO{//{{{
int id;
IO(const int &id = 0) : id(id){}
inline IO &setId(const int &id = 0){ this->id = id; return *this; }
inline IO &operator<<(const long long &x){
PutLL(id, x); Send(id);
return *this;
}
inline IO &operator<<(const int &x){
PutInt(id, x); Send(id);
return *this;
}
inline IO &operator<<(const char &c){
PutChar(id, c); Send(id);
return *this;
}
inline IO &operator>>(int &x){
Receive(id); x = GetInt(id);
return *this;
}
inline IO &operator>>(long long &x){
Receive(id); x = GetLL(id);
return *this;
}
inline IO &operator>>(char &x){
Receive(id); x = GetChar(id);
return *this;
}
inline int ni(){ Receive(id); return GetInt(id); }
inline long long nll(){ Receive(id); return GetLL(id); }
inline char nc(){ Receive(id); return GetChar(id); }
inline int ni(const int &id){ Receive(id); return GetInt(id); }
inline long long nll(const int &id){ Receive(id); return GetLL(id); }
inline char nc(const int &id){ Receive(id); return GetChar(id); }
};//}}}
struct BufferedIO{//{{{
int id;
BufferedIO(const int &id = 0) : id(id){}
~BufferedIO() { /* send_all(); */ }
inline BufferedIO &setId(const int &id = 0){ this->id = id; return *this; }
inline void send(){ Send(id); }
inline void send(const int &id){ Send(id); }
inline void send_all(){ for(int i = NumberOfNodes()-1; i >= 0; --i) Send(i); }
inline BufferedIO &operator<<(const long long &x){
PutLL(id, x);
return *this;
}
inline BufferedIO &operator<<(const int &x){
PutInt(id, x);
return *this;
}
inline BufferedIO &operator<<(const char &c){
PutChar(id, c);
return *this;
}
inline BufferedIO &operator>>(int &x){
Receive(id); x = GetInt(id);
return *this;
}
inline BufferedIO &operator>>(long long &x){
Receive(id); x = GetLL(id);
return *this;
}
inline BufferedIO &operator>>(char &x){
Receive(id); x = GetChar(id);
return *this;
}
inline int ni(){ Receive(id); return GetInt(id); }
inline long long nll(){ Receive(id); return GetLL(id); }
inline char nc(){ Receive(id); return GetChar(id); }
inline int ni(const int &id){ Receive(id); return GetInt(id); }
inline long long nll(const int &id){ Receive(id); return GetLL(id); }
inline char nc(const int &id){ Receive(id); return GetChar(id); }
};//}}}
const int Nodes = NumberOfNodes();
const int Id = MyNodeId();
void solve_slave(){//{{{
ll n = GetN();
ll l, r; tie(l, r) = GetJob(n);
ll s = 0;
for(ll i = l; i < r; ++i) s += GetVal(i);
IO io; io << s;
}//}}}
void solve_master(){//{{{
IO io;
ll res = 0;
rep(i, Nodes) res += io.nll(i);
cout << res << endl;
}//}}}
signed main(){
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
cout << std::fixed << std::setprecision(10);
solve_slave();
if(MyNodeId() == 0) solve_master();
return 0;
}
// vim:set foldmethod=marker commentstring=//%s:
// Sample input 1, in CPP.
#include <cassert>
#include <fstream>
// if you add test cases, you should add 'case' statements
// in the select_case macro.
namespace TestCase1{
#include "problem_name1.h"
}
namespace TestCase2{
#include "problem_name2.h"
}
namespace TestCase3{
#include "problem_name3.h"
}
const int TestCaseID = ([&](){
fstream fs("test_case_id");
int x; fs >> x;
return x;
})();
// defines
#define select_case(func_call) \
switch(TestCaseID){ \
case 1: return TestCase1::func_call; \
case 2: return TestCase2::func_call; \
case 3: return TestCase3::func_call; \
default: assert(0); \
}
#define DEFFUNC0(result_type, func_name) \
inline result_type func_name(){ \
select_case(func_name()) \
}
#define DEFFUNC1(result_type, func_name, arg_type_1, arg_name_1) \
inline result_type func_name(arg_type_1 arg_name_1){ \
select_case(func_name(arg_name_1)) \
}
#define DEFFUNC2(result_type, func_name, arg_type_1, arg_name_1, arg_type_2, arg_name_2) \
inline result_type func_name(arg_type_1 arg_name_1, arg_type_2 arg_name_2){ \
select_case(func_name(arg_name_1, arg_name_2)) \
}
DEFFUNC0(long long, GetN);
DEFFUNC1(long long, GetVal, long long, i);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment