Time Limit: 3 Seconds      Memory Limit: 32768 KB

Now the God is very angry, so he wants to punish the lazy, greedy humans. He chooses to throw some lines of bricks (just down from the very high Heaven). These days the God lives in a 2D world, so he just throw the bricks in a vertical plane. Each time, the
God throws a line of bricks. The width of each brick is 1, and the length will be given.

t__nt is a hero in the world and he is trying his best to save the world. Now he has made m horizontal boards in the air with his magic power to stop the bricks. If one brick falls onto a board, it can not fall down any more. Notice that, for a line of bricks,
consecutive bricks are not connected. So when some bricks touch a board, the others will continues to fall down. Now, t__nt wants to know how many bricks each board holds after the God's crazy action. He asks you, an ACMer, to help him.

Input

There are no more then 10 cases. There is a blank line between consecutive cases. The first line of each case contains two integers n, m (0 < n, m <= 100000), indicating the number of lines of bricks and number of horizontal boards made by t__nt. n lines follow,
each contains two integers li, ri (0 <= li < ri <= 30000000). li and ri is the x-coordinates for the left side and the right side of the line of bricks. m lines follow, each contains three integers ai, bi, and hi (0 <= ai < bi <= 30000000; 0 < hi < 1000000000),
which means that board i is at height hi and the extreme points are ai and bi. You may assume no two boards with same height will overlap with each other.

Output

For each case, print m lines. The ith line means the number of bricks on board i at last. Print a blank line after each case.

Sample Input

1 2

1 8

1 5 8

3 7 6

Sample Output

4

2

这一题是the simple problem of integers的升级版,错了很多次。一开始我的思路是先放拦截的木板(木板先从低到高排序,因为高的会覆盖低的),然后依次读入木块,碰到混合的木块就继续向子树搜索,碰到同种颜色的木块就把加到这种颜色上,然后返回。但是这样如果颜色分的很杂的话,就会超时。所以我换了一种思路,先更新下落的木块,维护线段树的cnt和sum,cnt是延迟标志,表示这整段都要增加的长度,sum表示这段总的木块数(注意:这里的sum是实际的总覆盖数,也是加上cnt的覆盖数,即每次更新cnt的时候,sum也要更新,这里和the
simple problem of integers,因为那题cnt更新的时候可以不用更新sum,只有pushdown的时候才要把cnt变为0,sum+=cnt*(r-l+1).但是这一题多了一个清0操作,即要使这一段的数都变为0,如果不记录实际长度,就会出错,这里sum[i]=sum[i*2]+sum[i*2+1]是重点.)然后这题很坑的地方就是内存不足,我MLE了10多次,最后把long long开成int,然后把线段树的左右标记去掉,写进函数,终于A了。= =

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
#define ll long long
#define maxn 100010
int pos[4*maxn];
int a[maxn],d[maxn]; struct edge{
int l,r,h,id;
ll num1;
}c[maxn]; ll sum[16*maxn],cnt[16*maxn];
bool flag[16*maxn]; bool cmp1(edge a,edge b){
return a.h>b.h;
}
bool cmp2(edge a,edge b){
return a.id<b.id;
}
void build(int l,int r,int i)
{
int mid;
cnt[i]=sum[i]=0;flag[i]=false;
if(l==r)return;
mid=(l+r)/2;
build(l,mid,i*2);
build(mid+1,r,i*2+1);
} void update1(int l,int r,int L,int R,int i)
{
int mid;
if(L==l && R==r){
cnt[i]+=1;
sum[i]+=(ll)(pos[r+1]-pos[l]);
return;
}
mid=(L+R)/2;
if(cnt[i]){
cnt[i*2]+=cnt[i];
cnt[i*2+1]+=cnt[i];
sum[i*2]+=cnt[i]*(ll)(pos[mid+1]-pos[L]);
sum[i*2+1]+=cnt[i]*(ll)(pos[R+1]-pos[mid+1]);
cnt[i]=0;
}
if(r<=mid)update1(l,r,L,mid,i*2);
else if(l>mid)update1(l,r,mid+1,R,i*2+1);
else{
update1(l,mid,L,mid,i*2);
update1(mid+1,r,mid+1,R,i*2+1);
}
sum[i]=sum[i*2]+sum[i*2+1];
} void update2(int l,int r,int color,int L,int R,int i)
{
int mid;
if(flag[i])return;
if(l==L && R==r){
flag[i]=true;
c[color].num1+=sum[i];
sum[i]=0;
return;
}
mid=(L+R)/2;
if(cnt[i]){
cnt[i*2]+=cnt[i];
cnt[i*2+1]+=cnt[i];
sum[i*2]+=cnt[i]*(ll)(pos[mid+1]-pos[L]);
sum[i*2+1]+=cnt[i]*(ll)(pos[R+1]-pos[mid+1]);
cnt[i]=0;
}
if(r<=mid)update2(l,r,color,L,mid,i*2);
else if(l>mid)update2(l,r,color,mid+1,R,i*2+1);
else{
update2(l,mid,color,L,mid,i*2);
update2(mid+1,r,color,mid+1,R,i*2+1);
}
sum[i]=sum[i*2]+sum[i*2+1];
}
int main()
{
int n,m,i,j,t,tot,t1,t2;
while(scanf("%d%d",&n,&m)!=EOF)
{
t=0;
for(i=1;i<=n;i++){
scanf("%d%d",&a[i],&d[i]);
t++;pos[t]=a[i];
t++;pos[t]=d[i];
}
for(i=1;i<=m;i++){
scanf("%d%d%d",&c[i].l,&c[i].r,&c[i].h);
c[i].id=i;c[i].num1=0;
t++;pos[t]=c[i].l;
t++;pos[t]=c[i].r;
}
sort(pos+1,pos+1+t);
tot=1;
for(i=2;i<=t;i++){
if(pos[i]!=pos[tot]){
tot++;pos[tot]=pos[i];
}
}
build(1,tot-1,1);
for(i=1;i<=n;i++){
t1=lower_bound(pos+1,pos+1+tot,a[i])-pos;
t2=lower_bound(pos+1,pos+1+tot,d[i])-pos;
update1(t1,t2-1,1,tot-1,1);
}
sort(c+1,c+1+m,cmp1);
for(i=1;i<=m;i++){
t1=lower_bound(pos+1,pos+1+tot,c[i].l)-pos;
t2=lower_bound(pos+1,pos+1+tot,c[i].r)-pos;
update2(t1,t2-1,c[i].id,1,tot-1,1);
}
for(i=1;i<=m;i++){
printf("%lld\n",c[i].num1);
}
printf("\n");
}
return 0;
}

