hdu1156两颗线段树 点修改求和(数组存放扫描线)

  1 #include<stdio.h>
  2 #include<string.h>
  3 #include<algorithm>
  4 using namespace std;
  5 struct dian
  6 {
  7     int x,y;
  8 }s[200005];
  9 int leftnum[500005],rightnum[500005],p,v,ql,qr,yy[500005],qq[500005],g[500005];
 10 int cmpy(const dian a,const dian b)
 11 {
 12     return a.y<b.y;
 13 }
 14 int cmpx(const dian a,const dian b)
 15 {
 16     return a.x<b.x;
 17 }
 18 void update(int o,int l,int r,int f[])
 19 {
 20     int mid=l+(r-l)/2;
 21     if (l==r) f[o]+=v;
 22     else{
 23         if (p<=yy[mid]) update(o*2,l,mid,f);
 24         else update(o*2+1,mid+1,r,f);
 25         f[o]=f[o*2]+f[o*2+1];
 26     }
 27 }
 28 int query(int o,int l,int r,int f[])
 29 {
 30     int mid=l+(r-l)/2,ans=0;
 31     if (ql<=yy[l]&&yy[r]<=qr) return f[o];
 32     if (qr<yy[l]||ql>yy[r]) return 0;
 33     if (ql<=yy[mid]) ans+=query(o*2,l,mid,f);
 34     if (yy[mid]<qr) ans+=query(o*2+1,mid+1,r,f);
 35     return ans;
 36 }
 37 int main()
 38 {
 39     int n,i,j,sum,maxnum,cnt,pp,temp,temp2;
 40     while (~scanf("%d",&n)&&n)
 41     {
 42         memset(rightnum,0,sizeof(rightnum));
 43         memset(leftnum,0,sizeof(leftnum));
 44         for (i=1;i<=n;i++) scanf("%d%d",&s[i].x,&s[i].y);
 45         sort(s+1,s+n+1,cmpy);
 46         sum=1; yy[1]=s[1].y;
 47         for (i=2;i<=n;i++)
 48             if (s[i].y!=s[i-1].y)
 49             {
 50                 sum++;
 51                 yy[sum]=s[i].y;
 52             }
 53         sort(s+1,s+n+1,cmpx);
 54         for (i=1;i<=n;i++)
 55         {
 56             p=s[i].y; v=1;
 57             update(1,1,sum,rightnum);
 58         }
 59         maxnum=-1; cnt=0;
 60         pp=1; qq[1]=1; p=s[1].y; v=-1;
 61         update(1,1,sum,rightnum);
 62         for (i=2;i<=n+1;i++)
 63         {
 64             if (s[i].x==s[i-1].x) {
 65                 p=s[i].y; v=-1;
 66                 update(1,1,sum,rightnum);
 67                 pp++; qq[pp]=i;
 68             }
 69             if (s[i].x!=s[i-1].x||i==n+1)
 70             {
 71                 int temp01=-1,temp21=-1;
 72                 for (j=1;j<=pp;j++)
 73                 {
 74                     temp=0;
 75                     ql=yy[1]; qr=s[qq[j]].y-1; if (ql<=qr) temp+=query(1,1,sum,leftnum);
 76                     ql=s[qq[j]].y+1; qr=yy[sum]; if (ql<=qr) temp+=query(1,1,sum,rightnum);
 77                     temp2=0;
 78                     ql=yy[1]; qr=s[qq[j]].y-1; if (ql<=qr) temp2+=query(1,1,sum,rightnum);
 79                     ql=s[qq[j]].y+1; qr=yy[sum]; if (ql<=qr) temp2+=query(1,1,sum,leftnum);
 80                     if (temp2>temp21) { temp21=temp2; temp01=temp; }
 81                     else if (temp2==temp21&&temp<temp01) temp01=temp;
 82                 }
 83                 if (temp01==maxnum) {cnt++; g[cnt]=temp21;  }
 84                 else if (temp01>maxnum){maxnum=temp01; cnt=1; g[1]=temp21; }
 85                 for (j=1;j<=pp;j++)
 86                 {
 87                     p=s[qq[j]].y; v=1;
 88                     update(1,1,sum,leftnum);
 89                 }
 90                 p=s[i].y; v=-1;
 91                 update(1,1,sum,rightnum);
 92                 pp=1; qq[1]=i;
 93             }
 94         }
 95         sort(g+1,g+cnt+1);
 96         printf("Stan: %d; Ollie:",maxnum);
 97         printf(" %d",g[1]);
 98         for (i=2;i<=cnt;i++)
 99          if(g[i]!=g[i-1]) printf(" %d",g[i]); printf(";\n");
100     }
101 }

http://acm.hdu.edu.cn/showproblem.php?pid=1156

posted on 2014-07-28 03:04  xiao_xin  阅读(133)  评论(0编辑  收藏  举报

导航