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. Java开发之JSP指令

    一.page指令 page指令是最常用的指令,用来说明JSP页面的属性等.JSP指令的多个属性可以写在一个page指令里,也可以写在多个指令里.但需要注意的是,无论在哪个page指令里的属性,任何pa ...

  2. NOIP2003传染病控制[按层DFS]

    题目背景 近来,一种新的传染病肆虐全球.蓬莱国也发现了零星感染者,为防止该病在蓬莱国大范围流行,该国政府决定不惜一切代价控制传染病的蔓延.不幸的是,由于人们尚未完全认识这种传染病,难以准确判别病毒携带 ...

  3. 第五章项目:QuickHit

    需求概述: 根据输入速率和正确率将玩家分为不同级别,级别越高,一次显示的字符数越多,玩家正确输入一次的得分也越高.如果玩家在规定时间内完成规定次数的输入,正确率达到规定要求,则玩家升级(为了简单起见, ...

  4. guava常用操作

    Jack47 我思故我在 Google Java编程库Guava介绍 本系列想介绍下Java下开源的优秀编程库--Guava[ˈgwɑːvə].它包含了Google在Java项目中使用一些核心库,包含 ...

  5. SQL/LINQ/Lamda

    SQL LINQ Lambda SELECT * FROM HumanResources.Employee from e in Employees select e Employees   .Sele ...

  6. 【QCon笔记】Native 和 Web 融合

    #main img{width:100%;} 简介 理清 Native 和 Web 的亮点和痛点,借鉴对方亮点解决自身的痛点,并给出淘系 App 在这些方面的实践. Mobile Web 的协作能力底 ...

  7. SAE上安装第三方模块

    当sae上没有自己所需要的第三方模块时,可以使用saecloud install package [package...]将所需要的模块安装到本地应用文件夹下,然后在index.wsgi下添加如何代码 ...

  8. NOI2018准备Day1

    今天刷基础题,字符串实在不想刷,做了20到多维数组题.老师说要10分钟一道,然而我加上整理差不多半小时一道吧... 总感觉自己效率比别人低了好多好多好多倍. 基础不牢,地动山摇,最近还是好好稳固基础题 ...

  9. SQL server 数据库备份还原Sql

    /************ 一.数据库备份 ************/ --完整备份默认追加到现有的文件 backup database DBXS To disk='d:\backup\DBXS_fu ...

  10. 数据库mark

    LOAD DATA INFILE 'I:\QQpwd\\1.txt' IGNORE INTO TABLE sgk.top1 FIELDS TERMINATED BY '----' OPTIONALLY ...