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. Saddle Point ZOJ - 3955 题意题

    Chiaki has an n × m matrix A. Rows are numbered from 1 to n from top to bottom and columns are numbe ...

  2. kali wireless driver install

    acer-gateway nv47h94c 1.lspci -nn |grep 0280 get pci-id root@silee:/# lspci -nn |grep 028003:00.0 Ne ...

  3. Makefile中的 =,:=,?=,+= 的差异

    在Makefile中常常遇见这几种等操作,总结一下具体区别. =  是最基本的赋值 :=  是用右值覆盖左值 ?=  判断,如果左值没有被赋值过就赋以右值,否则,不做赋值动作 += 在左值后面连接右值 ...

  4. sql service (case when then else end ..... group by)

    1. 原表: courseid coursename score ------------------------------------- 1 java 70 2 oracle 90 3 xml 4 ...

  5. windows 安装elk日志系统

    1.前往https://www.elastic.co官网下载对应的elasticsearch .kibana和logstash他们的版本号一致. 2.elasticsearch 解压后前往bin文件下 ...

  6. Redis 键值数据类型及基本操作

    到目前为止,Redis 支持的键值数据类型如下: 字符串(String) 哈希(Map) 列表(list) 集合(sets) 有序集合(sorted sets)   1. String 字符串类型 s ...

  7. 【spoj1182/usaco-Cow Queueing, 2003 Dec-二进制编号】数位dp

    题意:定义新的排序:先按一个数中二进制中1的个数从小到大排序,如果1的个数相同则按数的大小从小到大排序.问[A,B]之间有第K大的数是哪个.-2^31<=A,B<=2^31(A,B必定同正 ...

  8. 获取应用版本号,版本名称,包名,AppName,图标,是否是系统应用,获取手机中所有应用,所有进程

    PackageManager packageManager = getPackageManager(); PackageInfo packageInfo; = packageManager.getPa ...

  9. 记一次Powershell反混淆 (1)

    样本地址: https://www.virustotal.com/#/file/6f9034646e6fcead5342f708031412e3c2efdb4fb0f37bba43133a471d1c ...

  10. php中的__call()函数重载

    <?php #调用类中没有的方法时, 会自动调用__call方法重载 #第一个参数是调用时的方法名, 第二个参数为参数组成的数组 class Cat{ public function Hello ...