zoukankan      html  css  js  c++  java
  • BZOJ4237 : 稻草人

    考虑按x坐标排序后分治,只需考虑计算左下角在[l,mid],右上角在[mid+1,r]的矩形数。

    对于[l,mid]的点,从右往左考虑,求出它可以贡献到的纵坐标区间。

    对于[mid+1,r]的点,从左往右考虑,求出它可以接受的纵坐标区间。

    然后扫描线+Treap维护即可,时间复杂度$O(nlog^2n)$。

    #include<cstdio>
    #include<cstdlib>
    #include<algorithm>
    #define N 100010
    using std::sort;
    int n;long long ans;
    struct P{int x,y;P(){}P(int _x,int _y){x=_x,y=_y;}}a[N<<1],b[N],c[N];
    inline bool cmp(const P&a,const P&b){return a.x<b.x;}
    inline bool cmp2(const P&a,const P&b){return a.y>b.y;}
    inline void read(int&a){char c;while(!(((c=getchar())>='0')&&(c<='9')));a=c-'0';while(((c=getchar())>='0')&&(c<='9'))(a*=10)+=c-'0';}
    struct node{
      int val,sum,p;node*l,*r;
      node(){val=sum=p=0;l=r=NULL;}
      inline void up(){sum=l->sum+r->sum+1;}
    }*blank=new(node),*T,pool[N*3],*cur;
    inline void Rotatel(node*&x){node*y=x->r;x->r=y->l;x->up();y->l=x;y->up();x=y;}
    inline void Rotater(node*&x){node*y=x->l;x->l=y->r;x->up();y->r=x;y->up();x=y;}
    void Ins(node*&x,int y){
      if(x==blank){x=cur++;x->val=y;x->l=x->r=blank;x->sum=1;x->p=std::rand();return;}
      x->sum++;
      if(y<x->val){
        Ins(x->l,y);
        if(x->l->p>x->p)Rotater(x);
      }else{
        Ins(x->r,y);
        if(x->r->p>x->p)Rotatel(x);
      }
    }
    inline int Pre(node*x,int y){
      int t=-1;
      while(x!=blank)if(y<x->val)x=x->l;else t=x->val,x=x->r;
      return t+1;
    }
    inline int Nxt(node*x,int y){
      int t=1000000001;
      while(x!=blank)if(y<x->val)t=x->val,x=x->l;else x=x->r;
      return t-1;
    }
    inline int Ask(node*x,int y){
      int t=0;
      while(x!=blank)if(y<x->val)x=x->l;else t+=x->l->sum+1,x=x->r;
      return t;
    }
    void solve(int l,int r){
      if(l==r)return;
      int mid=(l+r)>>1,i,j,m1=0,m2=0;
      solve(l,mid),solve(mid+1,r);
      for(cur=pool,T=blank,i=mid;i>=l;i--)b[m1++]=P(a[i].y,Nxt(T,a[i].y)),Ins(T,a[i].y);
      for(T=blank,i=mid+1;i<=r;i++)c[m2++]=P(Pre(T,a[i].y),a[i].y),Ins(T,a[i].y);
      sort(b,b+m1,cmp2),sort(c,c+m2,cmp2);
      for(T=blank,i=j=0;i<m2;i++){
        while(j<m1&&b[j].y>=c[i].y)Ins(T,b[j++].x);
        ans+=Ask(T,c[i].y)-Ask(T,c[i].x-1);
      }
    }
    int main(){
      blank->l=blank->r=blank;
      read(n);
      for(int i=1;i<=n;i++)read(a[i].x),read(a[i].y);
      sort(a+1,a+n+1,cmp);
      solve(1,n);
      return printf("%lld",ans),0;
    }
    

      

  • 相关阅读:
    SVN报错working copy is not uptodate
    AndroidStudio中获得的VersionCode一直为1和VersionName一直为1.0
    OkHttp
    MockWebServer使用指南(转载)
    Android的Toolbar(含溢出菜单设置[弹出菜单的使用])的使用PopMenu的样式
    8-13笔记-安卓兼容
    自定义Dialog
    安卓圆角Button XML文件
    递归方法扫面文件夹(JAVA控制台程序)
    8月12笔记-安卓文件扫描
  • 原文地址:https://www.cnblogs.com/clrs97/p/4738228.html
Copyright © 2011-2022 走看看