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. 老男孩python学习自修第六天【pycharm的使用】

    1.在工程右键可选新建文件夹,包盒python文件 文件夹和包的区别在于,包包含一个空的__init__.py文件,而文件夹没有 2.pycharm的断点调试 点击Debug表示进入调试状态 点击Re ...

  2. How to install macOS Sierra on Skylake

    create usb installer sudo /Applications/Install\ macOS\ Sierra.app/contents/resources/createinstallm ...

  3. javap指令

    栈和局部变量操作将常量压入栈的指令aconst_null 将null对象引用压入栈iconst_m1 将int类型常量-1压入栈iconst_0 将int类型常量0压入栈iconst_1 将int类型 ...

  4. 从身份证号码中获取性别、出生日期、籍贯,并更新mongodb

    有这样的需求,人员信息是存在mongodb中,需要存放人员的身份证.性别.出生日期.籍贯等信息.通过脚本导入这些信息,但是只导入了身份证号码,其他信息空缺.现在需要补全其他信息. 其实身份证信息就包含 ...

  5. Django的View(视图)

    Django的View(视图) 一个视图函数(类),简称视图,是一个简单的Python 函数(类),它接受Web请求并且返回Web响应. 响应可以是一张网页的HTML内容,一个重定向,一个404错误, ...

  6. 微信小程序——引入背景图片【六】

    前言 之前写了一些小程序的博文只是看文档边看边写,了解下他,这次可是真枪真刀的做了! 框架使用的是美团的mpvue,我也是一边学习,一边写的,如有错误之处,还望大家指出. 在这里我有个问题,为什么微信 ...

  7. HDU3966-Aragorn's Story-树链剖分-点权

    很模板的树链剖分题 注意什么时候用线段树上的标号,什么时候用点的标号. #pragma comment(linker, "/STACK:102400000,102400000") ...

  8. codeforces 1051F The Shortest Statement

    题目链接:codeforces 1051F The Shortest Statement 题意:\(q\)组询问,求任意两点之间的最短路,图满足\(m-n\leq 20\) 分析:一开始看这道题:fl ...

  9. bzoj 4542: [Hnoi2016]大数 (莫队)

    Description 小 B 有一个很大的数 S,长度达到了 N 位:这个数可以看成是一个串,它可能有前导 0,例如00009312345.小B还有一个素数P.现在,小 B 提出了 M 个询问,每个 ...

  10. 【题解】 bzoj1135: [POI2009]Lyz (线段树+霍尔定理)

    题面戳我 Solution 二分图是显然的,用二分图匹配显然在这个范围会炸的很惨,我们考虑用霍尔定理. 我们任意选取穿\(l,r\)的号码鞋子的人,那么这些人可以穿的鞋子的范围是\(l,r+d\),这 ...