Gym - 101755G Underpalindromity (树状数组)
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 = bk, b2 = 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
3 2
3 1 2
3
5 3
2 3 3 1 4
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 (树状数组)的更多相关文章
- CF Gym 100463A (树状数组求逆序数)
题意:给你一个序列,和标准序列连线,求交点数. 题解:就是求逆序对个数,用树状数组优化就行了.具体过程就是按照顺序往树状数组了插点(根据点的大小),因为第i大的点应该排在第i位,插进去的时候他前面本该 ...
- GYM 100741A Queries(树状数组)
A. Queries time limit per test 0.25 seconds memory limit per test 64 megabytes input standard input ...
- GYM 101889F(树状数组)
bit扫描坐标套路题,注意有重复的点,莽WA了. const int maxn = 1e5 + 5; struct node { ll B, F, D; bool operator < (con ...
- Codeforces Gym 100114 H. Milestones 离线树状数组
H. Milestones Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descripti ...
- Gym 101908C - Pizza Cutter - [树状数组]
题目链接:https://codeforces.com/gym/101908/problem/C 题意: 一块正方形披萨,有 $H$ 刀是横切的,$V$ 刀是竖切的,不存在大于等于三条直线交于一点.求 ...
- Codeforces Gym 100269F Flight Boarding Optimization 树状数组维护dp
Flight Boarding Optimization 题目连接: http://codeforces.com/gym/100269/attachments Description Peter is ...
- Gym 100960G (set+树状数组)
Problem Youngling Tournament 题目大意 给一个序列a[i],每次操作可以更改一个数,每次询问 将序列排序后有多少个数a[i]>=sum[i-1]. n<=10^ ...
- Gym - 100269F Flight Boarding Optimization(dp+树状数组)
原题链接 题意: 现在有n个人,s个位置和你可以划分长k个区域你可以把s个位置划分成k个区域,这样每个人坐下你的代价是该区域内,在你之前比你小的人的数量问你怎么划分这s个位置(当然,每个区域必须是连续 ...
- 【容斥原理】【推导】【树状数组】Gym - 101485G - Guessing Camels
题意:给你三个1~n的排列a,b,c,问你在 (i,j)(1<=i<=n,1<=j<=n,i≠j),有多少个有序实数对(i,j)满足在三个排列中,i都在j的前面. 暴力求的话是 ...
随机推荐
- 在Linq to sql 和 Entity framework 中使用lambda表达式实现left join
在Linq to sql 和 Entity framework 中使用lambda表达式实现left join 我们知道lambda表达式在Linq to sql 和 Entity framework ...
- fftshift
说明:本文为转载http://blog.csdn.net/myathappy/article/details/51344618 Matlab fftshift 详解 一.实信号情况 因为实信号以fs为 ...
- JavaScript的 sourcemap 的理解
当我们在使用vue-cli 开发项目完成后, 就要进行部署,执行npm run build 命令,你会发现它生成.js文件的同时,还会生成一个对应的.map 文件. 当时查了一下, .map 文件的主 ...
- 在没有 Emacs 的情况下使用 Org 模式
导读 每到年初似乎总有这么一个疯狂的冲动来寻找提高生产率的方法.新年决心,正确地开始一年的冲动,以及“向前看”的态度都是这种冲动的表现.软件推荐通常都会选择闭源和专利软件.但这不是必须的. 这是我 2 ...
- maven加载本地jar包到repository
maven加载本地jar到repository 这是一个常见场景,此处以本地opencv jar文件导入repository为例 1.Ubuntu下 mvn install:install-file ...
- DRF 分页组件
Django Rest Framework 分页组件 DRF的分页 为什么要使用分页 其实这个不说大家都知道,大家写项目的时候也是一定会用的, 我们数据库有几千万条数据,这些数据需要展示,我们不可能直 ...
- 对Redis的理解
1.redis使用的场景 热点数据(经常会被查询,但是不经常被修改或者删除的数据)
- SSL加速卡调研的原因及背景
SSL加速卡调研的原因及背景 SSL加速卡调研的原因及背景 网络信息安全已经成为电子商务和网络信息业发展的一个瓶颈,安全套接层(SSL)协议能较好地解决安全处理问题,而SSL加速器有效地提高了网络安全 ...
- fullcalendar 日历插件3.9.0 -- 基本插件使用
以下主要结构,直接执行即可以使用 ,仅用参考: html: <!DOCTYPE html> <html> <head> <title>test</ ...
- opencv 仿射变换
import cv2 as cv import numpy as np img = cv.imread('../images/face.jpg') h, w = img.shape[:2] mat_s ...