编程环境
Windows 7/10
- 编辑器:Dev-C++
- 字体:
- 编译选项:
-O2 -std=c++14 -static -Wall -Wextra -fsanitize=address -fsanitize=undefined
缺省源
编译时使用-std=c++11。
代码
#include<bits/stdc++.h>
//#define FileIO
//#define Debug
namespace Template{
#ifdef ONLINE_JUDGE
#undef FileIO
#undef Debug
#endif
// === Shortening ===
#define Made_by return 0;
#define Dark_Crown }int main(){Start_Timer(Main);Main();return 0
#define Multitest int T=0;cin>>T;while(T--)
#define endl '\n'
#define mkpr make_pair
#define fi first
#define se second
#define ls(_) ((_)<<1)
#define rs(_) ((_)<<1|1)
#define fa(_) ((_)>>1)
#define mid(_,__) (((_)+(__))>>1)
#define _mid(_,__) (((_)+(__)+1)>>1)
#define db(_) setprecision(_)
#define None ;
typedef long long Int;
typedef unsigned long long Idx;
typedef long double Double;
typedef std::pair<Int,Int> pir;
typedef std::vector<Int> vec;
const Int INF=1e18+7;
const Double eps=1e-8;
// === IO Ways ===
#ifdef FileIO
#define Problem(_) freopen(#_".in","r",stdin);freopen(#_".out","w",stdout)
#else
#define Problem(_) None
#endif
#define IOSIZE 100000
unsigned int precision = 6, POW[10] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000};
char obuf[IOSIZE], *p3 = obuf;
#if defined(ONLINE_JUDGE)||defined(FileIO)
char ibuf[IOSIZE], *p1 = ibuf, *p2 = ibuf;
#define getchar() ((p1==p2)and(p2=(p1=ibuf)+fread(ibuf,1,IOSIZE,stdin),p1==p2)?(EOF):(*p1++))
#define putchar(x) ((p3==obuf+IOSIZE)&&(fwrite(obuf,p3-obuf,1,stdout),p3=obuf),*p3++=x)
#endif
template<typename T> inline bool read(T &s) { s = 0; int w = 1; char ch; while (ch = getchar(), !(ch>47&&ch<58)&&(ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while ((ch>47&&ch<58)) s = s * 10 + ch - 48, ch = getchar(); return s *= w, true; }
template<typename T> inline void print(T x) { if (x < 0) putchar('-'), x = -x; if (x > 9) print(x / 10); putchar(x % 10 + 48); }
inline bool read(char &s) { while (s = getchar(), isspace(s)&&s != EOF); return s != EOF; }
inline void print(char x) { putchar(x); }
inline bool read(char *s) { char ch; while (ch = getchar(), (ch<33) && ch != EOF); if (ch == EOF) return false; while (!(ch<33)&&(ch != EOF)) *s++ = ch, ch = getchar(); *s = '\0'; return true; }
inline void print(char *x) { while (*x) putchar(*x++); }
inline void print(const char *x) { for (int i = 0; x[i]; i++) putchar(x[i]); }
inline bool read(std::string& s) { s = ""; char ch; while (ch = getchar(), (ch<33)&&ch != EOF); if (ch == EOF) return false; while (!(ch<33)) s += ch, ch = getchar(); return true; }
inline void print(std::string x) { for (int i = 0, n = x.size(); i < n; i++) putchar(x[i]); }
inline bool read(bool &b) { char ch; while(ch=getchar(), (ch<33)&&ch != EOF); return ch == EOF ?false :(b=ch^48, true); }
inline void print(bool b) { putchar(b+48); }
inline bool read(double &x) { char ch = getchar(); int f = 1; for(x=0; (ch<48||57<ch)&&(ch != EOF); ch=getchar()) if(ch == '-') f = -1; if(ch == EOF) return false; for(x=0; 47<ch&&ch<58; ch=getchar()) x = x*10 + (ch^48); if(ch != '.') return x *= f, true; double y = 0.1; for(ch=getchar(); 47<ch&&ch<58; ch=getchar()) x += y*(ch^48), y /= 10; return x *= f, true; }
inline void print(double x) { if(x < 0) putchar('-'), x = -x; if(!precision) print((unsigned long long)(x-(unsigned long long)(x)>=0.5 ?x+1 :x)); else { unsigned long long xx = x; double y = ((x-xx)*POW[precision]); unsigned long long yy = (y-(unsigned long long)(y)>=0.5 ?y+1 :y); if(yy == POW[precision]) xx++, yy = 0; print(xx), putchar('.'); for(int j=precision-1; ~j; j--) putchar(48+yy/POW[j]%10); } }
template<typename T, typename... T1> inline int read(T& a, T1&... other) { return read(a) + read(other...); }
template<typename T, typename... T1> inline void print(T a, T1... other) { print(a), print(other...); }
template<typename T> inline T read() { T a; return read(a), a; }
struct Fast_IO {
bool flag = 1;
inline ~Fast_IO() { fwrite(obuf, p3 - obuf, 1, stdout); }
inline void SetPrecision(int x) { if(x > 9) throw precision = 9; else if(x < 0) precision = 0; else precision = x; }
inline operator bool() { return flag; }
} io;
Fast_IO& operator >> (Fast_IO &io, char *b) { return io.flag &= read(b), io; }
template<typename T> Fast_IO& operator >> (Fast_IO &io, T &b) { return io.flag &= read(b), io; }
template<typename T> Fast_IO& operator << (Fast_IO &io, T b) { return print(b), io; }
#define cout io
#define cin io
// === Debug ===
void _Debug(std::string Str,Int Value){std::cerr<<Str<<": "<<Value<<endl;}
template<class ...Args>void _Debug(std::string Str,Int Value,Args... args){Idx Pos=0;while(Str[Pos]!=',')std::cerr<<Str[Pos++];std::cerr<<": "<<Value<<" ";_Debug(Str.erase(0,Pos+1),args...);}
#ifdef Debug
#define debug(...) _Debug(#__VA_ARGS__,__VA_ARGS__)
#define Start_Timer(_) _Time(_)
#else
#define debug(...) None
#define Start_Timer(_) None
#endif
// === Timer ===
#if __cplusplus >= 201103
#define _Time(_) Timer _timer( #_ )
class Timer{private:std::chrono::high_resolution_clock::time_point start;std::string Point_name;public:Timer(std::string Name):start(std::chrono::high_resolution_clock::now()){Point_name=Name;}~Timer(){auto end=std::chrono::high_resolution_clock::now();auto duration=std::chrono::duration_cast<std::chrono::milliseconds>(end-start);std::cerr<<"\n"<<Point_name<<": "<<duration.count()<<"ms\n";}};
#else
#define _Time(_) None
#endif
// === Max & Min ===
#define max _Max
#define min _Min
template<typename T>T _Max(const T& value){return value;}
template<class ...Args,typename T>T _Max(T value,Args... args){T _max=_Max(args...);if(_max<value)return value;return _max;}
template<typename T>T _Min(const T& value){return value;}
template<class ...Args,typename T>T _Min(T value,Args... args){T _min=_Min(args...);if(_min>value)return value;return _min;}
// === Math ===
inline Int Qpow(Int a,Int b,Int mod){Int res=1;while(b){if(b&1)res=res*a%mod;a=a*a%mod;b>>=1;}return res;}
inline Int Qpow(Int a,Int b){Int res=1;while(b){if(b&1)res=res*a;a=a*a;b>>=1;}return res;}
inline Int Gcd(Int a,Int b){return b?Gcd(b,a%b):a;}
inline Int Lcm(Int a,Int b){return a/Gcd(a,b)*b;}
// === Directions ===
const int Dx[4]={0,1,0,-1};
const int Dy[4]={1,0,-1,0};
const int _Dx[8]={0,1,1,1,0,-1,-1,-1};
const int _Dy[8]={1,1,0,-1,-1,-1,0,1};
// === Array ===
Int Find_First(Int nums[],Int l,Int r,Int value){if(l>r)return -1;Int is_found=-1;while(l<r){if(nums[mid(l,r)]==value)is_found=mid(l,r);if(nums[mid(l,r)]<value)l=mid(l,r)+1;else r=mid(l,r);}if(nums[mid(l,r)]==value)is_found=mid(l,r);return is_found;}
Int Find_Last(Int nums[],Int l,Int r,Int value){if(l>r)return -1;Int is_found=-1;while(l<r){if(nums[_mid(l,r)]==value)is_found=_mid(l,r);if(nums[_mid(l,r)]>value)r=_mid(l,r)-1;else l=_mid(l,r);}if(nums[_mid(l,r)]==value)is_found=_mid(l,r);return is_found;}
Int Count(Int nums[],Int l,Int r,Int value){Int begin=Find_First(nums,l,r,value),end=Find_Last(nums,l,r,value);if(begin==-1)return 0;return end-begin+1;}
// === String ===
char NumToChar(Int num){if(0<=num&&num<=9)return '0'+num;if(10<=num&&num<=35)return 'A'+num-10;if(36<=num&&num<=61)return 'a'+num-36;return ' ';}
Int CharToNum(char ch){if('0'<=ch&&ch<='9')return ch-'0';if('A'<=ch&&ch<='Z')return ch+10-'A';if('a'<=ch&&ch<='z')return ch+36-'a';return INF;}
std::string StringToNum(Int num,Int base){std::string str="";if(num<0)str+='-',num=-num;while(num){str=NumToChar(num%base)+str;num/=base;}return str;}
Int NumToString(std::string str,Int base){Int num=0,f=1;for(unsigned i=0;i<str.size();i++)if(str[i]=='-')f=-1;else num=num*base+CharToNum(str[i]);return num*f;}
}using namespace std;using namespace Template;
const Int Mod=0;
const Int Sizn=0;
//Define var or func here
Int Main(){
Problem();
//Code here
Made_by Dark_Crown;
}开发工具
代码
namespace GameTool{
#include<windows.h>
void SetColor(int frt,int bak){
WORD wColor=((bak&0x0F)<<4)+(frt&0x0F);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),wColor);
}
void SetSize(int con,int row){
char cmd[64];
sprintf(cmd,"mode con cols=%d lines=%d",con,row);
system(cmd);
}
void SetTitle(char name[]){
SetConsoleTitleA(name);
}
void HideCursor(){
CONSOLE_CURSOR_INFO cur={1,0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cur);
}
void ResetColor(){
SetColor(15,0);
}
void ClearScreen(){
COORD CD={0,0};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),CD);
}
class Random{
private:
std::mt19937_64 rng;
public:
Random():rng(std::chrono::steady_clock::now().time_since_epoch().count()){}
int Roll(int l,int r){
return std::uniform_int_distribution<int>(l,r)(rng);
}
};
}
using namespace GameTool;算法
背包
代码
void BackPack(vector<Int>& dp,Int c,Int v,Int sum){
if(sum==0||dp.size()<=c*sum)for(int i=dp.size();i>=c;i--)dp[i]=max(dp[i],dp[i-c]+v);
else if(sum==1)for(int i=c;i<=dp.size();i++)dp[i]=max(dp[i],dp[i-c]+v);
else{
for(Int k=1;k<sum;sum-=k,k<<=1)BackPack(dp,c*k,v*k,1);
BackPack(dp,c*sum,v*sum,1);
}
}最大流(ISAP)
代码
class ISAP{
public:
struct edge{
long long v,w,nxt;
};
vector<edge>e;
vector<int>dep,gap,head,cur;
long long maxflow,ecnt,s,t,siz;
ISAP(int sz){
siz=sz;
ecnt=0;
dep.resize(siz+5);
gap.resize(siz+5);
head.resize(siz+5);
cur.resize(siz+5);
fill(head.begin(),head.end(),-1);
}
void addedge(int x,int y,long long val){
e.push_back({y,val,head[x]});
head[x]=ecnt;
++ecnt;
e.push_back({x,0,head[y]});
head[y]=ecnt;
++ecnt;
}
void bfs(){
queue<int>que;
fill(dep.begin(),dep.end(),-1);
fill(gap.begin(),gap.end(),0);
dep[t]=0;
gap[0]=1;
que.push(t);
while(!que.empty()){
int p=que.front();que.pop();
for(int i=head[p];~i;i=e[i].nxt){
if(~dep[e[i].v])continue;
dep[e[i].v]=dep[p]+1;
++gap[dep[e[i].v]];
que.push(e[i].v);
}
}
}
long long dfs(int u,int flow){
if(u==t){
maxflow+=flow;
return flow;
}
int used=0;
for(int i=cur[u];~i;i=cur[u]=e[i].nxt){
if(e[i].w&&dep[e[i].v]+1==dep[u]){
long long f=dfs(e[i].v,min(e[i].w,flow-used));
if(f)e[i].w-=f,e[i^1].w+=f,used+=f;
if(used==flow)return used;
}
}
--gap[dep[u]];
if(!gap[dep[u]])dep[s]=siz;
++dep[u],++gap[dep[u]];
return used;
}
long long run(int s0,int t0){
s=s0;
t=t0;
maxflow=0;
bfs();
while(dep[s]<siz)cur=head,dfs(s,1e9+7);
return maxflow;
}
};