Can of Worms 【迭代/线段树】
题意:一条直线上有n个炸弹,给出每个炸弹的爆炸半径,可以引爆另一个炸弹爆炸。问:每个炸弹爆炸后,最多有几个炸弹一起爆炸?
迭代,用线段树更新。
#include <cstdio>
#include <algorithm>
#include <iostream>
#define ll long long
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define X first
#define Y second
#define mp make_pair
#define pii pair<int, int>
#define gg puts("gg");
using namespace std;
const int N = 1e5+;
struct p{
int n, pos, ra;
p(){}
p(int pos, int ra):pos(pos), ra(ra){}
};
bool cmppos(p A, p B){
return A.pos < B.pos;
} p pp[N];
//after sort
int pos[N];
int lpos[N], rpos[N], now; struct Node{
int l, r;
Node(){}
Node(int l, int r):l(l), r(r){}
};
Node T[N<<];
void pushup(int rt){
T[rt].l = min(T[rt<<].l, T[rt<<|].l);
T[rt].r = max(T[rt<<].r, T[rt<<|].r);
}
void gao(Node& x, Node y){
if(x.l > y.l) x.l = y.l;
if(x.r < y.r) x.r = y.r;
}
void update(int x, Node y, int l, int r, int rt){
if(x == l&&x == r){
T[rt] = y;
return ;
}
int m = l+r >> ;
if(x <= m) update(x, y, lson);
else update(x, y, rson);
pushup(rt);
}
void build(int l, int r, int rt){
if(l == r){
T[rt].l = lpos[now];
T[rt].r = rpos[now];
now++;
return ;
}
int m = l+r >> ;
build(lson);
build(rson);
pushup(rt);
}
void query(Node& ans, int L, int R, int l, int r, int rt){
if(L <= l&& r <= R){
gao(ans, T[rt]);
return ;
}
int m = l+r >> ;
if(L <= m) query(ans, L, R, lson);
if(m < R) query(ans, L, R, rson);
}
int ans[N];
void debug(int i){
printf("%d: lpos:%d, rpos:%d\n", i, lpos[i], rpos[i]);
} int main(){
int n;
while(scanf("%d", &n), n){
for(int i = ; i <= n; i++){
pp[i].n = i;
scanf("%d%d", &pp[i].pos, &pp[i].ra);
pos[i] = pp[i].pos;
}
sort(pos+, pos+n+);
sort(pp+, pp+n+, cmppos); for(int i = ; i <= n; i++)
lpos[i] = pp[i].pos-pp[i].ra, rpos[i] = pp[i].pos+pp[i].ra; now = ;
build(, n, ); bool tag = true;
while(tag){
tag = false;
for(int i = ; i <= n; i++){
int lcan = lower_bound(pos+, pos+n+, lpos[i])-pos, rcan = upper_bound(pos+, pos+n+, rpos[i])-pos-;
ans[ pp[i].n ] = rcan-lcan+;
Node ret = Node(2e9, -2e9);
query(ret, lcan, rcan, , n, );
if(lpos[i] > ret.l||rpos[i] < ret.r) {
tag = true;
if(lpos[i] > ret.l) lpos[i] = ret.l;
if(rpos[i] < ret.r) rpos[i] = ret.r;
update(i, ret, , n, );
}
}
}
for(int i = ; i <= n; i++)
printf("%d%c", ans[i], " \n"[i == n]);
}
return ;
}
Can of Worms 【迭代/线段树】的更多相关文章
- zkw线段树详解
转载自:http://blog.csdn.net/qq_18455665/article/details/50989113 前言 首先说说出处: 清华大学 张昆玮(zkw) - ppt <统计的 ...
- 有趣的 zkw 线段树(超全详解)
zkw segment-tree 真是太棒了(真的重口味)!写篇博客纪念入门 emmm...首先我们来介绍一下 zkw 线段树这个东西(俗称 "重口味" ,与 KMP 类似,咳咳. ...
- 线段树(区间树)之区间染色和4n推导过程
前言 线段树(区间树)是什么呢?有了二叉树.二分搜索树,线段树又是干什么的呢?最经典的线段树问题:区间染色:正如它的名字而言,主要解决区间的问题 一.线段树说明 1.什么是线段树? 线段树首先是二叉树 ...
- ZKW线段树入门
Part 1 来说说它的构造 线段树的堆式储存 我们来转成二进制看看 小学生问题:找规律 规律是很显然的 一个节点的父节点是这个数左移1,这个位运算就是低位舍弃,所有数字左移一位 一个节点的子节点是这 ...
- BZOJ 4408: [Fjoi 2016]神秘数 可持久化线段树
4408: [Fjoi 2016]神秘数 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=4408 Description 一个可重复数字集 ...
- POJ2528:Mayor's posters(线段树区间更新+离散化)
Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral electio ...
- CodeForces 266E More Queries to Array...(线段树+式子展开)
开始觉得是规律题的,自以为是的推了一个规律,结果测试数据都没过....看了love神的博客才发现只是把式子展开就找到规律了.不过挺6的是我虽然想错了,但是维护的的东西没有错,只是改改(改了进两个小时好 ...
- ACM学习历程—HDU5696 区间的价值(分治 && RMQ && 线段树 && 动态规划)
http://acm.hdu.edu.cn/showproblem.php?pid=5696 这是这次百度之星初赛2B的第一题,但是由于正好打省赛,于是便错过了.加上2A的时候差了一题,当时有思路,但 ...
- HDU5152 线段树 + 数论
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5152 ,线段树区间更新 + 点更新 + 数论知识(数论是重点QAQ),好题值得一做. BestCode ...
随机推荐
- 加载 pcntl 多进程
加载 pcntl 有两种方式 一种重新编译安装,在编译时加 --enable-pcntl ./configure --prefix=/usr/local/php --with-mysql=/usr/l ...
- 每日一九度之 题目1031:xxx定律
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:6870 解决:4302 题目描述: 对于一个数n,如果是偶数,就把n砍掉一半:如果是奇数,把n变成 3*n+ 1后砍掉一半,直到该数 ...
- 鼠标放上去,div高度随文字增加,并显示剩余的文字。
/*这里是鼠标放上去显示全名 */ .kb2wText{display:block; height:20px; width:150px; line-height:20px; color:#0 ...
- sp_sys_ERPTrigger_base
USE [GalaxyPointDB24]GO/****** Object: StoredProcedure [dbo].[sp_zy_Process_scrap] Script Date: ...
- The Blacksmith学习的相关资源
1.Unity官网的Blacksmith主页 https://unity3d.com/pages/the-blacksmith 2.WRINKLE MAPS IN THE BLACKSMITH 褶皱贴 ...
- linux socket高性能服务器处理框架
这个博客很多东西 http://blog.csdn.net/luozhonghua2014/article/details/37041765 思考一种高性能的服务器处理框架 1.首先需要一个内存池 ...
- 谷歌Chrome浏览器如何设置网页的默认编码方法
设置->高级->自定义字体->编码->utf-8
- github pages搭建个人网站如何添加导航
折腾过github pages的同学都清楚使用jekyll搭建个人网站的目录结构,而导航最终的代码是在使用的主题目录中的default.html文件中的. 我的结构如下: . ├── .html ├─ ...
- wpfのpack协议
当引用的资源需要做成dll时,要用此协议 协议:pack:// 授权:有两种.一种用于访问编译时已经知道的文件,用application:///.一种用于访问编译时不知道.运行时才知道的文件 ...
- 2016年11月10日 星期四 --出埃及记 Exodus 20:1
2016年11月10日 星期四 --出埃及记 Exodus 20:1 And God spoke all these words: 神吩咐这一切的话说,