POJ2828 Buy Tickets(线段树之插队问题)
插入到pos这个位置,如果当前pos无人,那最好了,直接把他插入即可。但如果pos这个位置有人了,从现实意义上讲,第i号人插入之后,相当于他

#include<stdio.h>
#include<algorithm>
using namespace std;
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1 const int maxn = ; int sum[maxn<<],pos[maxn],val[maxn],ans[maxn];
void PushUP(int rt)
{
sum[rt]=sum[rt<<]+sum[rt<<|];
}
void build(int l,int r,int rt)
{
if(l==r)
{
sum[rt]=;
return ;
}
int m = (l+r) >> ;
build(lson);
build(rson);
PushUP(rt);
}
int update(int pos,int l,int r,int rt)
{
sum[rt]--;
if(l==r)
{
return l;
} int m=(r+l) >> ;
if(sum[rt<<]>pos)
return update(pos,lson);
else
return update(pos-sum[rt<<],rson); }
int main( )
{
int n;
while(scanf("%d",&n)!=EOF)
{
for(int i= ; i<n ; i++)
scanf("%d%d",&pos[i],&val[i]);
build(,n,);
for(int i=n- ; i>= ; i--)
{
ans[update(pos[i],,n,)]=val[i];
}
for(int i= ; i<n ; i++)
{
printf("%d ",ans[i]);
}
printf("%d\n",ans[n]);
}
return ;
}
小贴士:在往右子的时候 update(pos-sum[rt<<1],rson); 为什么要pos-sum[rt<<1],rson:呢? 可以把结果在子叶点对应的位置看成是最终答案,例如我要排在第3为,而我1,2是左结点的,那我是不是就是要减去左结点的数值,在能是右结点的相对位置
POJ2828 Buy Tickets(线段树之插队问题)的更多相关文章
- poj2828 Buy Tickets (线段树 插队问题)
Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 22097 Accepted: 10834 Des ...
- [poj2828] Buy Tickets (线段树)
线段树 Description Railway tickets were difficult to buy around the Lunar New Year in China, so we must ...
- poj-----(2828)Buy Tickets(线段树单点更新)
Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 12930 Accepted: 6412 Desc ...
- POJ 2828 Buy Tickets(线段树·插队)
题意 n个人排队 每一个人都有个属性值 依次输入n个pos[i] val[i] 表示第i个人直接插到当前第pos[i]个人后面 他的属性值为val[i] 要求最后依次输出队中各个人的属性 ...
- [POJ2828]Buy Tickets(线段树,单点更新,二分,逆序)
题目链接:http://poj.org/problem?id=2828 由于最后一个人的位置一定是不会变的,所以我们倒着做,先插入最后一个人. 我们每次处理的时候,由于已经知道了这个人的位置k,这个位 ...
- 【poj2828】Buy Tickets 线段树 插队问题
[poj2828]Buy Tickets Description Railway tickets were difficult to buy around the Lunar New Year in ...
- poj 2828 Buy Tickets (线段树(排队插入后输出序列))
http://poj.org/problem?id=2828 Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissio ...
- Buy Tickets(线段树)
Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 16607 Accepted: 8275 Desc ...
- POJ 2828 Buy Tickets 线段树 倒序插入 节点空位预留(思路巧妙)
Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 19725 Accepted: 9756 Desc ...
- POJ 2828 Buy Tickets (线段树 or 树状数组+二分)
题目链接:http://poj.org/problem?id=2828 题意就是给你n个人,然后每个人按顺序插队,问你最终的顺序是怎么样的. 反过来做就很容易了,从最后一个人开始推,最后一个人位置很容 ...
随机推荐
- spring读取classpath目录下的配置文件通过表达式去注入属性值.txt
spring读取配置文件: 1. spring加载配置文件: <context:property-placeholder location="classpath:config/syst ...
- appium如何连接模拟器代码实例
from appium import webdriver def connect(self): self.desired_caps = {} self.desired_caps['platformNa ...
- Strophe.Status的所有值
ERROR: 0 CONNECTING: 1 CONNFAIL: 2 AUTHENTICATING: 3 AUTHFAIL: 4 CONNECTED: 5 DISCONNECTED: 6 DISCON ...
- springmvc 初始化参数绑定(使用属性编辑器) 来处理类型转换问题
处理一种日期格式 处理器中的写法: index.jsp中的写法: 处理多种日期格式: 处理器的写法: 自定义的属性编辑器: index.jsp的写法:
- bootstrap媒体查询常用写法
@media (max-width: 768px) { /*超小屏幕设备 手机*/ } @media (min-width: 768px) and (max-width: 992px) { /*小屏幕 ...
- C++面向对象类的实例题目二
题目描述: 编写一个程序,设计一个产品类Product,其定义如下: class Product { public: Product(char *n,int p,int q); //构造函数 ~Pro ...
- 51NOD 1616 最小集合
传送门 分析 不难发现集合中的数一定是集合内其它一堆数的$gcd$ 于是我们枚举$i$,统计原来集合中有几个数是$i$的倍数,设这个值为$f(i)$ 之后对于每个$i$如果不存在$f(x*i) = f ...
- 把Spark SQL的metadata存储到mysql
1:安装配置mysql yum install mysql mysql-server service mysqld start mysqladmin -u root password newpass ...
- Batch Normalization 与Dropout 的冲突
BN或Dropout单独使用能加速训练速度并且避免过拟合 但是倘若一起使用,会产生负面效果. BN在某些情况下会削弱Dropout的效果 对此,BN与Dropout最好不要一起用,若一定要一起用,有2 ...
- TinkerPop中的遍历:图的遍历步骤(1/3)
图遍历步骤(Graph Traversal Steps) 在最一般的层次上,Traversal<S,E>实现了Iterator,S代表起点,E代表结束.遍历由四个主要组成部分组成: Ste ...