题意有两种操作
一种是往集合中加入一个数,而且保证这个数从未出现过。
另一种是询问集合中的数 x mod y 值最小的数的序号,多种情况取序号最大
数最大是50w,询问次数是4W,Y最大100w
因为数最大是50w,所以就想到建一颗长度为50W的树,每个点默认值为inf,然后增加数时,将相应点的值更新为该数
然后分区询问区间最小值即可。
全部都分区会TLE。
对应单次询问,暴力复杂度是 集合中元素个数N
分区查询复杂度为 分区个数*log2(分区长度)
所以就是在暴力复杂度小于分区复杂度时进行暴力找最小值即可。
/*===============*\
| *** *** *** *** |
| * ** * * * |
| *** *** * *** |
| ID: ZERO |
| LANG: C++ |
\*===============*/
#include <vector>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <queue>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <string>
#include <cassert>
using namespace std;
#define mod (int)(1e9+7)
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
#define ls rt << 1
#define rs rt << 1 | 1
#define maxn 500001
#define inf 1000000009
int min(int x,int y){return x<y?x:y;}
int MIN[maxn<<2];
void PushUP(int rt) { //向上更新
MIN[rt]=min(MIN[ls],MIN[rs]);//直接去最小值
}
void build(int l,int r,int rt) {//建树
MIN[rt]=inf;
if (l == r) {
return ;
}
int m = (l + r) >> 1;
build(lson);
build(rson);
PushUP(rt);
}
void update(int p,int l,int r,int rt) {//更新
if (l==r) {
MIN[rt]=p;
return ;
}
int m=(l+r) >> 1;//middle
if (p<=m) update(p , lson);
else update(p , rson);
PushUP(rt);
}
int query(int L,int R,int l,int r,int rt) {//询问
if(MIN[rt]==inf) return inf;
if (L <= l && r <= R) {
return MIN[rt];
}
int m=(l+r) >> 1;//middle
int ret = inf;
if (L <= m) ret = min(ret,query(L,R,lson));
if (R > m) ret = min(ret,query(L,R,rson));
return ret;
}
char s[2];
int mp[500001];
int arr[500001];
int main(){
int t,cas=0,i,j;
while (~scanf("%d",&t),t) {
printf("Case %d:\n",++cas);
int ma=500000,top=0;
build(1,ma,1);
int now=1;
for(j=0;j<t;j++){
int n;
scanf("%s%d",s,&n);
if(s[0]=='B'){
update(n,1,ma,1);
mp[n]=now++;
arr[top++]=n;//将数放入集合中
}
else{
int mi=inf,tag=-1;
if(ma*(log(ma)/log(2))>top*n){//检查暴力复杂度是否小于分区间查询
for(i=top-1;i>=0;i--){
if((arr[i]%n)<mi)mi=arr[i]%n,tag=mp[arr[i]];
if(mi==0) break;
}
}
else{
for(i=1;;i++){
int l,r;
l=n*(i-1);//区间l
r=n*i-1;//区间r
if(i==1) l=1;
if(l>ma) break;
if(r>ma) r=ma;
int nn=query(l,r,1,ma,1);
if(nn==inf) continue;
if((nn%n)<=mi) {
if((nn%n)==mi){
if(mp[nn]>tag)tag=mp[nn];
mi=nn%n;
}
else mi=nn%n,tag=mp[nn];
}
}
}
printf("%d\n",tag);
}
}
printf("\n");
}
return 0;
}