洛谷 P1001 A+B Problem

时间:2022-07-15 16:11:56

题目描述

输入两个整数a,b,输出它们的和(|a|,|b|<=10^9)。 注意 1、pascal使用integer会爆掉哦!

2、有负数哦!

3、c/c++的main函数必须是int类型,而且最后要return 0。这不仅对洛谷其他题目有效,而且也是noip/noi比赛的要求!

好吧,同志们,我们就从这一题开始,向着大牛的路进发。

“任何一个伟大的思想,都有一个微不足道的开始。”

输入输出格式

输入格式:

两个整数以空格分开

输出格式:

一个数

输入输出样例

输入样例#1:
20 30
输出样例#1:
50
#include <algorithm>
#include <cstdio>
#include <vector>
#include <queue> using namespace std;
int a,b;
long long ans;
class Segmenttree
{
private:
int dis[];
struct Segment
{
int l,r,sum;
Segment *ch[];
Segment()
{
ch[]=ch[]=NULL;
}
};
void build(Segment *&k,int l,int r)
{
k=new Segment;
k->l=l;k->r=r;
if(l==r) {k->sum=dis[l];return;}
int mid=(l+r)>>;
build(k->ch[],l,mid);
build(k->ch[],mid+,r);
k->sum=k->ch[]->sum+k->ch[]->sum;
}
int Tree_Query(Segment *&k,int l,int r)
{
if(k->l==l&&k->r==r) return k->sum;
int mid=(k->l+k->r)>>;
if(l>mid) return Tree_Query(k->ch[],l,r);
else if(r<=mid) return Tree_Query(k->ch[],l,r);
else return Tree_Query(k->ch[],l,mid)+Tree_Query(k->ch[],mid+,r);
}
public:
int get()
{
Segment *root=new Segment;
dis[]=a;
dis[]=b;
build(root,,);
return Tree_Query(root,,);
}
};
class Segmenttree segment;
class chairmantree
{
private:
int rs[],ls[],sum[],rt[],v1[],v2[],tot,Size;
int build(int l,int r)
{
int now=++tot;
sum[now]=;
if(l==r) return now;
int mid=(l+r)>>;
ls[now]=build(l,mid);
rs[now]=build(mid+,r);
return now;
}
inline int rank(int x) {return lower_bound(v2+,v2++Size,x)-v2;}
void update(int l,int r,int x,int &y,int t)
{
y=++tot;
sum[y]=sum[x]+;
if(l==r) return;
ls[y]=ls[x];
rs[y]=rs[x];
int mid=(l+r)>>;
if(t<=mid) update(l,mid,ls[x],ls[y],t);
else update(mid+,r,rs[x],rs[y],t);
}
int ask(int l,int r,int x,int y,int k)
{
if(l==r) return l;
int mid=(l+r)>>;
if(sum[ls[y]]-sum[ls[x]]>=k) return ask(l,mid,ls[x],ls[y],k);
else {k-=sum[ls[y]]-sum[ls[x]];return ask(mid+,r,rs[x],rs[y],k);}
}
public:
int get()
{
v1[]=v2[]=a;
v1[]=v2[]=b;
sort(v2+,v2++);
Size=unique(v2+,v2++)-v2-;
rt[]=build(,Size);
for(int i=;i<=;++i) update(,Size,rt[i-],rt[i],rank(v1[i]));
return v2[ask(,Size,rt[],rt[],)]+v2[ask(,Size,rt[],rt[],)];
}
};
class chairmantree chairman;
class BIT
{
private:
int tag[],n;
inline int lowbit(int x) {return x&(-x);}
void modify(int x,int y)
{
for(;x<=n;x+=lowbit(x))
tag[x]+=y;
}
int ask(int x)
{
int ret=;
for(;x;x-=lowbit(x)) ret+=tag[x];
return ret;
}
public:
int get()
{
n=;
tag[]=tag[]=;
modify(,a);
modify(,b);
return ask()-ask();
}
};
class BIT bit;
class SPFA
{
private:
vector<pair<int,int> >G[];
int dis[],n;
bool vis[];
void Spfa(int s)
{
queue<int>q;
for(int i=;i<=n;++i) vis[i]=,dis[i]=0x7fffffff;
dis[s]=;
q.push(s);
for(int now;!q.empty();)
{
now=q.front();
q.pop();
vis[now]=;
for(int i=;i<G[now].size();++i)
{
int v=G[now][i].first,w=G[now][i].second;
if(dis[v]>dis[now]+w)
{
dis[v]=dis[now]+w;;
if(!vis[v])
{
vis[v]=;
q.push(v);
}
}
}
}
}
public:
int get()
{
n=;
G[].push_back(make_pair(,a));
G[].push_back(make_pair(,b));
Spfa();
return dis[];
}
};
class SPFA spfa;
class FLOYD
{
private:
#define inf 0x3f3f3f3f
inline int min(int a,int b) {return a>b?b:a;}
int f[][],n;
int floyd()
{
for(int k=;k<=n;++k)
for(int i=;i<=n;++i)
for(int j=;j<=n;++j)
f[i][j]=min(f[i][j],f[i][k]+f[k][j]);
return f[][];
}
public:
int get()
{
n=;
for(int i=;i<=n;++i)
for(int j=;j<=n;++j)
f[i][j]=(i!=j)*inf;
f[][]=a;
f[][]=b;
return floyd();
}
};
class FLOYD floyd;
class DINIC
{
private:
inline int min(int a,int b) {return a>b?b:a;}
#define inf 0x3f3f3f3f
int s,t,nextt[],to[],flow[],head[],cnt,dep[];
bool bfs(int s,int t)
{
queue<int>q;
for(int i=s;i<=t;++i) dep[i]=-;
q.push(s);
dep[s]=;
for(int now;!q.empty();)
{
now=q.front();
q.pop();
for(int i=head[now];i;i=nextt[i])
{
int v=to[i];
if(dep[v]==-&&flow[i])
{
dep[v]=dep[now]+;
if(v==t) return true;
q.push(v);
}
}
}
return false;
}
int dfs(int now,int t,int Limit)
{
if(now==t||!Limit) return Limit;
int f,ret=;
for(int i=head[now];i;i=nextt[i])
{
int v=to[i];
if(dep[v]==dep[now]+&&flow[i]&&(f=dfs(v,t,min(Limit,flow[i]))))
{
flow[i]-=f;
flow[i^]+=f;
Limit-=f;
ret+=f;
if(!Limit) break;
}
}
if(ret!=Limit) dep[now]=-;
return ret;
}
int dinic(int s,int t)
{
int ret=;
for(;bfs(s,t);ret+=dfs(s,t,inf));
return ret;
}
void ins(int u,int v,int l)
{
nextt[++cnt]=head[u];
to[cnt]=v;
flow[cnt]=l;
head[u]=cnt;
}
public:
int get()
{
s=;t=;cnt=;
ins(s,,a);
ins(,s,);
ins(s,,b);
ins(,s,);
ins(,t,a);
ins(t,,);
ins(,t,b);
ins(t,,);
return dinic(s,t);
}
};
class DINIC dinic;
class DP
{
private:
inline int max(int a,int b) {return a>b?a:b;}
int f[],v[],w[];
public:
int get()
{
v[]=v[]=;
w[]=a;if(a<) f[]+=a,f[]+=a,f[]+=a;
w[]=b;if(b<) f[]+=b,f[]+=b,f[]+=b;
for(int i=;i<=;++i)
for(int j=;j>=v[i];--j)
f[j]=max(f[j-v[i]]+w[i],f[j]);
return f[];
}
};
class DP dp;
class Kruskal
{
private:
int fa[];
struct node
{
int x,y,z;
bool operator<(node a)const
{
return z<a.z;
}
}e[];
int find_(int x) {return x==fa[x]?x:fa[x]=find_(fa[x]);}
public:
int get()
{
int n=,cnt=;
for(int i=;i<=n;++i) fa[i]=i;
e[++cnt].x=;
e[cnt].y=;
e[cnt].z=a;
e[++cnt].x=;
e[cnt].y=;
e[cnt].z=b;
sort(e+,e++cnt);
int num=,ret=;
for(int i=;i<=cnt;++i)
{
int fx=find_(e[i].x),fy=find_(e[i].y);
if(fx!=fy)
{
fa[fy]=fx;
num++;
ret+=e[i].z;
if(num==n-) break;
}
}
return ret;
}
};
class Kruskal kruskal;
int Main()
{
scanf("%d%d",&a,&b);
ans+=segment.get();
ans+=bit.get();
ans+=chairman.get();
ans+=spfa.get();
ans+=floyd.get();
ans+=dinic.get();
ans+=dp.get();
ans+=kruskal.get();
printf("%lld\n",ans/);
return ;
}
int sb=Main();
int main(int argc,char *argv[]){;}