ps:这题也可以先放砖块,然后放木板。思路是这样的:用线段树维护cnt(每段线段的增量),h(这一段的高度)。先把砖块更新,记录每一段的增量,然后把模板按从低到高排序,依次更新木板的高度,这里可以用成段更新,也可以不用,因为只要后面找每一个叶子节点到根节点的这一段路径中最大的高度就行了,然后用map把h和id连在一起,用于后面的更新。但这方法速度要慢90ms。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
#define ll long long
#define maxn 100010
map<int,int>mp;
map<int,int>::iterator it;
int pos[4*maxn];
int a[maxn],d[maxn],h[16*maxn];
ll num1[maxn];
struct edge{
int l,r,h,id;
}c[maxn]; ll cnt[16*maxn]; bool cmp1(edge a,edge b){
return a.h<b.h;
}
bool cmp2(edge a,edge b){
return a.id<b.id;
}
void build(int l,int r,int i)
{
int mid;
cnt[i]=h[i]=0;
if(l==r)return;
mid=(l+r)/2;
build(l,mid,i*2);
build(mid+1,r,i*2+1);
} void update1(int l,int r,int L,int R,int i)
{
int mid;
if(L==l && R==r){
cnt[i]+=1;
return;
}
mid=(L+R)/2;
if(cnt[i]){
cnt[i*2]+=cnt[i];
cnt[i*2+1]+=cnt[i];
cnt[i]=0;
}
if(r<=mid)update1(l,r,L,mid,i*2);
else if(l>mid)update1(l,r,mid+1,R,i*2+1);
else{
update1(l,mid,L,mid,i*2);
update1(mid+1,r,mid+1,R,i*2+1);
}
} void update2(int l,int r,int color,int L,int R,int i)
{
int mid;
if(l==L && R==r){
h[i]=color;
return;
}
mid=(L+R)/2;
if(cnt[i]){
cnt[i*2]+=cnt[i];
cnt[i*2+1]+=cnt[i];
cnt[i]=0;
}
if(r<=mid)update2(l,r,color,L,mid,i*2);
else if(l>mid)update2(l,r,color,mid+1,R,i*2+1);
else{
update2(l,mid,color,L,mid,i*2);
update2(mid+1,r,color,mid+1,R,i*2+1);
}
} void question(int id,int ht,int L,int R,int i)
{
int mid;
if(L==id && R==id){
ht=max(ht,h[i]);
num1[mp[ht]]+=(ll)cnt[i]*(pos[R+1]-pos[R]);
return;
}
ht=max(ht,h[i]);
if(cnt[i]){
cnt[i*2]+=cnt[i];
cnt[i*2+1]+=cnt[i];
cnt[i]=0;
}
mid=(L+R)/2;
if(id<=mid)question(id,ht,L,mid,i*2);
else question(id,ht,mid+1,R,i*2+1);
} int main()
{
int n,m,i,j,t,tot,t1,t2;
while(scanf("%d%d",&n,&m)!=EOF)
{
t=0;
for(i=1;i<=n;i++){
scanf("%d%d",&a[i],&d[i]);
t++;pos[t]=a[i];
t++;pos[t]=d[i];
}
for(i=1;i<=m;i++){
scanf("%d%d%d",&c[i].l,&c[i].r,&c[i].h);
c[i].id=i;num1[i]=0;
t++;pos[t]=c[i].l;
t++;pos[t]=c[i].r;
mp[c[i].h]=c[i].id;
}
sort(pos+1,pos+1+t);
tot=1;
for(i=2;i<=t;i++){
if(pos[i]!=pos[tot]){
tot++;pos[tot]=pos[i];
}
}
build(1,tot-1,1);
for(i=1;i<=n;i++){
t1=lower_bound(pos+1,pos+1+tot,a[i])-pos;
t2=lower_bound(pos+1,pos+1+tot,d[i])-pos;
update1(t1,t2-1,1,tot-1,1);
}
sort(c+1,c+1+m,cmp1);
for(i=1;i<=m;i++){
t1=lower_bound(pos+1,pos+1+tot,c[i].l)-pos;
t2=lower_bound(pos+1,pos+1+tot,c[i].r)-pos;
update2(t1,t2-1,c[i].h,1,tot-1,1);
}
for(i=1;i<=tot-1;i++){
question(i,0,1,tot-1,1);
}
for(i=1;i<=m;i++){
printf("%lld\n",num1[i]);
}
printf("\n");
}
return 0;
}

