题目地址:http://soj.me/1136(这是一道虐了我几天的题目)

一开始看到这道题,顿时欣喜不少,发现就是最大子数组问题,但是看看数据,就吓尿了,真心觉得常规解法过不了(但还是抱着试试的态度写了,结果直接time limited了),想了好久没头绪,果断google之,发现了一个叫做线段树的东西。

这里先推荐这篇博客(我最终从这里走出了误区):http://blog.csdn.net/lifajun90/article/details/8190443。

当然我一开始是看到的这篇:http://blog.csdn.net/fanfank/article/details/8930394这篇有一个地方误导了我好久(lmax和rmax那里没说是保存在哪个节点,我根据上下文就理解错误了)。

据我所理解的线段树,就是要查询的区间分成很多子区间,然后把子区间的合并到一个区间,关键就是合并这个过程比较难(ps:如果是求子区间的最大数,那就当简单了,直接父节点 = max(左节点,右节点)),这道题的合并第一个博客利用了前面的一个函数,当然第二篇博客原理也是一样。下面附上我的代码:

#include <iostream>
#include <cstring>
#include <cstdio>
#define MAX 111111
#define leftson l, m, (l+r) >> 1
#define rightson m+1, r, (l+r) >> 1 | 1
using namespace std;
struct Node {int sum;int sl, sr, smax;int ll, lmax, rr, rmax;
};
Node n[MAX*4];
int count_;
int a[MAX];
Node pushUp(int root, int lson, int rson) {n[root].sum = n[lson].sum + n[rson].sum;int temp = n[rson].lmax + n[lson].sum;if (temp > n[lson].lmax) {n[root].lmax = temp;n[root].ll = n[rson].ll;} else {n[root].lmax = n[lson].lmax;n[root].ll = n[lson].ll;}temp = n[rson].sum + n[lson].rmax;if (temp >= n[rson].rmax) {n[root].rmax = temp;n[root].rr = n[lson].rr;} else {n[root].rmax = n[rson].rmax;n[root].rr = n[rson].rr;}n[root].smax = n[lson].smax;n[root].sl = n[lson].sl;n[root].sr = n[lson].sr;temp = n[lson].rmax + n[rson].lmax;if (temp > n[root].smax || (temp == n[root].smax && n[lson].rr < n[root].sl)) {n[root].smax = temp;n[root].sl = n[lson].rr;n[root].sr = n[rson].ll;}if (n[rson].smax > n[root].smax) {n[root].smax = n[rson].smax;n[root].sl = n[rson].sl;n[root].sr = n[rson].sr;}return n[root];
}
void build(int l, int r, int root) {if (l == r) {n[root].smax = n[root].rmax = n[root].lmax = n[root].sum = a[l];n[root].ll = n[root].sl = l;n[root].rr = n[root].sr = r;return;}int m = (l + r) >> 1;build(l, m, root*2);build(m+1, r, root*2+1);pushUp(root, root*2, root*2+1);
}
int query(int s, int e, int l, int r, int root) {if (s <= l && e >= r) {return root;}int m = (l + r) >> 1;if ( e <= m) {return query(s, e, l, m, root*2);} else if (s > m) {return query(s, e, m+1, r, root*2+1);} else {int temp1 = query(s, e, l, m, root*2);int temp2 = query(s, e, m+1, r, root*2+1);int k = count_++;pushUp(k, temp1, temp2);return k;}
}
int main() {int n1, n2;scanf("%d%d", &n1, &n2);for (int i = 1; i <= n1; i++)scanf("%d", a+i);build(1, n1, 1);while (n2--) {int start, end;scanf("%d%d", &start, &end);count_ = 3*n1+1;int index = query(start, end, 1, n1, 1);printf("%d %d %d\n", n[index].sl, n[index].sr, n[index].smax);}//system("pause");return 0;
}    

最后再送上一个学线段树的好地址:http://www.notonlysuccess.com/index.php/segment-tree-complete/

