BZOJ3941 : [Usaco2015 Feb]Fencing the Herd

若所有点同侧则表明将各个点带入直线解析式ax+by-c后得到的值均同号
等价于最大值和最小值同号
考虑CDQ分治,每一步分治的过程中求出上下凸壳,然后三分答案即可
时间复杂度$O(n\log^2n)$

#include<cstdio>
#include<algorithm>
typedef long long ll;
const int N=200010;
const ll inf=1LL<<60;
struct P{int x,y;P(){}P(int _x,int _y){x=_x,y=_y;}}b[N],q1[N],q2[N],now;
struct Q{int a,b,q;ll c;Q(){}Q(int _a,int _b,ll _c,int _q){a=_a,b=_b,c=_c,q=_q;}}a[N];
int n,m,i,op,A,B,cnt,t1,t2,m1,m2,len;ll C,s1,s2,ans1[N],ans2[N];
inline bool cmp1(P a,P b){return a.x==b.x?a.y>b.y:a.x<b.x;}
inline bool cmp2(P a,P b){return a.x==b.x?a.y<b.y:a.x<b.x;}
inline ll mul(P b){return(ll)now.x*b.x+(ll)now.y*b.y;}
inline void Max(ll&a,ll b){if(a<b)a=b;}
inline void Min(ll&a,ll b){if(a>b)a=b;}
inline void read(int&a){
  char c;bool f=0;a=0;
  while(!((((c=getchar())>=‘0‘)&&(c<=‘9‘))||(c==‘-‘)));
  if(c!=‘-‘)a=c-‘0‘;else f=1;
  while(((c=getchar())>=‘0‘)&&(c<=‘9‘))(a*=10)+=c-‘0‘;
  if(f)a=-a;
}
inline void read(ll&a){
  char c;bool f=0;a=0;
  while(!((((c=getchar())>=‘0‘)&&(c<=‘9‘))||(c==‘-‘)));
  if(c!=‘-‘)a=c-‘0‘;else f=1;
  while(((c=getchar())>=‘0‘)&&(c<=‘9‘))(a*=10)+=c-‘0‘;
  if(f)a=-a;
}
inline void askmax1(ll&t){
  for(int l=0,r=t1;l<=r&&t<C;){
    len=(r-l)/3;
    if((s1=mul(q1[m1=l+len]))>(s2=mul(q1[m2=r-len])))Max(t,s1),r=m2-1;else Max(t,s2),l=m1+1;
  }
}
inline void askmax2(ll&t){
  for(int l=0,r=t2;l<=r&&t<C;){
    len=(r-l)/3;
    if((s1=mul(q2[m1=l+len]))>(s2=mul(q2[m2=r-len])))Max(t,s1),r=m2-1;else Max(t,s2),l=m1+1;
  }
}
inline void askmin1(ll&t){
  for(int l=0,r=t1;l<=r&&t>C;){
    len=(r-l)/3;
    if((s1=mul(q1[m1=l+len]))<(s2=mul(q1[m2=r-len])))Min(t,s1),r=m2-1;else Min(t,s2),l=m1+1;
  }
}
inline void askmin2(ll&t){
  for(int l=0,r=t2;l<=r&&t>C;){
    len=(r-l)/3;
    if((s1=mul(q2[m1=l+len]))<(s2=mul(q2[m2=r-len])))Min(t,s1),r=m2-1;else Min(t,s2),l=m1+1;
  }
}
void solve(int l,int r){
  if(l==r)return;
  int mid=(l+r)>>1;
  solve(l,mid),solve(mid+1,r);
  for(cnt=0,i=l;i<=mid;i++)if(!a[i].q)b[cnt++]=P(a[i].a,a[i].b);
  if(!cnt)return;
  for(std::sort(b,b+cnt,cmp1),q1[t1=0]=b[0],i=1;i<cnt;i++)if(b[i].x!=b[i-1].x){
    while(t1&&(ll)(q1[t1].y-q1[t1-1].y)*(b[i].x-q1[t1].x)<=(ll)(b[i].y-q1[t1].y)*(q1[t1].x-q1[t1-1].x))t1--;
    q1[++t1]=b[i];
  }
  for(std::sort(b,b+cnt,cmp2),q2[t2=0]=b[0],i=1;i<cnt;i++)if(b[i].x!=b[i-1].x){
    while(t2&&(ll)(q2[t2].y-q2[t2-1].y)*(b[i].x-q2[t2].x)>=(ll)(b[i].y-q2[t2].y)*(q2[t2].x-q2[t2-1].x))t2--;
    q2[++t2]=b[i];
  }
  for(i=r;i>mid;i--)if(a[i].q){
    now=P(a[i].a,a[i].b),C=a[i].c;
    if(a[i].b>0)askmin2(ans1[i]),askmax1(ans2[i]);else askmin1(ans1[i]),askmax2(ans2[i]);
  }
}
int main(){
  read(n),read(m);
  for(i=1;i<=n;i++)read(A),read(B),a[i]=Q(A,B,0,0);
  for(i=1;i<=m;i++){
    read(op),read(A),read(B);
    if(op==1)a[n+i]=Q(A,B,0,0);else read(C),a[n+i]=Q(A,B,C,1);
    ans1[n+i]=inf,ans2[n+i]=-inf;
  }
  solve(1,n+=m);
  for(i=1;i<=n;i++)if(a[i].q)puts(ans2[i]<a[i].c||ans1[i]>a[i].c?"YES":"NO");
  return 0;
}

  

