牛客多校第3场 J 思维+树状数组+二分

传送门:https://ac.nowcoder.com/acm/contest/883/J

题意:

给你q个询问,和一个队列容量f

询问有两种操作:

0.访问操作,如果询问的name在队列中存在的话,那么就输出队列中name对应的val值,然后将队列中name对应的元素扔到队列的尾部去,否则就直接将该元素插入到队列的尾部去

1.插入操作,得到队列中对应的name元素的v值为k,查询第k+v个元素的v值是多少

题解:

已知,对于插入操作,我们需要很快的查询对应的name值的val

所以我们考虑map映射,为了解决string过慢的问题,我们将stringhash成一个unsigned long long的值

我们记录有多少个数被插入了,还有每个点的绝对坐标,如果在0操作中 点x被丢弃了,那么我们就对x位置打上标记1,这样,在我们查询第y个操作时,实际上,这个y操作对于的位置就是 查询这个y对应的pos位减去当前位置之前被删掉的数的和加上 v值

我们用tot统计被插入数的数量,因为一开始就是空串,所以tot实际上就代表队列中有多少个数,如果tot>m,我们就丢弃队头的元素

对于每次查询,我们是找当前位置向左的位置,因为我们删除的数实际上只是给打上了一个+1的标记,所以我们需要找到左边真正存在的那个数,这个右端点不太好找,所以我们考虑二分,二分当前位置是否是第r个点

check就直接用二分出来的mid减去mid之前的丢弃的个数,如果这个个数>当前个数,那么就改变二分的上界r,否则改变二分的下界l

然后就可以得到满足真正的pos+1的位置的值的数了

emmm复杂度应该是两个log 被卡map的操作然后有迭代器 实现mp的操作,这样卡过去了 600+ms 海星吧

代码:

#include <set>
#include <map>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#include <assert.h> using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
#define ls rt<<1
#define rs rt<<1|1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define bug printf("*********\n")
#define FIN freopen("input.txt","r",stdin);
#define FON freopen("output.txt","w+",stdout);
#define IO ios::sync_with_stdio(false),cin.tie(0)
#define debug1(x) cout<<"["<<#x<<" "<<(x)<<"]\n"
#define debug2(x,y) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<"]\n"
#define debug3(x,y,z) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<" "<<#z<<" "<<z<<"]\n"
const int maxn = 5e5 + 5;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double Pi = acos(-1);
const int seed = 1331;
LL quick_pow(LL x, LL y) {
LL ans = 1;
while(y) {
if(y & 1) {
ans = ans * x % mod;
} x = x * x % mod;
y >>= 1;
} return ans;
}
int lowbit(int x) {
return x & (-x);
}
int bit[maxn];
void add(int pos, int val) {
while(pos < maxn) {
bit[pos] += val;
pos += lowbit(pos);
}
}
int query(int pos) {
int res = 0;
while(pos) {
res += bit[pos];
pos -= lowbit(pos);
}
return res;
}
void clear(int pos, int MM) {
while(pos < MM) {
bit[pos] = 0;
pos += lowbit(pos);
}
}
uLL Hash(char *x) {
uLL h = 0;
for(int i = 0; x[i]; i++) {
h = h * seed + x[i];
}
return h;
}
int op;
int v;
int val[maxn];
uLL hname[maxn];
char name[20];
map<uLL, int> mp;
int get_pos(int p, int tot) { //虽然名字是tot,但是要传入index
int pos = -1;
int l = 1, r = tot;
while(r >= l) {
int mid = (l + r) / 2;
int num = mid - query(mid);
if(num >= p) {
r = mid - 1;
pos = mid;
} else {
l = mid + 1;
}
}
assert(pos != -1);
return pos;
} map<uLL, int>:: iterator iter;
int main() { int T;
scanf("%d", &T);
while(T--) { mp.clear();
int m, k;
scanf("%d%d", &k, &m);
memset(bit, 0, sizeof(bit));
int Index = 0;
int tot = 0;
for(int i = 1; i <= k; i++) {
scanf("%d%s%d", &op, name, &v);
uLL h = Hash(name);
iter = mp.find(h);
if(op == 0) {
if(iter != mp.end()) {
int pos = iter->second;
printf("%d\n", val[pos]);
add(pos, 1);
Index++;//charu
hname[Index] = h;
val[Index] = val[pos];
iter->second = Index;
} else {
printf("%d\n", v);
tot++;
if(tot > m) {
tot--;
int pos = get_pos(1, Index);
add(pos, 1);
iter = mp.find(hname[pos]);
if(iter != mp.end())mp.erase(iter);
}
Index++;
mp[h] = Index;
hname[Index] = h;
val[Index] = v;
}
} else {
if(iter == mp.end()) {
printf("Invalid\n");
continue;
}
int vv = iter->second;
int pos = vv - query(vv - 1);
pos += v;
if(pos > tot || pos < 1) {
printf("Invalid\n");
} else {
pos = get_pos(pos, Index);
printf("%d\n", val[pos]);
}
}
}
}
return 0;
}

