题意:一个圆环上有树,猴子上下其中一棵树,再沿着换跑,再上下另一棵树。给出一个区间,问最大的运动距离是。

给出区间大小dst,和数高数组arr。

设区间[x,y],a[x]=2*arr[x]+dst[1]+dst[2]+......dst[x]。b[x]=2*arr[x]-dst[1]-dst[2]-......dst[x]。[x,y]在线段树中就是a[y]+b[x]。线段树分为很多区间,用数组a,b,s,记录当前区间的a,b最大值,s记录该区间里所有的情况中的爬树+路程的最大值,就是两个子区间中a最大的和b最大的。叶子节点用上面的公式计算。叶子节点s为0,其它的要么区间的最大值来自子区间的s值,要么是左区间的a值+右区间的b值,或者左的b值+右的a,以上几个的最大值。查询的时候查s值。查询结果要返回a,b,s。比如查[1,3]分为[1,2]和[3],那么结果是[1,2]的s,[3]的s,[1,2]的a+[3]的b,[1,2]的b+[3]的a,以上的最大值。因此a,b,c都要返回。

乱码:

#pragma comment(linker,"/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
#include <stack>
using namespace std;
const int SZ=8e5+,INF=0x7FFFFFFF;
typedef long long lon;
lon mod=;
lon srcdst[SZ],dst[SZ],tall[SZ],a[SZ],b[SZ],s[SZ],arr[SZ];
lon n,m; void init(lon n)
{
for(lon i=;i<=n+;++i)
{
cin>>dst[i];
}
for(lon i=n+;i<*n+;++i)
{
dst[i]=dst[i-n];
}
for(lon i=;i<=*n+;++i)
{
dst[i]+=dst[i-];
}
for(lon i=;i<=n;++i)cin>>arr[i];
} void pushup(lon rt)
{
a[rt]=max(a[rt*],a[rt*+]);
b[rt]=max(b[rt*],b[rt*+]);
lon m1=max(s[rt*],s[rt*+]);
lon m2=max(a[rt*]+b[rt*+],a[rt*+]+b[rt*]);
s[rt]=max(m1,m2);
} void build(lon ll,lon rr,lon rt)
{
if(ll==rr)
{
lon real=(ll-)%(n/)+;
a[rt]=*arr[real]+dst[ll];
b[rt]=*arr[real]-dst[ll];
//cout<<ll<<" "<<a[rt]<<" "<<b[rt]<<endl;
return;
}
if(ll>rr)return;
lon mid=(ll+rr)/;
build(ll,mid,rt*);
build(mid+,rr,rt*+);
pushup(rt);
} struct nd{
lon a,b,s;
nd (lon _a=,lon _b=,lon _s=):a(_a),b(_b),s(_s){}
bool operator==(const nd &rbs)const
{
return a==rbs.a&&b==rbs.b&&s==rbs.s;
}
}; nd qry(lon ll,lon rr,lon ql,lon qr,lon rt)
{
nd res1,res2,rbs;
if(ll>=ql&&rr<=qr)
{
nd tmp;
tmp.a=a[rt],tmp.b=b[rt],tmp.s=s[rt];
return tmp;
}
//cout<<ll<<" "<<rr<<" "<<ql<<" "<<qr<<endl;
if(rr<ql||ll>qr)return rbs;
lon mid=(ll+rr)/;
if(rr>=ql)res1=qry(ll,mid,ql,qr,rt*);
if(ll<=qr)res2=qry(mid+,rr,ql,qr,rt*+);
if(res1==rbs)return res2;
else if(res2==rbs)return res1;
else
{
lon max1=max(res1.s,res2.s);
lon max2=max(res1.a+res2.b,res1.b+res2.a);
res1.s=max(max1,max2);
res1.a=max(res1.a,res2.a);
res1.b=max(res1.b,res2.b);
return res1;
}
} int main()
{
std::ios::sync_with_stdio();
//freopen("d:\\1.txt","r",stdin);
cin>>n>>m;
init(n);
n=*n+;
build(,n,);
for(lon i=;i<m;++i)
{
lon ql,qr;
cin>>ql>>qr;
if(ql<=qr)
{
swap(ql,qr);
++ql;
qr+=n/-;
}
else
{
swap(ql,qr);
++ql,--qr;
}
cout<<qry(,n,ql,qr,).s<<endl;
}
return ;
}

