http://poj.org/problem?id=2482

线段树扫描线

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 20003;
int in() {
int k = 0, fh = 1; char c = getchar();
for(; c < '0' || c > '9'; c = getchar())
if (c == '-') fh = -1;
for(; c >= '0' && c <= '9'; c = getchar())
k = (k << 3) + (k << 1) + c - '0';
return k * fh;
} namespace SegmentTree {
int ma[N << 2], lazy[N << 2], L, R, c, top;
void init(int num) {
top = num;
}
void pushdown(int rt, int l, int r) {
if (lazy[rt]) {
lazy[rt << 1] += lazy[rt];
lazy[rt << 1 | 1] += lazy[rt];
ma[rt << 1] += lazy[rt];
ma[rt << 1 | 1] += lazy[rt];
lazy[rt] = 0;
}
}
void pushup(int rt) {
ma[rt] = max(ma[rt << 1], ma[rt << 1 | 1]);
}
void update(int rt, int l, int r) {
if (L <= l && r <= R) {
ma[rt] += c;
lazy[rt] += c; return;
}
int mid = (l + r) >> 1;
pushdown(rt, l, r);
if (L <= mid) update(rt << 1, l, mid);
if (R > mid) update(rt << 1 | 1, mid + 1 ,r);
pushup(rt);
}
void cover(int l, int r, int num) {
L = l; R = r; c = num;
update(1, 1, top);
}
int query() {
return ma[1];
}
} int n, w, h, H[10003], dis[10003], cnt, down[10003];
struct node {
int x, y, z;
bool operator < (const node &A) const {
return x < A.x;
}
} S[10003]; int main() {
while (~scanf("%d%d%d", &n, &w, &h)) {
cnt = 0;
for(int i = 1; i <= n; ++i) {
S[i].x = in(); S[i].y = in(); S[i].z = in();
H[++cnt] = S[i].y;
}
sort(H + 1, H + cnt + 1);
cnt = unique(H + 1, H + cnt + 1) - H;
for(int i = 1; i <= n; ++i)
S[i].y = lower_bound(H + 1, H + cnt, S[i].y) - H;
sort(S + 1, S + n + 1);
int head = cnt - 1, tail = 1;
for(int i = cnt - 1; i >= 1; --i) {
while (head >= 1 && H[i] - H[head] < h) --head;
down[i] = head + 1;
} SegmentTree::init(cnt - 1); head = 1; tail = 1;
while (tail <= n && S[tail].x - S[1].x < w) {
SegmentTree::cover(down[S[tail].y], S[tail].y, S[tail].z);
++tail;
}
int ans = SegmentTree::query();
while (head <= n) {
SegmentTree::cover(down[S[head].y], S[head].y, -S[head].z);
++head;
while (head <= n && S[head].x == S[head - 1].x) {
SegmentTree::cover(down[S[head].y], S[head].y, -S[head].z);
++head;
}
while (tail <= n && S[tail].x - S[head].x < w) {
SegmentTree::cover(down[S[tail].y], S[tail].y, S[tail].z);
++tail;
}
ans = max(ans, SegmentTree::query());
}
printf("%d\n", ans);
} return 0;
}

【POJ 2482】Stars in Your Window的更多相关文章

  1. 【POJ 2482】 Stars in Your Window(线段树+离散化+扫描线)

    [POJ 2482] Stars in Your Window(线段树+离散化+扫描线) Time Limit: 1000MS   Memory Limit: 65536K Total Submiss ...

  2. 【POJ 2482】 Stars in Your Windows

    [题目链接] http://poj.org/problem?id=2482 [算法] 线段树 + 扫描线 [代码] #include <algorithm> #include <bi ...

  3. 【POJ2482】【线段树】Stars in Your Window

    Description Fleeting time does not blur my memory of you. Can it really be 4 years since I first saw ...

  4. 【POJ 2352】 Stars

    [题目链接] http://poj.org/problem?id=2352 [算法] 树状数组 注意x坐标为0的情况 [代码] #include <algorithm> #include ...

  5. 51nod 1208 && POJ 2482:Stars in Your Window

    1208 Stars in Your Window 题目来源: Poj 基准时间限制:2 秒 空间限制:131072 KB 分值: 160 难度:6级算法题  收藏  取消关注 整点上有N颗星星,每颗 ...

  6. POJ - 2482:Stars in Your Window (扫描线 )

    题意:二维平面上给你N颗星,给出星星的坐标,亮度: 然后给你一个W*H的窗口,问你最大的亮度和. 思路:扫描线,假设有一个inf*H的窗口,按照y排序,那么就把H范围内的星星放入了这个窗口(单调队列实 ...

  7. 【POJ2482】Stars in Your Window

    [POJ2482]Stars in Your Window 题面 vjudge 题解 第一眼还真没发现这题居然™是个扫描线 令点的坐标为\((x,y)\)权值为\(c\),则 若这个点能对结果有\(c ...

  8. bzoj 2295: 【POJ Challenge】我爱你啊

    2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec  Memory Limit: 128 MB Description ftiasch是个十分受女生欢迎的同学,所以 ...

  9. 【链表】BZOJ 2288: 【POJ Challenge】生日礼物

    2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 382  Solved: 111[Submit][S ...

随机推荐

  1. Unity3D中的线性插值Lerp()函数解析

    http://www.cnblogs.com/rongweijun/p/5739074.html

  2. GNU make简要介绍①指定变量、自动推导规则、清除工作目录过程文件

    Makefile简介 在执行make之前需要一个命名为Makefile的特殊文件来告诉make需要做些什么. 当使用 make 工具进行编译时,工程中以下几种文件在执行 make 时将会被编译 (重新 ...

  3. 浅谈Android Fragment嵌套使用存在的一些BUG以及解决方法

    时间 2014-03-18 18:00:55 eoe博客 原文  http://my.eoe.cn/916054/archive/24053.html 主题 安卓开发 自从Android3.0引入了F ...

  4. http协议(十)实体首部字段

    1.定义 包含在请求和响应中的实体部分所使用的首部,用于补充内容的更新时间等与实体相关的信息 2.Allow 通知客户端能够支持的Request-URI指定资源的所有http方法 如果服务器接收到不支 ...

  5. http应用优化和加速说明-负载均衡

    负载均衡技术       现代企业信息化应用越来越多的采用B/S应用架构来承载企业的关键业务,因此,确保这些任务的可靠运行就变得日益重要.随着越来越多的企业实施数据集中,应用的扩展性.安全性和可靠性也 ...

  6. Java中run(), start(), join(), wait(), yield(), sleep()的使用

    run(), start(), join(), yield(), sleep() 这些是多线程中常用到的方法. run(): 每个Thread中需要实现的方法, 如果直接调用的话, 会是和单线程一样的 ...

  7. 数据库Mark.2

    select count(*) as count,DATE_SUB('2016-10-04',INTERVAL regDay DAY) from result_1005 group by DATE_S ...

  8. 发布了Android的App,我要开源几个组件!

    做了一款App,本来是毕业设计但是毕业的时候还没有做完,因为大部分时间都改论文去了,你们都懂的.现在毕业了在工作之余把App基本上做完了.为什么说基本上呢,因为我觉得还有很多功能还没实现,还要很多bu ...

  9. Java 位运算(移位、位与、或、异或、非)

    Java提供的位运算符有:左移( << ).右移( >> ) .无符号右移( >>> ) .位与( & ) .位或( | ).位非( ~ ).位异或( ...

  10. C118+Osmocom-bb+Openbts搭建小型基站

    演示图片: 演示视频: 交流论坛:GsMsEc 交流Q群: