首页 > 其他分享 >POJ 2828(线段树 单点更新)

POJ 2828(线段树 单点更新)

时间:2023-08-15 17:38:00浏览次数:41  
标签:rt 单点 val int tree queue POJ 2828 was


Buy Tickets


Time Limit: 4000MS

 

Memory Limit: 65536K

Total Submissions: 16466

 

Accepted: 8201


Description


Railway tickets were difficult to buy around the Lunar New Year in China, so we must get up early and join a long queue…

The Lunar New Year was approaching, but unluckily the Little Cat still had schedules going here and there. Now, he had to travel by train to Mianyang, Sichuan Province for the winter camp selection of the national team of Olympiad in Informatics.

It was one o’clock a.m. and dark outside. Chill wind from the northwest did not scare off the people in the queue. The cold night gave the Little Cat a shiver. Why not find a problem to think about? That was none the less better than freezing to death!

People kept jumping the queue. Since it was too dark around, such moves would not be discovered even by the people adjacent to the queue-jumpers. “If every person in the queue is assigned an integral value and all the information about those who have jumped the queue and where they stand after queue-jumping is given, can I find out the final order of people in the queue?” Thought the Little Cat.


Input


There will be several test cases in the input. Each test case consists of N + 1 lines where N (1 ≤ N ≤ 200,000) is given in the first line of the test case. The next N lines contain the pairs of values Posi and Valiin the increasing order of i (1 ≤ i ≤ N). For each i, the ranges and meanings of Posi and Vali are as follows:

  • Posi ∈ [0, i − 1] — The i-th person came to the queue and stood right behind the Posi-th person in the queue. The booking office was considered the 0th person and the person at the front of the queue was considered the first person in the queue.
  • Vali ∈ [0, 32767] — The i-th person was assigned the value Vali.

There no blank lines between test cases. Proceed to the end of input.


Output


For each test cases, output a single line of space-separated integers which are the values of people in the order they stand in the queue.


Sample Input


4 0 77 1 51 1 33 2 69 4 0 20523 1 19243 1 3890 0 31492


Sample Output


77 33 69 51 31492 20523 3890 19243


Hint


The figure below shows how the Little Cat found out the final order of people in the queue described in the first test case of the sample input.



POJ 2828(线段树 单点更新)_ide


Source


POJ Monthly--2006.05.28, Zhu, Zeyuan


//做了这么多线段树了竟然还是这么弱。。。


//单点更新从后往前找空位


#include <iostream>
#include <stdio.h>
using namespace std;
int pos[200010],val[200010];
struct Node
{
    int l,r;
    int cnt,val;
}tree[200000*4];

void Buildtree(int rt,int l,int r)
{
    tree[rt].l=l;
    tree[rt].r=r;
    tree[rt].cnt=1;
    if(l!=r)
    {
        Buildtree(2*rt,l,(l+r)/2);
        Buildtree(2*rt+1,(l+r)/2+1,r);
        tree[rt].val=-1;
        tree[rt].cnt=tree[2*rt].cnt+tree[2*rt+1].cnt;
    }
}
void Update(int rt,int pos,int val)
{
    if(pos==1&&tree[rt].l==tree[rt].r)
    {
        tree[rt].val=val;
        tree[rt].cnt=0;
        return;
    }
    if(pos<=tree[2*rt].cnt)
        Update(2*rt,pos,val);
    else
        Update(2*rt+1,pos-tree[2*rt].cnt,val);
    tree[rt].cnt=tree[2*rt].cnt+tree[2*rt+1].cnt;
}
void print(int rt,int l,int r)
{
    if(l!=r)
    {
        print(2*rt,l,(l+r)/2);
        print(2*rt+1,(l+r)/2+1,r);
    }
    else
        if(tree[rt].l==tree[rt].r&&tree[rt].l==1)
            printf("%d",tree[rt].val);
        else
            printf(" %d",tree[rt].val);
}
int main()
{
    int t;
    while(~scanf("%d",&t))
    {
        Buildtree(1,1,t);
        for(int i=0;i<t;i++)
            scanf("%d%d",&pos[i],&val[i]);
        for(int i=t-1;i>=0;i--)
            Update(1,pos[i]+1,val[i]);
        print(1,1,t);
        printf("\n");
    }
    return 0;
}




