Holedox Eating HDU - 4302 2012多校C 二分查找+树状数组/线段树优化
题意
一个长度$n<=1e5$的数轴,$m<=1e5$个操作
有两种一些操作
$0$ $x$ 在$x$放一个食物
$1$ 一个虫子去吃最近的食物,如果有两个食物一样近,不转变方向的去吃
虫子一开始在$0$点,没吃的就不动
求最终虫子跑了多远?
解法:
用数组维护每个地点有几个食物,
用树状数组维护数轴区间和,每次对于左右进行二分区间求和,找到左右最近的那个点,取最小值即可
#include <bits/stdc++.h>
#define ll long long
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define pp pair<int,int>
#define rep(ii,a,b) for(int ii=a;ii<=b;ii++)
#define per(ii,a,b) for(int ii=a;ii>=b;ii--)
#define show(x) cout<<#x<<"="<<x<<endl
#define show2(x,y) cout<<#x<<"="<<x<<" "<<#y<<"="<<y<<endl
#define show3(x,y,z) cout<<#x<<"="<<x<<" "<<#y<<"="<<y<<" "<<#z<<"="<<z<<endl
#define showa(a,b) cout<<#a<<'['<<#b<<"]="<<a[b]<<endl
using namespace std;
const int maxn=1e5+10;
const int maxm=1e6+10;
const int INF=0x3f3f3f3f;
int casn,n,m,k;
#define lb(x) (x&(-x))
int bt[maxn];
inline void upd(int pos,int x){
while(pos<=n)
bt[pos]+=x,pos+=lb(pos);
}
inline int psum(int pos,int sum=0){
while(pos)
sum+=bt[pos],pos-=lb(pos);return sum;
}
inline int rsum(int l,int r){
// show2(l,r);
return psum(r)-psum(l);
} int main(){
//#define test
#ifdef test
freopen("in.txt","r",stdin);freopen("out.txt","w",stdout);
#endif
IO;
cin>>casn;
int t=0;
while(casn--){
int ans=0;
cin>>n>>m;
n++;
memset(bt,0,sizeof bt);
int pos=1,dir=1;
for(int i=0;i<m;i++){
int a,b;
cin>>a;
if(a==0){
cin>>b;
b++;
upd(b,1);
}else {
if(rsum(pos-1,pos)){
upd(pos,-1);
// show(1);
}else {
int l=1,r=pos-1;
int x1=INF,x2=INF;
if(pos>1&&rsum(0,pos)!=0){
while(l<r){
int mid=(l+r)>>1;
if(rsum(mid,pos)>0){
l=mid+1;
}else r=mid;
}
x1=pos-l;
}
if(pos<n&&rsum(pos,n)){
l=pos+1,r=n;
while(l<r){
int mid=(l+r)>>1;
if(rsum(pos,mid)>0){
r=mid;
}else l=mid+1;
}
x2=l-pos;
}
if(x1==x2&&x1==INF){
// show(1);
continue;
}
if(x1==x2){
ans+=x1;
pos+=dir*x1;
upd(pos,-1);
}else {
ans+=min(x1,x2);
if(x1<x2){
pos-=x1;
dir=-1;
upd(pos,-1);
}else{
pos+=x2;
dir=1;
upd(pos,-1);
}
}
// show2(x1,x2);
}
}
// show(ans);
}
cout<<"Case "<<++t<<": ";
cout<<ans<<endl;
} #ifdef test
fclose(stdin);fclose(stdout);system("out.txt");
#endif
return 0;
}
Holedox Eating HDU - 4302 2012多校C 二分查找+树状数组/线段树优化的更多相关文章
- hdu 3966 Aragorn's Story(树链剖分+树状数组/线段树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3966 题意: 给出一棵树,并给定各个点权的值,然后有3种操作: I C1 C2 K: 把C1与C2的路 ...
- hdu 1166 敌兵布阵——(区间和)树状数组/线段树
pid=1166">here:http://acm.hdu.edu.cn/showproblem.php?pid=1166 Input 第一行一个整数T.表示有T组数据. 每组数据第一 ...
- HDU 1166 敌兵布阵 树状数组||线段树
http://acm.hdu.edu.cn/showproblem.php?pid=1166 题目大意: 给定n个数的区间N<=50000,还有Q个询问(Q<=40000)求区间和. 每个 ...
- HDU 3303 Harmony Forever 前缀和+树状数组||线段树
Problem Description We believe that every inhabitant of this universe eventually will find a way to ...
- hdu 1166:敌兵布阵(树状数组 / 线段树,入门练习题)
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- hdu 1166 树状数组 线段树入门
点修改 区间求和 #include <cstdio> #include <cstdlib> #include <cmath> #include <map> ...
- hdu 1166 树状数组(线段树)
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- HDU 5293 Train chain Problem - 树链剖分(树状数组) + 线段树+ 树型dp
传送门 题目大意: 一颗n个点的树,给出m条链,第i条链的权值是\(w_i\),可以选择若干条不相交的链,求最大权值和. 题目分析: 树型dp: dp[u][0]表示不经过u节点,其子树的最优值,dp ...
- hdu 5147 Sequence II【树状数组/线段树】
Sequence IITime Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem ...
随机推荐
- python 面向对象(一)初识面向对象
##################################总结#################### 1. 面向过程:一切以事物的发展流程为中心 面向对象:一切以对象为中心,一切皆为对向, ...
- 转: Linux 系统调用sysconf 获取系统配置信息
1.前言 linux提供了sysconf系统调用可以获取系统的cpu个数和可用的cpu个数. 2.sysconf 函数 man一下sysconf,解释这个函数用来获取系统执行的配置信息.例如页大小. ...
- html页面导出为excel表格
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...
- MysqL_select for update锁详解
先来举一个在某些应用场景下会出现数据不一致的例子,当然存储引擎是InnoDB(至于为什么,后面再告诉你). 电商平台常见的下单场景: 一般商品表(goods)有基本的四个字段,id(主键),goods ...
- Java调用WebService就是这么简单
https://cloud.tencent.com/developer/article/1080966
- JS创建对象之原型模式
一.原型对象 只要创建了一个新函数,就会根据一组特定的规则为该函数创建一个prototype属性,这个属性指向函数的原型对象:在默认情况下,所有原型对象都会 自动获得一个constructor(构造函 ...
- MySQL自动编号与主键
1.自动编号(AUTO_INCREMENT),必须与主键组合使用 默认情况下,起始值为1,增量也为1. 2.主键(PRIMARY KEY) 每张数据表只能存在一个主键 主键保证记录的唯一性 主键自动为 ...
- oracle rman备份
rman 登录到cmd 打开cmd 输入 rman connect target jhpt/1@orcl C:\Documents and Settings\Administrator>rman ...
- 关于"Linux下使用Windows应用程序的尝试"总结
首推 Flatpak .Flatpak爽啊,命令行启动能不爽吗!? 其他的: 0. AppImage:AppImage试了下,唉,启动TIM时就没反应,其他的应用没试过 1. crossover:收费 ...
- Ubuntu18.04终端设置为zsh后的问题记录
1. 在将终端从bash切换成zsh后,需要将 .bashrc 下的一些配置迁移到 .zshrc 中: 例如,笔者在使用zsh中使用virtualenv及virtualenvwrapper的相关命令时 ...