题意描述

您需要维护一个数据结构,支持以下三种操作:

  • L id:在现在序列的左边插一个编号为id的物品
  • R id:在现在序列的右边插一个编号为id的物品
  • ? id:查询该点左面有几个元素,右面有几个元素,并取min输出

输入输出格式:

输入格式:

第一行,一个整数q,表示操作数

下面q行,每行2个数,表示一个操作

输出格式

对于每个“?”操作,输出一行一个整数,表示答案

思路:

差点暴力上treap

水题一道

因为只有100000个数,所以我们可以维护一个大小为200000的数组以及lll,rrr两个指针

第一个数放在100000这个位置,每次往左放就l--,反之r++

把数填进去,同时写一个映射,将ididid映射为位置

当前位置−l-l−l为左面的数的数量,r−r-r−当前位置为右面的数的数量

然后输出即可

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define rii register int i
#define rij register int j
using namespace std;
int l,r,now,ys[];
int q;
int main()
{
scanf("%d\n",&q);
for(rii=;i<=q;i++)
{
char cz;
int id;
scanf("%c%d\n",&cz,&id);
if(i==)
{
ys[id]=;
l=;
r=;
continue;
}
if(cz=='L')
{
l--;
ys[id]=l;
}
if(cz=='R')
{
r++;
ys[id]=r;
}
if(cz=='?')
{
printf("%d\n",min(ys[id]-l,r-ys[id]));
}
}
}

CF1066CBooks Queries(数组的特殊处理)的更多相关文章

  1. 985. Sum of Even Numbers After Queries

    We have an array A of integers, and an array queries of queries. For the i-th query val = queries[i] ...

  2. livequery源码解读

    从使用说起: 若干年前,有一天发现,通过js代码创建的html元素及ajax加载的html,无法被$([selector]).click(function(){...})绑定上事件,于是发现了jQue ...

  3. LeetCode.1170-比较字符串中最小字符的出现频率(Compare Strings by Frequency of the Smallest Char)

    这是小川的第412次更新,第444篇原创 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第263题(顺位题号是1170).在一个非空字符串s上定义一个函数f(s),该函数计算s中最小字 ...

  4. CodeChef DISTNUM2 Easy Queries 节点数组线段树

    Description You are given an array A consisting of N positive integers. You have to answer Q queries ...

  5. gym 100589A queries on the Tree 树状数组 + 分块

    题目传送门 题目大意: 给定一颗根节点为1的树,有两种操作,第一种操作是将与根节点距离为L的节点权值全部加上val,第二个操作是查询以x为根节点的子树的权重. 思路: 思考后发现,以dfs序建立树状数 ...

  6. Codeforces 375D Tree and Queries(DFS序+莫队+树状数组)

    题目链接  Tree and Queries 题目大意  给出一棵树和每个节点的颜色.每次询问$vj, kj$ 你需要回答在以$vj$为根的子树中满足条件的的颜色数目, 条件:具有该颜色的节点数量至少 ...

  7. GYM 100741A Queries(树状数组)

    A. Queries time limit per test 0.25 seconds memory limit per test 64 megabytes input standard input ...

  8. CodeForces - 369E Valera and Queries(树状数组)

    CodeForces - 369E Valera and Queries 题目大意:给出n个线段(线段的左端点和右端点坐标)和m个查询,每个查询有cnt个点,要求给出有多少条线段包含至少其中一个点. ...

  9. CodeForces - 375D Tree and Queries (莫队+dfs序+树状数组)

    You have a rooted tree consisting of n vertices. Each vertex of the tree has some color. We will ass ...

随机推荐

  1. ioc autofac简单示例

    1.winform用法: nuget安装autofac public interface ILog { bool Log(string msg); } public class TXTLogger : ...

  2. Angular-ui/bootstarp modal 主控制器与模态框控制器传值

    调用模态框: $scope.open = function (size) { //这里很关键,是打开模态框的过程 var modalInstance = $uibModal.open({ animat ...

  3. Android MediaPlayer播放音乐并实现进度条

    提前工作,往sd卡里放音乐文件 1.布局文件main.xml <?xml version="1.0" encoding="utf-8"?> < ...

  4. win10 x64 python3.6 pycharm 安装statsmodels

    在pycharm下,安装statsmodels,会出现需要vc++14.0的错误提示. 这时可以到网站 https://www.lfd.uci.edu/~gohlke/pythonlibs/#word ...

  5. .NET预处理器指令

    .NET预处理器指令 做开发以来很少接触到这部分内容,基本上没有用到,偶尔在一些框架中和一些开源项目中会见到,常常因为只关心实现逻辑忽略了这部分的功能.现在自己有点时间了,还是希望能够完整的对这部分做 ...

  6. java excel转pdf 工具类

    package com.elitel.hljhr.comm.web.main.controller; import java.io.File; import java.io.FileOutputStr ...

  7. 【Leetcode】【Medium】Remove Duplicates from Sorted List II

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

  8. yii2.0表单自带验证码

    Yii2.0的自带的验证依赖于GD2或者ImageMagick扩展. 使用步骤如下: 第一步,控制器: 在任意controller里面重写方法

  9. python csv写入数据,消除空行

    import csv rowlist=[{'first_name': 'mark', 'last_name': 'zhao','age':21}, {'first_name': 'tony', 'la ...

  10. commons.pool2 对象池的使用

    commons.pool2 对象池的使用 ? 1 2 3 4 5 <dependency>     <groupId>org.apache.commons</groupI ...