[分治 杂题] Codeforces Gym 101173 CERC 16 G & BZOJ 4792 Geohash Grid
来源:互联网 发布:win10缺少—个网络协议 编辑:程序博客网 时间:2024/06/05 14:51
先转化问题
If we lay down numbers on a single line and mark numbers within the polyline, We obtain the optimal k-approximation by removing
k−1 largest gaps.
我们考虑怎么求gap 我们把网格切成两半的时候也就是把值域切成了两半 我们只要递归模拟返回区间内最大最小值就可以求出所有gap
这样的gap会有很多 肯定会炸 其实我们可以证明gap的大小不会很多 是
我们用
而因为本质不同的切是
对于所有size 不同的gap就是
算法就很明显了
Finally, we’ll revise our gap-finding algorithm, not to visit both children when the split would result in two tiles that look the same with respect to intersecting with the polygon. Instead we visit only one children, but create twice as many gaps in that branch.
It can be shown that this way we’ll only visitO(n⋅m2) states. We have to look at the polygon through the viewport at every state to determine whether to split, so we have a total complexity ofO(n⋅m3) to find all gaps.
#include<cstdio>#include<cstdlib>#include<algorithm>#include<map>using namespace std;typedef long long ll;typedef pair<ll,ll> abcd;inline char nc(){ static char buf[100000],*p1=buf,*p2=buf; return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;}inline void read(int &x){ char c=nc(),b=1; for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1; for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;}const int M=205;int n,m;int x[M],y[M];map<ll,ll> Map;typedef map<ll,ll>::reverse_iterator ITER;#define _y1 min(y[i],y[j])#define _y2 max(y[i],y[j])#define _x1 min(x[i],x[j])#define _x2 max(x[i],x[j])inline abcd Solve(int x1,int y1,int x2,int y2,ll v0,ll t){ ll dx=x2-x1,dy=y2-y1,dv=(x2-x1)*(y2-y1)/2; int ins=0,p=0,h=0,v=0; for (int i=0,j=1;i<m;i++,j=(++j)%m){ p|=x[i]>x1 && x[i]<x2 && y[i]>y1 && y[i]<y2; ins^=x[i]==x[j] && x[i]<=x1 && _y1<=y1 && y1<_y2; h|=x[i]!=x[j] && y[i]>y1 && y[i]<y2 && max(_x1,x1)<min(_x2,x2); v|=x[i]==x[j] && x[i]>x1 && x[i]<x2 && max(_y1,y1)<min(_y2,y2); } if (!p && !v && !h) return ins?abcd(v0,v0+dx*dy-1):abcd(1LL<<40,-1LL<<40); abcd a,b; if (dx==dy){ if (!p && h) a=Solve(x1,y1,(x1+x2)/2,y2,v0,t<<1),b=abcd(a.first+dv,a.second+dv); else a=Solve(x1,y1,(x1+x2)/2,y2,v0,t),b=Solve((x1+x2)/2,y1,x2,y2,v0+dv,t); }else{ if (!p && v) a=Solve(x1,y1,x2,(y1+y2)/2,v0,t<<1),b=abcd(a.first+dv,a.second+dv); else a=Solve(x1,y1,x2,(y1+y2)/2,v0,t),b=Solve(x1,(y1+y2)/2,x2,y2,v0+dv,t); } if (a.second!=-1LL<<40 && b.first!=1LL<<40 && a.second+1<b.first) Map[b.first-a.second-1]+=t; return abcd(min(a.first,b.first),max(a.second,b.second));}int p;ll val[1000005],cnt[1000005],sum[1000005];int main(){ freopen("t.in","r",stdin); freopen("t.out","w",stdout); read(n); read(m); for (int i=0;i<m;i++) read(x[i]),read(y[i]); abcd t=Solve(0,0,1<<n,1<<n,0,1); ll ans=t.second-t.first+1; for (ITER it=Map.rbegin();it!=Map.rend();it++){ val[++p]=it->first; cnt[p]=cnt[p-1]+it->second; sum[p]=sum[p-1]+it->first*it->second; } int Q,K; read(Q); while (Q--){ read(K); K--; int it=lower_bound(cnt+1,cnt+p+1,K)-cnt; printf("%lld\n",ans-(sum[it-1]+(K-cnt[it-1])*val[it])); } return 0;}
- [分治 杂题] Codeforces Gym 101173 CERC 16 G & BZOJ 4792 Geohash Grid
- Codeforces Gym 101173 CERC 16 D & BZOJ 4790 Dancing Disks
- [2-SAT 构造] Codeforces Gym 101173 CERC 16 L. Lost Logic
- GYM CERC 16 K Key Knocking 构造
- codeforces gym 100548G
- CodeForces Gym 100735G
- CodeForces Gym 100735G
- Codeforces-GYM-100923G
- codeforces [Gym-100814G]
- Codeforces Gym-101617G
- Codeforces gym 101350G 数学
- Codeforces gym 101149 G 想法
- Codeforces Gym 101164 G. Pokemons
- Gym CERC 16 F Free Figuriness 思维+模拟
- [博弈论] Codeforces Gym 101190 NEERC 16 G. Game on Graph
- 2014-2015 ACM-ICPC, Central Europe Regional Contest (CERC 14) [Gym-100543G]
- Codeforces Gym 100625G Getting Through
- codeforces Gym 100431G Persistent Queue
- sdut_java_C/C++经典程序训练3---模拟计算器
- java异常的一些学习
- 文件与IO
- javaSE_05Java中方法(函数)与重载、递归-练习
- Springboot(热部署)
- [分治 杂题] Codeforces Gym 101173 CERC 16 G & BZOJ 4792 Geohash Grid
- 容器配接器(stacks)
- sdut_java_C/C++经典程序训练2---斐波那契数列
- VS+MFC+OPENCV摄像头跟踪蓝色物体并输出坐标并发送
- 【cqoi2011】动态逆序对
- 高斯日记
- RSA根据modulus,exponent生成公钥
- DHTML概述
- 链表求和