线段树的区间合并 B - LCIS
这个是一个很简单很明显的线段树的区间合并,不过区间合并的题目都还是有点难写,建议存个板子。
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn = 1e5 + ;
struct node
{
int l, r, rp, lp, len;
int max_pre, max_sub, max_last;
}tree[maxn*];
int a[maxn]; void push_up(int id)
{
tree[id].rp = tree[id<<|].rp;
tree[id].lp = tree[id << ].lp;
tree[id].max_pre = tree[id << ].max_pre;
tree[id].max_last = tree[id << | ].max_last;
tree[id].max_sub = max(tree[id << ].max_sub, tree[id << | ].max_sub);
if(tree[id<<].rp<tree[id<<|].lp)
{
if (tree[id << ].max_pre == tree[id << ].len) tree[id].max_pre += tree[id << | ].max_pre;
if (tree[id << | ].max_last == tree[id << | ].len) tree[id].max_last += tree[id << ].max_last;
tree[id].max_sub = max(tree[id].max_sub, tree[id << ].max_last + tree[id<<|].max_pre);
}
} void build(int id,int l,int r)
{
tree[id].l = l;
tree[id].r = r;
tree[id].len = r - l + ;
if(l==r)
{
tree[id].rp = tree[id].lp = a[l];
tree[id].max_pre = tree[id].max_last = tree[id].max_sub = ;
return;
}
int mid = (l+r) >> ;
build(id << , l, mid);
build(id << | , mid + , r);
push_up(id);
} int query(int id,int x,int y)
{
int ans = ;
if(x<=tree[id].l&&tree[id].r<=y)
{
return tree[id].max_sub;
}
int mid = (tree[id].l + tree[id].r) >> ;
if (x <= mid) ans = max(ans, query(id << , x, y));
if (y > mid) ans = max(ans, query(id << | , x, y));
if(tree[id<<].rp<tree[id<<|].lp)
{
ans = max(ans, min(mid - x + , tree[id << ].max_last) + min(y - mid, tree[id << | ].max_pre));
}
return ans;
} void update(int id,int x,int z)
{
if(tree[id].l==tree[id].r&&tree[id].l==x)
{
tree[id].rp = z;
tree[id].lp = z;
return;
}
int mid = (tree[id].l + tree[id].r) >> ;
if (x <= mid) update(id << , x, z);
if (x > mid) update(id << | , x, z);
push_up(id);
} int main()
{
int t;
scanf("%d", &t);
while(t--)
{
int n, m;
scanf("%d%d", &n, &m);
for(int i=;i<=n;i++)
{
scanf("%d", &a[i]);
}
build(, , n);
while(m--)
{
char s[];
int x, y;
scanf("%s %d %d", s, &x, &y);
if(s[]=='Q')
{
int ans = query(, x+, y+);
printf("%d\n", ans);
}
else update(, x+, y);
}
}
}
线段树的区间合并 B - LCIS的更多相关文章
- 线段树:CDOJ1592-An easy problem B (线段树的区间合并)
An easy problem B Time Limit: 2000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Pr ...
- CodeForces - 587E[线段树+线性基+差分] ->(线段树维护区间合并线性基)
题意:给你一个数组,有两种操作,一种区间xor一个值,一个是查询区间xor的结果的种类数 做法一:对于一个给定的区间,我们可以通过求解线性基的方式求出结果的种类数,而现在只不过将其放在线树上维护区间线 ...
- Codeforces Round #222 (Div. 1) D. Developing Game 线段树有效区间合并
D. Developing Game Pavel is going to make a game of his dream. However, he knows that he can't mak ...
- [HDOJ3308]LCIS(线段树,区间合并)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3308 题意:给定n个数,两个操作: U A B:将位置A的数值改成B Q A B:查询[A,B]内最长 ...
- POJ 3667 & HDU 3308 & HDU 3397 线段树的区间合并
看到讲课安排上 线段树有一节课"区间合并" 我是迷茫的 因为并没有见过 然后了解了一下题目 发现以前写过 还是很麻烦的树链剖分 大概是 解决带修改的区间查询"连续问题&q ...
- HDU_3308_线段树_区间合并
LCIS Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- POJ 2750 Potted Flower(线段树的区间合并)
点我看题目链接 题意 : 很多花盆组成的圆圈,每个花盆都有一个值,给你两个数a,b代表a位置原来的数换成b,然后让你从圈里找出连续的各花盆之和,要求最大的. 思路 :这个题比较那啥,差不多可以用DP的 ...
- HDU3308 线段树(区间合并)
LCIS Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- 线段树(区间合并)HDU - 1540
题意:输入n,m,给定n个相互连通的村庄,有m个操作,D x,表示破坏x村庄使其与相邻的两个村庄不相通,R 表示修复上一个被破坏的村庄,与相邻的两个村庄联通.Q x表示与x相连的村庄有多少个. 思路: ...
随机推荐
- CVE-2019-0193 远程命令执行-漏洞复现
0x01 漏洞简介 Apache Solr 是一个开源的搜索服务器.Solr 使用 Java 语言开发,主要基于 HTTP 和 Apache Lucene 实现.此次漏洞出现在Apache Solr的 ...
- 架构师修炼之设计模式 - 策略模式(Strategy) 【Python与C#实现】
程序员,我为你祝福 愿你有一个灿烂的前程 愿你有情人终成眷属 愿你在尘世获得幸福 我只想成为架构师,走遍江湖! 目录 模式定义 模式分类 模式结构 实例(C#与Python版) 优点 缺点 使用场景 ...
- 成员指针与mem_fn
本文是<functional>系列的第4篇. 成员指针是一个非常具有C++特色的功能.更低级的语言(如C)没有类,也就没有成员的概念:更高级的语言(如Java)没有指针,即使有也不会有成员 ...
- 3. pkg
程序打包成可执行文件(.exe) 1.) npm install -g pkg 2.) 单个文件:pkg entrance.js ( windows: pkg -t win entrance.js ...
- awd平台搭建
1.先是使用 https://github.com/m0xiaoxi/AWD_CTF_Platform 这个平台搭建 这个平台很好用,是python脚本自动搭建,基本不需要怎么更改,自带了四道题的源码 ...
- Spring5:面向切面
静态代理 缺点:一个真实角色就会产生一个代理角色,代码量会翻倍! 场景:要在写好的实现方法上加入日志功能(公共功能),不要修改原代码 1:原代码 业务接口: package com.spring; p ...
- form表单里的button调用js函数
近来发现一个特别奇怪的问题:在form表单里,button的onclick事件无法调用js函数.代码如下(这段代码放在form标签里): dropUpdateAddress调用的js函数为: 这个时候 ...
- react typescript jest config (一)
1. initialize project create a folder project Now we'll turn this folder into an npm package. npm in ...
- 关于Sysinternals Suite
sysinternals 的网站创立于1996年由Mark russinovich和布赖科格斯韦尔主办其先进的系统工具和技术资料·微软于2006年7月收购sysinternals公司 . 不管你是一个 ...
- Go语言: 万物皆异步
来源:https://www.jianshu.com/p/62c0cd107da3 同步和异步.阻塞和非阻塞 首先要明确的是,同步(Synchronous)和异步(Asynchronous),阻塞(B ...