poj 2482 Stars in Your Window 线段树扫描线

Stars in Your Window

Time Limit: 1 Sec  Memory Limit: 256 MB

题目连接

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个点,每个点都有一个权值,然后你用这个矩形去圈住他们,然后问你矩形所能圈住的最大权值是多少

题解:

 呀,把所有点都转化成矩形,然后就可以把这道题变成,找一个点,使的这个点所在的矩形的权值最大

然后就可以用扫描线搞一搞就好了

代码:

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 50000
#define mod 10007
#define eps 1e-9
//const int inf=0x7fffffff;   //无限大
const int inf=0x3f3f3f3f;
/*

int buf[10];
inline void write(int i) {
  int p = 0;if(i == 0) p++;
  else while(i) {buf[p++] = i % 10;i /= 10;}
  for(int j = p-1; j >=0; j--) putchar('0' + buf[j]);
  printf("
");
}
*/
//**************************************************************************************
inline ll read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
#define LL(x) (x<<1)
#define RR(x) (x<<1|1)
#define MID(a,b) (a+((b-a)>>1))
const int N=2000005;
typedef long long LL;

struct Line
{
    LL x,y1,y2,valu;
    Line(){}
    Line(LL a,LL b,LL c,LL d)
    { x=a;y1=b;y2=c;valu=d; }
    bool operator<(const Line&b)const
    {
        return x<b.x||(x==b.x&&valu<b.valu);
    }
};
struct node
{
    int lft,rht;
    int flag,mx;
    int mid(){return MID(lft,rht);}
    void fun(int valu)
    {
        mx+=valu;flag+=valu;
    }
};

vector<LL> y;
vector<Line> line;
map<LL,int> H;

struct Segtree
{
    node tree[N*4];
    void build(int lft,int rht,int ind)
    {
        tree[ind].lft=lft;    tree[ind].rht=rht;
        tree[ind].mx=0;        tree[ind].flag=0;
        if(lft+1!=rht)
        {
            int mid=tree[ind].mid();
            build(lft,mid,LL(ind));
            build(mid,rht,RR(ind));
        }
    }
    void updata(int st,int ed,int ind,int valu)
    {
        int lft=tree[ind].lft,rht=tree[ind].rht;
        if(st<=lft&&rht<=ed) tree[ind].fun(valu);
        else 
        {
            if(tree[ind].flag)
            {
                tree[LL(ind)].fun(tree[ind].flag);
                tree[RR(ind)].fun(tree[ind].flag);
                tree[ind].flag=0;
            }
            int mid=tree[ind].mid();
            if(st<mid) updata(st,ed,LL(ind),valu);
            if(ed>mid) updata(st,ed,RR(ind),valu);
            tree[ind].mx=max(tree[LL(ind)].mx,tree[RR(ind)].mx);
        }
    }
}seg;
int main()
{
    int n,w,h;
    while(scanf("%d%d%d",&n,&w,&h)!=EOF)
    {
        y.clear(); H.clear(); line.clear();

        for(int i=0;i<n;i++)
        {
            LL a,b,c;
            scanf("%lld%lld%lld",&a,&b,&c);
            line.push_back(Line(a,b,b+h,c));
            line.push_back(Line(a+w,b,b+h,-c));
            y.push_back(b); y.push_back(b+h); 
        }
        sort(line.begin(),line.end());
        sort(y.begin(),y.end());
        y.erase(unique(y.begin(),y.end()),y.end());
        
        for(int i=0;i<(int)y.size();i++) H[y[i]]=i;

        int mx=0;
        seg.build(0,(int)y.size()-1,1);
        for(int i=0;i<(int)line.size();i++)
        {
            seg.updata(H[line[i].y1],H[line[i].y2],1,line[i].valu);
            mx=max(mx,seg.tree[1].mx);
        }
        printf("%d
",mx);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/qscqesze/p/4433923.html