1. 程式人生 > >[51nod 1208] Stars in Your Window(線段樹,掃描線)

[51nod 1208] Stars in Your Window(線段樹,掃描線)

51nod clas html 題目 val col while cto pro

題目鏈接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1208

題意:也是矩形框點問題,不過每個點有權值,希望權值最大。

直接把出入的event中的sign變成對應權值,更新到線段樹上就行了。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 #define lrt rt << 1
 5 #define rrt rt << 1 | 1
 6 typedef struct Seg {
 7     int add, val;
8 }Seg; 9 typedef struct Event { 10 int l, r, h, val; 11 }Event; 12 const int maxn = 100500; 13 Seg seg[maxn<<4]; 14 vector<Event> event; 15 int hh[maxn<<1], hcnt; 16 int n, w, h; 17 18 bool cmp(Event a, Event b) { 19 if(a.h != b.h) return a.h < b.h; 20 return a.val > b.val;
21 } 22 23 void build(int l, int r, int rt) { 24 seg[rt].val = seg[rt].add = 0; 25 if(l == r) return; 26 int mid = (l + r) >> 1; 27 build(l, mid, lrt); 28 build(mid+1, r, rrt); 29 } 30 31 32 void pushup(int rt) { 33 seg[rt].val = max(seg[lrt].val, seg[rrt].val); 34 } 35
36 void pushdown(int rt) { 37 if(seg[rt].add) { 38 seg[lrt].add += seg[rt].add; 39 seg[rrt].add += seg[rt].add; 40 seg[lrt].val += seg[rt].add; 41 seg[rrt].val += seg[rt].add; 42 seg[rt].add = 0; 43 } 44 } 45 46 void update(int L, int R, int val, int l, int r, int rt) { 47 if(L <= l && r <= R) { 48 seg[rt].add += val; 49 seg[rt].val += val; 50 return; 51 } 52 pushdown(rt); 53 int mid = (l + r) >> 1; 54 if(L <= mid) update(L, R, val, l, mid, lrt); 55 if(mid < R) update(L, R, val, mid+1, r, rrt); 56 pushup(rt); 57 } 58 59 int id(int x) { 60 return lower_bound(hh, hh+hcnt, x) - hh + 1; 61 } 62 63 int main() { 64 // freopen("in", "r", stdin); 65 int x, y, v; 66 while(~scanf("%d%d%d",&n,&w,&h)) { 67 event.clear(); hcnt = 0; 68 for(int i = 0; i < n; i++) { 69 scanf("%d%d%d",&x,&y,&v); 70 event.push_back(Event{x, x+w, y, v}); 71 event.push_back(Event{x, x+w, y+h, -v}); 72 hh[hcnt++] = x; hh[hcnt++] = x + w; 73 } 74 sort(event.begin(), event.end(), cmp); 75 sort(hh, hh+hcnt); hcnt = unique(hh, hh+hcnt) - hh; 76 build(1, hcnt, 1); 77 int ret = 0; 78 for(int i = 0; i < event.size(); i++) { 79 int l = id(event[i].l); 80 int r = id(event[i].r); 81 int val = event[i].val; 82 update(l, r, val, 1, hcnt, 1); 83 ret = max(ret, seg[1].val); 84 } 85 printf("%d\n", ret); 86 } 87 return 0; 88 }

[51nod 1208] Stars in Your Window(線段樹,掃描線)