Ichuan is a store owner and his store has n products, numbered from 1 to ?.
Recently, his business is not very good, and he can occasionally sell a product, so he will confirm the information of the products sold.
???,it means that on the ??ℎ day, the product numbered ? is sold.
???,in currently sold products, those whose number is greater than or equal to ?, which one is the lowest number which sold before the ??ℎ day (including ??ℎ day) .
Input
the first line contains two positive integers ?,?(2≤?,?≤200000),which are the number of products and operations.
The following Q lines, each line contains one operator character (? or ?) and two integers ?,?.(1≤?≤109,1≤?≤?)
Output
For each request of type ???,output the number of required product. If no such product exists, output −1.
Examples
input
3 4
M 10 3
M 5 1
D 20 2
D 5 1
output
3
1
input
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
-1
-1
3
2
9
感觉这个题意写的不是很明确,感感觉改成在每次操作M后第x天发现了一笔y的订单,然后D操作是查询已经发现的订单中订购时间在第x天之前的编号大于等于y的最小编号。
题意感觉就是个二维偏序问题,因此cdq分治可做。
cdq分治的过程中我们维护x为递增序列,然后用set维护y,每次为W操作的时候加入set,D操作的时候更新答案。
复杂度: O ( n l o g 2 n ) O(nlog^2n) O(nlog2n)
AC代码
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5;
const int inf=0x3f3f3f3f;
struct node{
int op,x,y,id;
node(){
}
node(int op,int x,int y,int id