【ZJOI2017 Round2练习&BZOJ4826】D1T2 sf(主席树,单调栈)
题意:


思路:From http://blog.csdn.net/neither_nor/article/details/70211150
对每个点i,单调栈求出左边和右边第一个大于i的位置,记为l[i]和r[i]
那么(l[i],r[i])会产生p1的贡献
左端点为l[i],右端点在[i+1,r-1]的点对都会产生p1的贡献
右端点为r[i],左端点在[l+1,i-1]的点对都会产生p2的贡献
将点对看成平面上的点,横坐标左端点纵坐标右端点,上述贡献分别对应单点加和线段加
查询就是矩形求和
From MG:
考虑修改均为如(x,y1..y2)与(x1..x2,y)形式
因为询问均为矩形,则修改(y1..y2,x)与(x,y1..y2)等价
若统一转化为(x,y1..y2)形式,可用主席树维护
若统一转化为(y1..y2,x)形式,可用树状数组套主席树维护
注意点对(i,i)都有p1的贡献
var t:array[..]of record
a,s:int64;
l,r:longint;
end;
d:array[..,..]of longint;
l,r:array[..]of longint;
root,stk,a:array[..]of longint;
n,m,i,cnt,j,top,que,x,y,p1,p2:longint;
ans:int64; procedure update(l,r,x,y,v:longint;var p:longint);
var mid:longint;
begin
if (l>x)or(x>y) then exit;
inc(cnt); t[cnt]:=t[p]; p:=cnt;
t[p].s:=t[p].s+int64(v)*(y-x+);
if (l=x)and(r=y) then
begin
t[p].a:=t[p].a+v;
exit;
end;
mid:=(l+r)>>;
if y<=mid then update(l,mid,x,y,v,t[p].l)
else if x>mid then update(mid+,r,x,y,v,t[p].r)
else
begin
update(l,mid,x,mid,v,t[p].l);
update(mid+,r,mid+,y,v,t[p].r);
end;
end; procedure query(l,r,x,y,p1,p2:longint);
var mid:longint;
begin
if (l=x)and(r=y) then
begin
ans:=ans+t[p1].s-t[p2].s;
exit;
end;
ans:=ans+(t[p1].a-t[p2].a)*(y-x+);
mid:=(l+r)>>;
if y<=mid then query(l,mid,x,y,t[p1].l,t[p2].l)
else if x>mid then query(mid+,r,x,y,t[p1].r,t[p2].r)
else
begin
query(l,mid,x,mid,t[p1].l,t[p2].l);
query(mid+,r,mid+,y,t[p1].r,t[p2].r);
end;
end; procedure swap(var x,y:longint);
var t:longint;
begin
t:=x; x:=y; y:=t;
end; procedure qsort(l,r:longint);
var i,j,mid:longint;
begin
i:=l; j:=r; mid:=d[(l+r)>>,];
repeat
while mid>d[i,] do inc(i);
while mid<d[j,] do dec(j);
if i<=j then
begin
swap(d[i,],d[j,]);
swap(d[i,],d[j,]);
swap(d[i,],d[j,]);
swap(d[i,],d[j,]);
inc(i); dec(j);
end;
until i>j;
if l<j then qsort(l,j);
if i<r then qsort(i,r);
end; begin
assign(input,'bzoj4826.in'); reset(input);
assign(output,'bzoj4826.out'); rewrite(output);
readln(n,que,p1,p2);
for i:= to n do read(a[i]);
top:=; stk[top]:=; a[]:=maxlongint;
for i:= to n do
begin
while (top>)and(a[i]>a[stk[top]]) do dec(top);
if top= then l[i]:=
else l[i]:=stk[top];
inc(top); stk[top]:=i;
end;
top:=; stk[top]:=n+; a[n+]:=maxlongint;
for i:=n downto do
begin
while (top>)and(a[i]>a[stk[top]]) do dec(top);
if top= then r[i]:=n+
else r[i]:=stk[top];
inc(top); stk[top]:=i;
end;
for i:= to n do
begin
if (l[i]>)and(r[i]<=n) then
begin
inc(m); d[m,]:=l[i]; d[m,]:=r[i]; d[m,]:=r[i]; d[m,]:=p1;
end;
if l[i]> then
begin
inc(m); d[m,]:=l[i]; d[m,]:=i+; d[m,]:=r[i]-; d[m,]:=p2;
end;
if r[i]<=n then
begin
inc(m); d[m,]:=r[i]; d[m,]:=l[i]+; d[m,]:=i-; d[m,]:=p2;
end;
end;
qsort(,m);
j:=;
for i:= to n do
begin
root[i]:=root[i-];
while (j<=m)and(d[j,]=i) do
begin
update(,n,d[j,],d[j,],d[j,],root[i]);
inc(j);
end;
end;
for i:= to que do
begin
readln(x,y);
ans:=(y-x)*p1;
query(,n,x,y,root[y],root[x-]);
writeln(ans);
end;
close(input);
close(output);
end.
【ZJOI2017 Round2练习&BZOJ4826】D1T2 sf(主席树,单调栈)的更多相关文章
- 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 ...
- [AH2017/HNOI2017]影魔(主席树+单调栈)
设\(l[i]\)为i左边第一个比i大的数的下标.\(r[i]\)为i右边第一个比i大的数的下标. 我们把\(p1,p2\)分开考虑. 当产生贡献为\(p1\)时\(i\)和\(j\)一定满足,分别为 ...
- 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 ...
- Codeforces 1175F - The Number of Subpermutations(线段树+单调栈+双针/分治+启发式优化)
Codeforces 题面传送门 & 洛谷题面传送门 由于这场的 G 是道毒瘤题,蒟蒻切不动就只好来把这场的 F 水掉了 看到这样的设问没人想到这道题吗?那我就来发篇线段树+单调栈的做法. 首 ...
- [BZOJ4826][HNOI2017]影魔(主席树)
4826: [Hnoi2017]影魔 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 669 Solved: 384[Submit][Status][ ...
随机推荐
- RabbitMq安装成功后执行命令报错(Error: unable to connect to node 'rabbit@DESKTOP-LPKSION': nodedown)
我们直接来看解决方案吧.首先打开服务,找到RabbitMq服务. 双击打开后选择登陆选项卡: 点选此账户,输入你计算机的登录名称.点击浏览: 在这里输入你的用户名,点检索: 这里的密码输入你电脑开机登 ...
- 读懂mysql慢查询日志
我们来看一下如何去读懂这些慢查询日志.在跟踪慢查询日志之前,首先你得保证最少发生过一次慢查询.如果你没有可以自己制造一个:root@server# mysql -e 'SELECT SLEEP(8); ...
- linux C编程 gdb的使用
linux C编程 gdb的使用 通常来说,gdb是linux在安装时自带的,在命令行键入"gdb"字符并按回车键会启动gdb调试环境. 1.gdb的基本命令 命令 说明 file ...
- 基本的查询流【MSSQL】
4个DML(Data Manipulation Language)命令 SELECT INSERT UPDATE DELETE 查询语法有一个特有的固定顺序 SELECT - FROM - WHERE ...
- 简单js图片点击向左滚动
<style> .b_left{width:50px;height:75px;float:left;background:url(img/left_right.png) no-repeat ...
- 常用css属性总结
边框修饰:border------>top,bottom,left,right上下左右边框 分为:color,类型style{ groove,dashed,ridge,solid}一个值---- ...
- springMVC接收get请求传递多个参数
@RequestMapping(value = "/sendSignal/{state}/{limberId}/{account}", method = RequestMethod ...
- codeforces_455B
B. A Lot of Games time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- MySql学习笔记(三) —— 聚集函数的使用
1.AVG() 求平均数 select avg(prod_price) as avg_price from products; --返回商品价格的平均值 ; --返回生产商id为1003的商品价格平均 ...
- Redis系列(一)--基础API
Redis:Remote Dictionary Server 是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作数据库.缓存和消息中间件.C语言实现,单线程 Redis特性: 1.速度快 ...