BZOJ 1112 线段树
思路:
权值线段树 (找中位数用的) 记录下出现的次数和sum
一定要注意 有可能中位数的值有许多数 这怎么办呢 (离散化以后不去重就行了嘛…….)
(为什么他们想得那么麻烦)
//By SiriusRen
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 100500
#define int long long
int n,k,sum[N*10],tree[N*10],xx,ans=100000LL*1000000LL;
struct Node{
int num,pos;
friend bool operator < (Node a,Node b){
if(a.num!=b.num)return a.num<b.num;
return a.pos<b.pos;
}
}node[N],cpy[N];
void insert(int l,int r,int pos,int wei,int num){
if(l==r){
tree[pos]+=wei;
if(tree[pos])sum[pos]=cpy[l].num;
else sum[pos]=0;
return;
}
int mid=(l+r)>>1,lson=pos<<1,rson=pos<<1|1;
if(mid>=num)insert(l,mid,lson,wei,num);
else insert(mid+1,r,rson,wei,num);
tree[pos]=tree[lson]+tree[rson];
sum[pos]=sum[lson]+sum[rson];
}
void query(int l,int r,int pos,int num){
if(l==r){xx=l;return;}
int mid=(l+r)>>1,lson=pos<<1,rson=pos<<1|1;
if(tree[lson]>=num)query(l,mid,lson,num);
else query(mid+1,r,rson,num-tree[lson]);
}
int query_sum(int l,int r,int pos,int L,int R){
if(l>=L&&r<=R)return sum[pos];
int mid=(l+r)>>1,lson=pos<<1,rson=pos<<1|1;
if(mid<L)return query_sum(mid+1,r,rson,L,R);
else if(mid>=R)return query_sum(l,mid,lson,L,R);
else return query_sum(l,mid,lson,L,R)+query_sum(mid+1,r,rson,L,R);
}
signed main(){
scanf("%lld%lld",&n,&k);
for(int i=1;i<=n;i++)
scanf("%lld",&node[i].num),node[i].pos=i,cpy[i]=node[i];
sort(cpy+1,cpy+1+n);
for(int i=1;i<k;i++)
insert(1,n,1,1,lower_bound(cpy+1,cpy+1+n,node[i])-cpy);
for(int i=k;i<=n;i++){
int temp=0;
insert(1,n,1,1,lower_bound(cpy+1,cpy+1+n,node[i])-cpy);
query(1,n,1,k/2+1);
temp=cpy[xx].num*(k/2+1)-query_sum(1,n,1,1,xx);
temp+=query_sum(1,n,1,xx+1,n)-cpy[xx].num*(k-(k/2+1));
ans=min(ans,temp);
insert(1,n,1,-1,lower_bound(cpy+1,cpy+1+n,node[i-k+1])-cpy);
}
printf("%lld\n",ans);
}
BZOJ 1112 线段树的更多相关文章
- BZOJ 1798 (线段树||分块)的标记合并
我原来准备做方差的.. 结果发现不会维护两个标记.. 就是操作变成一个 a*x+b ,每次维护a , b 即可 加的时候a=1 ,b=v 乘的时候a=v ,b=0 #include <cstdi ...
- bzoj 3999 线段树区间提取 有序链剖
看错题目了,想成每个城市都可以买一个东西,然后在后面的某个城市卖掉,问最大收益.这个可以类似维护上升序列的方法在O(nlog^3n)的时间复杂度内搞定 这道题用到的一些方法: 1. 可以将有关的线段提 ...
- bzoj 3211 线段树
开方操作最多进行5次就可以把出现的任何数变成1. 所以用线段树暴力修改,以后修改时只需看一下是否当前区间都是0或1,如果是那么就直接返回. /***************************** ...
- bzoj 1018 线段树维护连通性
本题将一道LCT的题特殊化(支持加边和删边,询问图的连通性),将图变成了2×m的网格图,然后就神奇地可以用线段树来维护. 对于每个区间[l,r],维护其四个角落之间的连通性(仅仅通过[l,r]这段的边 ...
- bzoj 3212 线段树
裸的线段树 /************************************************************** Problem: User: BLADEVIL Langua ...
- bzoj 2120 线段树套平衡树
先吐下槽,改了快一个小时,最后发现是SBT的delete写错了,顿时就有想死的心..... 首先对于这道题,我们应该先做一下他的小问题,bzoj1878,虽然和这道题几乎一点关系没有, 但是能给我们一 ...
- bzoj 1901 线段树套平衡树+二分答案查询
我们就建一颗线段树,线段树的每一个节点都是一颗平衡树,对于每个询问来说,我们就二分答案, 查询每个二分到的mid在这个区间里的rank,然后就行了 /************************* ...
- BZOJ 1012 线段树||单调队列
非常裸的线段树 || 单调队列: 假设一个节点在队列中既没有时间优势(早点入队)也没有值优势(值更大),那么显然不管在如何的情况下都不会被选为最大值. 既然它仅仅在末尾选.那么自然能够满足以上的条件 ...
- BZOJ 3681 线段树合并+网络流
思路: 暴力建图有n*m条边 考虑怎么优化 (那就只能加个线段树了呗) 然后我就不会写了..... 抄了一波题解 //By SiriusRen #include <bits/stdc++.h&g ...
随机推荐
- 让div垂直居中
参考链接:https://www.cnblogs.com/softwarefang/p/6095806.html 以前我的方法总是比较粗暴,纯粹通过margin来实现,这个方法的缺点不仅在于需要多次微 ...
- Redis学习总结(3)——Redis整合Spring结合使用缓存实例
摘要:本文介绍了如何在Spring中配置redis,并通过Spring中AOP的思想,将缓存的方法切入到有需要进入缓存的类或方法前面. 一.Redis介绍 什么是Redis? redis是一个key- ...
- EularProject 42:单词解码出来的三角形数
Coded triangle numbers Problem 42 The nth term of the sequence of triangle numbers is given by, tn = ...
- nyoj33 蛇形填数
蛇形填数 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描写叙述 在n*n方陈里填入1,2,...,n*n,要求填成蛇形.比如n=4时方陈为: 10 11 12 1 9 16 ...
- [MST] Attach Behavior to mobx-state-tree Models Using Actions
Models are not just a nifty feature for type checking. They enable you to attach behavior to your ac ...
- Homebrew命令具体解释
Homebrew命令具体解释 作者:chszs,未经博主同意不得转载.经许可的转载需注明作者和博客主页:http://blog.csdn.net/chszs 一.安装Homebrew Shell环境下 ...
- CentOS-6.4-minimal版中源代码安装MySQL-5.5.38
完整版见https://jadyer.github.io/2014/09/29/centos-install-mysql/ /** * CentOS-6.4-minimal版中源代码安装MySQL-5 ...
- jsoncpp-src-0.5.0.tar.gz 源码错误!!!!
近期在做毕设,使用到了JsonCpp0.5.0版本号的源码! 依照网上的安装配置教程,搭建好环境后就能够使用了! 在这里就不浪费空间去将怎样搭建开发环境了!请大家去google一下就好了! 在解析一个 ...
- zzulioj--1746--三角形面积(几何水题)
1746: 三角形面积 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 100 Solved: 31 SubmitStatusWeb Board De ...
- HBase框架基础(一)
* HBase框架基础(一) 官方网址:http://hbase.apache.org/ * HBase是什么妖怪? 要解释HBase,我们就先说一说经常接触到的RDBMS,即关系型数据库: ** m ...