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. [PHP]swoole_server几个进程的分工

    readme.md-/Users/zjh/Documents/我的文章/[PHP]swoole_server几个进程的分工 html{font-family: sans-serif;-ms-text- ...

  2. 使用ZeroNet搭建P2P全球网站

    软件 ZeroNet是一个利用比特币加密和BT技术提供不受审查的网络与通信的BT平台,ZeroNet网络功能已经得到完整的种子的支持和加密连接,保证用户通信和文件共享的安全.使用ZeroNet,你可以 ...

  3. Gprinter热敏打印机光栅位图点阵数据解析工具

    最近参与的项目有一个需求,解析佳博热敏打印机的光栅位图点阵数据并保存为图片文件.数据是通过Bus Hound抓取的,如下图所示. 其中1b 40为初始化打印机的指令,对应的ASCII码为ESC @,1 ...

  4. 网页中导入特殊字体@font-face属性详解

    @font-face是CSS3中的一个模块,他主要是把自己定义的Web字体嵌入到你的网页中. 语法规则 首先我们一起来看看@font-face的语法规则: @font-face { font-fami ...

  5. POJ1094[有向环 拓扑排序]

    Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 33184   Accepted: 11 ...

  6. python pyperclip模块的使用

    用途: 复制,粘贴 用法: >>> import pyperclip >>> pyperclip.copy('Hello world!') >>> ...

  7. Android之监听手机软键盘弹起与关闭

    背景: 在很多App开发过程中需要在Activity中监听Android设备的软键盘弹起与关闭,但是Android似乎没有提供相关的的监听API给我们来调用,本文提供了一个可行的办法来监听软键盘的弹起 ...

  8. 查看oracle死锁进程并结束死锁

    查看锁表进程SQL语句1: select sess.sid, sess.serial#, lo.oracle_username, lo.os_user_name, ao.object_name, lo ...

  9. CF#345 (Div1)

    论蒟蒻如何被cf虐 以下是身败名裂后的题解菌=========== Div1 A.Watchmen 有n个点,每个点有一个坐标.求曼哈顿距离=欧几里得距离的点对数量. 只需要统计x或y一样的点对数量. ...

  10. Linux 网络编程详解一(IP套接字结构体、网络字节序,地址转换函数)

    IPv4套接字地址结构 struct sockaddr_in { uint8_t sinlen;(4个字节) sa_family_t sin_family;(4个字节) in_port_t sin_p ...