CodeForces 555C Case of Chocolate(STL:map的使用)

codeforces 555c 数据结构(map 的使用) Case of Chocolate time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output Andrewid the Android is a galaxy-known de 阅读详情

http://codeforces.com/problemset/problem/555/C

C. Case of Chocolate
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom.

A bar of chocolate can be presented as an n × n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge.

After each action, he wants to know how many pieces he ate as a result of this action.

Input

The first line contains integers n (1 ≤ n ≤ 109) and q (1 ≤ q ≤ 2·105) — the size of the chocolate bar and the number of actions.

Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≤ xi, yi ≤ n, xi + yi = n + 1) — the numbers of the column and row of the chosen cell and the character that represents the direction (L — left, U — up).

Output

Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action.

Sample test(s)
Input
6 5
3 4 U
6 1 L
2 5 L
1 6 U
4 3 U
Output
4
3
2
1
2
Input
10 6
2 9 U
10 1 U
1 10 U
8 3 L
10 1 L
6 5 U
Output
9
1
10
6
0
2
Note

Pictures to the sample tests:

The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten.

In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.


对于U的操作,能到达的y的上界和x值大于它最接近的操作的y的上界是一样的:

1.如果是L的操作,上界就是那个L操作的y值。

2.如果是U的操作,上界就是这次U操作所能到达的上界。


对于L的操作,则是找y值大于它最近的操作。

所以建立两个map U,L,一个key值是x,一个是y,因为map的lower_bound是按照key值查找的。


对于U操作,结束后U要插入pair(x,能到达的y值),在L中插入(y,x)

对于L操作 ,结束后L要插入pair(y,能到达的x值),在U中插入(x,y)


这个可以画一下图方便理解。


map知识:

map的元素是pair,用迭代器找到后使用first,second来调用元素的key和value

使用count来判断key存不存在

如果没有大于等于某元素的,lower_bound返回end()


#include<iostream>
#include<cstdio>
#include<map>
#include<cstring>
#include<set>
using namespace std;
int n,q;
map<int,int> u;
map<int,int> l;
int main()
{
    int x,y;
    char op[5];
    cin>>n>>q;
        while(q--)
        {
            scanf("%d%d%s",&x,&y,op);
            map<int,int>::iterator p;
            if(op[0]=='U')
            {
                int ans=0;
                p=u.lower_bound(x);
                if(u.count(x)){
                    printf("0\n");
                    continue;
                }
                if(p==u.end())
                {
                   ans=y;
                }
                else{
                    ans=y-(p->second);
                }
                printf("%d\n",ans);
                l[y]=x;
                u[x]=y-ans;
            }
            else
            {
                int ans=0;
                p=l.lower_bound(y);
                if(l.count(y))
                {
                    printf("0\n");
                    continue;
                }
                if(p==l.end())
                {
                    ans=x;
                }
                else
                {
                    ans=x-(p->second);
                }
                printf("%d\n",ans);
                l[y]=x-ans;
                u[x]=y;
            }
        }
    return 0;
}



从萤石云到GB28181:海康4G摄像机接入第三方平台的曲折之路 本文深入探讨了海康4G摄像机接入第三方平台时遇到的协议冲突问题,特别是GB28181与萤石云服务的互斥性。通过技术分析揭示了4G模块的定向流量限制和协议栈资源竞争等核心问题,并提供了有线接入测试和萤石云API二次开发等解决方案。最后总结了设备选型的关键参数和协议兼容性检查清单,为安防监控系统集成提供实用指导。 阅读详情

相关推荐

视觉机器人抓取实战:从物体定位到抓取估计的完整流程解析(附Python代码示例)

本文详细解析了视觉引导机器人抓取的完整技术流程,涵盖从物体定位、位姿估计到抓取估计的核心步骤。通过Python代码示例,演示了如何使用Open3D等工具进行点云处理、平面分割、PCA与ICP位姿估计,以及基于几何启发式的抓取生成方法,为机器人视觉抓取开发提供了清晰的实战指南。

peach的博客 913

[tarjan][树上差分] Codeforces 555E Case of Computer Network

题目描述 Andrewid the Android is a galaxy-known detective. Now he is preparing a defense against a possible attack by hackers on a major computer network. In this network arennvertices, some pairs of ...

weixin_30516243的博客 143

华为服务器RAID配置避坑指南:如何通过iBMC快速识别阵列卡型号并完成初始化

本文详细解析华为服务器RAID配置全流程,重点介绍通过iBMC快速识别阵列卡型号、硬盘健康诊断及RAID级别选择的场景化策略。提供从iBMC登录到缓存策略调优的实战技巧,帮助工程师避免常见配置陷阱,提升服务器存储系统性能和稳定性。

weixin_28475533的博客 492

Codeforces 555C Case of Chocolate 其他

