倒着插,倒着插,这道题是倒着插!

想一下如果 Posi 里面有若干个0,那么排在最前面的一定是最后一个0.

从后往前看,对于第i个数,就应该插在第Posi + 1个空位上,所以用线段树来维护区间空位的个数。

说一下那个坑爹的第56行的判断:

if(i > 1) printf(" ");

将输出的n个数用空格隔开,我感觉这是一个还算常用的写法啊,结果各种莫名TLE,加上输入挂也补救不回来。

去掉这个无谓的判断后,3594MS险过,加上输入挂3094MS,还算是起到了一定的加速作用。

 #include <cstdio>
#include <ctype.h> const int maxn = + ; int n, p[maxn], v[maxn], a[maxn];
int sum[maxn << ]; inline int Scan()
{
char c = getchar();
while(!isdigit(c)) c = getchar(); int x = ;
while(isdigit(c)) {
x = x * + c - '';
c = getchar();
}
return x;
} void build(int o, int L, int R)
{
if(L == R) { sum[o] = ; return; }
int M = (L + R) / ;
build(o<<, L, M);
build(o<<|, M+, R);
sum[o] = sum[o<<] + sum[o<<|];
} void update(int o, int L, int R, int p, int v)
{
if(L == R) { sum[o] = ; a[L] = v; return; }
int M = (L + R) / ;
if(sum[o<<] >= p) update(o<<, L, M, p, v);
else update(o<<|, M+, R, p-sum[o<<], v);
sum[o] = sum[o<<] + sum[o<<|];
} int main()
{
//freopen("in.txt", "r", stdin); while(scanf("%d", &n) == )
{
build(, , n);
for(int i = ; i < n; i++)
{
p[i] = Scan();
v[i] = Scan();
}
for(int i = n - ; i >= ; i--) update(, , n, p[i]+, v[i]);
printf("%d", a[]);
for(int i = ; i <= n; i++)
{
//if(i > 1) printf(" ");
printf(" %d", a[i]);
}
printf("\n");
} return ;
}

代码君

POJ 2828 (线段树 单点更新) Buy Tickets的更多相关文章

  1. poj 2828(线段树单点更新)

    Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 18561   Accepted: 9209 Desc ...

  2. POJ 2828 线段树单点更新 离线搞

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

  3. POJ 2886 线段树单点更新

    转载自:http://blog.csdn.net/sdj222555/article/details/6878651 反素数拓展参照:http://blog.csdn.net/ACdreamers/a ...

  4. poj 2892---Tunnel Warfare(线段树单点更新、区间合并)

    题目链接 Description During the War of Resistance Against Japan, tunnel warfare was carried out extensiv ...

  5. POJ 1804 Brainman(5种解法,好题,【暴力】,【归并排序】,【线段树单点更新】,【树状数组】,【平衡树】)

    Brainman Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 10575   Accepted: 5489 Descrip ...

  6. POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)

    POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...

  7. POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化)

    POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化) 题意分析 前置技能 线段树求逆序对 离散化 线段树求逆序对已经说过了,具体方法请看这里 离散化 有些数 ...

  8. HDU 1754 I Hate It 线段树单点更新求最大值

    题目链接 线段树入门题,线段树单点更新求最大值问题. #include <iostream> #include <cstdio> #include <cmath> ...

  9. HDU 1166 敌兵布阵(线段树单点更新)

    敌兵布阵 单点更新和区间更新还是有一些区别的,应该注意! [题目链接]敌兵布阵 [题目类型]线段树单点更新 &题意: 第一行一个整数T,表示有T组数据. 每组数据第一行一个正整数N(N< ...

随机推荐

  1. 8086CPU各寄存器的用途

    8086 有14个16位寄存器,这14个寄存器按其用途可分为(1)通用寄存器.(2)指令指针.(3)标志寄存器和(4)段寄存器等4类. 1.通用寄存器有8个, 又可以分成2组,一组是数据寄存器(4个) ...

  2. Unity3D战争迷雾效果

    原地址:http://liweizhaolili.blog.163.com/blog/static/16230744201431835652233/ 最近一直都在做Flash相关的东西,很久没有空搞U ...

  3. jquery json遍历和动态绑定事件

    <div id='tmpselectorList' style='border: 1px solid grey;max-height: 150px;position:absolute;text- ...

  4. Sqli-labs less 28a

    Less-28a 本关与28基本一致,只是过滤条件少了几个. http://127.0.0.1/sqllib/Less-28a/?id=100%27)unIon%0bsElect%0b1,@@base ...

  5. HDU 4006 The kth great number(multiset(或者)优先队列)

    题目 询问第K大的数 //这是我最初的想法,用multiset,AC了——好吧,也许是数据弱也有可能 //multiset运用——不去重,边插入边排序 //iterator的运用,插入的时候,如果是相 ...

  6. POJ 1411

    #include<iostream> #include<stdio.h> #include<math.h> #define MAXN 50000 using nam ...

  7. T_SQL基于列的逻辑表达式(case)

    本文摘自:http://www.cnblogs.com/kissdodog/p/3154371.html(感谢作者的分享,总结的很好) 基于列的逻辑表达式,其实就是CASE表达式.可以用在SELECT ...

  8. POJ 2007 Scrambled Polygon (简单极角排序)

    题目链接 题意 : 对输入的点极角排序 思路 : 极角排序方法 #include <iostream> #include <cmath> #include <stdio. ...

  9. 传说中的WCF(11):会话(Session)

    在标题中我加了一个大家都很熟悉的单词——Session,熟吧?玩过Web开发的朋友肯定在梦中都会见到她. 在Web中为什么要会话呢?毕竟每个用户在一个Web应用中可能不止进行一次操作,比如,某二手飞机 ...

  10. android-non-ui-to-ui-thread-communications-part-4-of-5

    In parts 1-3 of this series, I have explored three different means for an Android non-UI thread to c ...