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个矩形,输入每个矩形的左上角坐标和右下角坐标. 然后求矩形的总面积.(矩形可能相交). 题解: 前言: 先说说做这道题的感受: 刚看到这道题顿时就懵逼了,几何 烂的渣渣.后来从网上搜题解 ...
随机推荐
- python学习笔记15(面向对象编程)
虽然Python是解释性语言,但是它是面向对象的,能够进行对象编程. 一.如何定义一个类 在进行python面向对象编程之前,先来了解几个术语:类,类对象,实例对象,属性,函数和方法. 类是对现实世界 ...
- Java发送邮件(带附件)
实现java发送邮件的过程大体有以下几步: 准备一个properties文件,该文件中存放SMTP服务器地址等参数. 利用properties创建一个Session对象 利用Session创建Mess ...
- 1060: [ZJOI2007]时态同步 - BZOJ
Description小Q在电子工艺实习课上学习焊接电路板.一块电路板由若干个元件组成,我们不妨称之为节点,并将其用数字1,2,3….进行标号.电路板的各个节点由若干不相交的导线相连接,且对于电路板的 ...
- 无锁算法CAS 概述
无锁算法CAS 概述 JDK5.0以后的版本都引入了高级并发特性,大多数的特性在java.util.concurrent包中,是专门用于多线并发编程的,充分利用了现代多处理器和多核心系统的功能以编写大 ...
- 团体程序设计天梯赛-练习集L1-024. 后天
L1-024. 后天 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 如果今天是星期三,后天就是星期五:如果今天是星期六,后天就 ...
- The 6th Zhejiang Provincial Collegiate Programming Contest->Problem I:A Stack or A Queue?
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3210 题意:给出stack和queue的定义,一个是先进后出(FILO), ...
- linux踢人命令 pkill踢人用法
首先使用who命令查看在线用户,然后踢人. 强制踢人命令格式:pkill -kill -t tty 解释: pkill -kill -t 踢人命令 tty 所踢用户的TTY或者pts/x(x代表数字) ...
- MyEclipse server窗口 Could not create the view: An unexpected exception was thrown 错误解决
MyEclipse 打开后有时候莫名的在server窗口里抛出“Could not create the view: An unexpected exception was thrown”错误,解决办 ...
- highChartTable 切换
<!doctype html> <html lang="en"> <head> <script type="text/javas ...
- BZOJ 3747 POI2015 Kinoman
因为上午没有准备够题目,结果发现写完这道题没题可写了QAQ 又因为这道题范围是100w,我写了发线段树,以为要T,上午就花了一个小时拼命卡常数 结果下午一交居然过了QAQ 我们考虑枚举L,求最大R使得 ...