原文链接https://www.cnblogs.com/zhouzhendong/p/9272797.html 题目传送门 - CF555C 题意   给定一个 $n\times n(n\leq 10^9)$ 的方格阵列。   接下来,我们将从该方阵的副对角线开始进行一些操作。   操作 $"y\ x\ U"或"y\ x\ L"$ 分别表示一个人从第 $x$ 行第 $y$ 列开始走,$U...

weixin_30596735的博客 3476

模拟滤波器的基础知识和设计

使用Python实现多种模拟滤波器

faltas的博客 3530

自动驾驶轨迹规划算法——Apollo EM Planner

Apollo EM Planner 是百度自动驾驶系统中的轨迹规划算法,采用期望-最大化(EM)迭代优化思想。其核心是将轨迹规划分解为路径优化(E-Step)和速度优化(M-Step)两个子问题,在Frenet坐标系下交替求解。算法先通过动态规划(DP)搜索粗路径,再使用二次规划(QP)进行平滑优化,并结合障碍物信息生成安全走廊。相较Lattice、Hybrid A*等规划器,EM Planner具有更好的实时性、动态障碍物处理能力和轨迹平滑性,适用于城市道路、高速公路等复杂场景。该算法通过解耦路径与速度优

swaggytiger的博客 2666

Codeforces Round #555 (Div. 3) C1

给你n个数,都不同,每次可以从左边或者右边取一个数,要求每次取的数都要比上一次取的数大,问最多能取多少个数。 贪心,模拟着取就行了 #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; const int maxn=2e5+5; in...

c3po 169

地震数据可视化必备:5分钟读懂SEGY道头字段全解析(含坐标定位指南)

本文深入解析了SEGY文件格式中道头坐标字段的提取与可视化方法。针对地震数据处理中的常见痛点,详细介绍了sx/sy、gx/gy等关键坐标字段的二进制解析、比例因子应用、单位转换及常见问题解决方案,并提供了实用的Python代码示例,帮助开发者实现从SEGY文件到三维空间可视化的完整流程。

echo99的专栏 1011

codeforces 555C - Case of Chocolate

#include #define N 1000100 using namespace std; struct Seg{ int a[N<<2],lt[N<<2]; inline void push_down(int k){ a[k<<1]=max(a[k<<1],lt[k]); a[k<<1|1]=max(a[k<<1|1],lt[k]); lt[k<<1]=max(lt[k<<1

流水的博客 516

TSN新技术,让您的设备网络“更实时、更确定、更安全”

近日,工业和信息化部发布了2022年第23号公告,批准发布行业标准YD/T 4134-2022《工业互联网时间敏感网络需求及场景》。该标准是国内首个时间敏感网络(TSN)技术标准,标志着我国TSN技术标准体系建设迈出了坚实的一步,对构建工业互联网网络标准体系具有重要意义。TSN源于Time-Sensitive Networking的缩写,中文译名为时间敏感网络,因其具备的确定性和微秒级交互特性,受到了对实时性要求较高的工业控制领域的关注。TSN与工业数智化。

创龙,嵌入式一体化解决方案商 771

华为OD干了两个月,还是跑了

​本人工作了快13年,base南京。2024年2月份由于公司业务变动第一次拿到了公司发的大礼包,同时也面临着失业的问题,我对自己的技术还是很有信心的,但是,找了大概有一个多月时间,发现心意的工作的确不太好找,当然收到挺多家offer,就是薪资不太满意,外包也一直不想去,不过说实话外包给的薪资普遍要比自研的单位要高一些,最后为了钱还是进华为(既然选择外包那就选择好一些的吧),毕竟中年老男人工作不能断太久。

啊渊的专栏 1万+

Codeforces 555C Case of Chocolate

C. Case of Chocolate time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output Andrewid the Android is a galaxy-known

szh_0808的博客 414

【DaVinci Configurator专题】-6-将UDS&OBD诊断数据库文件CDD和PDX导入到CFG

CDD和ODX是两种诊断数据库文件。机器可读的诊断数据库其实就是将传统的Excel调查表等信息编辑成CDD或ODX文件。CDD是Vector私有的一种诊断数据库格式;ODX是国际标准的诊断数据交互格式。CANdelaStudio编辑CDD文件,以及也可以导出ODX文件;ODX Studio用于编辑ODX文件,诊断数据库的编辑其实就是诊断需求的定义。 下面以PDX为例,将其导入到DaVinci Configurator。 目录 1 打开DaVinci Configurator,导入PDX文件 2 解.

「汽车电子助手」的博客 6751

codeforces 555c// Case of Chocolate// Codeforces Round #310(Div. 1)

题意:直角边为n的网格巧克力,一格为一块,选择斜边上一点,从左或上吃,直到吃到空气,称为一次操作。给出几个操作,问各能吃几块。如果x是当前要吃的横坐标,在已经吃过的中找x1>=x的第一个x1,即lower_bound()。如果x1==x,结果就是0;否则假设x是往上吃,x1也是往上吃,因为x和x1间没有往左吃的,因此x1结束的地方也是x结束的地方。如果x往上吃,x1往左吃,因为x1横坐标最小...

