hdu3308--LCIS 最大连续递增序列长度
这个是动态的,所以要用线段树维护。代码里有注释因为ls敲成lsum,rs敲成rsum查错查了好久。。
- #include <set>
- #include <map>
- #include <cmath>
- #include <ctime>
- #include <queue>
- #include <stack>
- #include <cctype>
- #include <cstdio>
- #include <string>
- #include <vector>
- #include <cstdlib>
- #include <cstring>
- #include <iostream>
- #include <algorithm>
- using namespace std;
- typedef unsigned long long ull;
- typedef long long ll;
- const int inf = 0x3f3f3f3f;
- const double eps = 1e-;
- template <class T>
- inline bool scan_d(T &ret)
- {
- char c;
- int sgn;
- if(c=getchar(),c==EOF) return ;
- while(c!='-'&&(c<''||c>''))
- c=getchar();
- sgn=(c=='-')?-:;
- ret=(c=='-')?:(c-'');
- while(c=getchar(),c>=''&&c<='')
- ret=ret*+(c-'');
- ret*=sgn;
- return ;
- }
- const int maxn = 1e5+;
- int ls[maxn<<],rs[maxn<<]; //ls为区间左值,rs为区间右值 比较时要用到。
- int lsum[maxn<<],rsum[maxn<<],sum[maxn<<]; //lsum为区间左上升长度,rsum为区间右上升长度,sum为区间最大上升长度。
- void push_up(int l,int r,int pos)
- {
- ls[pos] = ls[pos<<];
- rs[pos] = rs[pos<<|];
- lsum[pos] = lsum[pos<<];
- rsum[pos] = rsum[pos<<|];
- sum[pos] = max(sum[pos<<],sum[pos<<|]);
- int mid = (l + r) >> ;
- if (ls[pos<<|] > rs[pos<<])
- {
- sum[pos] = max(sum[pos],rsum[pos<<] + lsum[pos<<|]);
- if (lsum[pos<<] == mid - l + ) //左区间左上升长度已满
- lsum[pos] += lsum[pos<<|];
- if (rsum[pos<<|] == r - mid) //同理,
- rsum[pos] += rsum[pos<<];
- }
- }
- void build (int l,int r,int pos)
- {
- if (l == r)
- {
- int tmp;
- scanf ("%d",&tmp);
- ls[pos] = tmp;
- rs[pos] = tmp;
- lsum[pos] = rsum[pos] = sum[pos] = ;
- return;
- }
- int mid =(l + r) >> ;
- build(l,mid,pos<<);
- build(mid+,r,pos<<|);
- push_up(l,r,pos);
- }
- void update(int l,int r,int pos,int x,int val)
- {
- if (l == r)
- {
- ls[pos] = rs[pos] = val;
- return;
- }
- int mid = (l + r) >> ;
- if (x <= mid)
- update(l,mid,pos<<,x,val);
- else
- update(mid+,r,pos<<|,x,val);
- push_up(l,r,pos);
- }
- int query(int l,int r,int pos,int ua,int ub)
- {
- if (ua <= l && ub >= r)
- {
- return sum[pos];
- }
- int mid = (l + r) >> ;
- int ans1 = ,ans2 = ,ans3 = ;
- if (ua <= mid)
- ans1 = query(l,mid,pos<<,ua,ub);
- if (ub > mid)
- ans2 = query(mid+,r,pos<<|,ua,ub);
- if (ls[pos<<|] > rs[pos<<])
- ans3 = min(lsum[pos<<|],ub-mid) + min(rsum[pos<<],mid-ua+);
- return max(ans1,max(ans2,ans3));
- }
- int main(void)
- {
- #ifndef ONLINE_JUDGE
- freopen("in.txt","r",stdin);
- #endif
- int t;
- cin>>t;
- while (t--)
- {
- int n,q;
- scanf ("%d%d",&n,&q);
- build(,n,);
- char op[];
- int x,y;
- for (int i = ; i < q; i++)
- {
- scanf ("%s%d%d",op,&x,&y);
- if (op[] == 'Q')
- printf("%d\n",query(,n,,x+,y+));
- if (op[] == 'U')
- update(,n,,x+,y);
- }
- }
- return ;
- }
hdu3308--LCIS 最大连续递增序列长度的更多相关文章
- LeetCode 674. Longest Continuous Increasing Subsequence (最长连续递增序列)
Given an unsorted array of integers, find the length of longest continuous increasing subsequence. E ...
- [Swift]LeetCode674. 最长连续递增序列 | Longest Continuous Increasing Subsequence
Given an unsorted array of integers, find the length of longest continuous increasing subsequence (s ...
- LeetCode 最长连续递增序列
给定一个未经排序的整数数组,找到最长且连续的的递增序列. 示例 1: 输入: [1,3,5,4,7] 输出: 3 解释: 最长连续递增序列是 [1,3,5], 长度为3. 尽管 [1,3,5,7] 也 ...
- LeetCode 674. Longest Continuous Increasing Subsequence最长连续递增序列 (C++/Java)
题目: Given an unsorted array of integers, find the length of longest continuous increasing subsequenc ...
- leetcode 674. 最长连续递增序列
1. 题目 给定一个未经排序的整数数组,找到最长且连续的的递增序列. 示例 1: 输入: [1,3,5,4,7] 输出: 3 解释: 最长连续递增序列是 [1,3,5], 长度为3. 尽管 [1,3, ...
- LeetCode 674. 最长连续递增序列(Longest Continuous Increasing Subsequence) 18
674. 最长连续递增序列 674. Longest Continuous Increasing Subsequence 题目描述 给定一个未经排序的整型数组,找到最长且连续的递增序列. Given ...
- Leetcode674.Longest Continuous Increasing Subsequence最长连续递增序列
给定一个未经排序的整数数组,找到最长且连续的的递增序列. 示例 1: 输入: [1,3,5,4,7] 输出: 3 解释: 最长连续递增序列是 [1,3,5], 长度为3. 尽管 [1,3,5,7] 也 ...
- Java实现 LeetCode 674 最长连续递增序列(暴力)
674. 最长连续递增序列 给定一个未经排序的整数数组,找到最长且连续的的递增序列. 示例 1: 输入: [1,3,5,4,7] 输出: 3 解释: 最长连续递增序列是 [1,3,5], 长度为3. ...
- C#LeetCode刷题之#674-最长连续递增序列( Longest Continuous Increasing Subsequence)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3734 访问. 给定一个未经排序的整数数组,找到最长且连续的的递增 ...
随机推荐
- [置顶] 白话二分匹配之最大匹配+附上hdu2063解题报告
最近开始学习图论的二分匹配,关于最大匹配做一次小总结,希望自己后面回头来看一目明了,也对刚接触的人有帮助: ps:开始有的文字很多....对于很多人来说一看到文字就烦啦...不过这个总结是针对匈牙利算 ...
- Linux下的bc计算器
bc = basic calculator scale:设置精度,默认为0 obase:设置输出进制,默认为10 ibase:设置输入进制,默认为10 原文:http://www.linuxidc.c ...
- 添加链接服务器 SQL SERVER
使用sql语句: exec sp_addlinkedserver @server='serverontest',@provider='sqloledb',@srvproduct='',@datasrc ...
- HDU4662+无
把目标中的 U 转化为 I. 又因为 I的个数是有规律的:1 2 4 8 16 ...再结合可以取消 6 12 18 ...个I...得解 #include<string.h> #incl ...
- MySQL数据库的环境及简单操作
***********************************************声明*************************************************** ...
- CH BR8(小学生放假了-clock()/CLOCKS_PER_SEC-斜率优化常错集锦)
小学生放假了 总时限 26s 内存限制 256MB 出题人 zsyzzsoft 提交情况 16/150 初始分值 1500 锁定情况 背景 我们能见到的最可怕的事情,莫过于小学生放假了! 描述 小学生 ...
- .NET基础拾遗(7)多线程开发基础4
一.多线程编程中的线程同步 1.C#中的lock关键字 lock关键字可能是我们在遇到线程同步的需求时最常用的方式,但lock只是一个语法糖,为什么这么说呢,下面慢慢道来. (1)lock的等效代码其 ...
- angularJS环境安装
第一步: 安装node.js,进入node.js官网(http://nodejs.org/)下载安装相应的node.js版本:
- JavaScript随机排序算法1
1.对数组循环,每一项与随机的某一项位置调换 <ul id="listOne"></ul> <div id="tempOne"&g ...
- 【nodejs学习】1.文件操作
1.小文件拷贝,使用nodejs内置模块 var fs = require('fs'); function copy(src, dst){ fs.writeFileSync(dst, fs.readF ...