sicily 1136 山海经相关推荐

  1. Sicily 1136 山海经 (SOJ 1136) 【Segment Tree 线段树】

    原题地址:点击打开链接 这题花了整整一天来做,错误基本都是TLE,但是做完非常哈皮,因为感觉比较好地运用了线段树这个数据结构.话说这几天广东不是一般热,中午根本睡不着,满身黏糊糊,课室和图书馆倒成了平 ...

  2. sicily题目分类

    sicily题目分类 1. 编程入门 2. 数据结构 3. 字符串 4. 排序 5. 图遍历 6. 图算法 7. 搜索:剪枝,启发式搜索 8. 动态规划/递推 9. 分治/递归 10. 贪心 11. ...

  3. [sicily]部分题目分类

    sicily题目分类 1. 编程入门 2. 数据结构 3. 字符串 4. 排序 5. 图遍历 6. 图算法 7. 搜索:剪枝,启发式搜索 8. 动态规划/递推 9. 分治/递归 10. 贪心 11. ...

  4. Sicily 题目分类

    依照自己水平挑着做→ →~~ 1. 编程入门 2. 数据结构 3. 字符串 4. 排序 5. 图遍历 6. 图算法 7. 搜索:剪枝,启发式搜索 8. 动态规划/递推 9. 分治/递归 10. 贪心 ...

  5. 初学者acm的练习题指南

    上机练习题参考题 忘了在哪找的啦~~希望对大家有帮助呦 <!--[if !supportLists]-->1.    <!--[endif]-->Programming Bas ...

  6. 编程题目分类(剪辑)

    1. 编程入门 2. 数据结构 3. 字符串 4. 排序 5. 图遍历 6. 图算法 7. 搜索:剪枝,启发式搜索 8. 动态规划/递推 9. 分治/递归 10. 贪心 11. 模拟 12. 算术与代 ...

  7. git拉取请求_创建有效拉取请求的技巧

    git拉取请求 Pull Requests (PR) are crucial to almost all software development these days. They have beca ...

  8. 中大SICILY分类

    原文出处:http://linguifan2010.blog.163.com/blog/static/1315127442010102131322482/ ********************** ...

  9. 【线段树】【SOJ1136】【cogs775】山海经

    775. [SOJ 1136] 山海经 ★★★   输入文件: hill.in   输出文件: hill.out    简单对比 时间限制:1 s   内存限制:128 MB [问题描述] " ...

最新文章

  1. Python经典面试题100道(附PDF下载地址)
  2. Java定时器Timer
  3. c++ 和 C语言 中数组语法的比较
  4. a标签的四个伪类是什么?如何排序?为什么?
  5. linux对web后端重要吗,基于Linux的Web服务器性能测试
  6. CF813E Army Creation
  7. shell 学习之for语句
  8. “21天好习惯”第一期-6
  9. 计算机组成与人体类比,2020年安徽公务员考试真题模拟:类比推理(7.13)
  10. XRD测试常见问题及解答(三)
  11. 华为android phone 驱动,Huawei 手机 驱动程序下载——更新 Huawei 软件
  12. 纤亿通之光纤传输知识必备大全
  13. 洛谷入门-- P3717
  14. 第三阶段应用层——1.7 数码相册—电子书(1)—实现
  15. Karas中LSTM模型的各个参数的含义
  16. 【原创】解决JT2Go二次开发提示license key无效问题
  17. Android使用Mp4v2用h264流和aac流合成mp4
  18. 揭秘:广告拦截软件如何赚钱?
  19. 如何有效清理C盘?清除Windows更新后残留文件?磁盘清理?
  20. 做生意和追女人,“绝色真经”!

热门文章

  1. c语言中找不到EXE,老鸟解决windows7系统出现找不到文件c:\Windows\system32\msdt.exe的具体处理步骤...
  2. 2018宝宝取名常用字(带释义)
  3. 计算机丢失divxdecoder.dll,divxdecoder.dll
  4. 《金色梦乡》金句摘抄(七)
  5. JMeter常见问题集合
  6. TCP的三次握手过程图解
  7. Spring Boot 系列:过滤器+拦截器+监听器
  8. [ 常用工具篇 ] kali 忘记 root 密码 -- 修改 root 密码
  9. 死磕算法第一弹——数组、集合与散列表
  10. 实现全场景智慧,没产业生态不行