weixin_30501857的博客 153

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

codeforces 555C Case of Chocolate(map)

/* 题目大意:给一个巧克力,把副对角线(参照线代)下方的先都吃掉,然后每次给出一个询问, 从对角线出发,要么向上,要么向左,吃到已经吃过的地方或者边缘为止,输出每次能吃多少小格巧克力。 */ /* 对于U的操作,能到达的y的上界和x值大于它最接近的操作的y的上界是一样的: 1.如果是L的操作,上界就是那个L操作的y值。 2.如果是U的操作,上界就是这次U操作所能到达的上界。 对于L的操作,则

苍竹林 857

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

A. Case of the Zeros and Ones

time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Andrewid the Android is a galaxy-famous detective. In his free ti

无知的我 1460

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

CodeForces 567C Case of Matryoshkas map+递推

原题: http://codeforces.com/contest/556/problem/C题目:Case of Matryoshkas time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output Andrewid the Androi

ShineRoyal 765

codeforces #310 555C C. Case of Chocolate(set的应用)

题目链接: 点击打开链接 题目大意: 给出一个反对角线之上的三角形,每次吃一条,可以选择向左吃,或向上吃,只能从反对角线上的点开始,问每次吃的时候能吃多少小块 题目分析: 首先考虑每次吃的时候,如果是向上吃,那么能够影响它的就是它右侧的点,找到目前它右侧的点中最近的左吃的,然后这个点会影响到当前点吃的高度,如果向左吃,那么找到左侧最近的向上吃的点,会影响向左吃的距离,那么获得最近的这种就

llin-黎辰 1016

Codeforces Round #310 (Div. 2) A. Case of the Zeros and Ones 水题

A. Case of the Zeros and Ones Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/556/problem/A Description Andrewid the Android is a galaxy-famous detective. In his fre...

weixin_34032621的博客 225

Codeforces Round #310 (Div. 1) C. Case of Chocolate (线段树)

题目地址:传送门 这题虽然是DIV1的C。。但是挺简单的。。只要用线段树分别维护一下横着和竖着的值就可以了,先离散化再维护。每次查找最大的最小值<=tmp的点,可以直接在线段树里搜,也可以二分去找。 代码如下:#include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm

scf0920 1195

cf #310 E. Case of Chocolate (upper_bound())

题目:http://codeforces.com/contest/556/problem/E 题意:一块只有左上角的巧克力,每次从对角线位置向左或者向上吃,并且只能连续地吃。有q吃查询,问每次能吃的长度。 E. Case of Chocolate time limit per test 3 seconds memory limit per te

w20810的专栏 619

Codeforces 555C. Case of Chocolate (SET应用)

对于U的操作,能到达的y的上界和x值大于它最接近的操作的y的上界是一样的: 1.如果是L的操作,上界就是那个L操作的y值。 2.如果是U的操作,上界就是这次U操作所能到达的上界。 对于L的操作,则是找y值大于它最近的操作。 所以建立两个map U,L,一个key值是x,一个是y,因为map的lower_bound是按照key值查找的。 对于U操作,结束后U要插入pair(

Baoli1008的专栏 771

离散化+线段树 codefores555C Case of Chocolate

传送门:点击打开链接 题意:阶梯形的巧克力,每次选择边缘的一个格子,然后向上吃或者向左吃,直到边界或者空白位置停止,吃过的位置之后就是空白。 思路:n很大,先把q查询的那些位置离散化一下,注意x+1和y+1也需要离散化一下。 之后y会限制后来的x,x会限制后来的y,搞两个分开的线段树分别维护x和y就行了 还要弄个vis来判断某一行或某一列是否已经放过,就ok了 总的来说只要想清楚了细节,

qwb的博客 504

Codeforces Round #310 (Div. 1) C. Case of Chocolate stl应用

C. Case of Chocolate time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard output Andrewid the Android is a galaxy-known detective. Now he does not inv

习惯与规则决定命运 1106

Codeforces Round #310 (Div. 1)B,C,D(set+线段树)

B. Case of FugitiveAndrewid the Android is a galaxy-famous detective. He is now chasing a criminal hiding on the planet Oxa-5, the planet almost fully covered with water.The only dry land there is an a

龙崎的专栏 1178

codeforces 556B Case of Fake Numbers

Andrewid the Android is a galaxy-famous detective. He is now investigating a case of frauds who make fake copies of the famous Stolp’s gears, puzzles that are as famous as the Rubik’s cube once was.Its

总有那么一群人喜欢盯着我的博客看,估计这是他们见过最长的博客名,我必须得告诉他们博客名最长可以写多长 628
上一篇: ZOJ 3204 Connect them(最小生成树:kruscal算法)
下一篇: CodeForces 553A Kyoya and Colored Balls (排列组合)
hellohelloC
博客等级 码龄12年 15粉丝 107原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值