【题目链接】
http://poj.org/problem?id=2482
【算法】
线段树 + 扫描线
【代码】
#include <algorithm> #include <bitset> #include <cctype> #include <cerrno> #include <clocale> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <exception> #include <fstream> #include <functional> #include <limits> #include <list> #include <map> #include <iomanip> #include <ios> #include <iosfwd> #include <iostream> #include <istream> #include <ostream> #include <queue> #include <set> #include <sstream> #include <stdexcept> #include <streambuf> #include <string> #include <utility> #include <vector> #include <cwchar> #include <cwctype> #include <stack> #include <limits.h> using namespace std; #define MAXN 10010 typedef long long ll; struct info { ll x,l,r,v; } a[MAXN<<1]; ll i,n,w,h,len,lth,ans,l,r; ll x[MAXN],y[MAXN],c[MAXN],val[MAXN<<1]; struct SegmentTree { struct Node { ll l,r; ll mx,tag; } Tree[MAXN<<4]; inline void build(ll index,ll l,ll r) { ll mid; Tree[index].l = l; Tree[index].r = r; Tree[index].mx = 0; if (l == r) return; mid = (Tree[index].l + Tree[index].r) >> 1; build(index<<1,l,mid); build(index<<1|1,mid+1,r); } inline void update(ll index) { Tree[index].mx = max(Tree[index<<1].mx,Tree[index<<1|1].mx); } inline void pushdown(ll index) { if (Tree[index].tag) { Tree[index<<1].mx += Tree[index].tag; Tree[index<<1|1].mx += Tree[index].tag; Tree[index<<1].tag += Tree[index].tag; Tree[index<<1|1].tag += Tree[index].tag; Tree[index].tag = 0; } } inline void add(ll index,ll l,ll r,ll val) { ll mid; if (l > r) return; if (Tree[index].l == l && Tree[index].r == r) { Tree[index].mx += val; Tree[index].tag += val; } else { pushdown(index); mid = (Tree[index].l + Tree[index].r) >> 1; if (mid >= r) add(index<<1,l,r,val); else if (mid + 1 <= l) add(index<<1|1,l,r,val); else { add(index<<1,l,mid,val); add(index<<1|1,mid+1,r,val); } update(index); } } inline ll get() { return Tree[1].mx; } } T; inline bool cmp(info a,info b) { return (a.x != b.x) ? (a.x < b.x) : (a.v < b.v); } int main() { while (scanf("%lld%lld%lld",&n,&w,&h) != EOF) { len = lth = ans = 0; for (i = 1; i <= n; i++) { scanf("%lld%lld%lld",&x[i],&y[i],&c[i]); a[++len] = (info){x[i],y[i],y[i]+h,c[i]}; a[++len] = (info){x[i]+w,y[i],y[i]+h,-c[i]}; val[++lth] = y[i]; val[++lth] = y[i] + h; } sort(val+1,val+lth+1); lth = unique(val+1,val+lth+1) - val - 1; sort(a+1,a+len+1,cmp); T.build(1,1,lth); for (i = 1; i <= len; i++) { l = lower_bound(val+1,val+lth+1,a[i].l) - val; r = lower_bound(val+1,val+lth+1,a[i].r) - val - 1; T.add(1,l,r,a[i].v); ans = max(ans,T.get()); } printf("%lld ",ans); } return 0; }