HDU3564 --- Another LIS (线段树维护最值问题)
Another LIS
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1244 Accepted Submission(s): 431
For every test case, an integer N (1 <= N <= 100000) comes first, then there are N numbers, the k-th number Xk means that we add number k at position Xk (0 <= Xk <= k-1).See hint for more details.
In the sample, we add three numbers to the sequence, and form three sequences.
a. 1
b. 2 1
c. 2 1 3
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 1e5+;
int seg[maxn<<],a[maxn]; // seg表示该区间 位置剩余量
void build (int l,int r,int pos)
{
if (l == r)
{
seg[pos] = ;
return;
}
int mid = (l + r) >> ;
build(l,mid,pos<<);
build(mid+,r,pos<<|);
seg[pos] = seg[pos<<] + seg[pos<<|];
}
int ans[maxn]; //ans[i]表示 元素i的位置为ans[i]
void Insert(int l,int r,int pos,int x,int k)
{
if (l == r)
{
ans[k] = l;
seg[pos] = ;
return ;
}
int mid = (l + r) >> ;
if (seg[pos<<] >= x) //左孩子的区间 位置剩余量大于x时,递归进入左孩子,否则右孩子同时 x-seg[pos<<1]
Insert(l,mid,pos<<,x,k);
else
Insert(mid+,r,pos<<|,x - seg[pos<<],k);
seg[pos] = seg[pos<<] + seg[pos<<|];
}
void update(int l,int r,int pos,int x,int v)
{
if (l == r)
{
seg[pos] = v;
return;
}
int mid = (l + r) >> ;
if (x <= mid)
update(l,mid,pos<<,x,v);
else
update(mid+,r,pos<<|,x,v);
seg[pos] = max(seg[pos<<],seg[pos<<|]);
}
int query(int l,int r,int pos,int ua,int ub)
{
if (ua <= l && ub >= r)
{
return seg[pos];
}
int mid = (l + r) >> ;
int t1 = ,t2 = ;
if (ua <= mid)
t1 = query(l,mid,pos<<,ua,ub);
if (ub > mid)
t2 = query(mid+,r,pos<<|,ua,ub);
return max(t1,t2);
} int main(void)
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
int t,cas = ;
scanf ("%d",&t);
while (t--)
{
int n;
scanf ("%d",&n);
for (int i = ; i <= n; i++)
scanf ("%d",a+i);
build (,n,);
for (int i = n; i >= ; i--)
Insert(,n,,a[i] + ,i); //倒序 确定每个元素 最终的位置保存在ans里
memset(seg,,sizeof(seg));
printf("Case #%d:\n",cas++);
for (int i = ; i <= n; i++)
{
int t1 = query(,n,,,n);
int t2 = query(,n,,,ans[i]);
update(,n,,ans[i],t2+);
if (t2 < t1) //输出当前区间的LIS
printf("%d\n",t1);
else
printf("%d\n",+t2);
}
printf("\n");
}
return ;
}
HDU3564 --- Another LIS (线段树维护最值问题)的更多相关文章
- MemSQL Start[c]UP 2.0 - Round 1 F - Permutation 思维+线段树维护hash值
F - Permutation 思路:对于当前的值x, 只需要知道x + k, x - k这两个值是否出现在其左右两侧,又因为每个值只有一个, 所以可以转换成,x+k, x-k在到x所在位置的时候是否 ...
- BZOJ 2124 线段树维护hash值
思路: http://blog.csdn.net/wzq_QwQ/article/details/47152909 (代码也是抄的他的) 自己写得垃圾线段树怎么都过不了 隔了两个月 再写 再挂 又隔了 ...
- cf213E 线段树维护hash
链接 https://codeforces.com/contest/213/problem/E 题目大意 给出两个排列a.b,长度分别为n.m,你需要计算有多少个x,使 得\(a_1 + x; a_2 ...
- FJUT3568 中二病也要敲代码(线段树维护区间连续最值)题解
题意:有一个环,有1~N编号,m次操作,将a位置的值改为b,问你这个环当前最小连续和多少(不能全取也不能不取) 思路:用线段树维护一个区间最值连续和.我们设出两个变量Lmin,Rmin,Mmin表示区 ...
- BZOJ.1036 [ZJOI2008]树的统计Count ( 点权树链剖分 线段树维护和与最值)
BZOJ.1036 [ZJOI2008]树的统计Count (树链剖分 线段树维护和与最值) 题意分析 (题目图片来自于 这里) 第一道树链剖分的题目,谈一下自己的理解. 树链剖分能解决的问题是,题目 ...
- 【uoj#164】[清华集训2015]V 线段树维护历史最值
题目描述 给你一个长度为 $n$ 的序列,支持五种操作: $1\ l\ r\ x$ :将 $[l,r]$ 内的数加上 $x$ :$2\ l\ r\ x$ :将 $[l,r]$ 内的数减去 $x$ ,并 ...
- 【bzoj3064】Tyvj 1518 CPU监控 线段树维护历史最值
题目描述 给你一个序列,支持4种操作:1.查询区间最大值:2.查询区间历史最大值:3.区间加:4.区间赋值. 输入 第一行一个正整数T,表示Bob需要监视CPU的总时间. 然后第二行给出T个数表示在你 ...
- 滑动窗口(poj,线段树维护区间最值)
题目描述 现在有一堆数字共N个数字(N<=10^6),以及一个大小为k的窗口.现在这个从左边开始向右滑动,每次滑动一个单位,求出每次滑动后窗口中的最大值和最小值. 例如: The array i ...
- CF213E Two Permutations 线段树维护哈希值
当初竟然看成子串了$qwq$,不过老师的$ppt$也错了$qwq$ 由于子序列一定是的排列,所以考虑插入$1$到$m$到$n-m+1$到$n$; 如何判断呢?可以用哈希$qwq$: 我们用线段树维护哈 ...
随机推荐
- android 几种发送短信的方法
android中发送短信很简单, 首先要在Mainfest.xml中加入所需要的权限: ? 1 2 3 <uses-permission android:name="android.p ...
- WIN32_FIND_DATA 循环获取文件大小BUG
今天在调试程序时发现一个 WIN32_FIND_DATA 的BUG,在循环读取一个目录下的图片文件时,发现结构体中 nFileSizeLow 和 nFileSizeHigh 值 == 0的情况.即能获 ...
- 用户与 Oracle DB 交互具体过程
与 Oracle DB 交互 以下的演示样例从最主要的层面描写叙述 Oracle DB 操作.该演示样例说明了一种 Oracle DB 配置,在该配置中,用户和关联server进程执行于通过网络连接的 ...
- boost之ThreadPool
threadpool是基于boost库实现的一个线程池子库,但线程池实现起来不是很复杂.我们从threadpool中又能学到什么东西呢? 它是基于boost库实现的,如果大家对boost库有兴趣,看看 ...
- autochannel 指定栏目
(> DedeCMS 4,DedeCMS5) 名称:autochannel 功能:指定排序位置的单个栏目的链接 语法: {dede:autochannel partsort='2' typeid ...
- linux wc命令
Linux系统中的wc(Word Count)命令的功能为统计指定文件中的字节数.字数.行数,并将统计结果显示输出. 1.命令格式: wc [选项]文件... 2.命令功能: 统计指定文件中的字节数. ...
- ComboBox( 下拉列表框)
一. 加载方式//class 加载方式<select id="box" class="easyui-combobox" name="box&qu ...
- 网页JavaScript1
DOM的操作 windows对象操作 属性: opener,打开当前窗口的源窗口,首次启动 是null. dialogArgument,对话框的返回值 子对象: history , location ...
- 如何查看Android SDK源码版本
PLATFORM_VERSION := 4.2.2 位于/build/core/version_defaults.mk # # Copyright (C) 2008 The Android Open ...
- Google推出iOS功能性UI测试框架EarlGrey
经过了一段时间的酝酿后,Google很高兴地宣布了EarlGrey,一款针对于iOS的功能性UI测试框架.诸如YouTube.Google Calendar.Google Photos.Google ...