Codeforces Zip-line 650D 345Div1D(LIS)
传送门
大意:给出一个序列,求修改一个数过后的最长上升子序列。
思路:可以用主席树在线搞,也可以用树状数组离线搞,明显后者好写得多。我们首先读取所有的询问,然后就把询问绑在给出的位置,然后我们正向做一遍LIS,反向做一遍LDS,然后就可以解决这个问题了。
#include <cstdio>
#include <algorithm>
#include <vector>
#define MAXN 400005
using namespace std;
inline void GET(int &n) {
char c; n = 0;
do c = getchar(); while('0' > c || c > '9');
do n=n*10+c-'0',c=getchar();while('0' <= c && c <= '9');
}
vector<int> q[MAXN];
int lm[MAXN << 1], rm[MAXN << 1], B[MAXN << 1], a[MAXN];
int n, m, N, ask[MAXN], ans[MAXN], f[MAXN], g[MAXN], cnt[MAXN], qid[MAXN];
inline void gmax(int &a, int b) { if(a < b) a = b; }
inline void A2l(int x, int v) {
for(; x <= N; x += x&-x) gmax(lm[x], v);
}
inline void A2r(int x, int v) {
for(; x > 0; x -= x&-x) gmax(rm[x], v);
}
inline int Qml(int x, int v = 0) {
for(; x > 0; x -= x&-x) gmax(v, lm[x]); return v;
}
inline int Qmr(int x, int v = 0) {
for(; x <= N; x += x&-x) gmax(v, rm[x]); return v;
}
inline int BS(int p) {
int l = 1, r = N, mid, ans = 0;
while(l <= r) {
mid = (l + r) >> 1;
if(B[mid] >= p) { ans = mid; r=mid-1; }
else l = mid+1;
}
return ans;
}
int main() {
GET(n); GET(m); int id;
for(int i = 1; i <= n; ++ i) {
GET(a[i]); B[++ N] = a[i];
}
for(int i = 1; i <= m; ++ i) {
GET(qid[i]); GET(ask[i]);
q[qid[i]].push_back(i);
B[++ N] = ask[i];
ans[i] = 1;
}
sort(B + 1, B + N + 1);
N = unique(B + 1, B + N + 1) - B-1;
for(int i = 1; i <= n; ++ i)
a[i] = BS(a[i]);
for(int i = 1; i <= m; ++ i)
ask[i] = BS(ask[i]);
int lgst = 0;
for(int i = 1; i <= n; ++ i) {
for(auto j : q[i]) ans[j] += Qml(ask[j]-1);
f[i] = Qml(a[i]-1) + 1;
A2l(a[i], f[i]); lgst = max(lgst, f[i]);
}
for(int i = n; i > 0; -- i) {
for(auto j : q[i]) ans[j] += Qmr(ask[j]+1);
g[i] = Qmr(a[i]+1) + 1; A2r(a[i], g[i]);
}
for(int i = 1; i <= n; ++ i) if(f[i] + g[i] == lgst + 1) ++ cnt[ f[i] ];
for(int i = 1; i <= m; ++ i)
if(f[qid[i]] + g[qid[i]] == lgst + 1 && 1 == cnt[ f[qid[i]] ]) printf("%d\n", max(ans[i], lgst-1));
else printf("%d\n", max(ans[i], lgst));
return 0;
}
Codeforces Zip-line 650D 345Div1D(LIS)的更多相关文章
- Codeforces Gym101246H:``North-East''(LIS+思维)
http://codeforces.com/gym/101246/problem/H 题意:在二维平面上有n个点,从最左下角的点出发,每次走只能走在当前的点的右上角的点(xj > xi, yj ...
- Codeforces 490F. Treeland Tour 暴力+LIS
枚举根+dfs 它可以活 , 我不知道有什么解决的办法是积极的 ...... F. Treeland Tour time limit per test 5 seconds memory limit p ...
- Codeforces 1304D. Shortest and Longest LIS 代码(构造 贪心)
https://codeforces.com/contest/1304/problem/D #include<bits/stdc++.h> using namespace std; voi ...
- Codeforces 1304D. Shortest and Longest LIS
根据题目,我们可以找最短的LIS和最长的LIS,找最短LIS时,可以将每一个increase序列分成一组,从左到右将最大的还未选择的数字填写进去,不同组之间一定不会存在s[i]<s[j]的情况, ...
- CodeForces 7C Line
ax+by+c=0可以转化为ax+by=-c: 可以用扩展欧几里德算法来求ax1+by1=gcd(a,b)来求出x1,y1 此时gcd(a,b)不一定等于-c,假设-c=gcd(a,b)*z,可得z= ...
- codeforces 629D 树状数组+LIS
题意:n个圆柱形蛋糕,给你半径 r 和高度 h,一个蛋糕只能放在一个体积比它小而且序号小于它的蛋糕上面,问你这样形成的上升序列中,体积和最大是多少 分析:根据他们的体积进行离散化,然后建树状数组,按照 ...
- CodeForces - 650D:Zip-line (LIS & DP)
Vasya has decided to build a zip-line on trees of a nearby forest. He wants the line to be as long a ...
- Codeforces Round #345 (Div. 1) D. Zip-line 上升子序列 离线 离散化 线段树
D. Zip-line 题目连接: http://www.codeforces.com/contest/650/problem/D Description Vasya has decided to b ...
- codeforces #345 (Div. 1) D. Zip-line (线段树+最长上升子序列)
Vasya has decided to build a zip-line on trees of a nearby forest. He wants the line to be as long a ...
随机推荐
- 注解式开发spring定时器
1:spring 配置文件中增加这句 <task:annotation-driven/> 2:确保扫描程序能够扫描后 下面第3步骤的java类 <context:co ...
- Android开源框架——Volley
Volley 是 Google 在 2013 I/O 大会上推出的 Android 异步网络请求框架和图片加载框架.特别适合数据量小,通信频繁的网络操作.Volley 主要是通过两种 Diapatch ...
- MySQL数据库免安装版配置
参考文章: http://www.bkjia.com/Mysql/1093427.html
- dojo布局(layout)
使用BorderContainer和ContentPane实现布局 1.效果图如下: 2.HTML代码: <div id="appLayout" class="de ...
- [问题解决]《GPU高性能编程CUDA实战》中第4章Julia实例“显示器驱动已停止响应,并且已恢复”问题的解决方法
以下问题的出现及解决都基于"WIN7+CUDA7.5". 问题描述:当我编译运行<GPU高性能编程CUDA实战>中第4章所给Julia实例代码时,出现了显示器闪动的现象 ...
- div赋值,取值和input赋值,取值
一.div取值 <div id="txtXiaofei" class="txt-panel">你好</div> 获取div的值$(&qu ...
- Ajax调用SpringMVC ModelAndView 无返回情况
在项目中使用Ajax的时候,success中返回的data一直都是null,也没有报错.在确定Ajax语法没有错误,也没有牵扯跨域问题后,用排除法挨着删除代码,发现是因为Spring MVC会自动把方 ...
- for xml 字符串拼接
SELECT TOP 10 B.user_gid,LEFT(StuList,LEN(StuList)-1) as hobby FROM (SELECT user_gid,( SELECT CAST(a ...
- 托管项目到github
将项目托管到github上面其实很简单,主要有以下几个步骤: 1.注册github账号 2.创建一个新的respository:命名这个respository(假设名字为Test),选择权限 3.创建 ...
- 构造 & 析构 & 匿名对象
以前仅知道创建对象,但对匿名对象的了解基本为0. 通过阅读google chromium源代码 中关于 log 的使用,查阅相关资料,了解了一下匿名对象,予以记录. 什么是匿名对象 匿名对象可以理 ...