poj 2828 Buy Tickets【线段树 单点更新】
倒着插,先不理解意思,后来看一篇题解说模拟一下
手动模拟一下就好理解了-----
不过话说一直写挫---一直改啊-----
好心塞------
#include <cstdio>
#include <ctime>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std; #define getmid(l,r) ((l) + ((r) - (l)) / 2) typedef long long LL;
const double eps = 1e-;
const int INF = ( << ) - ;
const int maxn = ; struct node{
int l,r,s;
}t[*maxn]; struct Edge{
int pos,val;
}a[maxn]; int n;
int ans[maxn]; void Push_up(int p){
t[p].s = t[p<<].s + t[p<<|].s;
} void Build_tree(int p,int l,int r){
t[p].l = l;
t[p].r = r;
if(l == r){
t[p].s = ;
return;
}
int mid = getmid(l,r);
Build_tree(p<<,l,mid);
Build_tree(p<<|,mid+,r);
Push_up(p);
} void update(int idx,int p){
if(t[p].l == t[p].r){
t[p].s--;
return;
}
int mid = getmid(t[p].l,t[p].r);
if(idx <= mid) update(idx,p<<);
else update(idx,p<<|);
Push_up(p);
} int query(int idx,int p){
if(t[p].l == t[p].r) return t[p].l;
if(t[p<<].s >= idx) return query(idx,p<<);
else return query(idx-t[p<<].s,p<<|);
} void solve(){
Build_tree(,,n);
// for(int i = 1;i <= 2*n;i++)
// printf("t[%d].l = %d t[%d].r = %d t[%d].s = %d\n",i,t[i].l,i,t[i].r,i,t[i].s);
for(int i = n;i >= ;i--){
int pos = a[i].pos;
int id = query(pos,);
ans[id] = a[i].val;
update(id,);
}
printf("%d",ans[]);
for(int i = ;i <= n;i++) printf(" %d",ans[i]);
printf("\n");
} int main(){
while(scanf("%d",&n) != EOF){
for(int i = ;i <= n;i++){
scanf("%d %d",&a[i].pos,&a[i].val);
a[i].pos++;
}
solve();
}
return ;
}
加油啊~~~~
poj 2828 Buy Tickets【线段树 单点更新】的更多相关文章
- POJ 2828 Buy Tickets(线段树单点)
https://vjudge.net/problem/POJ-2828 题目意思:有n个数,进行n次操作,每次操作有两个数pos, ans.pos的意思是把ans放到第pos 位置的后面,pos后面的 ...
- POJ - 2828 Buy Tickets (段树单点更新)
Description Railway tickets were difficult to buy around the Lunar New Year in China, so we must get ...
- poj 2828 Buy Tickets (线段树(排队插入后输出序列))
http://poj.org/problem?id=2828 Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissio ...
- POJ 2828 Buy Tickets (线段树 or 树状数组+二分)
题目链接:http://poj.org/problem?id=2828 题意就是给你n个人,然后每个人按顺序插队,问你最终的顺序是怎么样的. 反过来做就很容易了,从最后一个人开始推,最后一个人位置很容 ...
- POJ 2828 Buy Tickets 线段树 倒序插入 节点空位预留(思路巧妙)
Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 19725 Accepted: 9756 Desc ...
- 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] 要求最后依次输出队中各个人的属性 ...
- POJ 2828 Buy Tickets | 线段树的喵用
题意: 给你n次插队操作,每次两个数,pos,w,意为在pos后插入一个权值为w的数; 最后输出1~n的权值 题解: 首先可以发现,最后一次插入的位置是准确的位置 所以这个就变成了若干个子问题, 所以 ...
- poj 2828 Buy Tickets (线段树)
题目:http://poj.org/problem?id=2828 题意:有n个人插队,给定插队的先后顺序和插在哪个位置还有每个人的val,求插队结束后队伍各位置的val. 线段树里比较简单的题目了, ...
- poj 2892---Tunnel Warfare(线段树单点更新、区间合并)
题目链接 Description During the War of Resistance Against Japan, tunnel warfare was carried out extensiv ...
随机推荐
- 几个书本上不常见到的C语言函数
函数名称:getcwd #include <unistd.h> char *getcwd(char *buf, size_t size); 作用:把当前目录的绝对地址保存到 buf 中,b ...
- Selenium3+python自动化008-常用操作
一.元素常用操作 1. clear() 清除文本 2. send_keys() 模拟输入 3. click() 单击元素 4.get_attr ...
- CSS3实现1前端常用Loading效果
此页动画效果都是gif图的,不想用代码写的话,下载图片就可使用. 第1种效果: 代码如下 <div class="loading"> <span></ ...
- 当使用junit4 对spring框架中controller/service/mapper各层进行测试时,需要添加的配置
@RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration @ContextConfiguration(locations = {&quo ...
- atom安装与插件配置
下载:https://atom.io 安装 必备插件安装步骤: File-->settings-->Install-->Install Packages下面的输入框中输入要安装的插件 ...
- windows 查看端口号被占用
1.netstat -ano 2.tasklist | findstr xxx 3.进程管理杀掉
- NYIST 119 士兵杀敌(三)
士兵杀敌(三)时间限制:2000 ms | 内存限制:65535 KB难度:5 描述南将军统率着N个士兵,士兵分别编号为1~N,南将军经常爱拿某一段编号内杀敌数最高的人与杀敌数最低的人进行比较,计算出 ...
- HDU 2815
特判B不能大于等于C 高次同余方程 #include <iostream> #include <cstdio> #include <cstring> #includ ...
- 【LeetCode-面试算法经典-Java实现】【120-Triangle(三角形)】
[120-Triangle(三角形)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a triangle, find the minimum path s ...
- 【机房重构】—上机&订餐
前几天通过UML图中的时序图.让我对于机房重构中的每一条线理解的更加清晰.曾经认为上机特别的乱,在一次偶遇中,得知了原来它能够转化成我们平时订餐.以下就听我说一说上机&订餐的故事吧! 又是发生 ...