题目链接

C. Store

time limit per test

1.0 s

memory limit per test

256 MB

input

standard input

output

standard output

Ichuan is a store owner and his store has n products, numbered from 11 to nn.

Recently, his business is not very good, and he can occasionally sell a product, so he will confirm the information of the products sold.

MXYMXY,it means that on the XthXth day, the product numbered YY is sold.

DXYDXY,in currently sold products, those whose number is greater than or equal to YY, which one is the lowest number which sold before the XthXth day (including XthXth day) .

Input

the first line contains two positive integers NN,QQ(2≤N,Q≤200000)(2≤N,Q≤200000),which are the number of products and operations.

The following Q lines, each line contains one operator character (MM or DD) and two integers x,yx,y.(1≤x≤109,1≤y≤n1≤x≤109,1≤y≤n)

Output

For each request of type DXYDXY,output the number of required product. If no such product exists, output −1−1.

Examples

input

Copy

3 4
M 10 3
M 5 1
D 20 2
D 5 1

output

Copy

3
1

input

Copy

10 10
M 20 10
D 1 9
M 2 3
D 17 10
M 20 2
D 8 2
M 40 1
D 25 2
M 33 9
D 37 9

output

Copy

-1
-1
3
2
9

【题意】

有n件产品编号为1-n,q次操作,操作M X Y为在第X天卖出商品Y,操作D X Y为查询在第X天前卖出的编号大于等于Y且最小的商品编号,若不存在在输出-1.

【解题思路】

对于商品卖出的时间建一棵线段树,通过线段树来维护连续的一段商品区间最早卖出的商品的卖出时间。因为是维护一个最小值,所以一开始一定要记得对每个节点做正无穷处理。对于卖出商品操作,在线段树上进行单点更新,更新对于商品的卖出时间。

对于查询操作,答案在l=y,r=n中,所以可以二分答案,利用线段树区间查询最早卖出时间,若查询结果小于等于X,则可以继续二分。

【代码】

#include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=2e5+5;
int val[maxn<<2];
int n,q;
void build(int root,int l,int r,int v,int idx)
{if(l==r){val[root]=v;return;}int mid=(l+r)/2;if(idx<=mid)build(root*2,l,mid,v,idx);if(idx>mid)build(root*2+1,mid+1,r,v,idx);val[root]=min(val[root*2],val[root*2+1]);
}
int query(int root,int l,int r,int ql,int qr)
{if(l>=ql && r<=qr)return val[root];int mid=(r+l)/2;int ret=INF;if(ql<=mid)ret=min(ret,query(root*2,l,mid,ql,qr));if(qr>mid)ret=min(ret,query(root*2+1,mid+1,r,ql,qr));return ret;
}
int  binary(int l,int r,int v)
{int ans=-1;while(l<=r){int mid=(l+r)/2;if(query(1,1,n,l,mid)<=v){r=mid-1;ans=mid;}else l=mid+1;}return ans;
}
int main()
{scanf("%d%d",&n,&q);memset(val,INF,sizeof(val));while(q--){getchar();char ch;int x,y;scanf("%c %d %d",&ch,&x,&y);if(ch=='M')build(1,1,n,x,y);else printf("%d\n",binary(y,n,x));}
}