标签:rt,单点,val,int,tree,queue,POJ,2828,was
From: https://blog.51cto.com/u_3936220/7091375

相关文章

  • Spring Authorization Server (十一)单点登录
    前段时间有人问到单点登录如何实现,那本篇就来介绍一下单点登录的实现。由于单点登录涉及到多个客户端,本篇的客户端就以订单服务、商品服务为例。以下是单点登录中,订单服务、商品服务、认证服务器的交互时序图。认证服务器搭建认证服务的搭建可以参照本系列中的《SpringAuthorizati......
  • Spring Boot + Vue3前后端分离实战wiki知识库系统<十二>--用户管理&单点登录开发一
    目标:在上一次https://www.cnblogs.com/webor2006/p/17533745.html我们已经完成了文档管理的功能模块开发,接下来则开启新模块的学习---用户登录,这块还是有不少知识点值得学习的,先来看一下整体的效果,关于效果官网有一个体验地址:wiki.courseimooc.com,如下:其效果也是人人熟知的,下面......
  • vue 实现动态表单点击新增 增加一行输入框
    点击增加后会新增一行,点击每行后面的删除图标则会删除该行,新增按钮只会出现在最后一行<el-col:span="12"class="mb20"> <el-form-item :label="index==0?'选择原材料':''" v-for="(item,index)inform.productItemList"......
  • Abp vNext单点登录
    AbpvNext单点登录使用AbpvNext6.0分析AbpvNext说OpenIddict是支持单点登录的,不过我找不到相关内容OpenIddictmoduleprovidesanintegrationwiththeOpenIddictwhichprovidesadvancedauthenticationfeatureslikesinglesign-on,singlelog-out,andAPIacces......
  • POJ-3619 Speed Reading
    POJ-3619SpeedReading#include<iostream>usingnamespacestd;typedeflonglongll;#defineIOSios::sync_with_stdio(0);cin.tie(0);cout.tie(0);//#defineiosios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);constintN=1e6+10;......
  • POJ 1392 Ouroboros Snake
    \(POJ\)\(1392\)-\(Ouroboros\)\(Snake\)//这道题和上面那道题几乎一样,算是变形题把,这道题要求构造的01字符串就是必须是字典序最小的,//在上面那道题的注意下建边的顺序即可.因为是链式前向星法,应该大边在前。\(Code\)#include<iostream>#include<algorithm>#......
  • Spring Boot 文件夹用途 DAO、DTD、VIEW、POJO
    DAO文件夹:用于存放数据访问对象(DataAccessObject),这些类用于封装对数据库的访问和操作,提供了一种与底层数据存储交互的接口。DAO层负责处理数据的持久化和检索,将数据操作与业务逻辑解耦。DTO文件夹:用于存放数据传输对象(DataTransferObject),这些类用于在不同层之间传输数据......
  • POJ 2513 Colored Sticks
    \(POJ\)\(2513\)\(Colored\)\(Sticks\)一、题目描述一堆木棍左右两端涂有颜色,相同颜色的可以连接在一起,问所有木棍能否都连上二、解题代码#include<map>#include<queue>#include<stack>#include<string>#include<vector>#include<stdio.h>#include<std......
  • POJ 1041 John's trip
    \(POJ\)\(1041\)\(John's\)\(trip\)一、题目大意多组数据,输入\(x,y,z\),表示结点\(x\)和结点\(y\)之间有一条序号为\(z\)的边,如果这个无向图中存在欧拉回路,就输出字典序最小的欧拉回路,如果不存在欧拉回路就输出Roundtripdoesnotexist.。当输入00表示一组数据输入结束......
  • POJ 3020 Antenna Placement
    \(POJ\)\(3020\)\(Antenna\)\(Placement\)一、题目描述*--代表城市,o--代表空地给城市安装无线网,一个无线网最多可以覆盖两座城市,问覆盖所有城市最少要用多少无线。公式:最小路径覆盖=总节点数-最大匹配数我们要尽可能多的建立能覆盖两个城市的基站(二分匹配最大匹配),剩下的......