【cogs 775】山海经 ——Segment Tree

题目链接:

      TP

题解:

    我数据结构真心是弱啊= =。

  线段树好厉害啊,一直不会区间最大连续和,今天刚学习了一下233。

  维护前缀最大和后缀最大,越界最大(?),再维护一个区间最大,瞎搞搞就好了,RE了一遍233。

代码: 

  

 1 #define Troy
 2
 3 #include <bits/stdc++.h>
 4
 5 using namespace std;
 6
 7 inline int read(){
 8     int s=0,k=1;char ch=getchar();
 9     while(ch<‘0‘|ch>‘9‘)    ch==‘-‘?k=-1:0,ch=getchar();
10     while(ch>47&ch<=‘9‘)    s=s*10+(ch^48),ch=getchar();
11     return s*k;
12 }
13
14 const int N=100005;
15
16 int n,m,a[N];
17
18 struct node {
19     int l,r,val;
20     friend bool operator <(node x,node y){
21         return x.val!=y.val?x.val<y.val:(x.l!=y.l?x.l>y.l:x.r>y.r);
22     }
23     friend node operator +(node x,node y){
24         node z;
25         z.l=min(x.l,y.l);z.r=max(x.r,y.r);
26         z.val=x.val+y.val;return z;
27     }
28     inline void out(){printf("%d %d %d\n",l,r,val);}
29 };
30
31 struct Tree{
32     node prefix,suffix,middle,section;
33     Tree *lc,*rc;
34 }*root,tree[N*20],*ans;int cnt;
35
36 inline void update( Tree *u){
37     u->middle=u->lc->suffix+u->rc->prefix;
38     u->prefix=max(u->lc->prefix,u->lc->section+u->rc->prefix);
39     u->suffix=max(u->rc->suffix,u->rc->section+u->lc->suffix);
40     u->section=u->lc->section+u->rc->section;
41     u->middle=max(u->middle,max(u->lc->middle,max(u->rc->middle,max(u->prefix,u->suffix))));
42 }
43
44 inline void build(Tree *&u,int l,int r){
45     u=tree+cnt;++cnt;
46     if(l==r){
47         u->prefix=u->suffix=u->middle=u->section=(node){l,r,a[l]};
48         return ;
49     }
50     int mid=l+r>>1;
51     build(u->lc,l,mid);
52     build(u->rc,mid+1,r);
53     update(u);
54 }
55
56 inline void query(Tree *u,int l,int r,int x,int y){
57     if(x<=l&&r<=y){
58         if(ans==NULL)   ans=u;
59         else{
60             Tree *now=ans;ans=tree+cnt,++cnt;
61             ans->lc=now,ans->rc=u;
62             update(ans);
63         }
64         return ;
65     }
66     int mid=l+r>>1;
67     if(x<=mid)  query(u->lc,l,mid,x,y);
68     if(y>mid)   query(u->rc,mid+1,r,x,y);
69 }
70
71 int main(){
72     freopen("hill.in","r",stdin);
73     freopen("hill.out","w",stdout);
74     n=read(),m=read();
75     for(int i=1;i<=n;++i)   a[i]=read();
76     build(root,1,n);
77     for(int i=1;i<=m;++i){
78         int l=read(),r=read();
79         ans=NULL;query(root,1,n,l,r);
80         ans->middle.out();
81     }
82 }
时间: 2024-10-29 19:05:37

【cogs 775】山海经 ——Segment Tree的相关文章

cogs 775 山海经

775. 山海经 ★★★   输入文件:hill.in   输出文件:hill.out   简单对比时间限制:1 s   内存限制:128 MB [问题描述] "南山之首日鹊山.其首日招摇之山,临于西海之上,多桂,多金玉.有草焉,其状如韭而青华,其名日祝余,食之不饥--又东三百里,日堂庭之山,多棪木,多白猿,多水玉,多黄金. 又东三百八十里,日猨翼之山,其中多怪兽,水多怪鱼,多白玉,多蝮虫,多怪蛇,名怪木,不可以上.--" <山海经>是以山为纲,以海为线记载古代的河流.植物

COGS 775. 山海经 【线段树】

775. 山海经 [问题描述] “南山之首日鹊山.其首日招摇之山,临于西海之上,多桂,多金玉.有草焉,其状如韭而青华,其名日祝余,食之不饥……又东三百里,日堂庭之山,多棪木,多白猿,多水玉,多黄金. 又东三百八十里,日猨翼之山,其中多怪兽,水多怪鱼,多白玉,多蝮虫,多怪蛇,名怪木,不可以上.……” <山海经>是以山为纲,以海为线记载古代的河流.植物.动物及矿产等情况,而且每一条记录路线都不会有重复的山出现.某天,你的地理老师想重游<山海经>中的路线,为了简化问题,老师已经把每座山用

Aizu 2450 Do use segment tree 树链剖分+线段树

Do use segment tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.bnuoj.com/v3/problem_show.php?pid=39566 Description Given a tree with n (1 ≤ n ≤ 200,000) nodes and a list of q (1 ≤ q ≤ 100,000) queries, process the queries in order and out

Segment Tree Build I &amp; II

Segment Tree Build I The structure of Segment Tree is a binary tree which each node has two attributes start and end denote an segment / interval. start and end are both integers, they should be assigned in following rules: The root's start and end i

Segment Tree Modify

For a Maximum Segment Tree, which each node has an extra value max to store the maximum value in this node's interval. Implement a modify function with three parameterroot, index and value to change the node's value with[start, end] = [index, index] 

Segment Tree Query I &amp; II

Segment Tree Query I For an integer array (index from 0 to n-1, where n is the size of this array), in the corresponding SegmentTree, each node stores an extra attribute max to denote the maximum number in the interval of the array (index from start

Segment Tree Build II

The structure of Segment Tree is a binary tree which each node has two attributes start and end denote an segment / interval. start and end are both integers, they should be assigned in following rules: The root's start and end is given by build meth

2-D range sum query implementation(2-D segment tree)

Google interview question:一个二维数组,有两个方法,一个是update(x,y),更新一个cell的值,一个是query(x1,y1,x2,y2),查询(x1,y1,x2,y2)矩形内所有元素的和. Senario 1. update调用次数远大于query. Senario 2. query调用次数远大于update. Senario 3. 两种方法调用一样多. 对于senario 1,只需要用一个二维数组储存数据,update相应的cell,时间复杂度O(1),qu

线段树+离散化 IP地址段检查 SEGMENT TREE

Problem: Give a series of IP segments, for example, [0.0.0.1-0.0.0.3], [123.234.232.21-123.245.21.1]... Now there is a new IP, find which IP segment it's in ? Solution: First, we could map the ends of IP segments into some intervals, since the IP add