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 23 1 2

Output
3

Input
5 32 3 3 1 4

Output
4

题意:给定一个数组,要把每一个长度为K的子串临时变为回文的(每个子串即使有重叠,也互不影响),需要加上的值的总和。

思路:按数字从小到大排序。树状数组我一共开了四个,分别用以维护区间内:奇数位置的数字和,奇数位置的数字个数,偶数位置的数字和,偶数位置的数字个数。对于某一个数,和它相关的数一定是奇偶一致的。而且与它有关的数,只要算上一次就行了。和它有关的数的区间,可以O(1)算出.设有一个数a,与其有关的有一个数b,那么这对数就有一个贡献是|a-b|所以我们便可以将数字从小到大排序,于是就可以很好处理这个绝对值了。然后对于某一个数,它与比它小的数字的贡献,就是和它有关的区间的数字数量*这个数-和它有关的区间的数字之和。与比它大的贡献,留给比它大的数来算。

接下来的问题就是算出和这个数的区间的。如果左边或者右边的长度大于k,那么结果是显而易见的。如果小于,对于左边,那就只要考虑以1开始的那个子串有关就行了。右边同理。

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<vector>
 4 #include<stack>
 5 #include<queue>
 6 #include<map>
 7 #include<set>
 8 #include<cstdio>
 9 #include<cstring>
10 #include<cmath>
11 #include<ctime>
12 #define fuck(x) cout<<#x<<" = "<<x<<endl;
13 #define ls (t<<1)
14 #define rs ((t<<1)+1)
15 using namespace std;
16 typedef long long ll;
17 typedef unsigned long long ull;
18 const int maxn = 200086;
19 const int inf = 2.1e9;
20 const ll Inf = 999999999999999999;
21 const int mod = 1000000007;
22 const double eps = 1e-6;
23 const double pi = acos(-1);
24 ll num1[maxn],val1[maxn],num2[maxn],val2[maxn];
25 int n,k;
26 struct node{
27     ll num;
28     int id;
29 }a[maxn];
30 int lowbit(int x){
31     return x&(-x);
32 }
33 void update(ll *bit,int p,ll num){
34     while(p<=n){
35         bit[p]+=num;
36         p+=lowbit(p);
37     }
38 }
39
40 ll query(ll *bit,int p){
41     ll ans=0;
42     while(p>0){
43         ans+=bit[p];
44         p-=lowbit(p);
45     }
46     return ans;
47 }
48
49 bool cmp(node a,node b){
50     return a.num<b.num;
51 }
52
53 int main()
54 {
55     scanf("%d%d",&n,&k);
56     for(int i=1;i<=n;i++){
57         scanf("%lld",&a[i].num);
58         a[i].id=i;
59     }
60     sort(a+1,a+1+n,cmp);
61     ll ans=0;
62     for(int i=1;i<=n;i++){
63         int l,r;
64         if(a[i].id>=k)l=a[i].id-k+1;
65         else{l=k-a[i].id+1;}
66         if(a[i].id+k-1<=n){r=a[i].id+k-1;}
67         else{r=2*n-a[i].id-k+1; }
68         if((a[i].id+k-1 )&1){
69             ans-=query(val1,r)-query(val1,l-1);
70             ans+=a[i].num*(query(num1,r)-query(num1,l-1));
71         }
72         else{
73             ans-=query(val2,r)-query(val2,l-1);
74             ans+=a[i].num*(query(num2,r)-query(num2,l-1));
75         }
76         if(a[i].id&1){
77             update(val1,a[i].id,a[i].num);
78             update(num1,a[i].id,1);
79         }
80         else{
81             update(val2,a[i].id,a[i].num);
82             update(num2,a[i].id,1);
83         }
84     }
85     printf("%lld\n",ans);
86
87     return 0;
88 }

View Code

转载于:https://www.cnblogs.com/ZGQblogs/p/10553792.html