时间: 2024-10-22 05:32:38

BZOJ3941 : [Usaco2015 Feb]Fencing the Herd的相关文章

bzoj3943[Usaco2015 Feb]SuperBull*

bzoj3943[Usaco2015 Feb]SuperBull 题意: n头牛进行锦标赛,每场比赛的好看程度是两头牛的编号异或和,并总有一方被淘汰.求安排比赛(可以决定比赛胜负)可以得到的最大总好看程度是多少.n≤2000 题解: 先求出牛两两之间的异或和,然后发现可以把比赛看做连边,且共有n-1场比赛,所以求最大生成树就行了.神犇们用的都是Prim,蒟蒻不会,用Kruscal结果时间排倒数. 代码: 1 #include <cstdio> 2 #include <cstring>

bzoj3942: [Usaco2015 Feb]Censoring

AC自动机.嗯bzoj3940弱化版.水过去了(跑的慢啊QAQ.想了想可以用hash写.挖坑 #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<queue> using namespace std; #define rep(i,s,t) for(int i=s;i<=t;i++) #define clr(x,c) memset

BZOJ 3942: [Usaco2015 Feb]Censoring

3942: [Usaco2015 Feb]Censoring Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 404  Solved: 221[Submit][Status][Discuss] Description Farmer John has purchased a subscription to Good Hooveskeeping magazine for his cows, so they have plenty of material

bzoj 3940: [Usaco2015 Feb]Censoring -- AC自动机

3940: [Usaco2015 Feb]Censoring Time Limit: 10 Sec  Memory Limit: 128 MB Description Farmer John has purchased a subscription to Good Hooveskeeping magazine for his cows, so they have plenty of material to read while waiting around in the barn during

[bzoj3943][Usaco2015 Feb]SuperBull_Kruskal

SuperBull bzoj-3943 Usaco-2015 Feb 题目大意:贝西和她的朋友们在参加一年一度的“犇”(足)球锦标赛.FJ的任务是让这场锦标赛尽可能地好看.一共有N支球队参加这场比赛,每支球队都有一个特有的取值在1-230-1之间的整数编号(即:所有球队编号各不相同).“犇”锦标赛是一个淘汰赛制的比赛——每场比赛过后,FJ选择一支球队淘汰,淘汰了的球队将不能再参加比赛.锦标赛在只有一支球队留下的时候就结束了.FJ发现了一个神奇的规律:在任意一场比赛中,这场比赛的得分是参加比赛两队

Bzoj3940 [Usaco2015 Feb]Censoring

Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 391  Solved: 183 Description Farmer John has purchased a subscription to Good Hooveskeeping magazine for his cows, so they have plenty of material to read while waiting around in the barn during milking

最大生成树 BZOJ3943 [Usaco2015 Feb]SuperBull

Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 374  Solved: 217[Submit][Status][Discuss] Description Bessie and her friends are playing hoofball in the annual Superbull championship, and Farmer John is in charge of making the tournament as exciting

【Usaco2015 FEB】Cow Hopscotch (Gold)

Just like humans enjoy playing the game of Hopscotch, Farmer John's cows have invented a variant of the game for themselves to play. Being played by clumsy animals weighing nearly a ton, Cow Hopscotch almost always ends in disaster, but this has surp

【bzoj3939】[Usaco2015 Feb]Cow Hopscotch 动态开点线段树优化dp

题目描述 Just like humans enjoy playing the game of Hopscotch, Farmer John's cows have invented a variant of the game for themselves to play. Being played by clumsy animals weighing nearly a ton, Cow Hopscotch almost always ends in disaster, but this has