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. Java权限访问修饰符

    私有的,以 private 修饰符指定,在同一类内可见. 默认的,也称为 default,在同一包内可见,不使用任何修饰符. 受保护的,以 protected 修饰符指定,对同一包内的类和所有子类可见 ...

  2. win10 新建文件夹没有了

    1运行-regedit 2 在打开的注册表编辑器窗口,展开HKEY_CLASSES_ROOT,在HKEY_CLASSES_ROOT展开项中找到:Directory,再依次展开:Directory\Ba ...

  3. spawn

    转载:http://motioo.blog.163.com/blog/static/117718291200954102830215/ 并行计算使用的节点数在开始运行程序时进行指定. 学习了FFT之后 ...

  4. <c:forEach>可以默认的把以逗号分隔的字符串作为一个集合来遍历

    <c:forEach>可以默认的把以逗号分隔的字符串作为一个集合来遍历

  5. .net core 2.0 MVC区域

    区域 创建对应的目录结构 Areas System Controllers Views 在Startup.cs 注册路由 在控制器上方加上`[Area("system")]` // ...

  6. Codeforces Round #440 Div. 1

    A:显然应该尽量拆成4.如果是奇数,先拆一个9出来即可. #include<iostream> #include<cstdio> #include<cmath> # ...

  7. 提高SqlServer数据库的安全性,禁用掉sa账户

    Sqlsever 数据库有两种登陆身份验证模式,一种是windows身份验证:一种是sqlserver 账户验证模式,在sqlserver 账户验证模式中,sa账户是大家所熟知的,并且sa也是内置的默 ...

  8. 二分图最小点覆盖König定理的简单证明 (加入自己理解)

    第一次更改:http://blog.sina.com.cn/s/blog_51cea4040100h152.html 讲的更细致 增广路:https://blog.csdn.net/qq_374572 ...

  9. SpringMVC 集成Log4j

    项目地址:https://github.com/xiaoqiu-duan/DataProject.git 1.添加jar <dependency> <groupId>org.s ...

  10. My Brute HDU - 3315(KM || 费用流)

    题意: 有S1到Sn这n个勇士要和X1到Xn这n个勇士决斗,初始时,Si的决斗对象是Xi. 如果Si赢了Xi,那么你将获得Vi分,否则你将获得-Vi分. Si和Xi对决时,Si有初始生命Hi,初始攻击 ...