Gym - 101755G Underpalindromity (树状数组)相关推荐

  1. Gym - 101889F Fundraising(树状数组求带权最长上升子序列)

    ICPC Latin American Regional – 2017 Problem F – Fundraising Author: Paulo Cezar Pereira Costa, Brasi ...

  2. Gym - 101744E卡了我半天的题。。。。原来是才学的树状数组。。。

    最近学的不错的树状数组...比赛的时候居然忘记了,害的我卡了半天,心态炸掉了...再写一下题解 http://codeforces.com/gym/101744/problem/E 其实本题就是一个给 ...

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

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

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

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

  5. Codeforces Gym 101142 G Gangsters in Central City (lca+dfs序+树状数组+set)

    题意: 树的根节点为水源,编号为 1 .给定编号为 2, 3, 4, -, n 的点的父节点.已知只有叶子节点都是房子. 有 q 个操作,每个操作可以是下列两者之一: + v ,表示编号为 v 的房子 ...

  6. 洛谷 P5057 [CQOI2006]简单题(树状数组)

    嗯... 题目链接:https://www.luogu.org/problem/P5057 首先发现这道题中只有0和1,所以肯定与二进制有关.然后发现这道题需要支持区间更改和单点查询操作,所以首先想到 ...

  7. Color the ball(HDU1556)树状数组

    每次对区间内气球进行一次染色,求n次操作后后所有气球染色次数. 树状数组,上下区间更新都可以,差别不大. 1.对于[x,y]区间,对第x-1位减1,第y位加1,之后向上统计 #include<b ...

  8. 【BZOJ2434】[NOI2011]阿狸的打字机 AC自动机+DFS序+树状数组

    [BZOJ2434][NOI2011]阿狸的打字机 Description 阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机.打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P ...

  9. Codeforces 629D Babaei and Birthday Cake(树状数组优化dp)

    题意: 线段树做法 分析: 因为每次都是在当前位置的前缀区间查询最大值,所以可以直接用树状数组优化.比线段树快了12ms~ 代码: #include<cstdio> #include< ...

最新文章

  1. 初级开发人员的缺点_这是我想放弃初级开发人员时所做的事情
  2. oracle时间戳找回数据库,【备份恢复】 闪回数据库(三) 基于时间戳闪回数据库...
  3. docker ubuntu镜像_Docker 入门指南 | Linux 中国
  4. 如何解释混合网络?—Vecloud微云
  5. 连休8天!关于2020年国庆节、中秋节放假安排的通知!!!
  6. Anroid-async-http封装网络请求框架源码分析
  7. java判断是否第一次出现_利用java判断字符首次出现的位置,java替换最后一个特定字符...
  8. php vim 补全,Vim 不使用 tags 文件补全 PHP 代码
  9. GCC全过程详解+剖析生成的.o文件[转]
  10. 51单片机入门——8X8点阵LED
  11. lnmp一键安装的步骤
  12. 当VR踏入足球赛事会是如何?用数学运算又是如何?
  13. PAT日志 1031
  14. 基于JAVA实现的农夫过河问题
  15. 视频流TS打包方式详解
  16. 解决bug 起止时间相同 搜索不到相关数据
  17. 移动硬盘-移动硬盘提示格式化的解决办法
  18. 用vue写了个瀑布流布局,看着还可以
  19. 【软件推荐】用mamsds把高考倒计时添加到电脑桌面
  20. 音视频同步 ffmpeg 推流

热门文章

  1. 实验楼 linux内核原理与分析,《Linux内核原理与分析》第一周作业 20189210
  2. python3 tkinter详解_python tkinter基本属性详解
  3. 服务启动不了,显示 config 异常的问题排查
  4. linux 文件怎么不让删,请问如何设置权限,可以禁止用户删除文件
  5. python继承语法_python中继承父类的例子(python3的语法)
  6. PHP Calendar 函数,wordpress函数get_calendar()用法示例
  7. 怎么使用mysql打表_MySQL的表使用
  8. (stl排序+检索)大理石在哪
  9. mysql查看用户名_Mysql创建数据表的方法介绍(附示例)
  10. 基于Java+SpringBoot+vue+node.js等疫情网课管理系统详细设计和实现