Stars in Your Window

Stars in Your Window

The meaning of problems: there in a plane N stars, each star in a luminance value, a rectangle with W * H to go around the stars, (not edge) seeking the maximum luminance value can be obtained.

Ideas: like a long time never understood only look at solving other people's reports. . . .
As long as the original clock, and converts it to be able to find the maximum value of each range can affect the star segment interval [(x, y), ( x + w-1, y + h-1)] and having a weight they could be coincidence, says this rectangle together, that is, as long as the overlapping rectangles obtained the maximum weight on the line.

In ascending order of x, y values of discrete projected onto the y-axis, then for each star ordinate, y, y + h-1 is that each star can affect rectangle then x, x + w- 1 is an event into the event and one out, the value carried by the opposite of each other. node [1] .sum save the current maximum value when all rectangles traversed again takes the maximum value thereof is ans

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=100010;
struct node{
    ll x,y1,y2,val;
    bool operator<(const node &b)const {
        if (x!=b.x) return x<b.x;else return val>b.val;
    }
}a[maxn*2];
vector<int>v;

struct node1{
    ll l,r,sum,laz;
}tree[maxn<<2];

void build(ll rt,ll l,ll r) {
    tree[rt].l = l;
    tree[rt].r = r;
    tree[rt].sum = tree[rt].laz = 0;
    if (l == r) return;
    ll mid = (l + r) >> 1;
    build(rt << 1, l, mid);
    build(rt << 1 | 1, mid + 1, r);
}

void push(int rt) {
    tree[rt << 1].sum += tree[rt].laz;
    tree[rt << 1 | 1].sum += tree[rt].laz;
    tree[rt << 1].laz += tree[rt].laz;
    tree[rt << 1 | 1].laz += tree[rt].laz;
    tree[rt].laz = 0;
}
void update(ll rt,ll l,ll r,ll k) {
    if (l == tree[rt].l && tree[rt].r == r) {
        tree[rt].laz += k;
        tree[rt].sum += k;
        return;
    }
    if (tree[rt].l == tree[rt].r) return;
    if (tree[rt].laz) push(rt);
    ll mid = (tree[rt].l + tree[rt].r) >> 1;
    if (r <= mid) update(rt << 1, l, r, k);
    else if (l > mid) update(rt << 1 | 1, l, r, k);
    else {
        update(rt << 1, l, mid, k);
        update(rt << 1 | 1, mid + 1, r, k);
    }
    tree[rt].sum = max(tree[rt << 1].sum, tree[rt << 1 | 1].sum);
}
int main() {
    ll n, w, h;
    while (~scanf("%lld%lld%lld", &n, &w, &h)) {
        v.clear();
        for (int i = 0; i < n; i++) {
            scanf("%lld%lld%lld", &a[i].x, &a[i].y1, &a[i].val);
            v.push_back(a[i].y1);
            v.push_back(a[i].y1+h-1);
            a[i].y2 = a[i].y1 + h - 1;
            a[i + n] = a[i];
            a[i + n].x = a[i].x + w - 1;
            a[i + n].val = -a[i].val;
        }
        sort(v.begin(),v.end());
        sort (a, a + n * 2);
        ll cnt=unique(v.begin(),v.end())-v.begin();
        v.erase(unique(v.begin(),v.end()),v.end());
        build(1, 1, cnt);
        ll ans = 0;
        for (int i = 0; i < n * 2; i++) {
            int l=lower_bound(v.begin(),v.end(),a[i].y1)-v.begin()+1;
            int r=lower_bound(v.begin(),v.end(),a[i].y2)-v.begin()+1;
            update(1, l, r, a[i].val);
            ans = max(ans, tree[1].sum);
        }
        printf("%lld\n", ans);
    }
}

  

Guess you like

Origin www.cnblogs.com/Accpted/p/11421877.html