int find_kth(int k)
{
int ans = , cnt = , i;
for (i = ; i >= ; i--)/
{
ans += ( << i);
if (ans >= maxn|| cnt + c[ans] >= k)
ans -= ( << i);
else
cnt += c[ans];
}
return ans + ;
}

题意:

口袋里可以放进不同大小的糖果,也可以拿出不同大小的糖果。查询过程中第k小的糖果体积是多少

输入T,有T组,接下来是输入m次操作

m行输入op和x

当op等于1的时候,放进x大小的糖果

当op等于2的时候,拿进x大小的糖果

当op等于3的时候,查询第x小的糖果,并输出答案

思路:二分+树状数组(比赛时限)

这里写一个优化的树状数组的写法

 #include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<string>
#define mem(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f
#define ll long long
#define lowbit(b) b&(-b)
using namespace std;
const int maxn=1e5+;
int c[maxn],f[];
inline int read() {
char ch = getchar(); int x = , f = ;
while(ch < '' || ch > '') {
if(ch == '-') f = -;
ch = getchar();
} while('' <= ch && ch <= '') {
x = x * + ch - '';
ch = getchar();
} return x * f;
}
inline void updata(int x, int y){
for (int i = x; i <= maxn; i += lowbit(i)){
c[i] += y;
}
}
int main()
{
int t,i;f[]=;
for(i=;i<=;i++){
f[i]=f[i-]*;
}
t=read();
while(t--){
for(i=;i<maxn;i++){c[i]=;}
int m;
m=read();
for(i=;i<m;i++){
int op,x;
op=read();x=read();
if(op==){
updata(x,);
}
else if(op==){
updata(x,-);
}
else{
int ans=,sum=,j;
for(j=;j>=;j--){
ans+=f[j];
if(ans>=maxn || sum+c[ans]>=x){
ans-=f[j];
}
else{
sum+=c[ans];
}
}
printf("%d\n",ans+);
}
}
}
return ;
}
/*
2
3
1 2
1 2
3 1
4
1 2
1 3
2 2
3 1
*/

19市赛 树状数组 第k个小的糖果的更多相关文章

  1. POJ2828 Buy Tickets[树状数组第k小值 倒序]

    Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 19012   Accepted: 9442 Desc ...

  2. hdu 5493 Queue 树状数组第K大或者二分

    Queue Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

  3. XJTUOJ wmq的队伍(树状数组求 K 元逆序对)

    题目链接:http://oj.xjtuacm.com/problem/14/[分析]二元的逆序对应该都会求,可以用树状数组.这个题要求K元,我们可以看成二元的.我们先从后往前求二元逆序对数, 然后对于 ...

  4. hrbust 1840 (树状数组第k大) 删点使用

    小橙子 Time Limit: 2000 MS Memory Limit: 32768 K Total Submit: 2(2 users) Total Accepted: 1(1 users) Ra ...

  5. Ryuji doesn't want to study 2018徐州icpc网络赛 树状数组

    Ryuji is not a good student, and he doesn't want to study. But there are n books he should learn, ea ...

  6. BZOJ.3110.[ZJOI2013]K大数查询(整体二分 树状数组/线段树)

    题目链接 BZOJ 洛谷 整体二分求的是第K小(利用树状数组).求第K大可以转为求第\(n-K+1\)小,但是这样好像得求一个\(n\). 注意到所有数的绝对值\(\leq N\),将所有数的大小关系 ...

  7. 树状数组求第K小值 (spoj227 Ordering the Soldiers &amp;&amp; hdu2852 KiKi&#39;s K-Number)

    题目:http://www.spoj.com/problems/ORDERS/ and pid=2852">http://acm.hdu.edu.cn/showproblem.php? ...

  8. 4.9 省选模拟赛 划分序列 二分 结论 树状数组优化dp

    显然发现可以二分. 对于n<=100暴力dp f[i][j]表示前i个数分成j段对于当前的答案是否可行. 可以发现这个dp是可以被优化的 sum[i]-sum[j]<=mid sum[i] ...

  9. bzoj4361 isn(树状数组优化dp+容斥)

    4361: isn Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 938  Solved: 485[Submit][Status][Discuss] ...

随机推荐

  1. UVA 12097 Pie(二分答案)

    题目链接: 这道题虽然不是一道典型的二分答案题,但同样也可以用二分答案来做. 来二分面积为$area$的派,然后看看条件是否矛盾. 与其矛盾的便是$f+1$个人是否每个人都会有. 一个半径为$r$的派 ...

  2. 微信小程序 (组件的使用)

    创建组件 在根目录创建components目录,然后创建列表组件 组件中内容 <view class="prolist"> //循环prolist列表 <view ...

  3. 阿里云打包成zip格式,批量下载

    /** * 从阿里云下载文件 (以附件形式下载) 多个文件进行zip下载 第二种 * @param request * @param response */ @ResponseBody @Reques ...

  4. kuangbin专题 专题九 连通图 POJ 1236 Network of Schools

    题目链接:https://vjudge.net/problem/POJ-1236 题目:有向图,有若干个连通图,点之间有单向边边就可以单向传递信息,问: (1)至少需要发送几份信息才能使得每个点都传递 ...

  5. SqlDataReader阅读器关闭时尝试调用 HasRows 无效

    SqlDataReader阅读器关闭时尝试调用 HasRows 无效 原创长白山上放羊娃 发布于2018-07-25 00:29:27 阅读数 538  收藏 展开 在SqlHelper中封装好的Sq ...

  6. 计算几何-多边形内核判定-HPI-poj3335

    This article is made by Jason-Cow.Welcome to reprint.But please post the article's address. 先解决一个问题, ...

  7. Mysql查看连接数(连接总数、活跃数、最大并发数)

    show variables like '%max_connection%'; set global max_connections=1000;

  8. Git - 07. gitignore

    1. 概述 开发的过程中, 无法保证项目文件夹下的所有东西, 都是想传到版本库的 比如 maven 项目的 target 目录 配置好之后, 使用 git add .命令, 这些文件\目录也不会被 s ...

  9. mysql 导出sql文件

    1:find  / -name mysqldump -print 先找到mysqldump 目录位置 2:/usr/local/mysql/bin/mysqldump -u root -p hanwe ...

  10. python-用正则表达式筛选文本信息

    [摘要]  本文主要介绍如何对多个文本进行读取,并采用正则表达式对其中的信息进行筛选,将筛选出来的信息存写到一个新文本. 打开文件:open(‘文件名’,‘打开方式’)>>>file ...