题意:求q次询问的静态区间连续最大和起始位置和终止位置 输出字典序最小的解.

思路:刘汝佳白书 每个节点维护三个值

pre, sub, suf 最大的前缀和, 连续和, 后缀和 然后这个题还要记录解的位置所以还要区间总和sum

 #include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<cstring>
#include<stack>
#include<cmath>
#include<queue>
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define ll long long
#define clc(a,b) memset(a,b,sizeof(a)) const int maxn = + ;
const int maxnode = + ;
typedef long long LL;
typedef pair<int,int> Interval; LL prefix_sum[maxn]; LL sum(int L, int R)
{
return prefix_sum[R] - prefix_sum[L-];
} LL sum(Interval p)
{
return sum(p.first, p.second);
} Interval better(Interval a, Interval b)
{
if(sum(a) != sum(b)) return sum(a) > sum(b) ? a : b;
return a < b ? a : b; // 利用pair自带的字典序
} int qL, qR; struct IntervalTree
{
int max_prefix[maxnode];
int max_suffix[maxnode];
Interval max_sub[maxnode]; void build(int o, int L, int R)
{
if(L == R)
{
max_prefix[o] = max_suffix[o] = L;
max_sub[o] = make_pair(L, L);
}
else
{
int M = L + (R-L)/;
// 递归创建子树
int lc = o*, rc = o*+;
build(lc, L, M);
build(rc, M+, R); // 递推max_prefix
LL v1 = sum(L, max_prefix[lc]);
LL v2 = sum(L, max_prefix[rc]);
if(v1 == v2) max_prefix[o] = min(max_prefix[lc], max_prefix[rc]);
else max_prefix[o] = v1 > v2 ? max_prefix[lc] : max_prefix[rc]; // 递推max_suffix
v1 = sum(max_suffix[lc], R);
v2 = sum(max_suffix[rc], R);
if(v1 == v2) max_suffix[o] = min(max_suffix[lc], max_suffix[rc]);
else max_suffix[o] = v1 > v2 ? max_suffix[lc] : max_suffix[rc]; // 递推max_sub
max_sub[o] = better(max_sub[lc], max_sub[rc]); // 完全在左子树或者右子树
max_sub[o] = better(max_sub[o], make_pair(max_suffix[lc], max_prefix[rc])); // 跨越中线
}
} Interval query_prefix(int o, int L, int R)
{
if(max_prefix[o] <= qR) return make_pair(L, max_prefix[o]);
int M = L + (R-L)/;
int lc = o*, rc = o*+;
if(qR <= M) return query_prefix(lc, L, M);
Interval i = query_prefix(rc, M+, R);
i.first = L;
return better(i, make_pair(L, max_prefix[lc]));
} Interval query_suffix(int o, int L, int R)
{
if(max_suffix[o] >= qL) return make_pair(max_suffix[o], R);
int M = L + (R-L)/;
int lc = o*, rc = o*+;
if(qL > M) return query_suffix(rc, M+, R);
Interval i = query_suffix(lc, L, M);
i.second = R;
return better(i, make_pair(max_suffix[rc], R));
} Interval query(int o, int L, int R)
{
if(qL <= L && R <= qR) return max_sub[o];
int M = L + (R-L)/;
int lc = o*, rc = o*+;
if(qR <= M) return query(lc, L, M);
if(qL > M) return query(rc, M+, R);
Interval i1 = query_prefix(rc, M+, R); // 右半的前缀
Interval i2 = query_suffix(lc, L, M); // 左半的后缀
Interval i3 = better(query(lc, L, M), query(rc, M+, R));
return better(make_pair(i2.first, i1.second), i3);
}
}; IntervalTree tree; int main()
{
int kase = , n, a, Q;
while(scanf("%d%d", &n, &Q) == )
{
prefix_sum[] = ;
for(int i = ; i < n; i++)
{
scanf("%d", &a);
prefix_sum[i+] = prefix_sum[i] + a;
}
tree.build(, , n);
printf("Case %d:\n", ++kase);
while(Q--)
{
int L, R;
scanf("%d%d", &L, &R);
qL = L;
qR = R;
Interval ans = tree.query(, , n);
printf("%d %d\n", ans.first, ans.second);
}
}
return ;
}

