AC日记——[Hnoi2017]影魔 bzoj 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的更多相关文章
- AC日记——[LNOI2014]LCA bzoj 3626
3626 思路: 离线操作+树剖: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 100005 #defin ...
- AC日记——[ZJOI2012]网络 bzoj 2816
2816 思路: 多个LCT: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 10005 #define l ...
- AC日记——[SCOI2009]游戏 bzoj 1025
[SCOI2009]游戏 思路: 和为n的几个数最小公倍数有多少种. dp即可: 代码: #include <bits/stdc++.h> using namespace std; #de ...
- AC日记——[HNOI2014]世界树 bzoj 3572
3572 思路: 虚树+乱搞: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 300005 #define ...
- AC日记——NOI2016区间 bzoj 4653
4653 思路: 线段树,指针滑动: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 1000005 #def ...
- AC日记——Rmq Problem bzoj 3339
3339 思路: 恶心: 代码: #include <cstdio> #include <cstring> #include <iostream> #include ...
- AC日记——[HNOI2008]越狱 bzoj 1008
1008 思路: 越狱情况=总情况-不越狱情况: 代码: #include <cstdio> #include <cstring> #include <iostream& ...
- AC日记——[FJOI2007]轮状病毒 bzoj 1002
1002 思路: 打表找规律: dp[i]=dp[i-1]*3-dp[i-2]+2; 套个高精就a了: 代码: #include <cstdio> #include <cstring ...
- AC日记——[Ahoi2013]作业 bzoj 3236
3236 思路: 莫队+树状数组维护: 代码: #include <cmath> #include <cstdio> #include <cstring> #inc ...
随机推荐
- Tree and Permutation dfs hdu 6446
Problem Description There are N vertices connected by N−1 edges, each edge has its own length.The se ...
- POJ 2226 Muddy Fields(二分匹配 巧妙的建图)
Description Rain has pummeled the cows' field, a rectangular grid of R rows and C columns (1 <= R ...
- ACM3790迪杰斯特拉算法运用
最短路径问题 Problem Description 给你n个点,m条无向边,每条边都有长度d和花费p,给你起点s终点t,要求输出起点到终点的最短距离及其花费,如果最短距离有多条路线,则输出花费最少的 ...
- [Leetcode] LRU 算法实现
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
- 字符串类dp的题目总结
熟练掌握回文串吧,大致有dp或者模拟类的吧 ①dp+预处理,懂得如何枚举回文串(一) ②dp匹配类型的题目(二) ③dp+预处理 子串类型 (三) ④字符串的组合数(四) 一:划分成回文串 UVA11 ...
- python3 mysql 时间参数错误
一.环境 mac OS + python3.6 + pymysql 二.执行 1.语句 select count(user_id) from chezhubangapppp.yfq_user wher ...
- 【BZOJ】4530: [Bjoi2014]大融合
[题意]给定n个点的树,从无到有加边,过程中动态询问当前图某条边两端连通点数的乘积,n<=10^5. [算法]线段树合并+并查集 (||LCT(LCT维护子树信息 LCT维护子树信息(+启发式合 ...
- js中的true和false
1.false undefined.NaN.0.null和空字符串''均被视为false 2.true 除上述以外的其它情况一律被视作true
- SQL注入文件读取通过from for分页读取
http://103.238.227.13:10088/?id=1 在读取文件的时候发现不能够一下子全部读取出来.经过百度学习了一下,看到别人使用from for说实在此前真不知道这操作. 先来看一下 ...
- vmware安装ubuntu " Intel VT-x 处于禁用状态"
vmware安装ubuntu " Intel VT-x 处于禁用状态" http://jingyan.baidu.com/article/fc07f98976710e12ffe51 ...