题意:插队问题;

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. [Unity3D]Unity3D游戏开发之Lua与游戏的不解之缘(下)

    ---------------------------------------------------------------------------------------------------- ...

  2. Android学习笔记之Spinner下拉列表使用案例

    (1)两种方法提冲Spinner中的数据源:通过list集合,或者是通过xml文件进行配置 (2)布局代码例如以下: <RelativeLayout xmlns:android="ht ...

  3. CANopen——总线基本知识

    1. 总线标准 2. 获取索引和子索引 2fh,2bh,23h,40h等,是不是对应cs的不同值: 主站1280h的对象字典?1280h-sub2,得到client的COB-ID值: 根据收到的m-& ...

  4. ResolveUrl in external JavaScript file in asp.net project

    https://stackoverflow.com/questions/11263425/page-resolveurl-is-not-working-in-javascript The proble ...

  5. PL/SQL -->隐式游标(SQL%FOUND)

    PL/SQL -->隐式游标(SQL%FOUND) 分类: SQL/PLSQL 基础2010-12-22 16:23 4084人阅读 评论(0) 收藏 举报 sqlexceptionoracle ...

  6. Code First: 五大映射模式

    映射一 Mapping the Table-Per-Hierarchy (TPH) Inheritance 模型文件 using System.Data.Entity; using System.Da ...

  7. bzoj2705 [SDOI2012]Longge的问题——因数

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2705 一开始自己想了半天... 有了点思路:遍历 n 的因数 k,每个因数要预处理出 gcd ...

  8. JS动态加载JS

    1.直接document.write <script language="javascript">     document.write("<scrip ...

  9. openstack dnsmasq

    killall dnsmasq systemctl restart openstack-nova-compute /sbin/dnsmasq --conf-file=/var/lib/libvirt/ ...

  10. ubuntu 16.04 Eclipse 图标显示为 ?(已解决)

    这个问题挺好解决: sudo gedit /usr/share/applications/eclipse.desktop在这个文件中将Icon=/home/soyo/eclipse/icon.xpm, ...