CF1066CBooks Queries(数组的特殊处理)
题意描述
您需要维护一个数据结构,支持以下三种操作:
- 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(数组的特殊处理)的更多相关文章
- 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] ...
- livequery源码解读
从使用说起: 若干年前,有一天发现,通过js代码创建的html元素及ajax加载的html,无法被$([selector]).click(function(){...})绑定上事件,于是发现了jQue ...
- LeetCode.1170-比较字符串中最小字符的出现频率(Compare Strings by Frequency of the Smallest Char)
这是小川的第412次更新,第444篇原创 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第263题(顺位题号是1170).在一个非空字符串s上定义一个函数f(s),该函数计算s中最小字 ...
- CodeChef DISTNUM2 Easy Queries 节点数组线段树
Description You are given an array A consisting of N positive integers. You have to answer Q queries ...
- gym 100589A queries on the Tree 树状数组 + 分块
题目传送门 题目大意: 给定一颗根节点为1的树,有两种操作,第一种操作是将与根节点距离为L的节点权值全部加上val,第二个操作是查询以x为根节点的子树的权重. 思路: 思考后发现,以dfs序建立树状数 ...
- Codeforces 375D Tree and Queries(DFS序+莫队+树状数组)
题目链接 Tree and Queries 题目大意 给出一棵树和每个节点的颜色.每次询问$vj, kj$ 你需要回答在以$vj$为根的子树中满足条件的的颜色数目, 条件:具有该颜色的节点数量至少 ...
- GYM 100741A Queries(树状数组)
A. Queries time limit per test 0.25 seconds memory limit per test 64 megabytes input standard input ...
- CodeForces - 369E Valera and Queries(树状数组)
CodeForces - 369E Valera and Queries 题目大意:给出n个线段(线段的左端点和右端点坐标)和m个查询,每个查询有cnt个点,要求给出有多少条线段包含至少其中一个点. ...
- 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 ...
随机推荐
- babel的使用以及安装配置
简介 babel是一个广泛使用的转码器,可以将ES6代码转化为ES5代码,从而在现有环境执行,这意味着,你可以现在就用ES6编写程序,而不用担心现有环境是否支持. 安装及配置 npm install ...
- 【转】Spark on Yarn遇到的几个问题
本文转自 http://www.cnblogs.com/Scott007/p/3889959.html 1 概述 Spark的on Yarn模式,其资源分配是交给Yarn的ResourceManage ...
- PHP 使用WampServer环境,如何配置虚拟主机域名
很多人不会配置虚拟主机,我这里简单交一下大家,分三步: 1.在 C:\Windows\System32\drivers\etc 文件夹中的文件 Hosts 文件修改代码为: 127.0.0.1 loc ...
- Tomcat下JDBC连接样例
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- python nose测试
前提: python3 安装nose 结果: nose目录下有子目录tests和mybag,在mybag下新建my_age.py, 内部有Students类,age属性. tests目录下写Sutde ...
- Java中short、int、long、float、double的取值范围
一.基本数据类型的特点,位数,最大值和最小值.1.基本类型:short 二进制位数:16 包装类:java.lang.Short 最小值:Short.MIN_VALUE=-32768 (-2的15此方 ...
- 【Leetcode】【Medium】Permutations
Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...
- 【Leetcode】【Medium】3Sum Closest
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- 【转】Java中的String,StringBuilder,StringBuffer三者的区别
https://www.cnblogs.com/su-feng/p/6659064.html 最近在学习Java的时候,遇到了这样一个问题,就是String,StringBuilder以及String ...
- centos 6 YUM安装 lnmp
准备篇: 1.配置防火墙,开启80端口.3306端口 vi /etc/sysconfig/iptables -A INPUT -m state --state NEW -m t ...