题意:插队问题;

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. MapReduce的Reduce side Join

    1. 简单介绍 reduce side  join是全部join中用时最长的一种join,可是这样的方法可以适用内连接.left外连接.right外连接.full外连接和反连接等全部的join方式.r ...

  2. UVa10048_Audiophobia(最短路/floyd)(小白书图论专题)

    解题报告 题意: 求全部路中最大分贝最小的路. 思路: 类似floyd算法的思想.u->v能够有另外一点k.通过u->k->v来走,拿u->k和k->v的最大值和u-&g ...

  3. 浅析java(多方面解读)

    昨天我简单的说了一下我的编程学习之路.假设你热爱编程.而不是仅为了赚钱,我想我的经历或许会给你带来一定的启示,假设你还没有看.请先慢慢读完我的编程学习之路,您肯定会有还有一番体会的.. 好了.废话不多 ...

  4. [git push] rejecteded 问题的解决方法

    错误信息: hint: Updates were rejected because a pushed branch tip is behind its remote hint: counterpart ...

  5. Zend Studio配置Xdebug

    按照网上的教程一直没有配置好,上官网看到一句话, If you don't know which one you need, please refer to the custom installati ...

  6. 协方差矩阵与主成分分析PCA

    今天看论文,作者是用主成分分析(PCA)的方法做的.仔细学习了一下,有一篇博客写的很好,介绍的深入浅出! 协方差:http://pinkyjie.com/2010/08/31/covariance/ ...

  7. TableLayout与MigLayout

    最近新接触的两个Layout,另外之前用的GridBagLayoutHelper以及最近听说的Qt for java的QCSS据说也不错, 只是Qt的跨平台需要单独发布,假如使用QT for java ...

  8. C# 函数的传值与传址(转)

    http://www.cnblogs.com/mdnx/archive/2012/09/04/2671060.html using System; using System.Collections.G ...

  9. c++ swap函数

    swap(a,b)也就是把a和b的值互换. 头文件:#include<algorithm>,swap要加using namespace std:

  10. [C++ STL] 常用算法总结

    1 概述 STL算法部分主要由头文件<algorithm>,<numeric>,<functional>组成.要使用 STL中的算法函数必须包含头文件<alg ...