Let us call underpalindromity of array b of length k the minimal number of times one need to increment some elements bj by 1 so that the array b would become a palindrome, that is, b1 = bkb2 = bk - 1, and so on.

The array of length n, consisting of integers, is given. Consider all its subarrays of length k, and for each of these subarrays its underpalindromity pi. It's needed to calculate sum of all pi (1 ≤ i ≤ n - k + 1).

Input

The first line contains two integers n and k (1 ≤ k ≤ n ≤ 200000) — the length of the array and the length of subarrays.

The second line contains n integers ai ( - 108 ≤ ai ≤ 108) — the elements of the array.

Output

Output a single integer — sum of underpalindromities of all subarrays of length k.

Examples

Input
3 2
3 1 2
Output
3
Input
5 3
2 3 3 1 4
Output
4

题意:
给定一个数组,要把每一个长度为K的子串临时变为回文的(每个子串即使有重叠,也互不影响),需要加上的值的总和。 思路:
按数字从小到大排序。
树状数组我一共开了四个,分别用以维护区间内:奇数位置的数字和,奇数位置的数字个数,偶数位置的数字和,偶数位置的数字个数。
对于某一个数,和它相关的数一定是奇偶一致的。而且与它有关的数,只要算上一次就行了。和它有关的数的区间,可以O(1)算出.
设有一个数a,与其有关的有一个数b,那么这对数就有一个贡献是|a-b|
所以我们便可以将数字从小到大排序,于是就可以很好处理这个绝对值了。
然后对于某一个数,它与比它小的数字的贡献,就是和它有关的区间的数字数量*这个数-和它有关的区间的数字之和。与比它大的贡献,留给比它大的数来算。 接下来的问题就是算出和这个数的区间的。
如果左边或者右边的长度大于k,那么结果是显而易见的。如果小于,对于左边,那就只要考虑以1开始的那个子串有关就行了。
右边同理。
 #include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#define fuck(x) cout<<#x<<" = "<<x<<endl;
#define ls (t<<1)
#define rs ((t<<1)+1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = ;
const int inf = 2.1e9;
const ll Inf = ;
const int mod = ;
const double eps = 1e-;
const double pi = acos(-);
ll num1[maxn],val1[maxn],num2[maxn],val2[maxn];
int n,k;
struct node{
ll num;
int id;
}a[maxn];
int lowbit(int x){
return x&(-x);
}
void update(ll *bit,int p,ll num){
while(p<=n){
bit[p]+=num;
p+=lowbit(p);
}
} ll query(ll *bit,int p){
ll ans=;
while(p>){
ans+=bit[p];
p-=lowbit(p);
}
return ans;
} bool cmp(node a,node b){
return a.num<b.num;
} int main()
{
scanf("%d%d",&n,&k);
for(int i=;i<=n;i++){
scanf("%lld",&a[i].num);
a[i].id=i;
}
sort(a+,a++n,cmp);
ll ans=;
for(int i=;i<=n;i++){
int l,r;
if(a[i].id>=k)l=a[i].id-k+;
else{l=k-a[i].id+;}
if(a[i].id+k-<=n){r=a[i].id+k-;}
else{r=*n-a[i].id-k+; }
if((a[i].id+k- )&){
ans-=query(val1,r)-query(val1,l-);
ans+=a[i].num*(query(num1,r)-query(num1,l-));
}
else{
ans-=query(val2,r)-query(val2,l-);
ans+=a[i].num*(query(num2,r)-query(num2,l-));
}
if(a[i].id&){
update(val1,a[i].id,a[i].num);
update(num1,a[i].id,);
}
else{
update(val2,a[i].id,a[i].num);
update(num2,a[i].id,);
}
}
printf("%lld\n",ans); return ;
}

