# 2019 Multi-University Training Contest 3

http://acm.hdu.edu.cn/contests/contest_show.php?cid=850

## 1004. Distribution of books

$$f_i$$表示前$$i$$个能分成最多的段数，使得每一段和都$$\leqslant x$$

#include<bits/stdc++.h>
using namespace std;

#define int long long

x=0;int f=1;char ch=getchar();
for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=-f;
for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';x*=f;
}

void print(int x) {
if(x<0) putchar('-'),x=-x;
if(!x) return ;print(x/10),putchar(x%10+48);
}
void write(int x) {if(!x) putchar('0');else print(x);putchar('\n');}

#define lf double

#define pii pair<int,int >
#define vec vector<int >

#define pb push_back
#define mp make_pair
#define fr first
#define sc second

#define FOR(i,l,r) for(int i=l,i##_r=r;i<=i##_r;i++)

const int maxn = 4e5+10;
const int inf = 1e9;
const lf eps = 1e-8;
const int mod = 1e9+7;

int n,a[maxn],f[maxn],s[maxn],k;

#define ls p<<1
#define rs p<<1|1

struct Segment_Tree {
int t[maxn*8];
void modify(int p,int l,int r,int x,int v) {
t[p]=max(t[p],v);
if(l==r) return ;int mid=(l+r)>>1;
if(x<=mid) modify(ls,l,mid,x,v);
else modify(rs,mid+1,r,x,v);
}

int query(int p,int l,int r,int x,int y) {
if(x<=l&&r<=y) return t[p];
int mid=(l+r)>>1,ans=0;
if(x<=mid) ans=max(ans,query(ls,l,mid,x,y));
if(y>mid) ans=max(ans,query(rs,mid+1,r,x,y));
return ans;
}

void build(int p,int l,int r) {
t[p]=0;if(l==r) return ;int mid=(l+r)>>1;
build(ls,l,mid),build(rs,mid+1,r);
}
}T;

int r[maxn],tot;

int check(int x) {
tot=0;
FOR(i,1,n) f[i]=0,r[++tot]=s[i],r[++tot]=s[i]+x;
sort(r+1,r+tot+1);int m=unique(r+1,r+tot)-r-1;
T.build(1,1,m);
for(int i=1;i<=n;i++) {
int pps=lower_bound(r+1,r+m+1,s[i])-r;
int ppsw=lower_bound(r+1,r+m+1,s[i]+x)-r;
int a=T.query(1,1,m,pps,m);
if(!a) f[i]=s[i]<=x?1:0;
else f[i]=a+1;
if(f[i]>=k) return 1;
T.modify(1,1,m,ppsw,f[i]);
}return 0;
}

void solve() {
for(int i=1;i<=n;i++) s[i]=s[i-1]+a[i];
int l=-2e14,r=1e9,ans=r;
while(l<=r) {
int mid=(l+r)>>1;
if(check(mid)) r=mid-1,ans=mid;
else l=mid+1;
}write(ans);
}

signed main() {
return 0;
}


## 1006. Fansblog

#include<bits/stdc++.h>
using namespace std;

#define int long long

x=0;int f=1;char ch=getchar();
for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=-f;
for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';x*=f;
}

void print(int x) {
if(x<0) putchar('-'),x=-x;
if(!x) return ;print(x/10),putchar(x%10+48);
}
void write(int x) {if(!x) putchar('0');else print(x);putchar('\n');}

#define lf double

#define pii pair<int,int >
#define vec vector<int >

#define pb push_back
#define mp make_pair
#define fr first
#define sc second

#define FOR(i,l,r) for(int i=l,i##_r=r;i<=i##_r;i++)

const int maxn = 1e6+10;
const int inf = 1e9;
const lf eps = 1e-8;
const int mod = 1e9+7;

const int pri[] = {0,2,3,5,7,11,13,23,31};

int gcd(int a,int b) {return !b?a:gcd(b,a%b);}

int mul(int x,int y,int p) {
int t=(long double)x*y/p;
int res=x*y-t*p;res%=p;if(res<0) res+=p;
return res;
}

int qpow(int a,int x,int p) {
int res=1;
for(;x;x>>=1,a=mul(a,a,p)) if(x&1) res=mul(res,a,p);
return res;
}

