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. 51nod1119 机器人走方格 V2

    终于学到了求组合数的正确姿势 //C(n+m-2,m-1) #include<cstdio> #include<cstring> #include<cctype> ...

  2. MySQL "replace into" 的坑

    MySQL 对 SQL 有很多扩展,有些用起来很方便,但有一些被误用之后会有性能问题,还会有一些意料之外的副作用,比如 REPLACE INTO. 比如有这样一张表: CREATE TABLE `au ...

  3. Session随便写的(抄书笔记)

    会话是web开发中常用的一种对象.会话是存在于服务器端的对象,因此会话超时是保证性能效率的必要手段,本章将学习几种常用的使会话失效的办法.大多数容器都使用cookie作为会话跟踪的基础,但是cooki ...

  4. activiti 引擎 数据库设计说明书

    1.结构设计 1.1.    逻辑结构设计 Activiti使用到的表都是ACT_开头的. ACT_RE_*: ’RE’表示repository(存储),RepositoryService接口所操作的 ...

  5. swun 1612 合并果子

      //思路:这题思路似乎很简单,每次取出最小的两个堆合并, //但是由于数据太大,不能采取每次进行排序的方式,所以 //想到用优先队列,以数据小的优先级更高为标准,但是 //优先队列中的数据默认情况 ...

  6. 【Python】类和对象、继承、使用文件、存储、异常、标准库(不懂)

    当你调用这个对象的方法MyObject.method(arg1, arg2)的时候,这会由Python自动转为MyClass.method(MyObject, arg1, arg2)——这就是self ...

  7. 选择符优先级-----:link伪类

    问题:请指出以下结构中,A标签内的字体颜色. <style> a{ color:#ccc}/* 灰色 */ .alink a{color:#F60}/* 橙色 */ h1 a{color: ...

  8. js COOKIE 记住帐号或者uuid

    当开始接到这个任务的时候,我对cookie还是没多少了解的,而uuid的生成也是一无所知.但是当你发现这个网址http://stackoverflow.com/questions/105034/how ...

  9. hdu 1527 取石子游戏(Wythoff Game)

    题意:Wythoff Game 思路:Wythoff Game #include<iostream> #include<stdio.h> #include<math.h& ...

  10. 使用python三方库xlrd解析excel数据

    excel是平常用的比较多的一种数据格式,而在自动化测试过程中,解析其数据以供脚本使用就是一个重要的工作,幸好已有现存的三方库供使用,而不必重新造轮子. 一.安装xlrd模块 到python官网下载h ...