Gym - 101755G Underpalindromity (树状数组)的更多相关文章

  1. CF Gym 100463A (树状数组求逆序数)

    题意:给你一个序列,和标准序列连线,求交点数. 题解:就是求逆序对个数,用树状数组优化就行了.具体过程就是按照顺序往树状数组了插点(根据点的大小),因为第i大的点应该排在第i位,插进去的时候他前面本该 ...

  2. GYM 100741A Queries(树状数组)

    A. Queries time limit per test 0.25 seconds memory limit per test 64 megabytes input standard input ...

  3. GYM 101889F(树状数组)

    bit扫描坐标套路题,注意有重复的点,莽WA了. const int maxn = 1e5 + 5; struct node { ll B, F, D; bool operator < (con ...

  4. Codeforces Gym 100114 H. Milestones 离线树状数组

    H. Milestones Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descripti ...

  5. Gym 101908C - Pizza Cutter - [树状数组]

    题目链接:https://codeforces.com/gym/101908/problem/C 题意: 一块正方形披萨,有 $H$ 刀是横切的,$V$ 刀是竖切的,不存在大于等于三条直线交于一点.求 ...

  6. Codeforces Gym 100269F Flight Boarding Optimization 树状数组维护dp

    Flight Boarding Optimization 题目连接: http://codeforces.com/gym/100269/attachments Description Peter is ...

  7. Gym 100960G (set+树状数组)

    Problem Youngling Tournament 题目大意 给一个序列a[i],每次操作可以更改一个数,每次询问 将序列排序后有多少个数a[i]>=sum[i-1]. n<=10^ ...

  8. Gym - 100269F Flight Boarding Optimization(dp+树状数组)

    原题链接 题意: 现在有n个人,s个位置和你可以划分长k个区域你可以把s个位置划分成k个区域,这样每个人坐下你的代价是该区域内,在你之前比你小的人的数量问你怎么划分这s个位置(当然,每个区域必须是连续 ...

  9. 【容斥原理】【推导】【树状数组】Gym - 101485G - Guessing Camels

    题意:给你三个1~n的排列a,b,c,问你在 (i,j)(1<=i<=n,1<=j<=n,i≠j),有多少个有序实数对(i,j)满足在三个排列中,i都在j的前面. 暴力求的话是 ...

随机推荐

  1. LoadRunner Vuser测试脚本添加前置条件举例

    调用接口前需要先获取登陆token,放入消息头中. /* * LoadRunner Java script. (Build: 3020) * * Script Description: 接口性能测试脚 ...

  2. 获取网络图片并显示在picturbox上,byte[]数组转换成Image:

    private void getWebPicture_Click(object sender, EventArgs e) { WebRequest request = WebRequest.Creat ...

  3. 待解决ava.lang.OutOfMemoryError: PermGen space at java.lang.ClassLoader.defineClass1(Native Method)

    java.lang.OutOfMemoryError: PermGen space at java.lang.ClassLoader.defineClass1(Native Method) at ja ...

  4. 神烦之float

    另外一篇文章 : css float 一 历史 Float的设计初衷仅仅是:文字环绕效果(向word中的文字环绕效果) 二 特性 1.包裹性:块级元素如果不设置float,它默认会撑满整个屏幕,而如果 ...

  5. 开源 DotNetty 实现的 Modbus TCP/IP 协议

    本项目的目的是为了学习 DotNetty 与 Modbus 协议,参考 modjn 实现功能 0x01: Read Coils (读取线圈/离散量输出状态) 0x02: Read Discrete I ...

  6. MySQL字段属性NUll的注意点

    MySQL字段属性应该尽量设置为NOT NULL 除非你有一个很特别的原因去使用 NULL 值,你应该总是让你的字段保持 NOT NULL.这看起来好像有点争议,请往下看. 空值("&quo ...

  7. js中session操作

    // 保存数据到sessionStorage sessionStorage.setItem('key', 'value');   // 从sessionStorage获取数据 var data = s ...

  8. Github提交本地代码

     第一步:建立git仓库 cd到你的本地项目根目录下,执行git命令 git init 第二步:将项目的所有文件添加到仓库中 git add . 如果想添加某个特定的文件,只需把.换成特定的文件名即可 ...

  9. Codeforces Round #445 Div. 1

    A:每次看是否有能走回去的房间,显然最多只会存在一个,如果有走过去即可,否则开辟新房间并记录访问时间. #include<iostream> #include<cstdio> ...

  10. 清北澡堂 Day2 上午 一些比较重要的关于数论的知识整理

    1.算数基本定理: 对于任意的大于1的正整数N,N一定能够分解成有限个质数的乘积,即 其中P1<P2<...<Pk,a1,a2,...,ak>=1; 证: 存在性: 若存在最小 ...