codeforces 516c// Drazil and Park// Codeforces Round #292(Div. 1)的更多相关文章

  1. CodeForces 516C Drazil and Park 线段树

    原文链接http://www.cnblogs.com/zhouzhendong/p/8990745.html 题目传送门 - CodeForces 516C 题意 在一个环上,有$n$棵树. 给出每一 ...

  2. 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. ...

  3. Codeforces Round #292 (Div. 1) C - Drazil and Park

    C - Drazil and Park 每个点有两个值Li 和 Bi,求Li + Rj (i < j) 的最大值,这个可以用线段树巧妙的维护.. #include<bits/stdc++. ...

  4. Codeforces 515E Drazil and Park (ST表)

    题目链接 Drazil and Park 中文题面 传送门 如果他选择了x和y,那么他消耗的能量为dx + dx + 1 + ... + dy - 1 + 2 * (hx + hy). 把这个式子写成 ...

  5. Codeforces Round #292 (Div. 1) B. Drazil and Tiles 拓扑排序

    B. Drazil and Tiles 题目连接: http://codeforces.com/contest/516/problem/B Description Drazil created a f ...

  6. Codeforces Round #292 (Div. 1)A. Drazil and Factorial 构造

    A. Drazil and Factorial 题目连接: http://codeforces.com/contest/516/problem/A Description Drazil is play ...

  7. Codeforces Round #292 (Div. 2) C. Drazil and Factorial

    题目链接:http://codeforces.com/contest/515/problem/C 给出一个公式例如:F(135) = 1! * 3! * 5!; 现在给你一个有n位的数字a,让你求这样 ...

  8. Codeforces Round #292 (Div. 1) B. Drazil and Tiles (类似拓扑)

    题目链接:http://codeforces.com/problemset/problem/516/B 一个n*m的方格,'*'不能填.给你很多个1*2的尖括号,问你是否能用唯一填法填满方格. 类似t ...

  9. Codeforces Round #292 (Div. 1) - B. Drazil and Tiles

    B. Drazil and Tiles   Drazil created a following problem about putting 1 × 2 tiles into an n × m gri ...

随机推荐

  1. linux下删除大量文件提示参数过长解决办法

    linux下删除大量文件提示参数过长解决办法:在当前目录下rm -rf * 在linux中删除大量文件时,直接用rm会出现:-bash: /bin/rm: 参数列表过长的错误. 这时可以用find命令 ...

  2. phpstudy升级mysql版本到5.7 ,重启mysql不启动

    phpstudy中mysql升级后MySQL服务无法启动 问题产生: 安装好phpstudy后,升级了MySQL后,通过phpstudy启动,Apache可以启动,Mysql无法启动. 解决方法: 之 ...

  3. Python Web学习笔记之GIL机制下的鸡肋多线程

    为什么有人会说 Python 多线程是鸡肋?知乎上有人提出这样一个问题,在我们常识中,多进程.多线程都是通过并发的方式充分利用硬件资源提高程序的运行效率,怎么在 Python 中反而成了鸡肋? 有同学 ...

  4. 01: html常用标签

    目录: 1.1 web开发的三把利器介绍 1.2 网页头部head标签中几个常用标签 1.3 html常用标签归类 1.4 input系列标签 1.5 HTML其他标签 1.1 web开发的三把利器介 ...

  5. CentOS 7 怎样自动连接网络

    https://jingyan.baidu.com/article/19192ad8f7c320e53e570728.html

  6. 20145105 《Java程序设计》实验五总结

    实验五 JAVA网络编程 一.实验内容 用老师给的代码,实现服务器和客户端 和结对同学实现服务器与客户端的连接 客户端输入数据并加密,将密文发送给服务器,服务器接收密文并解密 二.实验步骤 用老师给的 ...

  7. 20145105 《Java程序设计》第9周学习总结

    20145105 <Java程序设计>第9周学习总结 教材学习内容总结 第十六章 整合数据库 一.JDBC入门 (一)JDBC简介 厂商在操作JDBC驱动程序时,依操作方式可将驱动程序分为 ...

  8. 20145304 Exp4 恶意代码分析

    20145304 Exp4 恶意代码分析 实验后回答问题 (1)如果在工作中怀疑一台主机上有恶意代码,但只是猜想,所有想监控下系统一天天的到底在干些什么.请设计下你想监控的操作有哪些,用什么方法来监控 ...

  9. bzoj 1497 最大获利 - 最小割

    新的技术正冲击着手机通讯市场,对于各大运营商来说,这既是机遇,更是挑战.THU集团旗下的CS&T通讯公司在新一代通讯技术血战的前夜,需要做太多的准备工作,仅就站址选择一项,就需要完成前期市场研 ...

  10. Go第二篇之基本语法总结

    Go语言标识符 标识符以字母或下划线开头,大小写敏感 _是特殊标识符,用来忽略结果 Go语言变量的声明 变量(Variable)的功能是存储用户的数据.不同的逻辑有不同的对象类型,也就有不同的变量类型 ...