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个矩形,输入每个矩形的左上角坐标和右下角坐标. 然后求矩形的总面积.(矩形可能相交). 题解: 前言: 先说说做这道题的感受: 刚看到这道题顿时就懵逼了,几何 烂的渣渣.后来从网上搜题解 ...
随机推荐
- 【BZOJ 1202】 [HNOI2005]狡猾的商人
Description 刁姹接到一个任务,为税务部门调查一位商人的账本,看看账本是不是伪造的.账本上记录了n个月以来的收入情况,其中第i 个月的收入额为Ai(i=1,2,3...n-1,n), .当 ...
- K-meams文本聚类算法C++实现
FROM:http://www.cnblogs.com/finallyliuyu/archive/2010/09/03/1817348.html 头文件: #ifndef _Preprocess_H ...
- C#和Js 编码和解码方法
Server.UrlDecode(); Server.UrlEncode(); Server.HtmlDecode(); Server.HtmlEncode();
- poj 3207 Ikki's Story IV - Panda's Trick (2-SAT)
http://poj.org/problem?id=3207 Ikki's Story IV - Panda's Trick Time Limit: 1000MS Memory Limit: 13 ...
- 小啃机器学习(1)-----ID3和C4.5决策树
第一部分:简介 ID3和C4.5算法都是被Quinlan提出的,用于分类模型,也被叫做决策树.我们给一组数据,每一行数据都含有相同的结构,包含了一系列的attribute/value对. 其中一个属性 ...
- linux复制多个文件到文件夹
linux复制多个文件到文件夹 cp file1 file2 file3 directory即将文件file1 file2 file3复制到directory
- 严重: The web application [] appears to have started a thread named [Abandoned connection cleanup thread] but has failed to stop it.
今日在重新部署项目时出现此问题,虽然对项目无影响,但问题就是问题.完整信息如下(使用idea工具): 十二月 05, 2015 11:44:27 上午 org.apache.catalina.star ...
- Ubuntu 14.04 启用休眠
Ubuntu 14.04 启用休眠 Ubuntu 14.04 默认不启用关机菜单中的休眠选项.前提是要有swap分区(网上查询,未验证是否一定需要.PS:swap要不小于物理内存)不过首先最好还是确认 ...
- State Management
Samza的task可以把数据进行本地存储,并且对这些数据进行丰富的查询. 比较SQL中的select ... where...并不需要保存状态.但是aggregation和join就需要存储ro ...
- What is the innovator’s solution——什么才是创新的解决方案1
最近学习MOT(management of Technology),研读了Christensen的<创新者的窘境>和<创新者的解答>,以下简称创新者系列.总觉得需要写点儿什么. ...