luogu P4756 Added Sequence(凸包+思维)
一眼望去不会。
考虑问题中的\(f(i,j)=|\sum_{p=i}^{j}a_p |\)的实际意义。
其实就是前缀和相减的绝对值。
\(f(i,j)=|\ sum[j]-sum[i-1]\ |\)
\(f(i,j)=max(sum[j]-sum[i-1],sum[i-1]-sum[j])\)
那加上x呢。
\(f(i,j)=max[(sum[j]+xj)-(sum[i-1]+x(i-1)),(sum[i-1]+x(i-1))-(sum[j]+xj)]\)
\(sum[i]+xi\)联想到直线。
我们把这些直线放到坐标系里。
(这个图是从别人那里蒯过来的)

这是把样例转为坐标系中直线的效果图。
对于一个 \(x\) ,我们询问的实际上就是横坐标为x的两条直线纵坐标的差的最大值。
所以我们用半平面交的方法维护出上下两个凸包。
因为题目中的强制在线是假的(实际上处理出的\(x\in [-2n,2n]\))。
又因为凸包上的线段斜率是单调的,我们可以直接处理出所有 \(x\) 时的答案。
然后这题就解决了。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<map>
using namespace std;
const int N=201000;
const double eps=1e-8;
double sum[N];
struct node{
double x,y;
node(double xx=0,double yy=0){
x=xx;y=yy;
}
};
int top1,top2;
struct Line{
node u,v;
double w;
}a[N],stack1[N],stack2[N];
node operator +(node a,node b){
return node(a.x+b.x,a.y+b.y);
}
node operator -(node a,node b){
return node(a.x-b.x,a.y-b.y);
}
node operator *(node a,double b){
return node(a.x*b,a.y*b);
}
double chaji(node a,node b){
return a.x*b.y-a.y*b.x;
}
node jiao(Line a,Line b){
double A=chaji(b.u-a.u,b.v-a.u);
double B=chaji(b.v-a.v,b.u-a.v);
return a.u+(a.v-a.u)*(A/(A+B));
}
bool judge1(Line a,Line b,Line c){
node x=jiao(b,c);
if(chaji(x-a.u,a.v-a.u)+eps>=0)return true;
else return false;
}
bool judge2(Line a,Line b,Line c){
node x=jiao(b,c);
if(chaji(x-a.u,a.v-a.u)+eps<0)return true;
else return false;
}
int read(){
int sum=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){sum=sum*10+ch-'0';ch=getchar();}
return sum*f;
}
int n,m;
double w(Line a,double x){
// cout<<a.w<<endl;
return a.u.y+a.w*(x-(-2.0*n));
}
map<int,double>ans;
long long lastans;
int main(){
n=read();m=read();
for(int i=1;i<=n;i++)sum[i]=sum[i-1]+read();
for(int i=1;i<=n;i++){
a[i].u=node(-2.0*n,sum[i]+i*-2.0*n);
a[i].v=node(2.0*n,sum[i]+i*2.0*n);
a[i].w=i;
}
a[0].u=node(-2.0*n,0);a[0].v=node(2.0*n,0);
a[0].w=0;
for(int i=0;i<=n;i++){
while(top1>1&&judge1(a[i],stack1[top1-1],stack1[top1]))top1--;
stack1[++top1]=a[i];
}
for(int i=n;i>=0;i--){
while(top2>1&&judge2(a[i],stack2[top2-1],stack2[top2]))top2--;
stack2[++top2]=a[i];
}
int now1=1;int now2=1;
for(int i=-2*n;i<=n*2;i++){
while(now1<top1&&w(stack1[now1+1],i)>=w(stack1[now1],i))now1++;
while(now2<top2&&w(stack2[now2+1],i)<=w(stack2[now2],i))now2++;
double A=w(stack1[now1],i);
double B=w(stack2[now2],i);
ans[i]=A-B;
}
lastans=0;
while(m--){
int x=read();
x=(x+lastans)%(4*n+1)-2*n;
lastans=ans[x];
printf("%.0lf\n",ans[x]);
}
return 0;
}
luogu P4756 Added Sequence(凸包+思维)的更多相关文章
- BZOJ1345 Baltic2007 序列问题Sequence 【思维题】*
BZOJ1345 Baltic2007 序列问题Sequence Description 对于一个给定的序列a1,…,an,我们对它进行一个操作reduce(i),该操作将数列中的元素ai和ai+1用 ...
- luogu 2742 二维凸包
链接 luogu 模板一 上下利用斜率求凸包然后合并. #include <bits/stdc++.h> using namespace std; const int N=10005; c ...
- BZOJ 1069 Luogu P4166 最大土地面积 (凸包)
题目链接: (bzoj)https://www.lydsy.com/JudgeOnline/problem.php?id=1069 (luogu)https://www.luogu.org/probl ...
- HDU 2062:Subset sequence(思维)
Subset sequence Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
- Codeforces Round #529 (Div. 3) E. Almost Regular Bracket Sequence(思维)
传送门 题意: 给你一个只包含 '(' 和 ')' 的长度为 n 字符序列s: 给出一个操作:将第 i 个位置的字符反转('(' ')' 互换): 问有多少位置反转后,可以使得字符串 s 变为&quo ...
- Serval and Parenthesis Sequence【思维】
Serval and Parenthesis Sequence 题目链接(点击) Serval soon said goodbye to Japari kindergarten, and began ...
- luogu P4724 模板 三维凸包
LINK:三维凸包 一个非常古老的知识点.估计也没啥用. 大体上了解了过程 能背下来就背下来吧. 一个bf:暴力枚举三个点 此时只需要判断所有的点都在这个面的另外一侧就可以说明这个面是三维凸包上的面了 ...
- luogu P3829 [SHOI2012]信用卡凸包 凸包 点的旋转
LINK:信用卡凸包 当 R==0的时候显然是一个点的旋转 之后再求凸包即可. 这里先说点如何旋转 如果是根据原点旋转的话 经过一个繁杂的推导可以得到一个矩阵. [cosw,-sinw] [sinw, ...
- Funny Positive Sequence (思维+前缀)
There are n integers a 1,a 2,…,a n-1,a n in the sequence A, the sum of these n integers is larger th ...
随机推荐
- [ACM] hdu 1035 Robot Motion (模拟或DFS)
Robot Motion Problem Description A robot has been programmed to follow the instructions in its path. ...
- ETL (数据仓库技术)
ETL,是英文 Extract-Transform-Load 的缩写,用来描述将数据从来源端经过抽取(extract).转换(transform).加载(load)至目的端的过程.ETL一词较常用在数 ...
- 64位只有一种调用约定stdcall
procedure TForm2.Button1Click(Sender: TObject); function EnumWindowsProc(Ahwnd: hwnd; AlParam: lPara ...
- Ice_cream’s world III
Ice_cream's world III Time Limit : 3000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Othe ...
- javaWeb中URLEncoder.encode编码需要调用两次
今天碰到一个问题,在Controller类中一个方法跳转到该类中的另一个方法,带着中文参数,在跳转之前对该参数进行编码: msg = java.net.URLEncoder.encode(msg,&q ...
- Redis(三)、Redis主从复制
一.主从复制 主从复制:主节点负责写数据,从节点负责读数据,从而实现读写分离,提高redis的高可用性. 让一个服务器去复制(replicate)另一个服务器,我们称呼被复制的服务器为主节点(mast ...
- Redis(二)、Redis持久化RDB和AOF
一.Redis两种持久化方式 对Redis而言,其数据是保存在内存中的,一旦机器宕机,内存中的数据会丢失,因此需要将数据异步持久化到硬盘中保存.这样,即使机器宕机,数据能从硬盘中恢复. 常见的数据持久 ...
- 如何用SVG写一个环形进度条以及动画
本次案例主要使用了svg的三个元素,分别为circle.text.path,关于svg的介绍大家可以看MDN上的相关教程,传送门 由于svg可以写到HTML中,所以这里我们就可以很方便的做进度条加载动 ...
- window下svn开机自动启动
- JS网站图集相册特效
JS网站图集相册特效是一款可以直接使用鼠标进行前后导航,也可以通过缩略图来切换图片. 在线演示本地下载