[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 ...
随机推荐
- gbk编码
GBK 编码 GBK编码范围:8140-FEFE,汉字编码范围见第二节:码位分配及顺序. GBK编码,是对GB2312编码的扩展,因此完全兼容GB2312-80标准.GBK编码依然采用双字节编码方 ...
- pickle模块 no attribute 'dumps'
今天写了一个pickle.py的文件练习pickle模块,代码如下: import pickle dic = {"linga": ('football',)} dic2 = {&q ...
- js正则表达式注册页面表单验证
可以这样校验 <html> <head> <meta http-equiv="Content-Type" content="text/htm ...
- 【codeforces 508E】Artur and Brackets
[题目链接]:http://codeforces.com/problemset/problem/508/E [题意] 让你构造一个括号字符串; 使得每个从左往右数第i个左括号在这个括号序列中与之匹配的 ...
- Linux 上安装 Zookeepr
一.下载Zookeeper 百度网盘:https://pan.baidu.com/s/1BHV6vHcHIuj7lalvvR7w_g 密码:csvk 二.解压缩包 tar -zxvf zookeepe ...
- UVALive 3231 Fair Share
Fair Share Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. Origina ...
- vue2 router中的 @ 符号表示src
vue2 router中的 @ 符号表示src 学习了:https://segmentfault.com/q/1010000009549802 这个是webpack起的别名: 在build/webpa ...
- android自己定义Application全局变量不能类型转换的问题
今天弄了个全局变量AppContext ,但一直出现例如以下错误,原来继承 Application的得在清单文件声明. java.lang.RuntimeException: Unable to st ...
- Oracle SGA具体解释
SGA(SYSTEM Global Area )系统全局区 l 数据快速缓存 在Oracle进行数据处理的过程中,代价最昂贵的就是物理 I/O操作了.相同的数据从内存中得到要比从磁盘上读取快的多. 因 ...
- iOS开发实践之xib载入注意问题
xib都会addSubview加入到控制器view中时程序崩溃.错误提示: 'NSInvalidArgumentException', reason: '-[ UITapGestureRecogniz ...