Codeforces.264E.Roadside Trees(线段树 DP LIS)
\(Description\)

\(Solution\)
还是看代码好理解吧。
为了方便,我们将x坐标左右反转,再将所有高度取反,这样依然是维护从左到右的LIS,但是每次是在右边删除元素。
这样对于在p刚种的树,最多只有9棵树比它高,即它只会转移到这9棵树,除这9棵树外,它可以从1~p-1的任何树转移(其它9棵树除比它高的外 同样可以从它前面任何树转移)。
我们把这9棵树的DP值暴力删掉,然后从低到高 从1~pos[h]-1转移并更新。按高度更新就只需要考虑位置合不合法了。
我们对位置建线段树维护每个位置的DP值,就只有单点修改、区间max。
对于砍掉右数第k棵树,设位置为p,因为只有右边最多9棵树从它转移,同样将它们的DP值暴力删掉,然后删掉位置p的DP值。
但是右边10棵树不一定是最高的,虽然它们可以从前面所有树转移,但还要满足高度小于它们。
这可以二维线段树。但是我们只需要用另一棵线段树对每个高度维护同样的DP值(不同位置高度不同),就可以从左到右,直接用线段树查询并更新了。
这样在一棵线段树上更新完DP值后在另一棵上改一下即可。
复杂度\(O(10n\log n)\)。
总结:是最高的10棵就在维护位置DP值的线段树上转移,是最靠右的10棵就在维护高度DP值的线段树上转移。最后更新一下另一棵的DP值(都维护一样的)。
//840ms 12800KB
#include <set>
#include <cstdio>
#include <cctype>
#include <algorithm>
#define gc() getchar()
#define MAXIN 50000
//#define gc() (SS==TT&&(TT=(SS=IN)+fread(IN,1,MAXIN,stdin),SS==TT)?EOF:*SS++)
typedef long long LL;
const int N=2e5+15;
int pos[N],h[N];
std::set<int> st;
char IN[MAXIN],*SS=IN,*TT=IN;
struct Segment_Tree
{
#define ls rt<<1
#define rs rt<<1|1
#define lson l,m,ls
#define rson m+1,r,rs
#define S N<<2
int f[N],mx[S];
#undef S
#define Update(rt) mx[rt]=std::max(mx[ls],mx[rs])
void Modify(int l,int r,int rt,int p,int v)
{
if(l==r) {mx[rt]=v; return;}
int m=l+r>>1;
if(p<=m) Modify(lson,p,v);
else Modify(rson,p,v);
Update(rt);
}
int Query(int l,int r,int rt,int R)
{
if(r<=R) return mx[rt];
int m=l+r>>1;
if(m<R) return std::max(Query(lson,R),Query(rson,R));
return Query(lson,R);
}
void Insert(int p,int n)//对于新插入的p查询DP值并更新
{
Modify(0,n,1,p,f[p]=Query(0,n,1,p-1)+1);
}
}Tp,Th;
inline int read()
{
int now=0;register char c=gc();
for(;!isdigit(c);c=gc());
for(;isdigit(c);now=now*10+c-'0',c=gc());
return now;
}
int main()
{
#define Sp 0,n,1
#define Sh 0,m+10,1
int n=read(),m=read();//pos[i]:高i的树的位置 h[i]:i位置的树的高度
for(int t=1; t<=m; ++t)
if(read()==1)//plant
{
int p=n-read()+1,ht=t+10-read();
pos[ht]=p, h[p]=ht, st.insert(p);
for(int i=ht+1; i<=ht+9; ++i)
if(pos[i]) Tp.Modify(Sp,pos[i],0);
for(int i=ht; i<=ht+9; ++i)
if(pos[i])
{
Tp.Insert(pos[i],n);
Th.f[i]=Tp.f[pos[i]];
Th.Modify(Sh,i,Th.f[i]);
}
printf("%d\n",Tp.mx[1]);
}
else
{
int k=read();
std::set<int>::iterator it=st.end();
while(k--) --it, Th.Modify(Sh,h[*it],0);
Tp.Modify(Sp,*it,0), pos[h[*it]]=0;
for(st.erase(it++); it!=st.end(); ++it)
{
Th.Insert(h[*it],m+10);
Tp.f[*it]=Th.f[h[*it]];
Tp.Modify(Sp,*it,Tp.f[*it]);
}
printf("%d\n",Tp.mx[1]);
}
return 0;
}
Codeforces.264E.Roadside Trees(线段树 DP LIS)的更多相关文章
- Codeforces.833B.The Bakery(线段树 DP)
题目链接 \(Description\) 有n个数,将其分为k段,每段的值为这一段的总共数字种类,问最大总值是多少 \(Solution\) DP,用\(f[i][j]\)表示当前在i 分成了j份(第 ...
- CodeForces–833B--The Bakery(线段树&&DP)
B. The Bakery time limit per test 2.5 seconds memory limit per test 256 megabytes input standard inp ...
- 线段树解LIS
先是nlogn的LIS解法 /* LIS nlogn解法 */ #include<iostream> #include<cstring> #include<cstdio& ...
- [Codeforces 1197E]Culture Code(线段树优化建图+DAG上最短路)
[Codeforces 1197E]Culture Code(线段树优化建图+DAG上最短路) 题面 有n个空心物品,每个物品有外部体积\(out_i\)和内部体积\(in_i\),如果\(in_i& ...
- Tsinsen A1219. 采矿(陈许旻) (树链剖分,线段树 + DP)
[题目链接] http://www.tsinsen.com/A1219 [题意] 给定一棵树,a[u][i]代表u结点分配i人的收益,可以随时改变a[u],查询(u,v)代表在u子树的所有节点,在u- ...
- HDU 3016 Man Down (线段树+dp)
HDU 3016 Man Down (线段树+dp) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- Buses and People CodeForces 160E 三维偏序+线段树
Buses and People CodeForces 160E 三维偏序+线段树 题意 给定 N 个三元组 (a,b,c),现有 M 个询问,每个询问给定一个三元组 (a',b',c'),求满足 a ...
- CodeForces 877E DFS序+线段树
CodeForces 877E DFS序+线段树 题意 就是树上有n个点,然后每个点都有一盏灯,给出初始的状态,1表示亮,0表示不亮,然后有两种操作,第一种是get x,表示你需要输出x的子树和x本身 ...
- [Codeforces 1199D]Welfare State(线段树)
[Codeforces 1199D]Welfare State(线段树) 题面 给出一个长度为n的序列,有q次操作,操作有2种 1.单点修改,把\(a_x\)修改成y 2.区间修改,把序列中值< ...
随机推荐
- SpringMVC使用Burlap发布远程服务
参考这篇文章https://www.cnblogs.com/fanqisoft/p/10283156.html 将提供者配置类中的 @Bean public HessianServiceExporte ...
- error: Apache shutdown unexpectedly.
本地前端xmapp apache突然不能启动了, 报错 15:36:48 [Apache] Error: Apache shutdown unexpectedly.15:36:48 [Apache] ...
- emmc基础技术8:操作模式3-interrupt mode
1. 前言 eMMC总线操作包含: boot mode, device identification mode interrupt mode data transfer mode 本文主要描述inte ...
- 嵌入式系统C编程之堆栈回溯
前言 在嵌入式系统C语言开发调试过程中,常会遇到各类异常情况.一般可按需添加打印信息,以便观察程序执行流或变量值是否异常.然而,打印操作会占用CPU时间,而且代码中添加过多打印信息时会显得很凌乱.此外 ...
- BIM 相关资料
BIM 相关资料:http://pan.baidu.com/share/link?shareid=919219072&uk=966240844
- 一个python脚本解决安装mq的依赖问题
import tarfile import sys,os from _utils.patrol2 import run_cmd import zipfile def get_version(filen ...
- openstack常见问题解决方法总结
一.创建实例失败: 首先用下面命令查看服务是否正常 1. nova-manage service list 如果不正常,则使用下面命令重启,如果还不行,则查看日志, 1. service nova-a ...
- 【转】OpenCV—imread读取数据为空
之前遇到一个很郁闷的问题,因为从用OpenCV2.3.1改成OpenCV2.4.4,开始改用Mat和imread来代替Iplimage和cvLoadImage,出了点小问题:imread读入数据总是为 ...
- win32编程:L,_T() ,TEXT和_TEXT
L的使用: 在字符串前面的大写字母L,用来告诉编译器该字符串应该作为Unicode来编译.它用来将ASNI转换为Unicode,Unicode字符串中每个字符占16位(两个字节),而在ASNI中每个字 ...
- oracle 视图 参数
创建包: create or replace package p_view_param is function set_param(num number) return number; fu ...