zoj3299 Fall the Brick的更多相关文章

  1. 线段树总结 (转载 里面有扫描线类 还有NotOnlySuccess线段树大神的地址)

    转载自:http://blog.csdn.net/shiqi_614/article/details/8228102 之前做了些线段树相关的题目,开学一段时间后,想着把它整理下,完成了大牛NotOnl ...

  2. UVALive 4425 Another Brick in the Wall 暴力

    C - Another Brick in the Wall Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & ...

  3. Server asks us to fall back to SIMPLE auth, but this client is configured to only allow secure connections.

    我是在flume向hdfs 写(sink)数据时遇到的这个错误. Server (是指hdfs) asks us to fall back to SIMPLE auth, but this clien ...

  4. 10. Software, Software Engineering, water fall (瀑布模型),Code Complete等名词的来源

    ①.Software-软件”一词是20世纪60年代才出现的,软件Software——1958年由贝尔实验室的著名统计学家John Tukey 提出软件与硬件一起构成完整的计算机系统,它们是相互依存,缺 ...

  5. Mozilla Brick:一个Web组件Polyfill库

    Web组件是一个W3C规范,它旨在使Web开发人员能够定义具有非常丰富的视觉效果和高可交互性且易于组合的小组件.Brick库提供了新的自定义HTML标签,从而抽象了用户常用接口模式.在浏览器本身支持类 ...

  6. ZOJ 3299-Fall the Brick(线段树+离散化)

    题意: n个区间 ,给出区间的左右坐标 ,区间内填满宽度为1的箱子,有m个板子给出板子的高度和左右坐标(同高度不重叠) 所有箱子从上向下落,求每块板子能接到的箱子数. 分析: 首先给的区间很大,一开始 ...

  7. UVa 11587 - Brick Game

    称号:背景:brick game有N块,给你一个整数的定数S,两个人轮流木: 的木块数是集合S中存在的随意数字.取走最后木块的人获胜.无法取则对方获胜. 题干:如今让你先取,给你一个你的结果序列串T, ...

  8. 一语惊醒梦中人-《Before I Fall》

    I still remembered  I turned my attention to the title when I browsed in news by cellphone.I saw the ...

  9. 英语词汇(2)fall down,fall off和fall over

    一.fall down,fall off和fall over都表示"摔倒.跌倒"的意思,但它们各自的含义不同. 1.fall over 落在...之上, 脸朝下跌倒 fall ov ...

