传送门:

题面:

C. Books Queries

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You have got a shelf and want to put some books on it.

You are given qq queries of three types:

  1. L idid — put a book having index idid on the shelf to the left from the leftmost existing book;
  2. R idid — put a book having index idid on the shelf to the right from the rightmost existing book;
  3. ? idid — 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 idid 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 33 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 idids don't repeat in queries of first two types.

Your problem is to answer all the queries of type 33 in order they appear in the input.

Note that after answering the query of type 33 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.

Input

The first line of the input contains one integer qq (1≤q≤2⋅1051≤q≤2⋅105) — the number of queries.

Then qq lines follow. The ii-th line contains the ii-th query in format as in the problem statement. It is guaranteed that queries are always valid (for query type 33, 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 33 in the input.

In each query the constraint 1≤id≤2⋅1051≤id≤2⋅105 is met.

Output

Print answers to queries of the type 33 in order they appear in the input.

Examples

input

Copy

8
L 1
R 2
R 3
? 2
L 4
? 1
L 5
? 1

output

Copy

1
1
2

input

Copy

10
L 100
R 100000
R 123
L 101
? 123
L 10
R 115
? 100
R 110
? 115

output

Copy

0
2
1

Note

Let's take a look at the first example and let's consider queries:

  1. The shelf will look like [1][1];
  2. The shelf will look like [1,2][1,2];
  3. The shelf will look like [1,2,3][1,2,3];
  4. The shelf looks like [1,2,3][1,2,3] so the answer is 11;
  5. The shelf will look like [4,1,2,3][4,1,2,3];
  6. The shelf looks like [4,1,2,3][4,1,2,3] so the answer is 11;
  7. The shelf will look like [5,4,1,2,3][5,4,1,2,3];
  8. The shelf looks like [5,4,1,2,3][5,4,1,2,3] so the answer is 22.

Let's take a look at the second example and let's consider queries:

  1. The shelf will look like [100][100];
  2. The shelf will look like [100,100000][100,100000];
  3. The shelf will look like [100,100000,123][100,100000,123];
  4. The shelf will look like [101,100,100000,123][101,100,100000,123];
  5. The shelf looks like [101,100,100000,123][101,100,100000,123] so the answer is 00;
  6. The shelf will look like [10,101,100,100000,123][10,101,100,100000,123];
  7. The shelf will look like [10,101,100,100000,123,115][10,101,100,100000,123,115];
  8. The shelf looks like [10,101,100,100000,123,115][10,101,100,100000,123,115] so the answer is 22;
  9. The shelf will look like [10,101,100,100000,123,115,110][10,101,100,100000,123,115,110];
  10. The shelf looks like [10,101,100,100000,123,115,110][10,101,100,100000,123,115,110] so the answer is 11.

题意:

有三种操作:(1)‘L’,num,将num放入最左端(2)‘R’,num,(3)‘?’,num,让你从求出最小从左边或右边去除多少个数才能取得数num。

题目分析:

最开始想得特别复杂,认为需要模拟一个动态向两边拓展的数组,并在每一次操作(1)(2)后用线段树对现在的整个区间的[L,R]+1,最后用map记录一下最小的在哪里就好了。

但是看到一堆人过了这个题后才意识到,题目显然没有这么复杂!!!不知道为什么会想到用所谓的线段树

    考虑到要用最小的代价将物品取出,故对于某一个物品,必定是在“?”询问前的最后一次放入的才是最优位置。因此我们只需要用数组对每个数记录一下最优解,最后分别用取(最优位置-最左端位置,以及最右端-最优位置)中的最小值即可。

代码:

#include <bits/stdc++.h>
#define maxn 400005
using namespace std;
int a[maxn];
char str[2];
int main()
{int t;scanf("%d",&t);int l=200000;int r=l-1;while(t--){int num;scanf("%s",str);scanf("%d",&num);if(str[0]=='L'){l--;a[num]=l;}else if(str[0]=='R'){r++;a[num]=r;}else{int res=min(a[num]-l,r-a[num]);cout<<res<<endl;}}return 0;
}

转载于:https://www.cnblogs.com/Chen-Jr/p/11007168.html

Codeforces 1066 C(思维)相关推荐

  1. codeforces 有意思的思维题 1 ~ 15

    codeforces 思维题 1.给定数组,求满足i < j and ai * aj = i + j的数对数量 2.第 i 步向前跳 i 步或后退 1 步 3.给两个点,求正方形的另两个点 4. ...

  2. codeforces数学1600day4[贪心数学公式推导CodeForces - 1151D ,思维CodeForces - 1085C,数论同余+组合计数 CodeForces - 1056B]

    A - Stas and the Queue at the Buffet CodeForces - 1151D 题目大意:就是给你n个人在排队,每个人都有一个ai值和bi值,每个人的不满意度就是f(i ...

  3. CodeForces - 364A Matrix(思维+数学)

    题目链接:点击查看 题目大意:给出一个长度为 n 的,只由十进制数字组成的字符串 s,可以构造出一个大小为 n * n 的矩阵,构造方法如下:b[ i ][ j ] = s[ i ] * s[ j ] ...

  4. Task On The Board CodeForces - 1367D(思维)

    Polycarp wrote on the board a string s containing only lowercase Latin letters ('a'-'z'). This strin ...

  5. Orac and Game of Life CodeForces - 1350E(思维+BFS)

    Please notice the unusual memory limit of this problem. Orac likes games. Recently he came up with t ...

  6. Ehab and Path-etic MEXs CodeForces - 1325C(思维+贪心)

    You are given a tree consisting of n nodes. You want to write some labels on the tree's edges such t ...

  7. Mind Control CodeForces - 1291C(思维)

    You and your n−1n−1 friends have found an array of integers a1,a2,-,ana1,a2,-,an. You have decided t ...

  8. codeforces 1060a(思维水题)

    Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", ...

  9. CodeForces - 1102A(思维题)

    https://vjudge.net/problem/2135388/origin Describe You are given an integer sequence 1,2,-,n. You ha ...

最新文章

  1. plsql轻量版游标的使用
  2. LSTM(序列标注,自实现)
  3. php-rpm.conf,在新安装的宝塔面板中php无法启动起来
  4. Centos7 修改防火墙,开放端口、转发端口
  5. 计算机视觉论文-2021-06-14
  6. 原DTCoreText学习(三)-自定义DTAttributedTextCell
  7. jdk AbstractStringBuilder实现
  8. Linux 下删除非空目录
  9. pycharm 修改darcual(暗黑)主题滚动条颜色
  10. 010editor的破解
  11. shader之——shadowGun代码分析
  12. 集线器、交换机与路由器有什么区别?
  13. 微信生成公众号带参数二维码(一)
  14. 金属基功能单体/高折射率功能单体/特种丙烯酸酯单体/特种甲基丙烯酸酯单体
  15. Facebook 企业广告账户开户流程、资料准备、开户时间、开户须知及OE链接
  16. LaTeX--5--一个文档的基本结构/导言区/标题_作者_日期
  17. 35.前端笔记-CSS3-3D转换
  18. java经典问题:一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在 第10次落地时,共经过多少米?第10次反弹多高?
  19. linux安装BFE
  20. 全球及中国网络连接门铃对讲机系统行业市场运行状况与发展潜力分析报告2022-2028年

热门文章

  1. Ubuntu下文件权限管理
  2. 全国计算机等级考试题库二级C操作题100套(第90套)
  3. matlab strfind用法,findstr和strfind区别
  4. oracle00109,ORA-01034: 、ORA-01078: 和 LRM-00109: 的解决方法,ora-01034ora-01078
  5. oracle恢复指定数据文件,Oracle特殊恢复-BBED修改某个数据文件头
  6. 电脑软件:5个实用的Windows软件,大幅度提高你的工作效率!
  7. 分享四款非常好用的命令行软件,值得收藏!
  8. 手机知识:手机快充取决于充电头还是数据线,看完你就懂了!
  9. 操作系统:Linux环境变量相关知识总结
  10. 电脑技巧:C盘爆满该如何清理,实用的清理方案,小白必备