POJ_2828_Buy Tickets
题意:插队问题;
2016.5.20,复习这道题。
总结:线段树基础不牢,建树,更新尚不熟悉,注意加强理解记忆。
主要理解:(单点更新,逆序插入)
发生插队时,前面的队伍是连续没有空位的,即pos:2,1,这种情况不会出现,至少应该为pos:1,2,1
插入顺序是逆序的(最后插入的val的位置不会再发生变化),如果正序插入则每个val的顺序是动态的。
插入pos,那么在pos这个位置之前应该还有pos-1个空位。
访问右节点的时候注意pos要修改,改为pos-sum[rt],即整个线段的第pos个空位,在下一个右儿子那的第pos-sum[rt]个空位。
void Insert(int pos,int val,int l,int r,int rt)
{
if(l==r)
{
spare[rt]=0;
seq[l]=val;
return;
}
int mid=(r+l)>>1;
if(pos<=spare[rt<<1])
Insert(pos,val,l,mid,rt<<1);
else
Insert(pos-spare[rt<<1],val,mid+1,r,rt<<1|1);
PushUp(rt);
}
代码:
#include<iostream>
#include<cstdio>
using namespace std; #define N 200005 int spare[N<<2];
int seq[N]; void PushUp(int rt)
{
spare[rt]=spare[rt<<1]+spare[rt<<1|1];
} void build(int l,int r,int rt)
{
if(l==r)
{
spare[rt]=1;
return;
}
int mid=(l+r)>>1;
build(l,mid,rt<<1);
build(mid+1,r,rt<<1|1);
PushUp(rt);
} void Insert(int pos,int val,int l,int r,int rt)
{
if(l==r)
{
spare[rt]=0;
seq[l]=val;
return;
}
int mid=(r+l)>>1;
if(pos<=spare[rt<<1])
Insert(pos,val,l,mid,rt<<1);
else
Insert(pos-spare[rt<<1],val,mid+1,r,rt<<1|1);
PushUp(rt);
} int main()
{
int n,p[N],v[N];
while(scanf("%d",&n)!=EOF)
{
build(1,n,1);
int i;
for(i=1;i<=n;i++)
{
scanf("%d%d",&p[i],&v[i]);
p[i]++;
}
for(i=n;i>0;i--)
{
Insert(p[i],v[i],1,n,1);
}
for(i=1;i<=n;i++)
{
if(i!=n)
printf("%d ",seq[i]);
else
printf("%d\n",seq[i]);
}
/*for(i=1;i<=n;i++) //如果这样输出就会超时,线段树容易超时
{
printf("%d",seq[i]);
if(i!=n)
printf(" ");
else
printf("\n");
}*/
} return 0; }
POJ_2828_Buy Tickets的更多相关文章
- POJ2828 Buy Tickets[树状数组第k小值 倒序]
Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 19012 Accepted: 9442 Desc ...
- ACM: FZU 2112 Tickets - 欧拉回路 - 并查集
FZU 2112 Tickets Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u P ...
- Tickets——H
H. Tickets Jesus, what a great movie! Thousands of people are rushing to the cinema. However, this i ...
- POJ 2828 Buy Tickets(线段树 树状数组/单点更新)
题目链接: 传送门 Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Description Railway tickets were d ...
- 【poj2828】Buy Tickets
Description Railway tickets were difficult to buy around the Lunar New Year in China, so we must get ...
- [poj2828] Buy Tickets (线段树)
线段树 Description Railway tickets were difficult to buy around the Lunar New Year in China, so we must ...
- POJ 2828 Buy Tickets
Description Railway tickets were difficult to buy around the Lunar New Year in China, so we must get ...
- Buy Tickets(线段树)
Buy Tickets Time Limit:4000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
- 【poj2828】Buy Tickets 线段树 插队问题
[poj2828]Buy Tickets Description Railway tickets were difficult to buy around the Lunar New Year in ...
随机推荐
- FFmpeg的HEVC解码器源码简单分析:概述
===================================================== HEVC源码分析文章列表: [解码 -libavcodec HEVC 解码器] FFmpeg ...
- vSphere,ESXi,vCenter之间的关系
vSphere是什么? vSphere 是VMware公司公布的一整套产品包,包括类似于VMware ESXi hypervisor.VMware vCenter Server等产品 ESXi是什么? ...
- muduo源代码分析--Reactor模式在muduo中的使用
一. Reactor模式简单介绍 Reactor释义"反应堆",是一种事件驱动机制.和普通函数调用的不同之处在于:应用程序不是主动的调用某个API完毕处理.而是恰恰相反.React ...
- oracle 11g GRID 中 关于 OLR 须要知道的一些内容
oracle 11g GRID 中 关于 OLR 须要知道的一些内容 1.检查olr 的状态: [root@vmrac1 ~]# ocrcheck -local Status of Oracle ...
- MySQL-插入数据(INSERT)
Insert语句可将一行或多行插入到表中. INSERT语法: INSERT INTO table(column1,column2...) VALUES (value1,value2,...); 首先 ...
- web 开发之js---js 实现网页中播放wav的一种方法(flash播放器)
http://blog.csdn.net/whumr1/article/details/6948160
- cc1: error: bad value (armv5) for -march= switch【转】
本文转载自:https://stackoverflow.com/questions/23871924/cc1-error-bad-value-armv5-for-march-switch Ask Qu ...
- centos ifconfig 无法使用问题
centos ifconfig 无法使用问题 # ifconfig bash: ifconfig: command not found # yum search ifconfig Loaded plu ...
- Wedding(2-SAT)
传送门 稍微复杂一点的2-SAT. 读题之后发现有以下限制: 1.每一对夫妻(包括新郎和新娘)不能坐在桌子的一侧. 2.对于一些给定的非法(自行脑补)的关系,这两个人不能坐在新娘的同一侧. 因为每个人 ...
- TI BLE: Advertisement
#define GAPROLE_ADVERT_ENABLED 0x305 //!< Enable/Disable Advertising. Read/Write. Size is uint8. ...