Deque 加双指针

本篇博客介绍了一种使用Deque数据结构处理左右插入(L、R操作)和查询(?操作)的问题,其中涉及到如何维护序列的相对顺序,并在给定条件下计算将特定书籍放置于最左侧或最右侧所需的最小移除次数。题目来源于CF1066C,适合数据结构和算法爱好者阅读。
摘要由CSDN通过智能技术生成

CF1066C
题目描述
You have got a shelf and want to put some books on it.You are given q q q queries of three types:L id id id — put a book having index id id id on the shelf to the left from the leftmost existing book;R id id id — put a book having index id id id on the shelf to the right from the rightmost existing book;? id id id — calculate the minimum number of books you need to pop from the left or from the right in such a way that the book with index id id id will be leftmost or rightmost.You can assume that the first book you will put can have any position (it does not matter) and queries of type 3 3 3 are always valid (it is guaranteed that the book in each such query is already placed). You can also assume that you don’t put the same book on the shelf twice, so id id id s don’t repeat in queries of first two types.Your problem is to answer all the queries of type 3 3 3 in order they appear in the input.Note that after answering the query of type 3 3 3 all the books remain on the shelf and the relative order of books does not change.If you are Python programmer, consider using PyPy instead of Python when you submit your code.输入格式The first line of the input contains one integer q q q ( 1≤q≤2⋅105 1 \le q \le 2 \cdot 10^5 1≤q≤2⋅105 ) — the number of queries.Then q q q lines follow. The i i i -th line contains the i i i -th query in format as in the problem statement. It is guaranteed that queries are always valid (for query type 3 3 3 , it is guaranteed that the book in each such query is already placed, and for other types, it is guaranteed that the book was not placed before).It is guaranteed that there is at least one query of type 3 3 3 in the input.In each query the constraint 1≤id≤2⋅105 1 \le id \le 2 \cdot 10^5 1≤id≤2⋅105 is met.
输出格式Print answers to queries of the type 3 3 3 in order they appear in the input.
题意翻译题意描述您需要维护一个数据结构,支持以下三种操作:LLL ididid:在现在序列的左边插一个编号为ididid的物品RRR ididid:在现在序列的右边插一个编号为ididid的物品??? ididid:查询该点左面有几个元素,右面有几个元素,并取min输出输入输出格式:输入格式:第一行,一个整数qqq,表示操作数下面qqq行,每行2个数,表示一个操作输出格式对于每个“?”操作,输出一行一个整数,表示答案输入输出

样例输入 #
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值