ZOJ 3299-Fall the Brick(线段树+离散化)
题意:
n个区间 ,给出区间的左右坐标 ,区间内填满宽度为1的箱子,有m个板子给出板子的高度和左右坐标(同高度不重叠)
所有箱子从上向下落,求每块板子能接到的箱子数。
分析:
首先给的区间很大,一开始直接存ME了,所以要先把给定的区间离散化 箱子的宽度是1则使维护区间左闭右开,才能得正确的数量。
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <string>
#include <cctype>
#include <complex>
#include <cassert>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
#define lson l,m,rt<<1
#define pi acos(-1.0)
#define rson m+1,r,rt<<1|1
#define All 1,N,1
#define N 100010
#define read freopen("in.txt", "r", stdin)
const ll INFll = 0x3f3f3f3f3f3f3f3fLL;
const int INF= 0x7ffffff;
const int mod = ;
int res[*N],f[<<],x,y;
ll sum[<<],tmp[N],add[<<];
struct brick{
int l,r,lid,rid;
}a[N];
struct board{
int l,r,lid,rid,h,id;
}b[N];
bool cmp(board u,board v){
return u.h>v.h;
}
void pushup(int rt){
sum[rt]=sum[rt<<]+sum[rt<<|];
}
void pushdown(int rt,int l,int r){
int mid=(l+r)>>;
if(add[rt]){
add[rt<<]+=add[rt];
add[rt<<|]+=add[rt];
sum[rt<<]+=1LL*(res[mid+]-res[l])*add[rt];
sum[rt<<|]+=1LL*(res[r+]-res[mid+])*add[rt];
add[rt]=;
}
}
void build(int l,int r,int rt){
sum[rt]=add[rt]=f[rt]=;
if(l==r)return;
int m=(l+r)>>;
build(lson);
build(rson);
}
void update_add(int L,int R,int v,int l,int r,int rt){
if(L<=l&&R>=r){
add[rt]+=v;
sum[rt]+=(res[r+]-res[l])*v;
return;
}
pushdown(rt,l,r);
int m=(l+r)>>;
if(L<=m)update_add(L,R,v,lson);
if(R>m)update_add(L,R,v,rson);
pushup(rt);
}
void update_cle(int L,int R,int l,int r,int rt){
if(f[rt])return;
if(L<=l&&R>=r){
sum[rt]=;
f[rt]=;
return;
}
pushdown(rt,l,r);
int m=(l+r)>>;
if(L<=m)update_cle(L,R,lson);
if(R>m)update_cle(L,R,rson);
pushup(rt);
}
ll query(int L,int R,int l,int r,int rt){
if(f[rt])return ;
if(L<=l&&R>=r){
return sum[rt];
}
pushdown(rt,l,r);
ll num=;
int m=(l+r)>>;
if(L<=m)num+=query(L,R,lson);
if(R>m)num+=query(L,R,rson);
return num;
}
int main()
{
while(~scanf("%d%d",&x,&y)){
int len=;
for(int i=;i<x;++i){
scanf("%d%d",&a[i].l,&a[i].r);
res[len++]=a[i].l;
res[len++]=a[i].r;
}
for(int i=;i<y;++i){
scanf("%d%d%d",&b[i].l,&b[i].r,&b[i].h);
res[len++]=b[i].l;
res[len++]=b[i].r;
b[i].id=i;
}
//离散化
sort(res,res+len);
len=unique(res,res+len)-res;
for(int i=;i<x;++i){
a[i].lid=lower_bound(res,res+len,a[i].l)-res;
a[i].rid=lower_bound(res,res+len,a[i].r)-res;
}
for(int i=;i<y;++i){
b[i].lid=lower_bound(res,res+len,b[i].l)-res;
b[i].rid=lower_bound(res,res+len,b[i].r)-res;
}
build(,len-,);
for(int i=;i<x;++i)
update_add(a[i].lid,a[i].rid-,,,len-,);//左闭右开
sort(b,b+y,cmp);
for(int i=;i<y;++i){
tmp[b[i].id]=query(b[i].lid,b[i].rid-,,len-,);
update_cle(b[i].lid,b[i].rid-,,len-,);
}
for(int i=;i<y;++i){
printf("%I64d\n",tmp[i]);
}
printf("\n");
}
return ;
}
ZOJ 3299-Fall the Brick(线段树+离散化)的更多相关文章
- POJ 2528 Mayor's posters(线段树+离散化)
Mayor's posters 转载自:http://blog.csdn.net/winddreams/article/details/38443761 [题目链接]Mayor's posters [ ...
- poj 2528 Mayor's posters(线段树+离散化)
/* poj 2528 Mayor's posters 线段树 + 离散化 离散化的理解: 给你一系列的正整数, 例如 1, 4 , 100, 1000000000, 如果利用线段树求解的话,很明显 ...
- [poj2528] Mayor's posters (线段树+离散化)
线段树 + 离散化 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayor ...
- [UESTC1059]秋实大哥与小朋友(线段树, 离散化)
题目链接:http://acm.uestc.edu.cn/#/problem/show/1059 普通线段树+离散化,关键是……离散化后建树和查询都要按照基本法!!!RE了不知道多少次………………我真 ...
- poj 2528 Mayor's posters 线段树+离散化技巧
poj 2528 Mayor's posters 题目链接: http://poj.org/problem?id=2528 思路: 线段树+离散化技巧(这里的离散化需要注意一下啊,题目数据弱看不出来) ...
- BZOJ_4653_[Noi2016]区间_线段树+离散化+双指针
BZOJ_4653_[Noi2016]区间_线段树+离散化+双指针 Description 在数轴上有 n个闭区间 [l1,r1],[l2,r2],...,[ln,rn].现在要从中选出 m 个区间, ...
- D - Mayor's posters(线段树+离散化)
题目: The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campai ...
- 主席树||可持久化线段树+离散化 || 莫队+分块 ||BZOJ 3585: mex || Luogu P4137 Rmq Problem / mex
题面:Rmq Problem / mex 题解: 先离散化,然后插一堆空白,大体就是如果(对于以a.data<b.data排序后的A)A[i-1].data+1!=A[i].data,则插一个空 ...
- HDU5124:lines(线段树+离散化)或(离散化思想)
http://acm.hdu.edu.cn/showproblem.php?pid=5124 Problem Description John has several lines. The lines ...
- hdu1542 矩形面积并(线段树+离散化+扫描线)
题意: 给你n个矩形,输入每个矩形的左上角坐标和右下角坐标. 然后求矩形的总面积.(矩形可能相交). 题解: 前言: 先说说做这道题的感受: 刚看到这道题顿时就懵逼了,几何 烂的渣渣.后来从网上搜题解 ...
随机推荐
- .net sql connection pool leak
Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This ma ...
- vc编程时说“Cannot open include file: 'unistd.h': No such file or directory”
本文专自http://blog.csdn.net/mangobar/article/details/6314700 unistd.h是unix standard header之意,因此,Linux下开 ...
- Rust入门篇 (1)
Rust入门篇 声明: 本文是在参考 The Rust Programming Language 和 Rust官方教程 中文版 写的. 个人学习用 再PS. 目录这东东果然是必须的... 找个时间生成 ...
- 1058: [ZJOI2007]报表统计 - BZOJ
Description 小Q的妈妈是一个出纳,经常需要做一些统计报表的工作.今天是妈妈的生日,小Q希望可以帮妈妈分担一些工作,作为她的生日礼物之一.经过仔细观察,小Q发现统计一张报表实际上是维护一个非 ...
- aJax提交——服务端不能用request存储数据,session存数据客户端可以接收到
aJax提交与普通提交是两种迥异的提交方式,这两种提交方式决定了客户端与服务端交互时存储.传输数据的方式也不同. aJax提交,客户端的请求数据存储在data中,服务端用request.getPara ...
- Spark中shuffle的触发和调度
Spark中的shuffle是在干嘛? Shuffle在Spark中即是把父RDD中的KV对按照Key重新分区,从而得到一个新的RDD.也就是说原本同属于父RDD同一个分区的数据需要进入到子RDD的不 ...
- js 计时器
<html><head><script type="text/javascript">var count=0;var t ;function t ...
- Untiy 接入 移动MM 详解
原地址:http://www.cnblogs.com/alongu3d/p/3627936.html Untiy 接入 移动MM 详解 第一次接到师傅的任务(小龙),准备着手写untiy接入第三方SD ...
- C++11新特性:Lambda函数(匿名函数)
声明:本文参考了Alex Allain的文章http://www.cprogramming.com/c++11/c++11-lambda-closures.html 加入了自己的理解,不是简单的翻译 ...
- IText 中文字体解决方案 生成doc文档
IText生成doc文档需要三个包:iTextAsian.jar,iText-rtf-2.1.4.jar,iText-2.1.4.jar 亲测无误,代码如下: import com.lowagie.t ...