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 ...
随机推荐
- 打开开源项目总得.md文件
google了一些: 78 Tools for Writing and Previewing Markdown http://mashable.com/2013/06/24/markdown-too ...
- Linux下命令行安装WebLogic 10.3.6
1.创建用户useradd weblogic;创建用户成功linux系统会自动创建一个和用户名相同的分组,并将该用户分到改组中.并会在/home路径下创建一个和用户名相同的路径,比如我们创建的webl ...
- iOS-网址集
0. 在线工具 http://tool.lu 1. iOS学习笔记汇总链接 https://blog.6ag.cn/533.html 2.iOS开发内购全套图文教程http://mp.weixin.q ...
- ectouch第三讲之加载调用机制
加载与调用机制: 当地址栏键入/mobile,就会加载入口文件index.php:从入口文件里面会调用EcTouch.php公共入口文件,当进入公共入口文件,会定义一些变量,然后加载公 ...
- js笔记---拖动元素
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- SQL-字符串合并
create table tb(id int, value varchar(10)) insert into tb values(1, 'aa') insert into tb values(1, ...
- A Round Peg in a Ground Hole(凸包应用POJ 1584)
A Round Peg in a Ground Hole Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5684 Accepte ...
- 关于接收json以及使用json
Common: FileIO.cs using System; using System.Collections.Generic; //using System.Linq; using System. ...
- userdebug版本开机串口log打开
在/bootable/bootloader/lk/app/mt_boot/mt_boot.c里修改: if (!has_set_p2u) { #ifdef USER_BUILD sprintf(cmd ...
- 操作SQLite的dbhelper
操作SQLite的dbhelper public class DbHelper { string connStr = @"Data Source=" + System.Enviro ...