【Codeforces】WHU校赛2019 Store(线段树+二分)相关推荐

  1. WHU校赛2019(网络赛) 解题报告(CCNU_你们好强啊我们都是面包手) Apare_xzc

    WHU校赛2019(网络赛) 解题报告 CCNU_你们好强啊我们都是面包手(xzc zx lj) 战况: 比赛时3题,排名57,现在5题了 题目链接: WHU校赛2019 <-戳这里 以下题目按 ...

  2. codeforces 609F Frogs and mosquitoes 线段树+二分+multiset

    http://codeforces.com/problemset/problem/609/F There are n frogs sitting on the coordinate axis Ox. ...

  3. 春校赛--求索溪边树下的小瓜

    春校赛–求索溪边树下的小瓜 (10分) 题目内容: "三峡畔,长江边-"小瓜哼着校歌,慢悠悠的求索溪边散步.忽然,他在求索溪旁边发现一个非常漂亮的银杏树.于是小瓜走到树下一手拉着银 ...

  4. 牛客小白月赛28 E-会当凌绝顶,一览众山小 线段树+二分暴力模拟

    牛客小白月赛28 E-会当凌绝顶,一览众山小 线段树+二分暴力模拟 题意 思路 Code 传送门: https://ac.nowcoder.com/acm/contest/16081/E 题意 登山顺 ...

  5. 2019 牛客多校 C Governing sand 线段树

    链接:https://ac.nowcoder.com/acm/contest/887/C 来源:牛客网 Governing sand 时间限制:C/C++ 3秒,其他语言6秒 空间限制:C/C++ 6 ...

  6. Codeforces 1004F Sonya and Bitwise OR (线段树)

    题目链接 https://codeforces.com/contest/1004/problem/F 题解 这种水题都不会做了怎么.. 考虑一个序列的前缀 \(\text{or}\) 值只会变化 \( ...

  7. CodeForces - 1529E Trees of Tranquillity(贪心+线段树)

    题目链接:https://vjudge.net/problem/CodeForces-1529E 题目大意:给出两棵根节点为 111 的树,分别称为 AAA 树和 BBB 树,现在通过两棵树可以构造出 ...

  8. CodeForces - 1217F Forced Online Queries Problem(线段树分治+并查集撤销)

    题目链接:点击查看 题目大意:给出 nnn 个点,初始时互相不存在连边,需要执行 mmm 次操作,每次操作分为两种类型: 1xy1 \ x \ y1 x y:如果 (x,y)(x,y)(x,y) 之间 ...

  9. Codeforces 1045. A. Last chance(网络流 + 线段树优化建边)

    题意 给你 \(n\) 个武器,\(m\) 个敌人,问你最多消灭多少个敌人,并输出方案. 总共有三种武器. SQL 火箭 - 能消灭给你集合中的一个敌人 \(\sum |S| \le 100000\) ...

最新文章

  1. java双等比较值_java – 执行双值相等比较时,应该是epsilon值
  2. Android进阶:一、日志打印和保存策略
  3. 每天一道LeetCode-----寻找两个链表的交点
  4. 使用PLSQL developer编辑数据
  5. 如何检查字符串是否以指定的字符串开头? [重复]
  6. Java:JSON解析工具-org.json
  7. adb for linux的工具包,adb工具包下载_adb工具包官方下载「最新版」-太平洋下载中心...
  8. 用python对图片进行反色处理
  9. 分析公司盈利能力的方法
  10. 如何利用单片机的ADC模块(或者独立的ADC芯片)得到接入ADC管脚上的实际电压值?
  11. android+主流品牌手机,10大手机品牌共建Android联军 代表机型一览
  12. Linux的入门学习
  13. 某工控图片上传服务 CPU 爆高分析
  14. 机器学习(四)——Adaboost算法
  15. 云储存服务器系统内蒙古,内蒙古【存储服务器】怎么选_星际云储
  16. Python下数值型与字符型类别变量独热编码(One-hot Encoding)实现
  17. iview table点击单元格事件
  18. php注册推荐码,javascript - PHP注册邀请码
  19. FHQ-Treap(非旋treap/平衡树)——从入门到入坟
  20. 基于java的ftp客户端_基于Java的FTP客户端软件的设计

热门文章

  1. sql monitor简介
  2. Automatically manage signing
  3. Zabbix企业微信告警最新版
  4. linux下tomcat的访问权限,关于Linux权限引起的Tomcat项目404问题
  5. MySQL数据库常用查询(带练习示例)
  6. 【行业首发】蛙色VR全景作品支持对接高德地图!
  7. 微信企业号开发七:JSAPI模式
  8. 实现顺序表各种基本运算的算法
  9. C语言水洼数算法,水洼的数量算法 代码(C)
  10. python 批量转换docx只转换了一个出现pywintypes.com_error被调用的对象已与其客户端断开连接