思路:

权值线段树 (找中位数用的) 记录下出现的次数和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 线段树的更多相关文章

  1. BZOJ 1798 (线段树||分块)的标记合并

    我原来准备做方差的.. 结果发现不会维护两个标记.. 就是操作变成一个 a*x+b ,每次维护a , b 即可 加的时候a=1 ,b=v 乘的时候a=v ,b=0 #include <cstdi ...

  2. bzoj 3999 线段树区间提取 有序链剖

    看错题目了,想成每个城市都可以买一个东西,然后在后面的某个城市卖掉,问最大收益.这个可以类似维护上升序列的方法在O(nlog^3n)的时间复杂度内搞定 这道题用到的一些方法: 1. 可以将有关的线段提 ...

  3. bzoj 3211 线段树

    开方操作最多进行5次就可以把出现的任何数变成1. 所以用线段树暴力修改,以后修改时只需看一下是否当前区间都是0或1,如果是那么就直接返回. /***************************** ...

  4. bzoj 1018 线段树维护连通性

    本题将一道LCT的题特殊化(支持加边和删边,询问图的连通性),将图变成了2×m的网格图,然后就神奇地可以用线段树来维护. 对于每个区间[l,r],维护其四个角落之间的连通性(仅仅通过[l,r]这段的边 ...

  5. bzoj 3212 线段树

    裸的线段树 /************************************************************** Problem: User: BLADEVIL Langua ...

  6. bzoj 2120 线段树套平衡树

    先吐下槽,改了快一个小时,最后发现是SBT的delete写错了,顿时就有想死的心..... 首先对于这道题,我们应该先做一下他的小问题,bzoj1878,虽然和这道题几乎一点关系没有, 但是能给我们一 ...

  7. bzoj 1901 线段树套平衡树+二分答案查询

    我们就建一颗线段树,线段树的每一个节点都是一颗平衡树,对于每个询问来说,我们就二分答案, 查询每个二分到的mid在这个区间里的rank,然后就行了 /************************* ...

  8. BZOJ 1012 线段树||单调队列

    非常裸的线段树  || 单调队列: 假设一个节点在队列中既没有时间优势(早点入队)也没有值优势(值更大),那么显然不管在如何的情况下都不会被选为最大值. 既然它仅仅在末尾选.那么自然能够满足以上的条件 ...

  9. BZOJ 3681 线段树合并+网络流

    思路: 暴力建图有n*m条边 考虑怎么优化 (那就只能加个线段树了呗) 然后我就不会写了..... 抄了一波题解 //By SiriusRen #include <bits/stdc++.h&g ...

随机推荐

  1. Vim配置及使用

    Vim配置 1.打开~/.vimrc,将以下内容考入文件.vimrc中 "行号" set nu "高亮" syntax enable syntax on &qu ...

  2. 2015 Multi-University Training Contest 1 OO’s Sequence

    OO’s Sequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

  3. POJ——T3352 Road Construction

    http://poj.org/problem?id=3352 vis表示访问的次序 low的值相同的点在同一连通分量 #include <algorithm> #include <c ...

  4. zzulioj--1828-- 贪心的小猫咪(贪心模拟)

    1828: 贪心的小猫咪 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 14  Solved: 4 SubmitStatusWeb Board Des ...

  5. nyoj--1011--So Easy[II](数学几何水题)

    So Easy[II] 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描述 这是一道基础的计算几何问题(其实这不提示大家也都看的出).问题描述如下: 给你一个N边形.且N边形 ...

  6. android客户端向java服务端post发送json

    android 端: private void HttpPostData() {        try { HttpClient httpclient = new DefaultHttpClient( ...

  7. spring boot整合mail

    1.添加依赖 </dependency> <dependency> <groupId>org.springframework.boot</groupId> ...

  8. salt的grains

    grains作用: 1.匹配 minion 2.收集信息 (每次重启minion才会收集) grains 数据存储在minion端. salt '*' grains.ls salt '*' grain ...

  9. linux 下 .sh 文件语法

    转自:http://blog.sina.com.cn/s/blog_54f82cc201010hfz.html 介绍: 1 开头 程序必须以下面的行开始(必须方在文件的第一行): #!/bin/sh ...

  10. 安装Debugging Tools时出现错误Setup could not find the file WinSDK_amd64的处理

    安装Debugging Tools时出现错误Setup could not find the file WinSDK_amd64的处理 1.软件来源:  微软官网下载SDK ISO安装包(含有debu ...