[AH2017/HNOI2017]影魔(主席树+单调栈)
设\(l[i]\)为i左边第一个比i大的数的下标。\(r[i]\)为i右边第一个比i大的数的下标。
我们把\(p1,p2\)分开考虑。
当产生贡献为\(p1\)时\(i\)和\(j\)一定满足,分别为\(l[x],r[x]\)枚举每一个值为\(i\),\(j\)之间最大值可证。
党产生贡献为\(p2\)时\(i\)和\(j\)满足分别为\(l[x],[x+1,r[x]-1]\)或\([l[x]+1,x-1],r[x]\),此时\(a[x]\)为\(i\),\(j\)之间最大值,\(i\),\(j\)一个比\(a[x]\)大,一个比\(a[x]\)小。
然后就把问题转化为二维数点问题。产生贡献的点对对应坐标系中的一个点(为了避免重复计数如\((r[x],l[x])\)和\((l[x],r[x])\),可以把小的作为横坐标,大的作为纵坐标)。然后我们每一次询问就是横坐标在\([l,r]\)之间,纵坐标在\([l,r]\)之间的权值和。
然后就可以用主席树做了。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;
#define int long long
const int N=201000;
struct line{ int l,r,w; };
vector<line> vec[N];
int tot,root[N],lazy[N*70],ch[N*70][2],sum[N*70];
int n,m,p1,p2,a[N],stack[N],top,l[N],r[N];
void add(int l,int r,int L,int R,int w,int pre,int &now){
now=++tot;
ch[now][0]=ch[pre][0];
ch[now][1]=ch[pre][1];
lazy[now]=lazy[pre];
sum[now]=sum[pre]+(R-L+1)*w;
if(l==L&&r==R){
lazy[now]+=w;
return;
}
int mid=(l+r)>>1;
if(L>mid)add(mid+1,r,L,R,w,ch[pre][1],ch[now][1]);
else if(R<=mid)add(l,mid,L,R,w,ch[pre][0],ch[now][0]);
else{
add(l,mid,L,mid,w,ch[pre][0],ch[now][0]);
add(mid+1,r,mid+1,R,w,ch[pre][1],ch[now][1]);
}
}
int check(int l,int r,int L,int R,int pre,int now){
if(l==L&&r==R)return sum[now]-sum[pre];
int mid=(l+r)>>1;
if(L>mid)return (lazy[now]-lazy[pre])*(R-L+1)+check(mid+1,r,L,R,ch[pre][1],ch[now][1]);
else if(R<=mid)return (lazy[now]-lazy[pre])*(R-L+1)+check(l,mid,L,R,ch[pre][0],ch[now][0]);
else return (lazy[now]-lazy[pre])*(R-L+1)
+check(l,mid,L,mid,ch[pre][0],ch[now][0])
+check(mid+1,r,mid+1,R,ch[pre][1],ch[now][1]);
}
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;
}
signed main(){
n=read(),m=read(),p1=read(),p2=read();
for(int i=1;i<=n;i++)a[i]=read();
for(int i=1;i<=n;i++){
while(a[i]>a[stack[top]]&&top)r[stack[top--]]=i;
stack[++top]=i;
}
while(top)r[stack[top--]]=n+1;
for(int i=n;i>=1;i--){
while(a[i]>a[stack[top]]&&top)l[stack[top--]]=i;
stack[++top]=i;
}
while(top)l[stack[top--]]=0;
for(int i=1;i<=n;i++){
line x;
if(i!=n&&i+1<=r[i]-1){
x.l=i+1;x.r=r[i]-1;x.w=p2;
vec[l[i]].push_back(x);
}
if(l[i]+1<=i-1&&i!=1){
x.l=l[i]+1;x.r=i-1;x.w=p2;
vec[r[i]].push_back(x);
}
x.l=i;x.r=i;x.w=p1;
vec[l[i]].push_back(x);
vec[r[i]].push_back(x);
}
for(int i=1;i<=n;i++){
root[i]=root[i-1];
for(int j=0;j<vec[i].size();j++)
add(1,n,vec[i][j].l,vec[i][j].r,vec[i][j].w,root[i],root[i]);
}
for(int i=1;i<=m;i++){
int l=read(),r=read();
printf("%lld\n",check(1,n,l,r,root[l-1],root[r]));
}
return 0;
}
[AH2017/HNOI2017]影魔(主席树+单调栈)的更多相关文章
- bzoj 4826: [Hnoi2017]影魔 [主席树 单调栈]
4826: [Hnoi2017]影魔 题意:一个排列,点对\((i,j)\),\(p=max(i+1,j-1)\),若\(p<a_i,a_j\)贡献p1,若\(p\)在\(a_1,a_2\)之间 ...
- 【BZOJ3956】Count 主席树+单调栈
[BZOJ3956]Count Description Input Output Sample Input 3 2 0 2 1 2 1 1 1 3 Sample Output 0 3 HINT M,N ...
- [BZOJ4826][HNOI2017]影魔(主席树)
4826: [Hnoi2017]影魔 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 669 Solved: 384[Submit][Status][ ...
- 洛谷P3722 [AH2017/HNOI2017]影魔(线段树)
题意 题目链接 Sol 题解好神仙啊qwq. 一般看到这种考虑最大值的贡献的题目不难想到单调数据结构 对于本题而言,我们可以预处理出每个位置左边第一个比他大的位置\(l_i\)以及右边第一个比他大的位 ...
- [AH2017/HNOI2017] 影魔 - 线段树
#include<bits/stdc++.h> #define maxn 200010 using namespace std; int a[maxn],st[maxn][2],top,L ...
- Codeforces 781E Andryusha and Nervous Barriers 线段树 单调栈
原文链接https://www.cnblogs.com/zhouzhendong/p/CF781E.html 题目传送门 - CF781E 题意 有一个矩形,宽为 w ,高为 h .一开始会有 w 个 ...
- 洛谷P4425 转盘 [HNOI/AHOI2018] 线段树+单调栈
正解:线段树+单调栈 解题报告: 传送门! 1551又是一道灵巧连题意都麻油看懂的题,,,,所以先解释一下题意好了,,,, 给定一个n元环 可以从0时刻开始从任一位置出发 每次可以选择向前走一步或者在 ...
- 线段树+单调栈+前缀和--2019icpc南昌网络赛I
线段树+单调栈+前缀和--2019icpc南昌网络赛I Alice has a magic array. She suggests that the value of a interval is eq ...
- 牛客多校第四场sequence C (线段树+单调栈)
牛客多校第四场sequence C (线段树+单调栈) 传送门:https://ac.nowcoder.com/acm/contest/884/C 题意: 求一个$\max {1 \leq l \le ...
随机推荐
- Python 函数部分练习题
函 数 基 础 1.写函数,,用户传入修改的文件名,与要修改的内容,执行函数,完成批了修改操作2.写函数,计算传入字符串中[数字].[字母].[空格] 以及 [其他]的个数 3.写函数,判断用户传入的 ...
- oracle中单引号的处理
当想让输出的结果中字段带有单引号', 场景一:连续三个单引号''' select '''helin''' from dual; ---'helin' 场景二:拼接字段的结果集--连续4个单引号 sel ...
- Lua 中的 RSA 加解密实现
记得之前,部门某款游戏陆陆续续收到一些玩家反馈,抱怨在登录游戏时会等待很久.初步排查后基本断定可能是此游戏的登录服务器程序某块代码有问题,于是即安排了服务器同事作排查分析但一直无果. 之后我时间有了空 ...
- kafka内外网集群配置
linux下配置使用以第一台为例(先配置好jdk环境)1.解压kafka:2.10-0.10.1.12.修改zookeeper.properties 新增配置:maxClientCnxns=0 tic ...
- Linux下安装Solr7.5.0,并部署到Tomcat
收藏地址:https://blog.csdn.net/qq_39135287/article/details/84260724
- pytorch 7 optimizer 优化器 加速训练
import torch import torch.utils.data as Data import torch.nn.functional as F import matplotlib.pyplo ...
- SpringBoot下支付宝接口的使用
SpringBoot下支付宝接口的使用 前期准备: 参考之前写过的 支付宝接口引入servlet版本 Jar包引入: <!-- 支付宝 --> <dependency> < ...
- we标签
功能: · 辅助标签.配合其它标签使用,防止与标准html标签冲突 · 别名为test 使用方法: <we [name=key]>[value]</we& ...
- C++ Primer Plus的若干收获--(九)
这篇博文我接着上一篇来写,相同讲一些关于类的一些基础知识. 本篇将会继续使用上篇的股票类STock,这里给出接口 ifndef STOCKOO_H_ #define STOCKOO_H_ #inclu ...
- Codeforces Round #311 (Div. 2)题解
A. Ilya and Diplomas time limit per test 1 second memory limit per test 256 megabytes input standard ...