牛客多校第3场 J 思维+树状数组+二分的更多相关文章

  1. 18牛客多校训练第二场 J farm

    题意:一个n×m的农田, 每个小格子都有一种作物, 现在喷t次农药,每次农药覆盖一个矩形, 该矩形里面与农药类型不同的植物都会死掉, 求最后植物的死亡数是多少. 题解:二维树状数组. 每次喷农药的时候 ...

  2. 牛客网 牛客练习赛4 A.Laptop-二维偏序+离散化+树状数组

    A.Laptop 链接:https://ac.nowcoder.com/acm/contest/16/A来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 131072K,其 ...

  3. 牛客练习赛47 E DongDong数颜色 (树状数组维护区间元素种类数)

    链接:https://ac.nowcoder.com/acm/contest/904/E 来源:牛客网 DongDong数颜色 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 5242 ...

  4. 「BZOJ1669」D 饥饿的牛 [Usaco2006 Oct] Hungry Cows 牛客假日团队赛5 (LIS,离散化树状数组)

    链接:https://ac.nowcoder.com/acm/contest/984/D 来源:牛客网 饥饿的牛 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言 ...

  5. 牛客训练六:海啸(二维树状数组+vector函数的使用)

    题目链接:传送门 思路: 二维树状数组, vector(first,last)函数中assign函数相当于将first中的函数清空,然后将last中的值赋值给first. 参考文章:传送门 #incl ...

  6. 牛客多校第五场 J:Plan

    链接:https://www.nowcoder.com/acm/contest/143/J 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524 ...

  7. 牛客多校第六场 J Heritage of skywalkert 随即互质概率 nth_element(求最大多少项模板)

    链接:https://www.nowcoder.com/acm/contest/144/J来源:牛客网 skywalkert, the new legend of Beihang University ...

  8. 牛客多校第四场 J.Hash Function(线段树优化建图+拓扑排序)

    题目传送门:https://www.nowcoder.com/acm/contest/142/J 题意:给一个hash table,求出字典序最小的插入序列,或者判断不合法. 分析: eg.对于序列{ ...

  9. 2019牛客多校第四场J free——分层图&&最短路

    题意 一张无向图,每条边有权值,可以选择不超过 $k$ 条路使其权值变成0,求 $S$ 到 $T$ 的最短路.(同洛谷 P4568) 分析 首先,分层图最短路可以有效解决这种带有 「阶段性」的最短路, ...

随机推荐

  1. 【JZOJ1611】Dining

    题目描述 农夫JOHN为牛们做了很好的食品,但是牛吃饭很挑食.每一头牛只喜欢吃一些食品和饮料而别的一概不吃.虽然他不一定能把所有牛喂饱,他还是想让尽可能多的牛吃到他们喜欢的食品和饮料. 农夫JOHN做 ...

  2. poj1637&&hdu1956 混合欧拉回图判断

    欧拉路:经过所有路有且仅有1次,可以路过所有的点. 无向图:  图连通,所有点都是偶数度,或者只有两个点是奇数度.当所有点是偶数度时欧拉路起点可以是任意点:当有两个奇数度点时起点必须是奇数度点. 有向 ...

  3. Oracle安装 卸载 和常见问题

    Oracle的安装   全局数据库名:orcl  口令:orcl 或者以第三方工具SQLplus为例 系统用户:sys 和 system  练习账户:scott (密码:tiger) 登录账户为:sy ...

  4. 洛谷 2149 [SDOI2009]Elaxia的路线

    题目描述 最近,Elaxia和w的关系特别好,他们很想整天在一起,但是大学的学习太紧张了,他们 必须合理地安排两个人在一起的时间.Elaxia和w每天都要奔波于宿舍和实验室之间,他们 希望在节约时间的 ...

  5. 关于RESTful一些注意事项,和自己整理的接口开发规范

    https://blog.csdn.net/u013731455/article/details/56278168 最近在研究restful,公司开发要使用,所以自己就去网上找了好些资料,并整理了一套 ...

  6. 理解虚拟主机与VPS,云服务器CVM与云服务器ECS的区别

    1.理解虚拟主机与VPS的区别:VPS与虚拟主机的区别 2.理解云服务器CVM与云服务器ECS的区别:云服务器CVM与云服务器ECS的区别 3.锐速安装一键包

  7. Bert源码阅读

    前言 对Google开源出来的bert代码,来阅读下.不纠结于代码组织形式,而只是梳理下其训练集的生成,训练的self-attention和multi-head的具体实现. 训练集的生成 主要实现在c ...

  8. oracle优化EXPORT和IMPORT

    使用较大的BUFFER(比如10MB , 10,240,000)可以提高EXPORT和IMPORT的速度. ORACLE将尽可能地获取你所指定的内存大小,即使在内存不满足,也不会报错.这个值至少要和表 ...

  9. ngRoute

    ngRoute 模块中包含以下内容, 名称 所属 作用 ngView DIRECTIVE 提供不同路由模板插入的视图层 $routeProvider PROVIDER 提供路由配置 $route SE ...

  10. Jieba分词包(一)——解析主函数cut

    1. 解析主函数cut Jieba分词包的主函数在jieba文件夹下的__init__.py中,在这个py文件中有个cut的函数,这个就是控制着整个jieba分词包的主函数.    cut函数的定义如 ...