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

给出区间大小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. Spring整合ActiveMQ:spring+JMS+ActiveMQ+Tomcat

    一.目录结构 相关jar包 二.关键配置activmq.xml <?xml version="1.0" encoding="UTF-8"?> < ...

  2. linux常用命令:crontab 命令

    前一天学习了 at 命令是针对仅运行一次的任务,循环运行的例行性计划任务,linux系统则是由 cron (crond) 这个系统服务来控制的.Linux 系统上面原本就有非常多的计划性工作,因此这个 ...

  3. android 实践项目四

    android 实践项目四 本周主要是开发android baidumap实现公交的查询 1.权限的取得和对屏幕的支持 <uses-permission android:name="a ...

  4. spring boot 使用@ConfigurationProperties

    有时候有这样子的情景,我们想把配置文件的信息,读取并自动封装成实体类,这样子,我们在代码里面使用就轻松方便多了,这时候,我们就可以使用@ConfigurationProperties,它可以把同类的配 ...

  5. Educational Codeforces Round 27 A B C

    A. Chess Tourney   Berland annual chess tournament is coming! Organizers have gathered 2·n chess pla ...

  6. Leetcode ——Lowest Common Ancestor of a Binary Tree

    Question Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. ...

  7. tyvj 2075 [NOIP2012T5]借教室 区间更新+二分

    描述 在大学期间,经常需要租借教室.大到院系举办活动,小到学习小组自习讨论,都需要向学校申请借教室.教室的大小功能不同,借教室人的身份不同,借教室的手续也不一样.面对海量租借教室的信息,我们自然希望编 ...

  8. json字符串在javascript和java代码中的表示方式

    最近在使用js写json形式的字符串的时候,当我把json形式的字符串放到函数JSON.parse()的时候,总是报错Uncaught SyntaxError: Unexpected token ' ...

  9. python排序(插入排序) 从小到大顺序

    def insert_sort(ilist): for i in range(len(ilist)): for j in range(i): if ilist[i] < ilist[j]: il ...

  10. Java实现邮箱激活验证2

    SendEmail.java [java] view plaincopyprint?   package com.app.tools; import java.util.Date; import ja ...