4826

思路:

  主席树矩阵加减+单调栈预处理;

代码:

#include <bits/stdc++.h>
using namespace std;
#define maxn 200005
#define ll long long
#define maxtree maxn*30
class PTreeType
{
private:
int ch[maxtree][],root[maxn],tot,head[maxn],li[maxn<<],ri[maxn<<],E[maxn<<],cnt,val_[maxn<<];
ll val[maxtree],tag[maxtree];
public:
void add(int &now,int pre,int l,int r,int tl,int tr,ll x)
{
now=++tot,val[now]=val[pre],tag[now]=tag[pre];
val[now]+=((r<tr?r:tr)-(l>tl?l:tl)+)*x;
ch[now][]=ch[pre][],ch[now][]=ch[pre][];
if(l>=tl&&r<=tr){tag[now]+=x;return;}int mid=l+r>>;
if(tl<=mid) add(ch[now][],ch[pre][],l,mid,tl,tr,x);
if(tr>mid) add(ch[now][],ch[pre][],mid+,r,tl,tr,x);
}
ll query(int now,int pre,int l,int r,int tl,int tr)
{
if(l>=tl&&r<=tr) return val[now]-val[pre];
int mid=l+r>>;ll res=((r<tr?r:tr)-(l>tl?l:tl)+)*(tag[now]-tag[pre]);
if(tl<=mid) res+=query(ch[now][],ch[pre][],l,mid,tl,tr);
if(tr>mid) res+=query(ch[now][],ch[pre][],mid+,r,tl,tr);
return res;
}
void add(int l,int r,int size,int x,int to)
{
add(root[to],root[to],,size,l,r,x);
}
ll query(int l,int r,int size,int now,int pre)
{
return query(root[now],root[pre],,size,l,r);
}
void operation_add(int to,int l,int r,int pi)
{
E[++cnt]=head[to],li[cnt]=l,ri[cnt]=r,val_[cnt]=pi,head[to]=cnt;
}
void makeit(int size)
{
for(int i=;i<=size;i++)
{
root[i]=root[i-];
for(int v=head[i];v;v=E[v])
{
add(li[v],ri[v],size,val_[v],i);
}
}
}
};
class PTreeType xtree,ytree;
struct NodeType {
int l,r;
};
struct NodeType ai[maxn];
int n,m,p1,p2,ki[maxn],sta[maxn];
inline void in(int &now)
{
char Cget=getchar();now=;
while(Cget>''||Cget<'') Cget=getchar();
while(Cget>=''&&Cget<='')
{
now=now*+Cget-'';
Cget=getchar();
}
}
inline void in(ll &now)
{
char Cget=getchar();now=;
while(Cget>''||Cget<'')Cget=getchar();
while(Cget>=''&&Cget<='')
{
now=now*+Cget-'';
Cget=getchar();
}
}
int main()
{
in(n),in(m),in(p1),in(p2);int top=,op,l,r;ll ans;
for(int i=;i<=n;i++)
{
in(ki[i]);
while(top&&ki[sta[top]]<ki[i]) ai[sta[top]].r=i,top--;
ai[i].l=sta[top],sta[++top]=i;
}
while(top) ai[sta[top]].r=n+,top--;
for(int i=;i<=n;i++)
{
if(ai[i].l>&&ai[i].r<=n) xtree.operation_add(ai[i].l,ai[i].r,ai[i].r,p1);
if(i+<=ai[i].r-) xtree.operation_add(ai[i].l,i+,ai[i].r-,p2);
if(ai[i].l+<=i-) ytree.operation_add(ai[i].r,ai[i].l+,i-,p2);
}
xtree.makeit(n),ytree.makeit(n);
while(m--)
{
in(l),in(r),ans=;
ans+=xtree.query(l,r,n,r,l-);
ans+=ytree.query(l,r,n,r,l-);
ans+=(ll)p1*(r-l);
printf("%lld\n",ans);
}
return ;
}

AC日记——[Hnoi2017]影魔 bzoj 4826的更多相关文章

  1. AC日记——[LNOI2014]LCA bzoj 3626

    3626 思路: 离线操作+树剖: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 100005 #defin ...

  2. AC日记——[ZJOI2012]网络 bzoj 2816

    2816 思路: 多个LCT: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 10005 #define l ...

  3. AC日记——[SCOI2009]游戏 bzoj 1025

    [SCOI2009]游戏 思路: 和为n的几个数最小公倍数有多少种. dp即可: 代码: #include <bits/stdc++.h> using namespace std; #de ...

  4. AC日记——[HNOI2014]世界树 bzoj 3572

    3572 思路: 虚树+乱搞: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 300005 #define ...

  5. AC日记——NOI2016区间 bzoj 4653

    4653 思路: 线段树,指针滑动: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 1000005 #def ...

  6. AC日记——Rmq Problem bzoj 3339

    3339 思路: 恶心: 代码: #include <cstdio> #include <cstring> #include <iostream> #include ...

  7. AC日记——[HNOI2008]越狱 bzoj 1008

    1008 思路: 越狱情况=总情况-不越狱情况: 代码: #include <cstdio> #include <cstring> #include <iostream& ...

  8. AC日记——[FJOI2007]轮状病毒 bzoj 1002

    1002 思路: 打表找规律: dp[i]=dp[i-1]*3-dp[i-2]+2; 套个高精就a了: 代码: #include <cstdio> #include <cstring ...

  9. AC日记——[Ahoi2013]作业 bzoj 3236

    3236 思路: 莫队+树状数组维护: 代码: #include <cmath> #include <cstdio> #include <cstring> #inc ...

随机推荐

  1. 如何使用Navicat连接Oracle

    1.Navicat连接Oracle,需要使用OCI库.因此先要安装Oracle提供的客户端instantclient-basic, 请注意,32位的Navicat需要下载配置32位的instantcl ...

  2. Mybatis手工写sql语句及Mapper.xml方法

    首先在项目中 建一个mapper包,然后在spring集合mybatis的配置文件中设置扫描这个mapper包 然后,建 封装查询结果需要的 pojo 然后,在 mapper包中创建 Mapper接口 ...

  3. Machine Learning CodeForces - 940F (带修改的莫队)

    You come home and fell some unpleasant smell. Where is it coming from? You are given an array a. You ...

  4. eclipse ----- indexer

    使能indexer,可以实现变量.函数等的跳转, 即跳转到定义的位置

  5. gps各种地图坐标系转换

    原文地址:https://my.oschina.net/fankun2013/blog/338100 地图供应商比较多,产生了许多地图坐标.地图坐标正确转换是个问题.在之前开发地图应用的时候发现从WG ...

  6. Test Index

    top1 top11 top2 top1 top11 top2

  7. 增强学习Reinforcement Learning经典算法梳理3:TD方法

    转自:http://blog.csdn.net/songrotek/article/details/51382759 博客地址:http://blog.csdn.net/songrotek/artic ...

  8. CAS 逻辑流程图

  9. Value does not fall within the expected range 值不在预期的范围内

    用vs2012 打开web.config时,提示如下错误:“Value does not fall within the expected range”; 中文提示:“值不在预期的范围内” 解决方案: ...

  10. this指针再解

     this.new.call和apply的相关问题 讲解this指针的原理是个很复杂的问题,如果我们从javascript里this的实现机制来说明this,很多朋友可能会越来越糊涂,因此本篇打算换一 ...