CodeForces 516C Drazil and Park 线段树
原文链接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 线段树的更多相关文章
- 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. ...
- codeforces 516c// Drazil and Park// Codeforces Round #292(Div. 1)
题意:一个圆环上有树,猴子上下其中一棵树,再沿着换跑,再上下另一棵树.给出一个区间,问最大的运动距离是. 给出区间大小dst,和数高数组arr. 设区间[x,y],a[x]=2*arr[x]+dst[ ...
- codeforces Good bye 2016 E 线段树维护dp区间合并
codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...
- codeforces 22E XOR on Segment 线段树
题目链接: http://codeforces.com/problemset/problem/242/E E. XOR on Segment time limit per test 4 seconds ...
- Codeforces 588E. A Simple Task (线段树+计数排序思想)
题目链接:http://codeforces.com/contest/558/problem/E 题意:有一串字符串,有两个操作:1操作是将l到r的字符串升序排序,0操作是降序排序. 题解:建立26棵 ...
- Codeforces Gym 100803G Flipping Parentheses 线段树+二分
Flipping Parentheses 题目连接: http://codeforces.com/gym/100803/attachments Description A string consist ...
- Codeforces GYM 100114 D. Selection 线段树维护DP
D. Selection Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descriptio ...
- Codeforces 444C DZY Loves Colors(线段树)
题目大意:Codeforces 444C DZY Loves Colors 题目大意:两种操作,1是改动区间上l到r上面德值为x,2是询问l到r区间总的改动值. 解题思路:线段树模板题. #inclu ...
- Codeforces 85D Sum of Medians(线段树)
题目链接:Codeforces 85D - Sum of Medians 题目大意:N个操作,add x:向集合中加入x:del x:删除集合中的x:sum:将集合排序后,将集合中全部下标i % 5 ...
随机推荐
- SQL Server代码段
1.cast和convert ' as int) -- 123 ') -- 123 select CAST(123.4 as int) -- 123 select CONVERT(int, 123.4 ...
- Light OJ 1058
题意: 简单的就组合数 C(m,n): 数据多,大, 要预处理: #include<bits/stdc++.h> using namespace std; typedef long lon ...
- Linux查看所有用户和组信息
主要通过以下两个命令: cat /etc/passwd cat /etc/group 步骤一:cat /etc/passwd查看所有的用户信息,详情如下图: 步骤2:cat /etc/passwd|g ...
- 【原创】Linux基础之Shell脚本常用命令
#!/bin/sh 1 取脚本参数 $# 参数个数$0 当前脚本名$1 第1个参数$n 第n个参数$* 所有参数$@ 所有参数$? 上个命令的状态$$ 当前pid 2 日期 $ dateWed Mar ...
- 使用Gitblit 在Windows上部署Git Server
Windows平台下Git服务器搭建 首先要下载Java JDK,安装完成后设置环境变量,先把java环境配好,接下来才是下面的gitblit.关于java环境配置请看上一篇文章 gitblit下载 ...
- JNI 开发基础篇:Android开发中os文件的探索
正题: android开发中,时长会遇到os文件的使用,那么os文件到底是什么?在这篇文章中会进行说明. .os文件在android中意味着C语言书写的方法,经android提供的ndk进行编译,从而 ...
- .NET Windows API库(Cjwdev.WindowsApi)版本2.2发布
https://blog.cjwdev.co.uk/2011/06/12/net-windows-api-library-cjwdev-windowsapi-vesion-2-2-released/# ...
- swift项目初始化并添加忽略文件Swift.ignore
1 先去GitHub上去把最新的忽略文件下载下载 https://github.com/github/gitignore 2 然后找到Swift.gitignore 把里面的 pod 前面的# 删除 ...
- Java测试代码(很不完整,建议大家别看,过几天会再发一次难的版本)
package ATM; import java.io.BufferedReader; import java.io.InputStreamReader; class Account{ priv ...
- 【python】ftp连接,主被动,调试等级
示例代码如下: #!/usr/bin/env python # -*- coding: utf-8 -*- import os from ftplib import FTP def ftp_down( ...