bool MR(int n) {
if(n<2) return 1;
int e=(n-1)>>__builtin_ctz(n-1);
for(int i=1;i<=8;i++) {
if(pri[i]>=n) break;
for(int d=e,lst=qpow(pri[i],d,n),now;d!=n-1;d<<=1,lst=now) {
now=mul(lst,lst,n);
if(lst!=1&&lst!=n-1&&now==1) return 0;
}if(qpow(pri[i],n-1,n)!=1) return 0;
}return 1;
}

int p,q;

void solve() {
for(int i=p-1;;i--) {
if(MR(i)) {break;}
ans=mul(ans,qpow(i,p-2,p),p);
}
write(ans);
}

signed main() {
return 0;
}


## 1011. Squrirrel

#include<bits/stdc++.h>
using namespace std;

x=0;int f=1;char ch=getchar();
for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=-f;
for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';x*=f;
}

void print(int x) {
if(x<0) putchar('-'),x=-x;
if(!x) return ;print(x/10),putchar(x%10+48);
}
void write(int x) {if(!x) putchar('0');else print(x);putchar('\n');}

#define lf double

#define pii pair<int,int >
#define vec vector<int >

#define pb push_back
#define mp make_pair
#define fr first
#define sc second

#define FOR(i,l,r) for(int i=l,i##_r=r;i<=i##_r;i++)

const int maxn = 2e5+10;
const int inf = 1e9;
const lf eps = 1e-8;
const int mod = 1e9+7;

struct edge{int to,nxt,w;}e[maxn<<1];

void dfs(int x,int fa) {
if(dep[x]>dep[rt]) rt=x;nxt[x]=fa;
if(e[i].to!=fa) dep[e[i].to]=dep[x]+e[i].w,dfs(e[i].to,x);
}

void dp(int x,int fa) {
int mx1=0,mx2=0,ww,p;
if(e[i].to!=fa&&!vis[e[i].to]) {
dp(e[i].to,x);int t=f[e[i].to][0]+e[i].w;
f[x][0]=max(f[e[i].to][0]+e[i].w,f[x][0]);
if(t>=mx1) mx2=mx1,mx1=t,ww=e[i].w,p=e[i].to;
else if(t>mx2) mx2=t;
}
if(mx1==mx2) f[x][1]=f[x][0];
else f[x][1]=min(max(mx2,mx1-ww),max(mx2,f[p][1]+ww));
}

void solve() {
dep[1]=0;dfs(1,0);int sz=0;dep[rt]=0;dfs(rt,0);
for(int u=rt;u;u=nxt[u]) id[++sz]=u,vis[u]=1;
for(int i=1;i<=sz;i++) dp(id[i],0);
g[1][0]=f[id[1]][0],g[1][1]=f[id[1]][1];
for(int i=2;i<=sz;i++) {
int ww;
for(int j=head[id[i-1]];j;j=e[j].nxt) if(e[j].to==id[i]) ww=e[j].w;//,printf("ww :: %d %d %d\n",id[i-1],id[i],ww);
g[i][0]=max(g[i-1][0]+ww,f[id[i]][0]);
g[i][1]=min(max(g[i-1][0],f[id[i]][0]),max(g[i-1][1]+ww,f[id[i]][0]));
g[i][1]=min(g[i][1],max(g[i-1][0]+ww,f[id[i]][1]));
}
h[sz][0]=f[id[sz]][0],h[sz][1]=f[id[sz]][1];int ans=1e9,p;
for(int i=sz-1;i;i--) {
int ww;
h[i][0]=max(h[i+1][0]+ww,f[id[i]][0]);
h[i][1]=min(max(h[i+1][0],f[id[i]][0]),max(h[i+1][1]+ww,f[id[i]][0]));
h[i][1]=min(h[i][1],max(h[i+1][0]+ww,f[id[i]][1]));
}
for(int i=1;i<=sz;i++) {
//printf("%d ",id[i]);
// printf("%d %d %d\n",id[i],h[i][0],h[i][1]);
int tmp=min(max(g[i][0],h[i][1]),max(g[i][1],h[i][0]));
if(tmp<ans) ans=tmp,p=id[i];
}//puts("");
printf("%d %d\n",p,ans);
// write(ans);
}

void clear() {