POJ 2828

题目大意是说有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的更多相关文章

  1. POJ 2828Buy Tickets(线段树的单点维护)

    Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 20462   Accepted: 10096 Des ...

  2. poj 2828--Buy Tickets(线段树)

    Description Railway tickets were difficult to buy around the Lunar New Year in China, so we must get ...

  3. POJ 2828-Buy Tickets(线段树)

    题意: 有n个人,每人有一定的价值,给n个安排,每次安排有两个数 p,v p是这个人前面人的个数 (直接插在第p个人后面其他人后移),v是它的价值,n个安排后 求最终的价值序列. 分析: 越在后面的安 ...

  4. poj Buy Tickets

    题目链接:http://poj.org/problem?id=2828 类似的题目:http://www.cnblogs.com/lovychen/p/3674048.html 测试数据: in: 4 ...

  5. 【POJ】2828 Buy Tickets(线段树+特殊的技巧/splay)

    http://poj.org/problem?id=2828 一开始敲了个splay,直接模拟. tle了.. 常数太大.. 好吧,说是用线段树.. 而且思想很拽.. (貌似很久以前写过貌似的,,) ...

  6. poj 2828 Buy Tickets (线段树(排队插入后输出序列))

    http://poj.org/problem?id=2828 Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissio ...

  7. POJ 2828 Buy Tickets(排队问题,线段树应用)

    POJ 2828 Buy Tickets(排队问题,线段树应用) ACM 题目地址:POJ 2828 Buy Tickets 题意:  排队买票时候插队.  给出一些数对,分别代表某个人的想要插入的位 ...

  8. poj 2828 Buy Tickets(树状数组 | 线段树)

    题目链接:poj 2828 Buy Tickets 题目大意:给定N,表示有个人,给定每一个人站入的位置,以及这个人的权值,如今按队列的顺序输出每一个人的权值. 解题思路:第K大元素,非常巧妙,将人入 ...

  9. poj 2828 Buy Tickets 树状数组

    Buy Tickets Description Railway tickets were difficult to buy around the Lunar New Year in China, so ...

随机推荐

  1. 【转】 ARM Linux 3.x的设备树(Device Tree)

    1.    ARM Device Tree起源 http://blog.csdn.net/21cnbao/article/details/8457546 Linus Torvalds在2011年3月1 ...

  2. JS改变input的value值不触发onchange事件解决方案 (转)

    方法(一)(转载的网络资料) 需要了解的知识     首先,我们需要了解onchange和onpropertychange的不同: IE下,当一个HTML元素的属性改变的时候,都能通过 onprope ...

  3. GrepCode

    /***************************************************************************** * GrepCode * 声明: * 最近 ...

  4. over-fitting、under-fitting 与 regularization

    机器学习中一个重要的话题便是模型的泛化能力,泛化能力强的模型才是好模型,对于训练好的模型,若在训练集表现差,不必说在测试集表现同样会很差,这可能是欠拟合导致:若模型在训练集表现非常好,却在测试集上差强 ...

  5. 部署HBase远程访问的问题集合(Eclipse)

    实现远程访问HBase,可以通过Eclipse开发工具方便进行代码调试. 为了方便jar包各种版本的管理,才用maven进行代码构建 首先,下载并安装maven以及M2Eclipse插件 其次,配置m ...

  6. OpenGL学习之路(一)

    1 引子 虽然是计算机科班出身,但从小对几何方面的东西就不太感冒,空间想象能力也较差,所以从本科到研究生,基本没接触过<计算机图形学>.为什么说基本没学过呢?因为好奇(尤其是惊叹于三维游戏 ...

  7. Swift语法

    Swift语法 标签(空格分隔): Swift 1.打印输出语句 println("Hello, Swift!") 注意每行代码后面无需添加分号作为结束 2.简单值 let---常 ...

  8. shell脚本中一些特殊符号

    在shell中常用的特殊符号罗列如下: # ;   ;; . , / \\ 'string'| !   $   ${}   $? $$   $* \"string\"* **   ...

  9. C# 使用NPlot绘图

    首先要将下载的NPlot.dll加到工具箱里,拖一个控件到窗体上,声明using NPlot. 一.入门 1. 对所绘的图进行打印与保存 private void print() { myPlot.P ...

  10. bjfu1299 stl使用

    题目超简单,我写解题报告是因为我的代码用了些STL,使代码很简洁. * * Author : ben */ #include <cstdio> #include <cstdlib&g ...