bzoj 2811 覆盖#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#define ll long long
#define inf 1e9
#define eps 1e-10
#define md
#define N 20010
using namespace std;
struct point { int x,y;} p[N];
int vis[N];
int n,A,B,C,D;
bool jd(int x,int y,int len)
{
//printf(" jd: %d %d %d\n",x,y,len);
int A=inf,B=-inf,C=-inf,D=inf;
for (int i=1;i<=n;i++)
{
if (vis[i]==1) continue;
if (x<=p[i].x&&p[i].x<=x+len&&y<=p[i].y&&p[i].y<=y+len) vis[i]=2;
else
{
A=min(A,p[i].x); B=max(B,p[i].x);
C=max(C,p[i].y); D=min(D,p[i].y);
}
}
//for (int i=1;i<=n;i++) printf("%d ",vis[i]); printf("\n");
//printf(" abcd: %d %d %d %d\n",A,B,C,D);
if (A==inf||(B-A<=len&&C-D<=len)) return 1; else return 0;
}
bool solve(int x,int y,int len)
{
//printf("solve: %d %d %d\n",x,y,len);
memset(vis,0,sizeof(vis));
int A=inf,B=-inf,C=-inf,D=inf;
for (int i=1;i<=n;i++)
{
if (x<=p[i].x&&p[i].x<=x+len&&y<=p[i].y&&p[i].y<=y+len) vis[i]=1;
else
{
A=min(A,p[i].x); B=max(B,p[i].x);
C=max(C,p[i].y); D=min(D,p[i].y);
}
}
if (A==inf) return 1;
if (jd(A,D,len)) return 1;
if (jd(A,C-len,len)) return 1;
if (jd(B-len,D,len)) return 1;
if (jd(B-len,C-len,len)) return 1;
return 0;
}
bool ok(int len)
{
if (solve(A,D,len)) return 1;
if (solve(A,C-len,len)) return 1;
if (solve(B-len,D,len)) return 1;
if (solve(B-len,C-len,len)) return 1;
return 0;
}
int main()
{
scanf("%d",&n);
A=inf; B=-inf; C=-inf; D=inf;
for (int i=1;i<=n;i++)
{
scanf("%d%d",&p[i].x,&p[i].y);
A=min(A,p[i].x); B=max(B,p[i].x);
C=max(C,p[i].y); D=min(D,p[i].y);
}
int l=1,r=inf;
while (l!=r)
{
int mid=(l+r)>>1;
if (ok(mid)) r=mid; else l=mid+1;
}
printf("%d\n",l);
return 0;
}
bzoj 1367 sequence#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#define ll long long
#define inf 1e9
#define eps 1e-10
#define md
#define N 100010
using namespace std;
struct data { int l,r,c;} q[N],a[N];
int c[N],is[N],ql[N],qr[N],f[N],g[N];
int n;
bool cmp(data a,data b) { return a.l==b.l?a.r>b.r:a.l<b.l;}
void add(int x,int d)
{
for (;x<=n;x+=x&(-x)) c[x]+=d;
}
int qsum(int x)
{
int ans=0;
for (;x;x-=x&(-x)) ans+=c[x];
return ans;
}
int main()
{
int K,m;
scanf("%d%d%d",&n,&K,&m);
for (int i=1;i<=m;i++)
{
scanf("%d%d%d",&a[i].l,&a[i].r,&a[i].c);
if (a[i].c==0)
{
add(a[i].l,1); add(a[i].r+1,-1);
}
}
int w=0;
for (int i=1;i<=n;i++)
{
if (qsum(i)==0)
{
w++; is[w]=i; ql[i]=w; qr[i]=w;
}
}
if (w==K) { for (int i=1;i<=K;i++) printf("%d\n",is[i]); return 0;}
ql[n+1]=inf; for (int i=n;i;i--) if (!ql[i]) ql[i]=ql[i+1];
for (int i=1;i<=n;i++) if (!qr[i]) qr[i]=qr[i-1];
//for (int i=1;i<=n;i++) printf("%d %d ",ql[i],qr[i]); printf("\n");
int top=0;
for (int i=1;i<=m;i++) { a[i].l=ql[a[i].l]; a[i].r=qr[a[i].r];}
//for (int i=1;i<=m;i++) printf("%d %d %d\n",a[i].l,a[i].r,a[i].c); printf("\n");
sort(a+1,a+m+1,cmp);
for (int i=1;i<=m;i++)
{
if (a[i].c)
{
if (a[i].l>a[i].r) continue;
while (top&&q[top].l<=a[i].l&&a[i].r<=q[top].r) top--;
q[++top]=a[i];
}
}
//for (int i=1;i<=top;i++) printf("%d %d\n",q[i].l,q[i].r);
//q[top+1].l=q[top+1].r=inf;
int mn=0,mx=inf;
for (int i=1;i<=top;i++)
{
if (mn<q[i].l) { mn=q[i].r; f[i]=f[i-1]+1;}
else f[i]=f[i-1];
}
for (int i=top;i;i--)
{
if (mx>q[i].r) { mx=q[i].l; g[i]=g[i+1]+1;}
else g[i]=g[i+1];
}
//for (int i=1;i<=top;i++) printf("%d ",f[i]); printf("\n");
//for (int i=1;i<=top;i++) printf("%d ",g[i]); printf("\n");
bool hv=0;
for (int i=1;i<=top;i++)
{
if (q[i].l==q[i].r)
{
printf("%d\n",is[q[i].l]);
hv=1;
continue;
}
if (f[i]==f[i-1]) continue;
else
{
int pos=q[i].r-1;
int l=0,r=top+1;
while (l!=r)
{
int mid=(l+r+1)>>1;
if (q[mid].r<pos) l=mid; else r=mid-1;
}
int t1=l;
l=t1; r=top+1;
while (l!=r)
{
int mid=(l+r)>>1;
if (q[mid].l>pos) r=mid; else l=mid+1;
}
int t2=l;
if (f[t1]+g[t2]+1>K)
{
printf("%d\n",is[q[i].r]);
hv=1;
}
}
}
if (!hv) printf("-1\n");
return 0;
}
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#define ll long long
#define inf 1e9
#define eps 1e-10
#define md
#define N 1000010
using namespace std;
int a[N],v[N],lx[N],rx[N],sz[N],dis[N],root[N],L[N],R[N];
struct left_tree
{
int top(int x)
{
return v[x];
}
int merge(int x,int y)
{
if (!x) return y;
if (!y) return x;
if (v[x]<v[y]) swap(x,y);
rx[x]=merge(rx[x],y);
sz[x]=sz[lx[x]]+sz[rx[x]]+1;
if (dis[rx[x]]>dis[lx[x]]) swap(lx[x],rx[x]);
dis[x]=dis[rx[x]]+1;
return x;
}
} heap;
int main()
{
int n;
scanf("%d",&n);
for (int i=1;i<=n;i++) { scanf("%d",&a[i]); a[i]-=i; }
int top=0;
for (int i=1;i<=n;i++)
{
sz[i]=dis[i]=1; lx[i]=rx[i]=0; v[i]=a[i];
root[++top]=i; L[top]=R[top]=i;
while (top>1&&heap.top(root[top-1])>heap.top(root[top]))
{
top--;
root[top]=heap.merge(root[top],root[top+1]);
R[top]=R[top+1];
int tot=R[top]-L[top]+1;
while (sz[root[top]]*2>tot+1) root[top]=heap.merge(lx[root[top]],rx[root[top]]);
}
//for (int j=1;j<=top;j++) printf("L: %d R: %d mid: %d\n",L[j],R[j],heap.top(root[j])); printf("\n");
}
ll ans=0;
for (int i=1;i<=top;i++)
{
int mid=heap.top(root[i]); //printf("\nL:%d R:%d mid: %d\n",L[i],R[i],mid);
for (int j=L[i];j<=R[i];j++) ans+=abs(a[j]-mid); //printf("%d %d ",a[j],mid);
}//printf("\n");
printf("%lld\n",ans);
return 0;
}
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#define ll long long
#define inf 1e9
#define eps 1e-10
#define md
#define N 100500
using namespace std;
struct data { ll dt; int dep; } a[N];
struct cmp
{
bool operator () (data a,data b) { return a.dt==b.dt?a.dep>b.dep:a.dt>b.dt;}
};
priority_queue<data,vector<data>,cmp> q;
int main()
{
int n,k;
scanf("%d%d",&n,&k);
for (int i=1;i<=n;i++) { scanf("%lld",&a[i].dt); a[i].dep=1;}
while (k!=2&&(n%(k-1))!=1) { n++; a[n].dep=a[n].dt=0; }
for (int i=1;i<=n;i++) q.push(a[i]);
ll ans=0;
while (q.size()!=1)
{
ll sum=0; int mxdep=0;
data x;
for (int i=1;i<=k;i++)
{
x=q.top(); q.pop(); //printf("pop: %lld %d\n",x.dt,x.dep);
sum+=x.dt; mxdep=max(mxdep,x.dep);
}
x.dt=sum; x.dep=mxdep+1; ans+=sum;
q.push(x);
}
printf("%lld\n%d\n",ans,(q.top()).dep-1);
return 0;
}