随机推荐

  1. (十四)json、pickle与shelve模块

    任何语言,都有自己的数据类型,那么不同的语言怎么找到一个通用的标准? 比如,后端用Python写的,前端是js,那么后端如果传一个dic字典给前端,前端肯定不认. 所以就有了序列化这个概念. 什么是序 ...

  2. kubernets之卷

    一 卷的由来以及种类和常用的卷的类型 前面介绍了大部分都是pod的管理以及在集群内部和集群外部如何访问pod,但是我们也了解到,pod是有生命周期的,当pod所在节点下线,或者等其他原因原因导致pod ...

  3. win32 sdk 环境下创建状态栏

    今天在学习状态栏,出了好多的问题,这里记录下. 要创建状态栏用:CreateStatusWindow CreateStatusWindow函数创建一个状态窗口,通常用于显示应用程序的状态.窗口通常显示 ...

  4. 阿里云VOD(三)

    一.视频播放器 参考文档:https://help.aliyun.com/document_detail/125570.html?spm=a2c4g.11186623.6.1083.1c53448bl ...

  5. pytest fixtures装饰器的使用

    一.pytest中可以使用@pytest.fixture 装饰器来装饰一个方法,被装饰方法的方法名可以作为一个参数传入到测试方法中.可以使用这种方式来完成测试之前的初始化,也可以返回数据给测试函数. ...

  6. 阿里云ECS hadoop+spark+zookeeper+hive code-server 集群搭建

    懒得重新排版然后发到博客了.用在线文档看吧 https://www.kdocs.cn/l/srV6o8rABW9V 用线上IDE(code-server)写scala的时候,出现BUG可以参考下面两篇 ...

  7. notepad文件对比

    一/格式转换 我用的是json,就以这个为例吧: 打开软件--插件--插件管理 搜索着两个进行安装,自动重启打开 将文件的代码做好,选择语言--J--可以找到json 用刚安装的插件--json vi ...

  8. 在这个应用中,我使用了 MQ 来处理异步流程、Redis 缓存热点数据、MySQL 持久化数据,还有就是在系统中调用另外一个业务系统的接口,对我的应用来说这些都是属于 RPC 调用,而 MQ、MySQL 持久化的数据也会存在于一个分布式文件系统中,他们之间的调用也是需要用 RPC 来完成数据交互的。

    在这个应用中,我使用了 MQ 来处理异步流程.Redis 缓存热点数据.MySQL 持久化数据,还有就是在系统中调用另外一个业务系统的接口,对我的应用来说这些都是属于 RPC 调用,而 MQ.MySQ ...

  9. 服务端 TCP 连接的 TIME_WAIT 过多问题的分析与解决

    https://mp.weixin.qq.com/s/VRQ_12tzy3gRYD091cI7Ew

  10. vscode 远程开发安装

    1 首先windows 有ssh  程序 2 没有的话就直接使用git  bash 登录到远程服务器开发机上 3 再开一个Git bash  执行 ssh-keygen.exe   生成秘钥    然 ...