原文链接http://www.cnblogs.com/zhouzhendong/p/8990745.html

题目传送门 - CodeForces 516C

题意

  在一个环上,有$n$棵树。

  给出每一个树的高度$h_i$以及每一个树距离他顺时针方向后一个树的距离$d_i$。

  有$m$次询问,每次,都会有一段连续区间内的树萎掉。请你找两棵树$x,y$,最大化$2(h_x+h_y)+dist(x,y)$。其中$dist(x,y)$为这两棵树的距离,这个距离不能包含萎掉的树。

  $n,m\leq 10^5$

题解

  闭着眼睛都能想到线段树。

  我们首先闭着眼睛给$d_i$求个前缀和。

  然后开线段树,维护区间$2h_i-d_{i-1}$的最大值以及$2h_i+d_{i-1}$的最大值,以及区间匹配的两个树的最大的$2(h_x+h_y)+dist(x,y)$。

  然后倍长原序列,每次询问就是区间求$max$。

  UPD(发表这篇博文约30分钟后):

  代码重写了哈哈。又短又漂亮。

代码

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=200005;
const LL INF=1LL<<56;
int n,m;
LL d[N],h[N];
struct Segment{
LL val,v0,v1;
Segment(){}
Segment(LL a,LL b,LL c){
val=a,v0=b,v1=c;
}
}t[N<<2];
LL read(){
LL x=0;
char ch=getchar();
while (!('0'<=ch&&ch<='9'))
ch=getchar();
while ('0'<=ch&&ch<='9')
x=x*10+ch-48,ch=getchar();
return x;
}
Segment merge(Segment ls,Segment rs){
Segment rt;
rt.val=max(max(ls.val,rs.val),ls.v0+rs.v1);
rt.v0=max(ls.v0,rs.v0);
rt.v1=max(ls.v1,rs.v1);
return rt;
}
void build(int rt,int L,int R){
if (L==R){
t[rt].v0=h[L]-d[L-1],t[rt].v1=h[L]+d[L-1],t[rt].val=-INF;
return;
}
int mid=(L+R)>>1,ls=rt<<1,rs=ls|1;
build(ls,L,mid);
build(rs,mid+1,R);
t[rt]=merge(t[ls],t[rs]);
}
Segment query(int rt,int L,int R,int xL,int xR){
if (L>xR||R<xL||xL>xR)
return Segment(-INF,-INF,-INF);
if (xL<=L&&R<=xR)
return t[rt];
int mid=(L+R)>>1,ls=rt<<1,rs=ls|1;
return merge(query(ls,L,mid,xL,xR),query(rs,mid+1,R,xL,xR));
}
int main(){
scanf("%d%d",&n,&m);
for (int i=1;i<=n;i++)
d[i]=read()+d[i-1];
for (int i=1;i<=n;i++)
h[i]=read()*2;
for (int i=1;i<=n;i++)
d[i+n]=d[i]+d[n],h[i+n]=h[i];
build(1,1,n*2);
for (int i=1,a,b;i<=m;i++){
scanf("%d%d",&a,&b);
if (b>=a)
a+=n;
printf("%I64d\n",query(1,1,n*2,b+1,a-1).val);
}
return 0;
}

  

题解 - 续 (修改前的题解后一半以及代码)

  然而我代码写的很

  首先,区间求$max$的时候因为懒,为了少写一点代码,强行把一个$\log$的算法写成$\log^2$的。

  第二,我线段树维护的前两个信息不是最大值,而是编号……于是自找麻烦。

  第三,我没有倍长……

  (有空重写)

代码

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=100005;
int n,m;
LL d[N],h[N],val[N<<2];
int M[2][N<<2];
LL read(){
LL x=0;
char ch=getchar();
while (!('0'<=ch&&ch<='9'))
ch=getchar();
while ('0'<=ch&&ch<='9')
x=x*10+ch-48,ch=getchar();
return x;
}
LL c(int t,int i){
if (i==0)
return -(1LL<<55);
return t==0?(h[i]-d[i-1]):(h[i]+d[i-1]);
}
int pushup(int ls,int rs,int t){
return c(t,ls)>c(t,rs)?ls:rs;
}
void build(int rt,int L,int R){
if (L==R){
M[0][rt]=M[1][rt]=L,val[rt]=-(1LL<<55);
return;
}
int mid=(L+R)>>1,ls=rt<<1,rs=ls|1;
build(ls,L,mid);
build(rs,mid+1,R);
M[0][rt]=pushup(M[0][ls],M[0][rs],0);
M[1][rt]=pushup(M[1][ls],M[1][rs],1);
val[rt]=max(max(val[ls],val[rs]),c(0,M[0][ls])+c(1,M[1][rs]));
}
int query(int rt,int L,int R,int xL,int xR,int t){
if (L>xR||R<xL||xL>xR)
return 0;
if (xL<=L&&R<=xR)
return M[t][rt];
int mid=(L+R)>>1,ls=rt<<1,rs=ls|1;
return pushup(query(ls,L,mid,xL,xR,t),query(rs,mid+1,R,xL,xR,t),t);
}
LL query(int rt,int L,int R,int xL,int xR){
if (L>xR||R<xL||xL>xR)
return 0;
if (xL<=L&&R<=xR)
return val[rt];
int mid=(L+R)>>1,ls=rt<<1,rs=ls|1;
return max(max(query(ls,L,mid,xL,xR),query(rs,mid+1,R,xL,xR)),c(0,query(ls,L,mid,xL,xR,0))+c(1,query(rs,mid+1,R,xL,xR,1)));
}
int main(){
scanf("%d%d",&n,&m);
for (int i=1;i<=n;i++)
d[i]=read()+d[i-1];
for (int i=1;i<=n;i++)
h[i]=read()*2;
build(1,1,n);
for (int i=1,a,b;i<=m;i++){
scanf("%d%d",&a,&b);
if (b<a){
printf("%I64d\n",query(1,1,n,b+1,a-1));
}
else {
LL ans=max(query(1,1,n,1,a-1),query(1,1,n,b+1,n));
ans=max(ans,c(1,query(1,1,n,1,a-1,1))+d[n]+c(0,query(1,1,n,b+1,n,0)));
printf("%I64d\n",ans);
}
}
return 0;
}

  

