codeforces 516c// Drazil and Park// Codeforces Round #292(Div. 1)
题意:一个圆环上有树,猴子上下其中一棵树,再沿着换跑,再上下另一棵树。给出一个区间,问最大的运动距离是。
给出区间大小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)的更多相关文章
- CodeForces 516C Drazil and Park 线段树
原文链接http://www.cnblogs.com/zhouzhendong/p/8990745.html 题目传送门 - CodeForces 516C 题意 在一个环上,有$n$棵树. 给出每一 ...
- 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 Round #292 (Div. 1) C - Drazil and Park
C - Drazil and Park 每个点有两个值Li 和 Bi,求Li + Rj (i < j) 的最大值,这个可以用线段树巧妙的维护.. #include<bits/stdc++. ...
- Codeforces 515E Drazil and Park (ST表)
题目链接 Drazil and Park 中文题面 传送门 如果他选择了x和y,那么他消耗的能量为dx + dx + 1 + ... + dy - 1 + 2 * (hx + hy). 把这个式子写成 ...
- 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 ...
- Codeforces Round #292 (Div. 1)A. Drazil and Factorial 构造
A. Drazil and Factorial 题目连接: http://codeforces.com/contest/516/problem/A Description Drazil is play ...
- Codeforces Round #292 (Div. 2) C. Drazil and Factorial
题目链接:http://codeforces.com/contest/515/problem/C 给出一个公式例如:F(135) = 1! * 3! * 5!; 现在给你一个有n位的数字a,让你求这样 ...
- Codeforces Round #292 (Div. 1) B. Drazil and Tiles (类似拓扑)
题目链接:http://codeforces.com/problemset/problem/516/B 一个n*m的方格,'*'不能填.给你很多个1*2的尖括号,问你是否能用唯一填法填满方格. 类似t ...
- 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 ...
随机推荐
- Python实现Json结构对比的小工具兼谈编程求解问题
摘要: 通过使用Python编写一个解析Json结构对比的小工具,来提炼编程求解的通用步骤和技巧. 难度: 初级 先上代码. jsondiff.py #!/usr/bin/python #_*_enc ...
- VisualStudio: 窗口背景颜色设置成黑色
Tools>Options>Environment>General >Color theme
- MySQL Crash Course #21# Chapter 29.30. Database Maintenance & Improving Performance
终于结束这本书了,最后两章的内容在官方文档中都有详细介绍,简单过一遍.. 首先是数据备份,最简单直接的就是用 mysql 的内置工具 mysqldump MySQL 8.0 Reference Man ...
- 浅谈elasticsearch 集群
elasticsearch 集群 摘要: elasticsearch 集群 搭建elasticsearch的集群 现在假设我们有3台es机器,想要把他们搭建成为一个集群 基本配置 每个节点都要进行这样 ...
- CF#235E. Number Challenge
传送门 可以理解为上一道题的扩展板.. 然后我们就可以YY出这样一个式子 ${\sum_{i=1}^a\sum_{j=1}^b\sum_{k=1}^cd(ijk)=\sum_{i=1}^a\sum_{ ...
- 20145330 《网络对抗》 Web安全基础实践
20145330 <网络对抗> Web安全基础实践 1.实验后回答问题 (1)SQL注入攻击原理,如何防御 SQL注入,就是通过把SQL命令插入到Web表单提交或输入域名或页面请求的查询字 ...
- 20145212罗天晨 逆向及Bof基础实践
20145212罗天晨<网络对抗>第1周学习总结--逆向及Bof基础实践 逆向及Bof基础实践 一.实践目标 1.运行原本不可访问的代码片段 2.强行修改程序执行流 3.以及注入运行任意代 ...
- Cortex-M3基础
(一)寄存器 1 寄存器组 R0-R12: 通用寄存器 ------------------------------------------------------------------- ...
- 移植openssh到arm linux
一.在移植之前需要准备做一些前期准备: 1.移植zlib库 1.1获取zlib源码 1.2解压 tar xvf zlib-1.2.11.tar.xz 1.3交叉编译 1.3.1 指定交叉编译器 exp ...
- 【基础知识】ActiveMQ基本原理
“来,根据你的了解说下 ActiveMQ 是什么.” “这个简单,ActiveMQ 是一个 MOM,具体来说是一个实现了 JMS 规范的系统间远程通信的消息代理.它……” “等等,先解释下什么是 MO ...