题意:插队问题;

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的更多相关文章

  1. POJ2828 Buy Tickets[树状数组第k小值 倒序]

    Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 19012   Accepted: 9442 Desc ...

  2. ACM: FZU 2112 Tickets - 欧拉回路 - 并查集

     FZU 2112 Tickets Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u P ...

  3. Tickets——H

    H. Tickets Jesus, what a great movie! Thousands of people are rushing to the cinema. However, this i ...

  4. POJ 2828 Buy Tickets(线段树 树状数组/单点更新)

    题目链接: 传送门 Buy Tickets Time Limit: 4000MS     Memory Limit: 65536K Description Railway tickets were d ...

  5. 【poj2828】Buy Tickets

    Description Railway tickets were difficult to buy around the Lunar New Year in China, so we must get ...

  6. [poj2828] Buy Tickets (线段树)

    线段树 Description Railway tickets were difficult to buy around the Lunar New Year in China, so we must ...

  7. POJ 2828 Buy Tickets

    Description Railway tickets were difficult to buy around the Lunar New Year in China, so we must get ...

  8. Buy Tickets(线段树)

     Buy Tickets Time Limit:4000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit ...

  9. 【poj2828】Buy Tickets 线段树 插队问题

    [poj2828]Buy Tickets Description Railway tickets were difficult to buy around the Lunar New Year in ...

随机推荐

  1. 李洪强iOS开发之动态获取UILabel的bounds

    李洪强iOS开发之动态获取UILabel的bounds 在使用UILabel存放字符串时,经常需要获取label的长宽数据,本文列出了部分常用的计算方法. 1.获取宽度,获取字符串不折行单行显示时所需 ...

  2. python开发【第1篇】【基础知识】

    1.python解释执行原理 python代码——字节码——机器码——计算机 每次运行都要进行转换成字节码,然后再有虚拟机把字节码转换成机器语言,最后才能在硬件上运行. 2.python编码 unic ...

  3. iOS 保存视频AVAssetWriter

    错误的CMTime导致保存的视频无效,比如: frameTime CMTime 1122 600ths of a second value CMTimeValue 1122timescale CMTi ...

  4. ugc pgc ogc web2.0 mgc

    http://yjy.people.com.cn/n/2014/0120/c245079-24169402.html machine

  5. D1 模拟赛

    T1 note 数组开小 菜的真实 60分 题目大意: 一个字符串 分成若干段 使每段内都没有重复的字符 求最少的段数 思路: 可以贪心 #include<iostream> #inclu ...

  6. [noip模拟赛]算算数

    https://www.zybuluo.com/ysner/note/1298755 题面 有一天小胡同学看到了一种表达式.这个表达式有四个变量\(A,B,C,D\).这四 个变量都只有\(0\)和\ ...

  7. ubuntu 14.04中: 像ubuntu16.04 一样可以在文件夹内打开此路径下的shell

    sudo apt-get install nautilus-open-terminal 然后重启 ok!

  8. 使用IntelliJ IDEA 创建Maven项目(入门)

    一. 下载Maven 下载地址:http://maven.apache.org/download.cgi tar.gz压缩格式用于unix操作系统,而zip用于windows的操作系统,但在windo ...

  9. UVaLive 6680 Join the Conversation (DP)

    题意:给出n条发言,让你求最大的交流长度并输出标记顺序. 析:这个题要知道的是,前面的人是不能at后面的人,只能由后面的人at前面的,那就简单了,我们只要更新每一层的最大值就好,并不会影响到其他层. ...

  10. Linux 上安装 Node.js

    Linux 上安装 Node.js 直接使用已编译好的包(在个人阿里云服务器47.100.6.106上安装) Node 官网已经把 linux 下载版本更改为已编译好的版本了,我们可以直接下载解压后使 ...