CodeForces 516C Drazil and Park 线段树的更多相关文章

  1. Codeforces Round #292 (Div. 1) C. Drazil and Park 线段树

    C. Drazil and Park 题目连接: http://codeforces.com/contest/516/problem/C Description Drazil is a monkey. ...

  2. codeforces 516c// Drazil and Park// Codeforces Round #292(Div. 1)

    题意:一个圆环上有树,猴子上下其中一棵树,再沿着换跑,再上下另一棵树.给出一个区间,问最大的运动距离是. 给出区间大小dst,和数高数组arr. 设区间[x,y],a[x]=2*arr[x]+dst[ ...

  3. codeforces Good bye 2016 E 线段树维护dp区间合并

    codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...

  4. codeforces 22E XOR on Segment 线段树

    题目链接: http://codeforces.com/problemset/problem/242/E E. XOR on Segment time limit per test 4 seconds ...

  5. Codeforces 588E. A Simple Task (线段树+计数排序思想)

    题目链接:http://codeforces.com/contest/558/problem/E 题意:有一串字符串,有两个操作:1操作是将l到r的字符串升序排序,0操作是降序排序. 题解:建立26棵 ...

  6. Codeforces Gym 100803G Flipping Parentheses 线段树+二分

    Flipping Parentheses 题目连接: http://codeforces.com/gym/100803/attachments Description A string consist ...

  7. Codeforces GYM 100114 D. Selection 线段树维护DP

    D. Selection Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descriptio ...

  8. Codeforces 444C DZY Loves Colors(线段树)

    题目大意:Codeforces 444C DZY Loves Colors 题目大意:两种操作,1是改动区间上l到r上面德值为x,2是询问l到r区间总的改动值. 解题思路:线段树模板题. #inclu ...

  9. Codeforces 85D Sum of Medians(线段树)

    题目链接:Codeforces 85D - Sum of Medians 题目大意:N个操作,add x:向集合中加入x:del x:删除集合中的x:sum:将集合排序后,将集合中全部下标i % 5 ...

随机推荐

  1. Ubuntu解除"输入密码以解锁密钥环”

    解决办法有两种: 1.去掉默认密钥环的密码: 打开应用程序->附件->密码和加密密钥(如果你的没有,在终端中输入 seahorse),切换到密码选项卡,会看到一个密码密钥环(我的密钥环是 ...

  2. RedHat Linux关闭seLinux命令

    Redhat使用了SELinux来增强安全,关闭的办法为: 1. 永久有效 修改 /etc/selinux/config 文件中的 SELINUX="" 为 disabled ,然 ...

  3. RedHat Linux关闭防火墙的命令

    获得root 控制权限.在“#”下操作. 查看防火墙状态. systemctl status firewalld 临时关闭防火墙命令.重启电脑后,防火墙自动起来. systemctl stop fir ...

  4. 学习promise

    总概括 promise是js异步编程的一种解决方案 我对promise的认识(通俗):给一个承诺promise,如果未来发生的事情(异步操作)是符合满足相应条件,则接受resolve,否则失败reje ...

  5. Connection reset by [server_ip] port 22 (hexo d 部署博客出错)

    问题 在使用 hexo d 部署博客和使用 Git/Github 进行 git push -u origin master 时遇到了以下问题: git -c diff.mnemonicprefix=f ...

  6. ios蓝牙详解

    最近这段时间在研究蓝牙,也研究了一段时间了现在在下面做个总结 1 其实蓝牙连接只要明白了整体原理,其实挺简单的  2 大部分情况下,手机作为中心管理者,而连接的设备被称为外设,外设的结构有点像一颗大树 ...

  7. angularjs 监听 文档click 事件

    项目 上遇到  innHTML  放入 一大段的html 内容 中带有 click 事件 如onclick="callInMethod("http://www.crm.bmcc.c ...

  8. Java和C冒泡排序

    Java 示例代码: public class test { public static void main(String[] args) { String str = "321dca5&q ...

  9. Java 输入一组数字,用穷举的方法列出

    import java.util.Scanner; public class TestScanner { public static void main(String[] args) { Scanne ...

  10. caffe入门-人脸检测1

    最近刚入门caffe,跟着视频做了一个简单人脸检测.包括人脸二分类模型+方框框出人脸. 人脸二分类模型 1. 收集数据 我用的是lfw数据集,总共有13233张人脸图片.非人脸数据有两种选择.1. 用 ...