uvalive 3938 "Ray, Pass me the dishes!" 线段树 区间合并的更多相关文章

  1. UVALive 3938 - "Ray, Pass me the dishes!" - [最大连续子列和+线段树]

    题目链接:https://cn.vjudge.net/problem/UVALive-3938 参考刘汝佳书上说的: 题意: 给出一个长度为n的序列, 再给出m个询问, 每个询问是在序列 $[a,b] ...

  2. UVALive 3938 Ray, Pass me the dishes! (动态最大连续和)

    题意:求一个动态区间的最大连续和. 静态版本的O(n)算法显示不适用了,但是可以用线段树分治,因为一个连续和要在两边的区间,要么跨越两边,对于一个结点维护最大前缀和,后缀和,子区间连续和. 题目要求输 ...

  3. UVA 1400."Ray, Pass me the dishes!" -分治+线段树区间合并(常规操作+维护端点)并输出最优的区间的左右端点-(洛谷 小白逛公园 升级版)

    "Ray, Pass me the dishes!" UVA - 1400 题意就是线段树区间子段最大和,线段树区间合并,但是这道题还要求输出最大和的子段的左右端点.要求字典序最小 ...

  4. POJ 3667 Hotel(线段树 区间合并)

    Hotel 转载自:http://www.cnblogs.com/scau20110726/archive/2013/05/07/3065418.html [题目链接]Hotel [题目类型]线段树 ...

  5. HDU 3911 线段树区间合并、异或取反操作

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3911 线段树区间合并的题目,解释一下代码中声明数组的作用: m1是区间内连续1的最长长度,m0是区间内连续 ...

  6. HDU 3911 Black And White(线段树区间合并+lazy操作)

    开始以为是水题,结果...... 给你一些只有两种颜色的石头,0为白色,1为黑色. 然后两个操作: 1 l r 将[ l , r ]内的颜色取反 0 l r 计算[ l , r ]内最长连续黑色石头的 ...

  7. HYSBZ 1858 线段树 区间合并

    //Accepted 14560 KB 1532 ms //线段树 区间合并 /* 0 a b 把[a, b]区间内的所有数全变成0 1 a b 把[a, b]区间内的所有数全变成1 2 a b 把[ ...

  8. poj3667 线段树 区间合并

    //Accepted 3728 KB 1079 ms //线段树 区间合并 #include <cstdio> #include <cstring> #include < ...

  9. hdu3911 线段树 区间合并

    //Accepted 3911 750MS 9872K //线段树 区间合并 #include <cstdio> #include <cstring> #include < ...

随机推荐

  1. Android开发框架

    AsyncHttpClient 它把HTTP所有的通信细节全部封装在了内部,我们只需要简单调用几行代码就可以完成通信操作 Universal-Image-Loader 它使得在界面上显示网络图片的操作 ...

  2. python 魔法方法

    I am not a creator, I just a porter. Note: Everything is object in python. 对于Python来说一切都是对象,也就是函数的参数 ...

  3. 通知NSNotificationCenter

    注意:接受通知要写在 viewDidLoad 方法里面 取得系统全局的唯一广播站 NSNotificationCenter *notification = [NSNotificationCenter  ...

  4. 常用数据字典---bai

    --常用数据字典 -- system: normal; sysdba --查询所有的逻辑对象.所有. select count(1) from dba_objects; select * from d ...

  5. C#.net调用axis2webService

    用C#.net调用axis2webService的时候需要引用web服务, 比如访问地址为:http://111.21.32.213:8080/axis2/services/AdService/get ...

  6. map(function, sequence)

    map(function, sequence) :对sequence中的item依次执行function(item),见执行结果组成一个List返回: >>> lt = range( ...

  7. RubyCritic:一款不错的检测代码质量工具

    关注代码质量是高效开发必须要做的一件事,那么在 Ruby 开发的过程中,是否有什么好的代码质量检测工具呢?下面由 Ruby 工程师路英瑞介绍一下 RubyCritic--一款还不错的代码质量检测工具. ...

  8. 什么是实时应用程序自我保护(RASP)?

    什么产品可以定义为 RASP? RASP 英文为 Runtime application self-protection,它是一种新型应用安全保护技术,它将保护程序想疫苗一样注入到应用程序和应用程序融 ...

  9. 基于Qt5.x的QCA加解密开源项目的编译过程

    记录一下Qt5.x下的QCA的编译过程 需要注意的几点 针对windows环境 1.编译QCA源码前,必须先安装openssl二进制文件 2.需要安装好vs2008等 ( 64-bit Visual ...

  10. 【HDOJ】4516 威威猫系列故事——因式分解

    可解的算法太多了,采用的算法是试x的值.注意题目的输入x^3-2x^2不会写成x^3+-2x^2.一直RE在这儿. /* 4516 */ #include <iostream> #incl ...