POJ 2828Buy Tickets
题目大意是说有n个插入操作,每次把B插入到位置A,原来A以后的全部往后移动1,球最后的序列
tree里保存的应该是这整个区间还有多扫个位置可以插入数据,那么线段树里从后往前扫描依次插入数据
比如现在吧B插入到A位置,如果整个区间左侧还有<A个位置可以插入数据,那么就只能将其放到整个区间的右侧,递归下去就可以了
void update(int k, int L, int R, int x)
{
if(L == R) { pre[k] = r; ans[L] = val; return ; } int mid = (L+R)>>; if(x <= pre[k<<]) update(lson, x);//左侧的多余x else update(rson, x-pre[k<<]);//否则往右 pre[k] = pre[k<<] + pre[k<<|];
}
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 1e9
#define inf (-((LL)1<<40))
#define lson k<<1, L, mid
#define rson k<<1|1, mid+1, R
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
#define FOPENIN(IN) freopen(IN, "r", stdin)
#define FOPENOUT(OUT) freopen(OUT, "w", stdout)
template<class T> T CMP_MIN(T a, T b) { return a < b; }
template<class T> T CMP_MAX(T a, T b) { return a > b; }
template<class T> T MAX(T a, T b) { return a > b ? a : b; }
template<class T> T MIN(T a, T b) { return a < b ? a : b; }
template<class T> T GCD(T a, T b) { return b ? GCD(b, a%b) : a; }
template<class T> T LCM(T a, T b) { return a / GCD(a,b) * b; } typedef __int64 LL;
//typedef long long LL;
const int MAXN = ;
const int MAXM = ;
const double eps = 1e-;
const LL MOD = ; int N, pre[MAXN<<];
int id[MAXN], num[MAXN], ans[MAXN];
int r, val; int buildTree(int k, int L, int R)
{
if(L == R) return pre[k] = ;
int mid = (L+R)>>;
return pre[k] = buildTree(lson) + buildTree(rson);
} void update(int k, int L, int R, int x)
{
if(L == R) { pre[k] = r; ans[L] = val; return ; } int mid = (L+R)>>; if(x <= pre[k<<]) update(lson, x); else update(rson, x-pre[k<<]); pre[k] = pre[k<<] + pre[k<<|];
} int main()
{
while(~scanf("%d", &N))
{
buildTree(, , N);
for(int l=;l<=N;l++)
scanf("%d %d", &id[l], &num[l]);
r = ;
for(int i=N;i>;i--)
{
val = num[i];
update(, , N, id[i] + );
}
for(int i=;i<=N;i++) printf("%d%c", ans[i], i==N?'\n':' ');
}
return ;
}
POJ 2828Buy Tickets的更多相关文章
- POJ 2828Buy Tickets(线段树的单点维护)
Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 20462 Accepted: 10096 Des ...
- 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(线段树)
题意: 有n个人,每人有一定的价值,给n个安排,每次安排有两个数 p,v p是这个人前面人的个数 (直接插在第p个人后面其他人后移),v是它的价值,n个安排后 求最终的价值序列. 分析: 越在后面的安 ...
- poj Buy Tickets
题目链接:http://poj.org/problem?id=2828 类似的题目:http://www.cnblogs.com/lovychen/p/3674048.html 测试数据: in: 4 ...
- 【POJ】2828 Buy Tickets(线段树+特殊的技巧/splay)
http://poj.org/problem?id=2828 一开始敲了个splay,直接模拟. tle了.. 常数太大.. 好吧,说是用线段树.. 而且思想很拽.. (貌似很久以前写过貌似的,,) ...
- poj 2828 Buy Tickets (线段树(排队插入后输出序列))
http://poj.org/problem?id=2828 Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissio ...
- POJ 2828 Buy Tickets(排队问题,线段树应用)
POJ 2828 Buy Tickets(排队问题,线段树应用) ACM 题目地址:POJ 2828 Buy Tickets 题意: 排队买票时候插队. 给出一些数对,分别代表某个人的想要插入的位 ...
- poj 2828 Buy Tickets(树状数组 | 线段树)
题目链接:poj 2828 Buy Tickets 题目大意:给定N,表示有个人,给定每一个人站入的位置,以及这个人的权值,如今按队列的顺序输出每一个人的权值. 解题思路:第K大元素,非常巧妙,将人入 ...
- poj 2828 Buy Tickets 树状数组
Buy Tickets Description Railway tickets were difficult to buy around the Lunar New Year in China, so ...
随机推荐
- POJ 3628 Bookshelf 2【01背包】
题意:给出n头牛的身高,以及一个书架的高度,问怎样选取牛,使得它们的高的和超过书架的高度最小. 将背包容量转化为所有牛的身高之和,就可以用01背包来做=== #include<iostream& ...
- watch 命令实时命令执行监控
watch 命令 watch -d -n 1 'df; ls -FlAt /path' 在使用这条命令时你需要替换其中的 /path 部分,watch 是实时监控工具,-d 参数会高亮 显示变化的 ...
- LA 3635 Pie 派 NWERC 2006
有 f + 1 个人来分 n 个圆形派,每个人得到的必须是一整块派,而不是几块拼在一起,并且面积要相同.求每个人最多能得到多大面积的派(不必是圆形). 这题很好做,使用二分法就OK. 首先在读取所有派 ...
- 不要随随便便的distinct和order by
相关查询非常慢,通过程序拿到了相关sqlexplainexplain SELECT DISTINCT(o.orders_id), o.oa_order_id, customers_email_addr ...
- Java中原子类的实现
Java提供的原子类是靠sun基于CAS实现的,CAS是一种乐观锁.关于乐观锁与悲观锁. 原子变量类相当于一种泛化的volatile变量,能够支持原子的和有条件的读-改-写操作.AtomicInteg ...
- 小结JS中的OOP(下)
关于JS中OOP的具体实现,许多大神级的JS专家都给出了自己的方案. 一:Douglas Crockford 1.1 Douglas Crockford实现的类继承 /** * 原文地址:http:/ ...
- [转] 接触C# 反射 2
逆心 原文 反射基础,2013-4. 反射用于在程序运行过程中,获取类里面的信息或发现程序集并运行的一个过程.通过反射可以获得.dll和.exe后缀的程序集里面的信息.使用反射可以看到一个程序集内部的 ...
- PHP实现的Mysql读写分离
本代码是从uchome的代码修改的,是因为要解决uchome的效率而处理的.这个思维其实很久就有了,只是一直没有去做,相信也有人有同样的想法,如果有类似的,那真的希望提出相关的建议.封装的方式比较简单 ...
- 【LeetCode 234】Palindrome Linked List
Given a singly linked list, determine if it is a palindrome. 思路: 用快慢指针找到链表中点,反转后半部分链表,然后与前半部分进行匹配,随后 ...
- SynchronousQueue
SynchronousQueue是一个没有数据缓冲的BlockingQueue,生产者线程对其的插入操作put必须等待消费者的移除操作take,反过来也一样. 不像ArrayBlockingQueue ...