【POJ】2482 Stars in Your Window

 1 #include<cstdio>

 2 #include<cstring>

 3 #include<algorithm>

 4 #define MAXN 20010

 5 typedef __int64 LL;

 6 using namespace std;

 7 struct Seg

 8 {

 9     LL left,right,val,high;

10     friend bool operator<(Seg a,Seg b)

11     {

12         if(a.high!=b.high)

13             return a.high<b.high;

14         return a.val<b.val;

15     }

16 };

17 struct node

18 {

19     LL lazy,big;

20 };

21 Seg p[MAXN];

22 node tree[MAXN<<2];

23 LL X[MAXN];

24 inline void PushDown(int rt)

25 {

26     if(tree[rt].lazy)

27     {

28         tree[rt<<1].lazy+=tree[rt].lazy;

29         tree[rt<<1|1].lazy+=tree[rt].lazy;

30         tree[rt<<1].big+=tree[rt].lazy;

31         tree[rt<<1|1].big+=tree[rt].lazy;

32         tree[rt].lazy=0;

33     }

34 }

35 inline void PushUp(int rt)

36 {

37     tree[rt].big=max(tree[rt<<1].big,tree[rt<<1|1].big);

38 }

39 void Update(int x,int y,int val,int L,int R,int rt)

40 {

41     if(x<=L&&R<=y)

42     {

43         tree[rt].big+=val;

44         tree[rt].lazy+=val;

45     }

46     else

47     {

48         int mid=(L+R)>>1;

49         PushDown(rt);

50         if(x<=mid)

51             Update(x,y,val,L,mid,rt<<1);

52         if(y>mid)

53             Update(x,y,val,mid+1,R,rt<<1|1);

54         PushUp(rt);

55     }

56 }

57 int main()

58 {

59     int i,n,cnt;

60     LL w,h,x,y,c,ans;

61     while(~scanf("%d%I64d%I64d",&n,&w,&h))

62     {

63         for(i=cnt=0;i<n;i++)

64         {

65             scanf("%I64d%I64d%I64d",&x,&y,&c);

66             p[cnt].left=x;

67             p[cnt].right=x+w;

68             p[cnt].high=y;

69             p[cnt].val=c;

70             X[cnt++]=x;

71             p[cnt].left=x;

72             p[cnt].right=x+w;

73             p[cnt].high=y+h;

74             p[cnt].val=-c;

75             X[cnt++]=x+w;

76         }

77         sort(X,X+cnt);

78         sort(p,p+cnt);

79         n=unique(X,X+cnt)-X;

80         memset(tree,0,sizeof(tree));

81         for(ans=i=0;i<cnt;i++)

82         {

83             x=lower_bound(X,X+n,p[i].left)-X;

84             y=lower_bound(X,X+n,p[i].right)-X;

85             Update(x,y-1,p[i].val,0,n-2,1);

86             ans=max(ans,tree[1].big);

87         }

88         printf("%I64d\n",ans);

89     }

90     return 0;

91 }

你可能感兴趣的:(window)