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. Delphi中Owner和Parent的区别

    Owner为创建者,Parent为容器 他们的类型不同,Owner为TComponent(元件),Parent为TWinControl(窗体控件) Parent属性是指构件的包容器,构件只能在此范围内 ...

  2. elasticsearch索引合并

    参考地址:http://cwiki.apachecn.org/display/Elasticsearch/Reindex+API 1.首先插入准备数据,创建两个索引. (1).PUT  http:// ...

  3. Windows上安装 TensorFlow及简单命令

    1.官网及帮助文档 官网: https://www.tensorflow.org/install/install_windows 中文帮助文档:https://efeiefei.gitbooks.io ...

  4. How to enable AHCI on Windows7

    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\msahci

  5. Web API 2 自定义默认Identity Table Name

    One of the first issues you will likely encounter when getting started with ASP.NET Identity centers ...

  6. 使用RestTemplate测试视频上传的Post请求

    以往多用RestTemplate处理接口的调用以及与Ribbon/Feign配合使用调用微服务接口,近日写了一个处理Post文件上传的解决方案,其实就是将后台所需的MultipartFile,在请求P ...

  7. Calendar代替Date常用方法小记

    业务逻辑中遇到日期的相关操作,整理一下小做总结 日期操作离不开 java.util.Calendar 和 java.util.Date ,实体类中日期字段设计也可能用到java.sql.Date.但目 ...

  8. BZOJ1823[JSOI2010]满汉全席——2-SAT+tarjan缩点

    题目描述 满汉全席是中国最丰盛的宴客菜肴,有许多种不同的材料透过满族或是汉族的料理方式,呈现在數量繁多的菜色之中.由于菜色众多而繁杂,只有极少數博学多闻技艺高超的厨师能够做出满汉全席,而能够烹饪出经过 ...

  9. CF558E-A Simple Task-线段树+计数排序

    计数排序的原理,只要知道了有几个数比i小,就可以知道i的位置 这道题只有26个字母,搞26颗线段树,然后区间更新 #include <cstdio> #include <cstrin ...

  10. 洛谷3707 [SDOI2017] 相关分析 【线段树】

    分析: 化简一下就行了,注意一下平方和公式的运用以及精度的误差. 代码: #include<bits/stdc++.h> using namespace std; ; int n,m; i ...