线段树(单点更新) POJ 2828 Buy tickets
2024-08-30 14:53:07
/*
结点存储下面有几个空位
每次从根结点往下找找到该插入的位置,
同时更新每个节点的值
*/
#include <cstdio>
#define lson l, m, rt << 1
#define rson m+1, r, rt << 1 | 1 const int MAX_N = + ;
int pos[MAX_N], val[MAX_N];
int sum[MAX_N << ];
int que[MAX_N];
int id; void build(int l, int r, int rt)
{
sum[rt] = r - l + ; //节点有多少个空位置
if (l == r) return ;
int m = (l + r) >> ;
build (lson);
build (rson);
} void update(int p, int l, int r, int rt)
{
sum[rt]--; //插入一个减少一个空位置
if (l == r) //找到了传递该位置
{
id = l;
return ;
}
int m = (l + r) >> ;
if (p <= sum[rt<<])
{
update (p, lson);
}
else
{
p -= sum[rt<<]; //减去
update (p, rson);
}
} int main(void) //POJ 2828 Buy tickets
{
//freopen ("inE.txt", "r", stdin);
int n; while (~scanf ("%d", &n))
{
build (, n, );
for (int i=; i<=n; ++i)
{
scanf ("%d%d", &pos[i], &val[i]);
}
for (int i=n; i>=; --i) //最后插入的位置不变
{
update (pos[i]+, , n, );
que[id] = val[i];
}
printf ("%d", que[]);
for (int i=; i<=n; ++i)
{
printf (" %d", que[i]);
}
puts ("");
} return ;
}
最新文章
- HTML5中id可以用数字开头,但在css中不能正常使用
- 常用的JavaScript模式
- html5 video标签兼容性与自定义控件
- 对redis客户端jedis2.8.0的进一步封装
- 中文简体windows CMD显示中文乱码解决方案
- C++程序设计实践指导1.2二维数组的操作运算改写要求实现
- 批量 GBK 转 UTF8 java
- 分页:T-SQL存储过程和EF存储过程的使用
- 重读The C programming Lanuage 笔记二:运算符优先级
- MongoDB数据库的数据类型和$type操作符
- Openresty 数据共享API.Data Sharing within an Nginx Worker
- C#以管理员用户打开某个程序
- [No0000FB]C# 命名空间(Namespace)
- Spring framework3.2整合hibernate4.1报错:No Session found for current thread
- 下载8000首儿歌的python代码
- springboot 注解版案例
- ubuntu16.04中开启和关闭防火墙
- Linxu内核参数详解
- 年假小 Plan
- Python 爬虫 学习一