点更新用法很巧妙的一道题。倒序很容易的找到该人的位置,而update操作中需要不断的变化下标才能合理的插入。看了别人写的代码,学习了。

 #include <iostream>
#include <cstring>
#include <cstdio>
#include <map>
using namespace std; const int maxn = 2e5 + ;
int sumv[maxn << ], data[maxn]; void PushUp (int rt) {
sumv[rt] = sumv[rt << ] + sumv[rt << | ];
} void build (int l, int r, int rt) {
if (l == r) {
sumv[rt] = ;
return ;
}
int m = (l + r) / ;
build (l, m, rt << );
build (m + , r, rt << | );
PushUp (rt);
} void update (int p, int v, int l, int r, int rt) {
if (l == r) {
data[l] = v; sumv[rt] = ;
return ;
}
int m = (l + r) >> ;
if (sumv[rt * ] >= p) {
update (p, v, l, m, rt * );
} else {
update (p - sumv[rt * ], v, m + , r, rt * + );
}
PushUp(rt);
} int main () {
pair<int, int> p[maxn];
int n;
while (~scanf ("%d", &n)) {
for (int i = ; i < n; ++ i) {
scanf ("%d%d", &p[i].first, &p[i].second);
}
build (, n, );
for (int i = n - ; i >= ; -- i) {
update (p[i].first + , p[i].second, , n, );
}
for (int i = ; i <= n; ++ i) {
if (i == ) {
printf ("%d", data[i]);
} else {
printf (" %d", data[i]);
}
}
puts("");
}
return ;
}

【POJ】Buy Tickets(线段树)的更多相关文章

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

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

  2. POJ 2828 Buy Tickets 线段树 倒序插入 节点空位预留(思路巧妙)

    Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 19725   Accepted: 9756 Desc ...

  3. POJ 2828 Buy Tickets (线段树 or 树状数组+二分)

    题目链接:http://poj.org/problem?id=2828 题意就是给你n个人,然后每个人按顺序插队,问你最终的顺序是怎么样的. 反过来做就很容易了,从最后一个人开始推,最后一个人位置很容 ...

  4. POJ 2828 Buy Tickets | 线段树的喵用

    题意: 给你n次插队操作,每次两个数,pos,w,意为在pos后插入一个权值为w的数; 最后输出1~n的权值 题解: 首先可以发现,最后一次插入的位置是准确的位置 所以这个就变成了若干个子问题, 所以 ...

  5. POJ 2828 Buy Tickets(线段树&#183;插队)

    题意  n个人排队  每一个人都有个属性值  依次输入n个pos[i]  val[i]  表示第i个人直接插到当前第pos[i]个人后面  他的属性值为val[i]  要求最后依次输出队中各个人的属性 ...

  6. POJ 2828 Buy Tickets(线段树单点)

    https://vjudge.net/problem/POJ-2828 题目意思:有n个数,进行n次操作,每次操作有两个数pos, ans.pos的意思是把ans放到第pos 位置的后面,pos后面的 ...

  7. [poj2828] Buy Tickets (线段树)

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

  8. 【poj2828】Buy Tickets 线段树 插队问题

    [poj2828]Buy Tickets Description Railway tickets were difficult to buy around the Lunar New Year in ...

  9. poj-----(2828)Buy Tickets(线段树单点更新)

    Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 12930   Accepted: 6412 Desc ...

  10. Buy Tickets(线段树)

    Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 16607   Accepted: 8275 Desc ...

随机推荐

  1. python手记(39)

    #!/usr/bin/env python #-*- coding: utf-8 -*- #code:myhaspl@qq.com import cv2 import numpy as np fn=& ...

  2. oracle10.2 dblink impd 同库不同用户复制数据

    同库不同用户复制数据 1.授权用户导入表权限; SQL> grant exp_full_database to system; SQL> commit; 2.创建dblink; SQL&g ...

  3. lucene 使用教程

    原文转自:http://cloudera.iteye.com/blog/656459 1 lucene简介  1.1 什么是lucene  Lucene是一个全文搜索框架,而不是应用产品.因此它并不像 ...

  4. javascript 如何继承父类

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  5. iphone UIScrollView缩放

    allImageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 768, 1024)]; allImageScrol ...

  6. android布局属性具体解释

    RelativeLayout用到的一些重要的属性: 1:LinearLayout ( 线性布局 ) (里面仅仅能够有一个控件,而且不能设计这个控件的位置,控件会放到左上角) 线性布局分为水平线性和垂直 ...

  7. 【Linux】linux经常使用基本命令

    Linux中很多经常使用命令是必须掌握的,这里将我学linux入门时学的一些经常使用的基本命令分享给大家一下,希望能够帮助你们. 这个是我将鸟哥书上的进行了一下整理的,希望不要涉及到版权问题. 1.显 ...

  8. LVM管理

    一.步骤: 1.创建新的分区,并修改分区类型为8e 2.创建物理卷PV 3.将新建的PV添加到要扩展的VG中 4.用命令lvextend或lvresize来将新加入的PE添加到要扩展的LV中 5.用命 ...

  9. c#中关于virtual,override和new的理解

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...

  10. javascript之求最值

    求最值: var selections = $("#deliveryGridSalesOrGoods").datagrid('getRows'); var costPrice = ...