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. bzoj2396: 神奇的矩阵

    与51nod1140一样.不过这题是多组数据的...坑.... #include<cstdio> #include<cstring> #include<cctype> ...

  2. 51nod1202 子序列个数

    看到a[i]<=100000觉得应该从这个方面搞.如果a[x]没出现过,f[x]=f[x-1]*2;否则f[x]=f[x-1]*2-f[pos[a[x]]-1];ans=f[n]-1,然后WA了 ...

  3. C#基本数据类型

    在第一章我们了解了C#的输入.输出语句后,我这一节主要是介绍C#的基础知识,本节的内容也是后续章节的基础,好的开端等于成功的一半.在你阅读完本章后,你就有足够的C#知识编写简单的程序了.但还不能使用继 ...

  4. ASP.NET 时间方法大全

    DateTime dt = DateTime.Now; //当前时间 DateTime startWeek = dt.AddDays(- Convert.ToInt32(dt.DayOfWeek.To ...

  5. You must SET PASSWORD before executing this statement解决

    [转载] MySql5.6操作时报错:You must SET PASSWORD before executing this statement解决 转载: http://blog.csdn.net/ ...

  6. js中substring和substr的用法

    js中substring和substr的用法 substring 方法用于提取字符串中介于两个指定下标之间的字符 substring(start,end) 开始和结束的位置,从零开始的索引 参数    ...

  7. 【Unity3D】模仿制作“神庙逃亡”吃金币后金币飞出屏幕效果

    [前言] 玩过“神庙逃亡”的应该都知道,这款游戏不论从游戏流畅度.人物动画.场景的管理都很棒. 自己也做了一款简单的跑酷游戏,实现了简单的吃金币效果,但是发现不好看,于是就想模仿“神庙逃亡”中的这个效 ...

  8. 【转】itunes connect 如何修改主要语言

    原文网址:http://blog.csdn.net/yuedong56/article/details/50662181 刚开始提交app,没有做国际化(本地化),提交的app只有简体中文一种语言,第 ...

  9. 输出流 写文件 文本 换行nextLine

      FileOutputStream   fos   =   new   FileOutputStream( "c:\\test.txt ");  String   nextLin ...

  10. GitHub的代码托管和使用方法

    原文  GitHub托管 借助GitHub托管你的项目代码 PS:话说自己注册了GitHub都很久了,却没有怎么去弄,现在系统学习一下,也把自己的学习经历总结下来share给大家,希望大家都能把Git ...