zoukankan      html  css  js  c++  java
  • Stars in Your Window(线段树求最大矩形交)

    题目连接

    http://poj.org/problem?id=2482

    Description

    Fleeting time does not blur my memory of you. Can it really be 4 years since I first saw you? I still remember, vividly, on the beautiful Zhuhai Campus, 4 years ago, from the moment I saw you smile, as you were walking out of the classroom and turned your head back, with the soft sunset glow shining on your rosy cheek, I knew, I knew that I was already drunk on you. Then, after several months’ observation and prying, your grace and your wisdom, your attitude to life and your aspiration for future were all strongly impressed on my memory. You were the glamorous and sunny girl whom I always dream of to share the rest of my life with. Alas, actually you were far beyond my wildest dreams and I had no idea about how to bridge that gulf between you and me. So I schemed nothing but to wait, to wait for an appropriate opportunity. Till now — the arrival of graduation, I realize I am such an idiot that one should create the opportunity and seize it instead of just waiting.

    These days, having parted with friends, roommates and classmates one after another, I still cannot believe the fact that after waving hands, these familiar faces will soon vanish from our life and become no more than a memory. I will move out from school tomorrow. And you are planning to fly far far away, to pursue your future and fulfill your dreams. Perhaps we will not meet each other any more if without fate and luck. So tonight, I was wandering around your dormitory building hoping to meet you there by chance. But contradictorily, your appearance must quicken my heartbeat and my clumsy tongue might be not able to belch out a word. I cannot remember how many times I have passed your dormitory building both in Zhuhai and Guangzhou, and each time aspired to see you appear in the balcony or your silhouette that cast on the window. I cannot remember how many times this idea comes to my mind: call her out to have dinner or at least a conversation. But each time, thinking of your excellence and my commonness, the predominance of timidity over courage drove me leave silently.

    Graduation, means the end of life in university, the end of these glorious, romantic years. Your lovely smile which is my original incentive to work hard and this unrequited love will be both sealed as a memory in the deep of my heart and my mind. Graduation, also means a start of new life, a footprint on the way to bright prospect. I truly hope you will be happy everyday abroad and everything goes well. Meanwhile, I will try to get out from puerility and become more sophisticated. To pursue my own love and happiness here in reality will be my ideal I never desert.

    Farewell, my princess!

    If someday, somewhere, we have a chance to gather, even as gray-haired man and woman, at that time, I hope we can be good friends to share this memory proudly to relight the youthful and joyful emotions. If this chance never comes, I wish I were the stars in the sky and twinkling in your window, to bless you far away, as friends, to accompany you every night, sharing the sweet dreams or going through the nightmares together.

    Here comes the problem: Assume the sky is a flat plane. All the stars lie on it with a location (x, y). for each star, there is a grade ranging from 1 to 100, representing its brightness, where 100 is the brightest and 1 is the weakest. The window is a rectangle whose edges are parallel to the x-axis or y-axis. Your task is to tell where I should put the window in order to maximize the sum of the brightness of the stars within the window. Note, the stars which are right on the edge of the window does not count. The window can be translated but rotation is not allowed.

    Input

    There are several test cases in the input. The first line of each case contains 3 integers: n, W, H, indicating the number of stars, the horizontal length and the vertical height of the rectangle-shaped window. Then n lines follow, with 3 integers each: x, y, c, telling the location (x, y) and the brightness of each star. No two stars are on the same point. There are at least 1 and at most 10000 stars in the sky. 1<=W,H<=1000000, 0<=x,y<2^31.

    Output

    For each test case, output the maximum brightness in a single line.

    Sample Input

    3 5 4
    1 2 3
    2 3 2
    6 3 1
    3 5 4
    1 2 3
    2 3 2
    5 3 1

    Sample Output

    5
    6

    HINT

    将点转化成矩形。

    题意

    给你一个w*h的矩形,和N个点,每个点都有一个权值,然后你用这个矩形去圈住他们,然后问你矩形所能圈住的最大权值是多少?

    记得是多测哦。

    题解:

    我们可以反过来想有每个点(x,y)可以被哪些举行框住呢?(建议先想一下再往后看)
    显然右上角在(x,y)到(x+W,y+H)到的矩形都可以框住(x,y),所以对于每个点(x,y),我们将(x,y)到(x+W,y+H)的区域加上相应的权值。
    这样题目就转化成了求N个矩形中,权值最大的点。
    之后直接上扫描线,类似求矩形的并集,唯一不同是对于每一条扫描线我们记录的是最大值。
     
    最后记得开long long 但是不要像我蒟蒻一样开全局long long

    代码:

    //#include<bits/stdc++.h>
    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    #define N 100050
    #define LL long long 
    LL cnt,tot,n,W,H,kth[N<<2];
    struct Query
    {
      LL l,r,h,id;
      bool operator <(const Query &b)const
      {return h==b.h?id<b.id:h<b.h;}
    }que[N<<1];
    struct Tree{LL l,r,j,max;}tr[N<<2];
    template<typename T>void read(T&x)
    {
      LL k=0;char c=getchar();
      x=0;
      while(!isdigit(c)&&c!=EOF)k^=c=='-',c=getchar();
      if(c==EOF)exit(0);
      while(isdigit(c))x=x*10+c-'0',c=getchar();
      x=k?-x:x;
    }
    void push_up(LL x)
    {
      tr[x].max=tr[x].j;
      if (tr[x].r-tr[x].l>1)
        tr[x].max+=max(tr[x<<1].max,tr[x<<1|1].max);
    }
    void bt(LL x,LL l,LL r)
    {
      tr[x]=Tree{l,r,0,0};
      if(r-l==1)return;
      LL mid=(l+r)>>1;
      bt(x<<1,l,mid);
      bt(x<<1|1,mid,r);
    }
    void update(LL x,LL l,LL r,LL tt)
    {
      if (l<=tr[x].l&&tr[x].r<=r)
        {
          tr[x].j+=tt;
          push_up(x);
          return;
        }
      LL mid=(tr[x].l+tr[x].r)>>1;
      if (l<mid)update(x<<1,l,r,tt);
      if (mid<r)update(x<<1|1,l,r,tt);
      push_up(x);
    }
    void input()
    {
      read(n); read(W); read(H);
      LL x1,y1,x2,y2,tt;
      for(LL i=1;i<=n;i++)
        {
          read(x1); read(y1); read(tt);
          x2=x1+W; y2=y1+H;
          que[++tot]=Query{x1,x2,y1,tt};
          que[++tot]=Query{x1,x2,y2,-tt};
          kth[++cnt]=x1;
          kth[++cnt]=x2;
          kth[++cnt]=y1;
          kth[++cnt]=y2;
        }
    }
    void work()
    {
      sort(que+1,que+tot+1);
      sort(kth+1,kth+cnt+1);
      cnt=unique(kth+1,kth+cnt+1)-kth-1;
      bt(1,1,cnt);
      LL l,r,ans=0;
      for(LL i=1;i<=tot-1;i++)
        {
          l=lower_bound(kth+1,kth+cnt+1,que[i].l)-kth;
          r=lower_bound(kth+1,kth+cnt+1,que[i].r)-kth;
          update(1,l,r,que[i].id);
          ans=max(ans,tr[1].max);
        }
      printf("%lld
    ",ans);
    }
    void clear(){cnt=0;tot=0;}
    int main()
    {
      #ifndef ONLINE_JUDGE
      freopen("aa.in","r",stdin);
      #endif
      while(1)
        {
          clear();
          input();
          work();
        }
    }
    View Code
  • 相关阅读:
    区间dp体会
    P1083借教室 noip提高组复赛2012
    P2678跳石头体会 noip2015提高组
    tarjan求LCA的体会
    P1006 传纸条
    P1140 相似基因 详解
    UVA1025 城市里的间谍 A Spy in the Metro 题解
    DAG上的动规嵌套矩形问题(含思考题)
    洛谷P1030c++递归求解先序排列
    Test 2019.7.22
  • 原文地址:https://www.cnblogs.com/mmmqqdd/p/10726237.html
Copyright © 2011-2022 走看看