Stars in the window ACWing 248. | + lazy scan lines mark

Portal

Title Description

There are many stars (viewed as plane rectangular coordinate system), the known coordinates and luminance of each star (are integers) in a sky.

Seeking a width W, height H of the rectangular window (W, H is a positive integer) capable of trapping the sum of the luminance is the largest number of stars. (Stars on the rectangular boundary is not)

Input Format

Enter multiple sets of test cases.

Each embodiment comprises a first line with three integers: n, W, H, represents the number of stars, the width and height of the rectangular window.

Then n lines, each line has three integers: x, y, c, represent each position of the stars (x, y) and luminance.

No two stars on the same point.

Output Format

Each test case output a sum of the maximum luminance.

Each result row.

data range

1≤n≤10000,
1≤W,H≤1000000,
10≤x,y<231

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

Problem solution: https://www.cnblogs.com/l999q/p/11366840.html can refer to this blog, except that it is to maintain and brightness.

Code:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 2e4 + 10;
struct node{
    int x,y1,y2,val;
    node() {}
    node(int x, int y1, int y2,int val){
        this->x = x; this->val = val;
        this->y1 = y1; this->y2 = y2;
    }
    bool operator <(const node &t)const {
        return x<t.x;
    }
};
struct Tree{
    int l,r;
    int val,lazy;
}t[N<<2];
vector<node> a;
vector<int> v;
void build(int rt,int l,int r) {
    t[rt].l = l,t[rt].r = r;
    t[rt].val = 0; t[rt].lazy = 0;
    if (l == r) return;
    int mid = (l+r)>>1;
    build(rt<<1,l,mid);
    build(rt<<1|1,mid+1,r);
}
void update(int rt,int l,int r,int val) {
    if (l <= t[rt].l && r >= t[rt].r) {
        t[rt].val += val;
        t[rt].lazy += val;
        return;
    }
    if (t[rt].lazy) {
        t[rt<<1].val += t[rt].lazy;
        t[rt<<1].lazy += t[rt].lazy;
        t[rt<<1|1].val += t[rt].lazy;
        t[rt<<1|1].lazy += t[rt].lazy;
        t[rt].lazy = 0;
    }
    int mid = (t[rt].l+t[rt].r)>>1;
    if (l <= mid) update(rt<<1,l,r,val);
    if (r > mid) update(rt<<1|1,l,r,val);
    t[rt].val = max(t[rt<<1].val,t[rt<<1|1].val);
}
int main() {
    int n,w,h;
    while (~scanf("%d%d%d",&n,&w,&h) ) {
        v.clear();a.clear();
        for(int i = 0; i < n; i++) {
            int x,y,c;
            scanf("%d%d%d",&x,&y,&c);
            v.push_back(y);
            v.push_back(y+h-1);
            a.push_back(node(x,y,y+h-1,c));
            a.push_back (Node (X + W, Y, Y + H- . 1 , -C)); // Y coordinate of the border to be reduced, x coordinate not Save 
        } 
        v.push_back ( - . 1 ); 
        Sort (a.begin ( ), a.end ()); 
        Sort (v.begin (), v.end ()); 
        v.erase (UNIQUE (v.begin (), v.end ()), v.end ()); 
        n- = v.size (); 
        Build ( . 1 , 0 , n-);
         int ANS = 0 ;
         for ( int I = 0 ; I <n-; I ++ ) {
             int L = lower_bound (v.begin (), v.end (), a [i] .y1 ) - v.begin();
            int r = lower_bound(v.begin(),v.end(),a[i].y2) - v.begin()-1;
            update(1,l,r,a[i].val);
            if (a[i].val>0) ans = max(ans,t[1].val);
        }
        printf("%lld\n",ans);
    }
    return 0;
}
View Code

 

Guess you like

Origin www.cnblogs.com/l999q/p/11366867.html