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 ...
随机推荐
- 利用C++不使用递归,循环和goto,打印1到100 的某一答案分析
实验环境是在64位linux下使用g++编译器 下面是Mark Gordon的答案 The below one works on my system, can't guarantee res ...
- Git常用命令学习(2)
1):git branch -v --查看每一个分支的最后一次提交2):git branch --merged 与 --no-merged 这两个有用的选项可以过滤这个列表中已经合并或尚未合并到当前分 ...
- python学习之——pip的安装与使用
安装步骤: 前提:已经安装python 1.下载安装包并解压: easy_install 地址:https://pypi.python.org/pypi/setuptools#downloads pi ...
- C# 将文件嵌入DLL 。Log4net 配置
最近在弄使用Log4net记录日志. 将配置文件封装到的DLL中. 封装步骤: 1.将配置文件添加到类库中. 2.在配置文件上右键,选择属性. 3. 此时生成类库.DLL中就存在该配置文件啦.如图: ...
- colorbox 自适应 高度
$(".example3").colorbox({ inline: true, scrolling: false , onComplete: ...
- theano中的dimshuffle
theano中的dimshuffle函数用于对张量的维度进行操作,可以增加维度,也可以交换维度,删除维度. 注意的是只有shared才能调用dimshuffle() 'x'表示增加一维,从0d sca ...
- PHP函数的实现原理及性能分析
前言 在任何语言中,函数都是最基本的组成单元.对于php的函数,它具有哪些特点?函数调用是怎么实现的?php函数的性能如何,有什么使用建议?本文将从原理出发进行分析结合实际的性能测试尝试对这些问题进行 ...
- KindleEditor上传文件报404
初步怀疑是iis配置的允许上传大小太小了,然后就修改了配置文件但是不起作用. 后来百度了下iis版本是7.5,然后就按照iis8 的配置: IIS8请求筛选模块被配置为拒绝超过请求内容长度,在&quo ...
- html中的标签
1.<big></big>定义大字体的文字 2.<blockquote>标记长的引用:</blockquote>请注意,浏览器在 blockquote ...
- Ip地址查询
$url = 'http://ip.taobao.com/service/getIpInfo.php?ip=115.239.211.112'; $info = file_get_contents($u ...